UNPKG

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