HerbertHe / fetch-stream

A JavaScript library for fetching stream data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

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

Methods

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

License

MIT © Herbert He

About

A JavaScript library for fetching stream data

License:MIT License


Languages

Language:TypeScript 58.1%Language:JavaScript 21.4%Language:HTML 20.4%