1 | # @fatcherjs/middleware-aborter
|
2 |
|
3 | A 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/index.min.js"></script>
|
17 | ```
|
18 |
|
19 | ## Usage
|
20 |
|
21 | ```ts
|
22 | import { aborter } from '@fatcherjs/middleware-aborter';
|
23 | import { fatcher, isAbortError } from 'fatcher';
|
24 |
|
25 | fatcher({
|
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 | ### timeout
|
53 |
|
54 | - Type: `number`
|
55 | - DefaultValue: `0`
|
56 | - Description:
|
57 |
|
58 | If `timeout > 0`, will abort this request later.
|
59 |
|
60 | Aborted request will throw a DOMException which can use `isAbortError` to confirm.
|
61 |
|
62 | ### onAbort
|
63 |
|
64 | - Type: `(() => void) | null`
|
65 | - DefaultValue: `null`
|
66 | - Description:
|
67 |
|
68 | A callback when aborting this request.
|
69 |
|
70 | ## License
|
71 |
|
72 | [MIT](../../LICENSE)
|