Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/herberthe/fetch-stream
- Owner: HerbertHe
- License: mit
- Created: 2024-05-04T10:09:58.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-05-05T02:53:02.000Z (8 months ago)
- Last Synced: 2024-10-08T05:44:46.849Z (3 months ago)
- Language: TypeScript
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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