@facetlayer/fetch-sse-stream
v1.0.1
Published
HTTP client using fetch with Server-Sent Events parsing, using @facetlayer/streams
Readme
@andyfischer/fetch-sse-stream
HTTP client using fetch with Server-Sent Events (SSE) parsing.
One fetch call will return a Stream object from @andyfischer/streams.
Installation
npm install @andyfischer/fetch-sse-streamUsage
Basic Usage
import { fetchStream } from '@andyfischer/fetch-sse-stream';
import { c_item, c_fail, c_done } from '@andyfischer/streams';
const stream = fetchStream<ResponseType>('https://example.com/events');
// Process events as they arrive
stream.pipe(evt => {
switch (evt.t) {
case c_item:
const item: ResponseType = evt.item;
// ...
break;
case c_fail:
// ...
break;
case c_done:
// ...
break;
}
});Advanced Usage
Set custom headers in the request.
import { fetchStream } from '@andyfischer/fetch-sse-stream';
const stream = fetchStream('https://example.com/events', {
headers: {
'Authorization': 'Bearer your-token'
},
onOpen: () => console.log('Connected'),
onError: (error) => console.error('Connection error:', error)
});
// Process the stream
stream.pipe(evt => {
// Handle events...
});Using a Custom Fetch Function
You can provide your own fetch implementation (useful for testing, proxies, or custom authentication):
import { fetchStream } from '@andyfischer/fetch-sse-stream';
import nodeFetch from 'node-fetch';
const stream = fetchStream('https://example.com/events', {
fetch: nodeFetch as any, // Use node-fetch instead of built-in fetch
headers: {
'User-Agent': 'MyApp/1.0'
}
});API
fetchStream(url, options?)
Creates and connects to an SSE stream immediately.
url: The endpoint URLoptions: Optional configuration (extendsRequestInit)
Returns a Stream<SSEEventData> from @andyfischer/streams.
FetchSSEStream
Class for more control over the connection lifecycle.
Constructor Options
onOpen: Callback when connection opensonError: Callback for connection errorsfetch: Custom fetch function to use instead of global fetch- Plus all standard
RequestInitoptions (headers, method, etc.)
Methods
connect(): Start the connection and return the Streamclose(): Close the connection and cleanup resources
