Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/herberthe/fetch-stream

A JavaScript library for fetching stream data
https://github.com/herberthe/fetch-stream

Last synced: about 1 month ago
JSON representation

A JavaScript library for fetching stream data

Awesome Lists containing this project

README

        

# fetch-stream

[![version](https://img.shields.io/npm/v/@herberthe/fetch-stream.svg)](https://www.npmjs.com/package/@herberthe/fetch-stream)
[![download](https://img.shields.io/npm/dm/@herberthe/fetch-stream.svg)](https://www.npmjs.com/package/@herberthe/fetch-stream)

A JavaScript library for fetching stream data.

## Install

```bash
npm install @herberthe/fetch-stream
```

## Usage

```javascript
import { FetchStream } from "@herberthe/fetch-stream"
const fetchStream = new FetchStream({
preset: "text",
timeout: 5000,
onProcess: (val) => {
document.getElementById(
"content"
).innerHTML += `

${val}
`
},
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 | `(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