Skip to content

Commit

Permalink
doc: add httx.readAsSSE() into README
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian committed Jan 11, 2024
1 parent f73a65f commit 4c8f717
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"after": true
},
"parserOptions": {
"ecmaVersion": 8,
"ecmaVersion": 2023,
"sourceType": "script",
"ecmaFeatures": {
"jsx": true
Expand Down
57 changes: 23 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,54 +22,33 @@ npm install httpx --save

## Usage

```js
'use strict';

const httpx = require('httpx');

httpx.request('http://www.baidu.com/').then((response) => {
response.pipe(process.stdout);

response.on('end', () => {
process.stdout.write('\n');
});
}, (err) => {
// on error
});
```

Or with `co`.
### Request URL

```js
co(function* () {
var response = yield httpx.request('http://www.baidu.com/');

response.pipe(process.stdout);

response.on('end', () => {
process.stdout.write('\n');
});
});
(async function () {
const response = await httpx.request('http://www.baidu.com/');
const body = await httpx.read(response, 'utf-8');
console.log(body);
})();
```

Or with `async/await`.
### Request SSE URL

```js
(async function () {
var response = await httpx.request('http://www.baidu.com/');

response.pipe(process.stdout);

response.on('end', () => {
process.stdout.write('\n');
});
const response = await httpx.request('sse url');
for await (const event of httpx.readAsSSE(response)) {
console.log(event);
}
})();
```

## API

### `httpx.request(url[, options])`

It returns `Promise<Response>`.

Requests the url with options, then return the response.

- **url** String | Object - The URL to request, either a String or a Object that return by [url.parse](http://nodejs.org/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost).
Expand All @@ -85,11 +64,21 @@ Requests the url with options, then return the response.

### `httpx.read(response[, encoding])`

It returns `Promise<Buffer | String>`.

Consume the response and read all data from the response.

- **response** Response - the Client response. Don't setEncoding() for the response.
- **encoding** String - Optional. If specify the encoding, will return String. If not specify encoding, return the buffer.

### `httpx.readAsSSE(response)`

It returns `AsyncGenerator<Event, void, unknown>`.

Consume the response data with async iterator.

- **response** Response - the Client response. Don't setEncoding() for the response.

## Using with http proxy

```js
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ function readyToRead(readable) {
readable.removeListener('error', onError);
readable.removeListener('end', onEnd);
readable.removeListener('readable', onReadable);
}
};

onReadable = function () {
cleanup();
Expand All @@ -280,12 +280,12 @@ function readyToRead(readable) {
onEnd = function () {
cleanup();
resolve(true);
}
};

onError = function (err) {
cleanup();
reject(err);
}
};

readable.once('readable', onReadable);
readable.once('end', onEnd);
Expand Down

0 comments on commit 4c8f717

Please sign in to comment.