1.0.1 • Published 1 year ago

@herberthe/fetch-stream v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

fetch-stream

version download

A JavaScript library for fetching stream data.

Install

npm install @herberthe/fetch-stream

Usage

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")

Options

OptionRequiredTypeDefaultDescription
presetNo"text"undefinedpreset decoder
timeoutNonumber or -1undefinedtimeout in milliseconds(if the value is -1, it means no timeout)
decoderNo<T>(value?: Uint8Array) => Tundefinedcustom decoder
onProcessNo(value: T) => voidundefinedcallback function for processing each chunk of data
onDoneNo() => voidundefinedcallback function when all data is processed
onErrorNo(error: Error) => voidundefinedcallback function when an error occurs
onAbortNo() => voidundefinedcallback function when the request is aborted
onTimeoutNo() => voidundefinedcallback function when the request times out

Methods

MethodDescription
createFetchRequest(url: string, options?: RequestInit)create a fetch request
abort()abort the fetch request

License

MIT © Herbert He

1.0.1

1 year ago

1.0.0

1 year ago