A JavaScript library for fetching stream data.
npm install @herberthe/fetch-stream
import { FetchStream } from "@herberthe/fetch-stream"
const fetchStream = new FetchStream({
preset: "text",
timeout: 5000,
onProcess: (val) => {
document.getElementById(
"content"
).innerHTML += `<div>${val}<div>`
},
onDone: () => {
console.log("Done!")
},
onError: (err) => {
alert(err)
},
})
fetchStream.createFetchRequest("/mock")
Option | Required | Type | Default | Description |
---|---|---|---|---|
preset | No | "text" |
undefined |
preset decoder |
timeout | No | number or -1 |
undefined |
timeout in milliseconds(if the value is -1 , it means no timeout) |
decoder | No | <T>(value?: Uint8Array) => T |
undefined |
custom decoder |
onProcess | No | (value: T) => void |
undefined |
callback function for processing each chunk of data |
onDone | No | () => void |
undefined |
callback function when all data is processed |
onError | No | (error: Error) => void |
undefined |
callback function when an error occurs |
onAbort | No | () => void |
undefined |
callback function when the request is aborted |
onTimeout | No | () => void |
undefined |
callback function when the request times out |
Method | Description |
---|---|
createFetchRequest(url: string, options?: RequestInit) |
create a fetch request |
abort() |
abort the fetch request |
MIT © Herbert He