fetch-stream-observable
v1.1.2
Published
Fetch with streams as an observable
Downloads
54
Readme
fetch-stream-observable
Fetch API which returns an observable.
- OpenAI stream parsing supported out of the box.
- Gracefully degrades to calling
subscription
only once for non streaming response (json
andtext
for eg). - Typescript
- Lightweight (3.7kb min+gzip)
Installation
$ npm i fetch-stream-observable
Usage
import { fetchObservable } from 'fetch-stream-observable';
fetchObservable("https://api.openai.com/v1/chat/completions", {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer <OPEN_AI_API_KEY>`,
},
method: "POST",
body: JSON.stringify({
/*
OpenAI body payload.
*/
// Tells openAI to send streaming response.
stream: true,
}),
// This disables automatic parsing of open ai response.
doNotParseOpenAIStream: false,
// This will disable returning an additive complete response string.
doNotConcatTextStream: false,
})
.subscribe((data: string) => {
// Logs the complete string with each event,
// helpful with React `setState`.
console.log(data);
});