fetch-event-stream
Advanced tools
Comparing version 0.0.0 to 0.0.1
import * as utils from './utils.js'; | ||
/** | ||
* Convert a `Response` body containing Server Sent Events (SSE) into an Async Iterator that yields {@linkcode ServerSentEventMessage} objects. | ||
* | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events} | ||
* | ||
* @example | ||
* ```js | ||
* // Optional | ||
* let abort = new AbortController; | ||
* | ||
* // Manually fetch a Response | ||
* let res = await fetch('https://...', { | ||
* method: 'POST', | ||
* signal: abort.signal, | ||
* headers: { | ||
* 'api-key': 'token <value>', | ||
* 'content-type': 'application/json', | ||
* }, | ||
* body: JSON.stringify({ | ||
* stream: true, // <- hypothetical | ||
* // ... | ||
* }) | ||
* }); | ||
* | ||
* if (res.ok) { | ||
* let stream = events(res, abort.signal); | ||
* for await (let event of stream) { | ||
* console.log('<<', event.data); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export async function* events(res, signal) { | ||
@@ -32,2 +64,27 @@ // TODO: throw error? | ||
} | ||
/** | ||
* Convenience function that will `fetch` with the given arguments and, if ok, will return the {@linkcode events} async iterator. | ||
* | ||
* If the response is not ok (status 200-299), the `Response` is thrown. | ||
* | ||
* @example | ||
* ```js | ||
* // NOTE: throws `Response` if not 2xx status | ||
* let events = await stream('https://api.openai.com/...', { | ||
* method: 'POST', | ||
* headers: { | ||
* 'Authorization': 'Bearer <token>', | ||
* 'Content-Type': 'application/json', | ||
* }, | ||
* body: JSON.stringify({ | ||
* stream: true, | ||
* // ... | ||
* }) | ||
* }); | ||
* | ||
* for await (let event of events) { | ||
* console.log('<<', JSON.parse(event.data)); | ||
* } | ||
* ``` | ||
*/ | ||
export async function stream(input, init) { | ||
@@ -34,0 +91,0 @@ let req = new Request(input, init); |
{ | ||
"name": "fetch-event-stream", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Server Sent Event (SSE) streaming via `fetch` and Web Streams API", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -0,4 +1,5 @@ | ||
<!-- deno-fmt-ignore-file --> | ||
# fetch-event-stream [![CI](https://github.com/lukeed/fetch-event-stream/workflows/CI/badge.svg)](https://github.com/lukeed/fetch-event-stream/actions?query=workflow%3ACI) [![licenses](https://licenses.dev/b/npm/fetch-event-stream)](https://licenses.dev/npm/fetch-event-stream) | ||
> Server Sent Event (SSE) streaming via `fetch` and Web Streams API | ||
> A tiny (742b) utility for Server Sent Event (SSE) streaming via `fetch` and Web Streams API | ||
@@ -15,5 +16,6 @@ | ||
```js | ||
import { stream, events } from 'fetch-event-stream'; | ||
// TODO | ||
```ts | ||
import { events, stream } from 'fetch-event-stream'; | ||
// or | ||
import { events, stream } from 'https://deno.land/x/fetch_event_stream'; | ||
``` | ||
@@ -24,11 +26,88 @@ | ||
### events() | ||
Returns: `String` | ||
### events(res, signal?) | ||
#### todo | ||
Type: `todo` | ||
Convert a `Response` body containing Server Sent Events (SSE) into an Async Iterator that yields | ||
`ServerSentEventMessage` objects. | ||
_**Example**_ | ||
```js | ||
// Optional | ||
let abort = new AbortController(); | ||
// Manually fetch a Response | ||
let res = await fetch('https://...', { | ||
method: 'POST', | ||
signal: abort.signal, | ||
headers: { | ||
'api-key': 'token <value>', | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
stream: true, // <- hypothetical | ||
// ... | ||
}), | ||
}); | ||
if (res.ok) { | ||
let stream = events(res, abort.signal); | ||
for await (let event of stream) { | ||
console.log('<<', event.data); | ||
} | ||
} | ||
``` | ||
#### res | ||
Type: `Response` | ||
The `Response` to consume. Must contain a body that follows the Server-Sent Event message protocol. | ||
#### signal | ||
Type: `AbortSignal` | ||
Optional. Use the `AbortController` interface to stop iteration. The stream will be destroyed. | ||
### stream(input, init?) | ||
Convenience function that will `fetch` with the given arguments and, if ok, will return the [`events`](#eventsres-signal) async iterator. | ||
> **Note:** Accepts the same arguments as `fetch` but **does not** return a `Response`! | ||
> **Important:** Will `throw` the `Response` if received non-`2xx` status code. | ||
_**Example**_ | ||
```js | ||
// NOTE: throws `Response` if not 2xx status | ||
let events = await stream('https://api.openai.com/...', { | ||
method: 'POST', | ||
headers: { | ||
'Authorization': 'Bearer <token>', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
stream: true, | ||
// ... | ||
}), | ||
}); | ||
for await (let event of events) { | ||
console.log('<<', JSON.parse(event.data)); | ||
} | ||
``` | ||
#### input | ||
Type: `Request | URL | string` | ||
Refer to [`fetch#resource`](https://developer.mozilla.org/en-US/docs/Web/API/fetch#resource) documentation. | ||
#### init | ||
Type: `RequestInit` | ||
Refer to [`fetch#options`](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) documentation. | ||
## License | ||
MIT © [Luke Edwards](https://lukeed.com) |
@@ -28,2 +28,34 @@ "use strict"; | ||
const utils = __importStar(require("./utils.js")); | ||
/** | ||
* Convert a `Response` body containing Server Sent Events (SSE) into an Async Iterator that yields {@linkcode ServerSentEventMessage} objects. | ||
* | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events} | ||
* | ||
* @example | ||
* ```js | ||
* // Optional | ||
* let abort = new AbortController; | ||
* | ||
* // Manually fetch a Response | ||
* let res = await fetch('https://...', { | ||
* method: 'POST', | ||
* signal: abort.signal, | ||
* headers: { | ||
* 'api-key': 'token <value>', | ||
* 'content-type': 'application/json', | ||
* }, | ||
* body: JSON.stringify({ | ||
* stream: true, // <- hypothetical | ||
* // ... | ||
* }) | ||
* }); | ||
* | ||
* if (res.ok) { | ||
* let stream = events(res, abort.signal); | ||
* for await (let event of stream) { | ||
* console.log('<<', event.data); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
async function* events(res, signal) { | ||
@@ -60,2 +92,27 @@ // TODO: throw error? | ||
exports.events = events; | ||
/** | ||
* Convenience function that will `fetch` with the given arguments and, if ok, will return the {@linkcode events} async iterator. | ||
* | ||
* If the response is not ok (status 200-299), the `Response` is thrown. | ||
* | ||
* @example | ||
* ```js | ||
* // NOTE: throws `Response` if not 2xx status | ||
* let events = await stream('https://api.openai.com/...', { | ||
* method: 'POST', | ||
* headers: { | ||
* 'Authorization': 'Bearer <token>', | ||
* 'Content-Type': 'application/json', | ||
* }, | ||
* body: JSON.stringify({ | ||
* stream: true, | ||
* // ... | ||
* }) | ||
* }); | ||
* | ||
* for await (let event of events) { | ||
* console.log('<<', JSON.parse(event.data)); | ||
* } | ||
* ``` | ||
*/ | ||
async function stream(input, init) { | ||
@@ -62,0 +119,0 @@ let req = new Request(input, init); |
import type { ServerSentEventMessage } from './deps/deno.land/std@0.220.1/http/server_sent_event_stream.js'; | ||
export type { ServerSentEventMessage }; | ||
/** | ||
* Convert a `Response` body containing Server Sent Events (SSE) into an Async Iterator that yields {@linkcode ServerSentEventMessage} objects. | ||
* | ||
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events} | ||
* | ||
* @example | ||
* ```js | ||
* // Optional | ||
* let abort = new AbortController; | ||
* | ||
* // Manually fetch a Response | ||
* let res = await fetch('https://...', { | ||
* method: 'POST', | ||
* signal: abort.signal, | ||
* headers: { | ||
* 'api-key': 'token <value>', | ||
* 'content-type': 'application/json', | ||
* }, | ||
* body: JSON.stringify({ | ||
* stream: true, // <- hypothetical | ||
* // ... | ||
* }) | ||
* }); | ||
* | ||
* if (res.ok) { | ||
* let stream = events(res, abort.signal); | ||
* for await (let event of stream) { | ||
* console.log('<<', event.data); | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
export declare function events(res: Response, signal?: AbortSignal): AsyncGenerator<ServerSentEventMessage, void, unknown>; | ||
/** | ||
* Convenience function that will `fetch` with the given arguments and, if ok, will return the {@linkcode events} async iterator. | ||
* | ||
* If the response is not ok (status 200-299), the `Response` is thrown. | ||
* | ||
* @example | ||
* ```js | ||
* // NOTE: throws `Response` if not 2xx status | ||
* let events = await stream('https://api.openai.com/...', { | ||
* method: 'POST', | ||
* headers: { | ||
* 'Authorization': 'Bearer <token>', | ||
* 'Content-Type': 'application/json', | ||
* }, | ||
* body: JSON.stringify({ | ||
* stream: true, | ||
* // ... | ||
* }) | ||
* }); | ||
* | ||
* for await (let event of events) { | ||
* console.log('<<', JSON.parse(event.data)); | ||
* } | ||
* ``` | ||
*/ | ||
export declare function stream(input: RequestInfo | URL, init?: RequestInit): Promise<AsyncGenerator<ServerSentEventMessage, void, unknown>>; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
25769
649
112
2