UNPKG

3.01 kBMarkdownView Raw
1# @fatcherjs/middleware-aborter
2
3A middleware for aborting fatcher request.
4
5[![codecov](https://codecov.io/gh/fatcherjs/middleware-aborter/branch/master/graph/badge.svg?token=TFKUGW6YNI)](https://codecov.io/gh/fatcherjs/middleware-aborter)
6[![install size](https://packagephobia.com/badge?p=@fatcherjs/middleware-aborter)](https://packagephobia.com/result?p=@fatcherjs/middleware-aborter)
7<a href="https://unpkg.com/@fatcherjs/middleware-aborter"><img alt="Size" src="https://img.badgesize.io/https://unpkg.com/@fatcherjs/middleware-aborter"></a>
8<a href="https://npmjs.com/package/@fatcherjs/middleware-aborter"><img src="https://img.shields.io/npm/v/@fatcherjs/middleware-aborter.svg" alt="npm package"></a>
9<a href="https://github.com/fatcherjs/middleware-aborter/actions/workflows/ci.yml"><img src="https://github.com/fatcherjs/middleware-aborter/actions/workflows/ci.yml/badge.svg?branch=master" alt="build status"></a>
10
11## Install
12
13### NPM
14
15```bash
16>$ npm install @fatcherjs/middleware-aborter
17```
18
19### CDN
20
21```html
22<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/aborter.min.js"></script>
23```
24
25## Usage
26
27```ts
28import { aborter, isAbortError } from '@fatcherjs/middleware-aborter';
29import { fatcher } from 'fatcher';
30
31fatcher({
32 url: '/bar/foo',
33 middlewares: [
34 aborter({
35 timeout: 10 * 1000, // 10s
36 onAbort: () => {
37 console.log('Request is Aborted.');
38 },
39 }),
40 ],
41})
42 .then(res => {
43 // Request success in 10s
44 console.log(res);
45 })
46 .catch(err => {
47 if (isAbortError(err)) {
48 //Run error when request aborted.
49 console.error(err);
50 }
51
52 // Other errors.
53 });
54```
55
56## Options
57
58| Name | Description | Type | DefaultValue |
59| ----------- | ----------------------------------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------ |
60| timeout | If `timeout > 0`, will abort this request later | `number` | `0` |
61| onAbort | A callback when aborting this request | `(() => void) \| null` | `null` |
62| concurrency | Request concurrency restrictions | `boolean` | `false` |
63| groupBy | Concurrency key | `(context: Readonly<Context>) => string` | `${context.url}_${context.method}_${new URLSearchParams(context.params).toString()}` |
64
65## License
66
67[MIT](https://github.com/fatcherjs/fatcher/blob/master/LICENSE)