@fatcherjs/middleware-aborter 1.0.0
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/LICENSE +21 -0
- package/README.md +72 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.esm.js +31 -0
- package/dist/index.js +35 -0
- package/dist/index.min.js +1 -0
- package/package.json +21 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 fatcherjs
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
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)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Middleware } from 'fatcher';
|
|
2
|
+
|
|
3
|
+
interface AborterOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Request timeout
|
|
6
|
+
*
|
|
7
|
+
* `ms`
|
|
8
|
+
*
|
|
9
|
+
* @default 0
|
|
10
|
+
*/
|
|
11
|
+
timeout?: number;
|
|
12
|
+
/**
|
|
13
|
+
* Callback with aborted request.
|
|
14
|
+
*
|
|
15
|
+
* @default null
|
|
16
|
+
*/
|
|
17
|
+
onAbort?: (() => void) | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A middleware for aborting fatcher request.
|
|
22
|
+
* @param options
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
declare function aborter(options?: AborterOptions): Middleware;
|
|
26
|
+
|
|
27
|
+
export { AborterOptions, aborter };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { warn } from 'fatcher';
|
|
2
|
+
|
|
3
|
+
function aborter(options = {}) {
|
|
4
|
+
const { timeout = 0, onAbort = null } = options;
|
|
5
|
+
let _timeout = timeout;
|
|
6
|
+
if (isNaN(timeout) || ~~timeout <= 0) {
|
|
7
|
+
warn("<Aborter> Timeout is not a valid number.");
|
|
8
|
+
_timeout = 0;
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
name: "fatcher-middleware-aborter",
|
|
12
|
+
async use(context, next) {
|
|
13
|
+
const abortController = new AbortController();
|
|
14
|
+
const requestTask = next({
|
|
15
|
+
signal: abortController.signal
|
|
16
|
+
});
|
|
17
|
+
if (!_timeout) {
|
|
18
|
+
return requestTask;
|
|
19
|
+
}
|
|
20
|
+
const timer = setTimeout(() => {
|
|
21
|
+
abortController.abort();
|
|
22
|
+
onAbort == null ? void 0 : onAbort();
|
|
23
|
+
}, _timeout);
|
|
24
|
+
const response = await requestTask;
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
return response;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { aborter };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var fatcher = require('fatcher');
|
|
6
|
+
|
|
7
|
+
function aborter(options = {}) {
|
|
8
|
+
const { timeout = 0, onAbort = null } = options;
|
|
9
|
+
let _timeout = timeout;
|
|
10
|
+
if (isNaN(timeout) || ~~timeout <= 0) {
|
|
11
|
+
fatcher.warn("<Aborter> Timeout is not a valid number.");
|
|
12
|
+
_timeout = 0;
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
name: "fatcher-middleware-aborter",
|
|
16
|
+
async use(context, next) {
|
|
17
|
+
const abortController = new AbortController();
|
|
18
|
+
const requestTask = next({
|
|
19
|
+
signal: abortController.signal
|
|
20
|
+
});
|
|
21
|
+
if (!_timeout) {
|
|
22
|
+
return requestTask;
|
|
23
|
+
}
|
|
24
|
+
const timer = setTimeout(() => {
|
|
25
|
+
abortController.abort();
|
|
26
|
+
onAbort == null ? void 0 : onAbort();
|
|
27
|
+
}, _timeout);
|
|
28
|
+
const response = await requestTask;
|
|
29
|
+
clearTimeout(timer);
|
|
30
|
+
return response;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.aborter = aborter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(e,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("fatcher")):typeof define=="function"&&define.amd?define(["exports","fatcher"],t):(e=typeof globalThis<"u"?globalThis:e||self,t(e.FatcherMiddlewareAborter={},e.Fatcher))})(this,function(e,t){"use strict";function u(a={}){const{timeout:r=0,onAbort:o=null}=a;let n=r;return(isNaN(r)||~~r<=0)&&(t.warn("<Aborter> Timeout is not a valid number."),n=0),{name:"fatcher-middleware-aborter",async use(l,c){const i=new AbortController,s=c({signal:i.signal});if(!n)return s;const f=setTimeout(()=>{i.abort(),o?.()},n),d=await s;return clearTimeout(f),d}}}e.aborter=u,Object.defineProperty(e,"__esModule",{value:!0})});
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fatcherjs/middleware-aborter",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.esm.js",
|
|
6
|
+
"browser": "dist/index.min.js",
|
|
7
|
+
"typings": "dist/index.d.ts",
|
|
8
|
+
"repository": "https://github.com/fatcherjs/middlewares/tree/master/packages/aborter",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"peerDependencies": {
|
|
14
|
+
"fatcher": "^1.0.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"dev": "rimraf dist && rollup -c rollup.config.ts -w",
|
|
18
|
+
"build": "rimraf dist && rollup -c rollup.config.ts"
|
|
19
|
+
},
|
|
20
|
+
"readme": "# @fatcherjs/middleware-aborter\n\nA middleware for aborting fatcher request.\n\n## Install\n\n### NPM\n\n```bash\n>$ npm install @fatcherjs/middleware-aborter\n```\n\n### CDN\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js\"></script>\n```\n\n## Usage\n\n```ts\nimport { aborter } from '@fatcherjs/middleware-aborter';\nimport { fatcher, isAbortError } from 'fatcher';\n\nfatcher({\n url: '/bar/foo',\n middlewares: [\n aborter({\n timeout: 10 * 1000, // 10s\n onAbort: () => {\n console.log('Request is Aborted.');\n },\n }),\n ],\n})\n .then(res => {\n // Request success in 10s\n console.log(res);\n })\n .catch(err => {\n if (isAbortError(err)) {\n //Run error when request aborted.\n console.error(err);\n }\n\n // Other errors.\n });\n```\n\n## Options\n\n### timeout\n\n- Type: `number`\n- DefaultValue: `0`\n- Description:\n\nIf `timeout > 0`, will abort this request later.\n\nAborted request will throw a DOMException which can use `isAbortError` to confirm.\n\n### onAbort\n\n- Type: `(() => void) | null`\n- DefaultValue: `null`\n- Description:\n\nA callback when aborting this request.\n\n## License\n\n[MIT](../../LICENSE)\n"
|
|
21
|
+
}
|