@fatcherjs/middleware-aborter 3.0.0-alpha-8 → 3.0.0-alpha-9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +69 -19
- package/dist/index.d.ts +11 -10
- package/dist/index.esm.js +38 -17
- package/dist/index.js +38 -16
- package/dist/index.min.js +1 -1
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -18,51 +18,101 @@
|
|
|
18
18
|
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
##
|
|
21
|
+
## Provider
|
|
22
|
+
|
|
23
|
+
### FatcherRequest
|
|
24
|
+
|
|
25
|
+
#### abort
|
|
22
26
|
|
|
23
27
|
```ts
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
declare module 'fatcher' {
|
|
29
|
+
interface FatcherRequest {
|
|
30
|
+
abort: (reason?: string) => void;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Middleware can get a `abort` function after call `aborter`;
|
|
26
36
|
|
|
37
|
+
```ts
|
|
27
38
|
fatcher('https://foo.bar', {
|
|
28
39
|
middlewares: [
|
|
29
|
-
aborter(
|
|
30
|
-
|
|
31
|
-
|
|
40
|
+
aborter(),
|
|
41
|
+
(req, next) => {
|
|
42
|
+
console.log(typeof req.abort); // 'function'
|
|
43
|
+
return next();
|
|
44
|
+
},
|
|
32
45
|
],
|
|
33
46
|
});
|
|
34
47
|
```
|
|
35
48
|
|
|
36
|
-
##
|
|
49
|
+
## Usage
|
|
37
50
|
|
|
38
|
-
###
|
|
51
|
+
### Basic
|
|
39
52
|
|
|
40
53
|
```ts
|
|
41
54
|
import { fatcher } from 'fatcher';
|
|
42
55
|
import { aborter } from '@fatcherjs/middleware-aborter';
|
|
43
56
|
|
|
44
57
|
fatcher('https://foo.bar', {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
onAbort: () => console.log('aborted'),
|
|
48
|
-
}),
|
|
49
|
-
],
|
|
58
|
+
onAbort: () => console.log('aborted'),
|
|
59
|
+
middlewares: [aborter()],
|
|
50
60
|
});
|
|
51
61
|
```
|
|
52
62
|
|
|
53
|
-
###
|
|
63
|
+
### Timeout
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { fatcher } from 'fatcher';
|
|
67
|
+
import { aborter, timeout } from '@fatcherjs/middleware-aborter';
|
|
68
|
+
|
|
69
|
+
fatcher('https://foo.bar', {
|
|
70
|
+
onAbort: () => console.log('aborted'),
|
|
71
|
+
timeout: 1000 * 10, // 10s
|
|
72
|
+
middlewares: [aborter(), timeout() /* must call after aborter */],
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### User Cancelable
|
|
54
77
|
|
|
55
78
|
```ts
|
|
56
79
|
import { fatcher } from 'fatcher';
|
|
57
80
|
import { aborter } from '@fatcherjs/middleware-aborter';
|
|
58
81
|
|
|
82
|
+
const abortController = new AbortController();
|
|
83
|
+
|
|
59
84
|
fatcher('https://foo.bar', {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
85
|
+
onAbort: () => console.log('aborted'),
|
|
86
|
+
abortController,
|
|
87
|
+
middlewares: [aborter()],
|
|
88
|
+
}).catch(error => {
|
|
89
|
+
// abort error
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
abortController.abort();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### isAbortError
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { fatcher } from 'fatcher';
|
|
99
|
+
import { aborter, isAbortError } from '@fatcherjs/middleware-aborter';
|
|
100
|
+
|
|
101
|
+
const abortController = new AbortController();
|
|
102
|
+
|
|
103
|
+
fatcher('https://foo.bar', {
|
|
104
|
+
onAbort: () => console.log('aborted'),
|
|
105
|
+
abortController,
|
|
106
|
+
middlewares: [aborter()],
|
|
107
|
+
}).catch(error => {
|
|
108
|
+
if (isAbortError(error)) {
|
|
109
|
+
// do something..
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
// other error
|
|
65
113
|
});
|
|
114
|
+
|
|
115
|
+
abortController.abort();
|
|
66
116
|
```
|
|
67
117
|
|
|
68
118
|
## License
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { FatcherMiddleware } from 'fatcher';
|
|
2
2
|
|
|
3
|
+
declare const aborter: () => FatcherMiddleware;
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Confirm an error whether is DOMException
|
|
5
7
|
* @param error
|
|
@@ -7,18 +9,17 @@ import { FatcherMiddleware } from 'fatcher';
|
|
|
7
9
|
*/
|
|
8
10
|
declare function isAbortError(error: unknown): error is DOMException;
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
signal?: AbortSignal;
|
|
13
|
-
abort?: () => void;
|
|
14
|
-
onAbort?: () => void;
|
|
15
|
-
}
|
|
12
|
+
declare const timeout: () => FatcherMiddleware;
|
|
13
|
+
|
|
16
14
|
declare module 'fatcher' {
|
|
17
15
|
interface FatcherRequest {
|
|
18
|
-
abort: () => void;
|
|
16
|
+
abort: (reason?: string) => void;
|
|
17
|
+
}
|
|
18
|
+
interface FatcherOptions {
|
|
19
|
+
timeout?: number;
|
|
20
|
+
onAbort?: (reason?: string) => void;
|
|
21
|
+
abortController?: AbortController;
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export { AborterOptions, aborter, isAbortError };
|
|
25
|
+
export { aborter, isAbortError, timeout };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,30 +1,51 @@
|
|
|
1
|
-
|
|
2
|
-
return error instanceof DOMException && error.name === "AbortError";
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
const aborter = (options = {}) => {
|
|
1
|
+
const aborter = () => {
|
|
6
2
|
return async (req, next) => {
|
|
7
|
-
const { onAbort,
|
|
8
|
-
const
|
|
3
|
+
const { onAbort, abortController = new AbortController() } = req.options;
|
|
4
|
+
const handler = () => {
|
|
5
|
+
onAbort?.(abortController.signal.reason);
|
|
6
|
+
abortController.signal.removeEventListener("abort", handler);
|
|
7
|
+
};
|
|
9
8
|
if (onAbort) {
|
|
10
|
-
abortController.signal.addEventListener("abort",
|
|
9
|
+
abortController.signal.addEventListener("abort", handler);
|
|
11
10
|
}
|
|
12
|
-
const
|
|
13
|
-
abort: abortController.abort,
|
|
11
|
+
const response = await next({
|
|
12
|
+
abort: abortController.abort.bind(abortController),
|
|
14
13
|
signal: abortController.signal
|
|
15
14
|
});
|
|
15
|
+
if (onAbort) {
|
|
16
|
+
abortController.signal.removeEventListener("abort", handler);
|
|
17
|
+
}
|
|
18
|
+
return response;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function isAbortError(error) {
|
|
23
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const timeout = () => {
|
|
27
|
+
return async (req, next) => {
|
|
28
|
+
const {
|
|
29
|
+
abort,
|
|
30
|
+
options: { timeout: _timeout }
|
|
31
|
+
} = req;
|
|
16
32
|
let timer = null;
|
|
17
|
-
if (
|
|
33
|
+
if (_timeout) {
|
|
18
34
|
timer = setTimeout(() => {
|
|
19
|
-
|
|
20
|
-
},
|
|
35
|
+
abort();
|
|
36
|
+
}, _timeout);
|
|
21
37
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
38
|
+
let response;
|
|
39
|
+
try {
|
|
40
|
+
response = await next();
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (timer) {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
}
|
|
45
|
+
throw error;
|
|
25
46
|
}
|
|
26
47
|
return response;
|
|
27
48
|
};
|
|
28
49
|
};
|
|
29
50
|
|
|
30
|
-
export { aborter, isAbortError };
|
|
51
|
+
export { aborter, isAbortError, timeout };
|
package/dist/index.js
CHANGED
|
@@ -2,30 +2,51 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
return error instanceof DOMException && error.name === "AbortError";
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const aborter = (options = {}) => {
|
|
5
|
+
const aborter = () => {
|
|
10
6
|
return async (req, next) => {
|
|
11
|
-
const { onAbort,
|
|
12
|
-
const
|
|
7
|
+
const { onAbort, abortController = new AbortController() } = req.options;
|
|
8
|
+
const handler = () => {
|
|
9
|
+
onAbort?.(abortController.signal.reason);
|
|
10
|
+
abortController.signal.removeEventListener("abort", handler);
|
|
11
|
+
};
|
|
13
12
|
if (onAbort) {
|
|
14
|
-
abortController.signal.addEventListener("abort",
|
|
13
|
+
abortController.signal.addEventListener("abort", handler);
|
|
15
14
|
}
|
|
16
|
-
const
|
|
17
|
-
abort: abortController.abort,
|
|
15
|
+
const response = await next({
|
|
16
|
+
abort: abortController.abort.bind(abortController),
|
|
18
17
|
signal: abortController.signal
|
|
19
18
|
});
|
|
19
|
+
if (onAbort) {
|
|
20
|
+
abortController.signal.removeEventListener("abort", handler);
|
|
21
|
+
}
|
|
22
|
+
return response;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function isAbortError(error) {
|
|
27
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const timeout = () => {
|
|
31
|
+
return async (req, next) => {
|
|
32
|
+
const {
|
|
33
|
+
abort,
|
|
34
|
+
options: { timeout: _timeout }
|
|
35
|
+
} = req;
|
|
20
36
|
let timer = null;
|
|
21
|
-
if (
|
|
37
|
+
if (_timeout) {
|
|
22
38
|
timer = setTimeout(() => {
|
|
23
|
-
|
|
24
|
-
},
|
|
39
|
+
abort();
|
|
40
|
+
}, _timeout);
|
|
25
41
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
42
|
+
let response;
|
|
43
|
+
try {
|
|
44
|
+
response = await next();
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (timer) {
|
|
47
|
+
clearTimeout(timer);
|
|
48
|
+
}
|
|
49
|
+
throw error;
|
|
29
50
|
}
|
|
30
51
|
return response;
|
|
31
52
|
};
|
|
@@ -33,3 +54,4 @@ const aborter = (options = {}) => {
|
|
|
33
54
|
|
|
34
55
|
exports.aborter = aborter;
|
|
35
56
|
exports.isAbortError = isAbortError;
|
|
57
|
+
exports.timeout = timeout;
|
package/dist/index.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(t,i){typeof exports=="object"&&typeof module!="undefined"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(t=typeof globalThis!="undefined"?globalThis:t||self,i(t.FatcherMiddlewareAborter={}))})(this,function(t){"use strict";const i=()=>async(o,a)=>{const{onAbort:r,abortController:e=new AbortController}=o.options,n=()=>{r==null||r(e.signal.reason),e.signal.removeEventListener("abort",n)};r&&e.signal.addEventListener("abort",n);const s=await a({abort:e.abort.bind(e),signal:e.signal});return r&&e.signal.removeEventListener("abort",n),s};function u(o){return o instanceof DOMException&&o.name==="AbortError"}const l=()=>async(o,a)=>{const{abort:r,options:{timeout:e}}=o;let n=null;e&&(n=setTimeout(()=>{r()},e));let s;try{s=await a()}catch(c){throw n&&clearTimeout(n),c}return s};t.aborter=i,t.isAbortError=u,t.timeout=l,Object.defineProperty(t,"__esModule",{value:!0})});
|
package/package.json
CHANGED
|
@@ -13,14 +13,16 @@
|
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
15
|
"peerDependencies": {
|
|
16
|
-
"fatcher": "3.0.0-alpha-
|
|
16
|
+
"fatcher": "3.0.0-alpha-9"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"fatcher": "3.0.0-alpha-
|
|
19
|
+
"fatcher": "3.0.0-alpha-9"
|
|
20
20
|
},
|
|
21
|
-
"version": "3.0.0-alpha-
|
|
21
|
+
"version": "3.0.0-alpha-9",
|
|
22
22
|
"scripts": {
|
|
23
23
|
"clean": "rimraf dist",
|
|
24
|
-
"build": "npm run clean && rollup -c rollup.config.ts"
|
|
24
|
+
"build": "npm run clean && rollup -c rollup.config.ts",
|
|
25
|
+
"test": "jest",
|
|
26
|
+
"test:cov": "jest --coverage"
|
|
25
27
|
}
|
|
26
28
|
}
|