@fatcherjs/middleware-aborter 3.0.0-alpha-11 → 3.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Fansy
3
+ Copyright (c) 2022 fatcherjs
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,8 +1,48 @@
1
1
  # @fatcherjs/middleware-aborter
2
2
 
3
- <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>
4
- [![install size](https://packagephobia.com/badge?p=@fatcherjs/middleware-aborter)](https://packagephobia.com/result?p=@fatcherjs/middleware-aborter)
5
- <a href="https://unpkg.com/@fatcherjs/middleware-aborter"><img alt="Size" src="https://img.badgesize.io/https://unpkg.com/@fatcherjs/middleware-aborter"></a>
3
+ <div align="center">
4
+ <a href="https://codecov.io/github/fatcherjs/middleware-aborter" >
5
+ <img src="https://codecov.io/github/fatcherjs/middleware-aborter/graph/badge.svg?token=BEJA311FY2"/>
6
+ </a>
7
+ <a href="https://www.jsdelivr.com/package/npm/@fatcherjs/middleware-aborter">
8
+ <img src="https://data.jsdelivr.com/v1/package/npm/@fatcherjs/middleware-aborter/badge?style=rounded" alt="jsDelivr">
9
+ </a>
10
+ <a href="https://packagephobia.com/result?p=@fatcherjs/middleware-aborter">
11
+ <img src="https://packagephobia.com/badge?p=@fatcherjs/middleware-aborter" alt="install size">
12
+ </a>
13
+ <a href="https://unpkg.com/@fatcherjs/middleware-aborter">
14
+ <img src="https://img.badgesize.io/https://unpkg.com/@fatcherjs/middleware-aborter" alt="Size">
15
+ </a>
16
+ <a href="https://npmjs.com/package/@fatcherjs/middleware-aborter">
17
+ <img src="https://img.shields.io/npm/v/@fatcherjs/middleware-aborter.svg" alt="npm package">
18
+ </a>
19
+ <a href="https://github.com/fatcherjs/middleware-aborter/actions/workflows/ci.yml">
20
+ <img src="https://github.com/fatcherjs/middleware-aborter/actions/workflows/ci.yml/badge.svg?branch=master" alt="build status">
21
+ </a>
22
+ </div>
23
+
24
+ ## Tips
25
+
26
+ If you want to abort request manually, you should use `AbortController` and pass the signal to `fatcher`
27
+
28
+ ```ts
29
+ import { fatcher } from 'fatcher';
30
+
31
+ const abortController = new AbortController();
32
+
33
+ fatcher('your-url', {
34
+ signal: abortController.signal,
35
+ }).catch(error => {
36
+ if (error instanceof DOMException && error.name === 'AbortError') {
37
+ // aborted.
38
+ return;
39
+ }
40
+
41
+ // other
42
+ });
43
+
44
+ abortController.abort();
45
+ ```
6
46
 
7
47
  ## Install
8
48
 
@@ -15,106 +55,77 @@
15
55
  ### CDN
16
56
 
17
57
  ```html
58
+ <script src="https://cdn.jsdelivr.net/npm/fatcher/dist/fatcher.min.js"></script>
18
59
  <script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
60
+
61
+ <script>
62
+ Fatcher.fatcher('xxx', {
63
+ middlewares: [FatcherMiddlewareAborter.timeout],
64
+ onTimeout: () => {},
65
+ timeout: 30000,
66
+ })
67
+ .then(response => {
68
+ console.log(response);
69
+ })
70
+ .catch(error => {
71
+ if (FatcherMiddlewareAborter.isTimeoutError(error)) {
72
+ // do somethings
73
+ return;
74
+ }
75
+
76
+ // do other thing
77
+ });
78
+ </script>
19
79
  ```
20
80
 
21
- ## Provider
81
+ ## Usage
22
82
 
23
- ### FatcherRequest
83
+ ### Timeout
24
84
 
25
- #### abort
85
+ #### Types
26
86
 
27
87
  ```ts
28
88
  declare module 'fatcher' {
29
- interface FatcherRequest {
30
- abort: (reason?: string) => void;
89
+ interface FatcherOptions {
90
+ timeout?: number; // default 60 * 1000 (60s)
91
+ onTimeout?: () => void;
31
92
  }
32
93
  }
33
94
  ```
34
95
 
35
- Middleware can get a `abort` function after call `aborter`;
36
-
37
- ```ts
38
- fatcher('https://foo.bar', {
39
- middlewares: [
40
- aborter(),
41
- (req, next) => {
42
- console.log(typeof req.abort); // 'function'
43
- return next();
44
- },
45
- ],
46
- });
47
- ```
48
-
49
- ## Usage
50
-
51
- ### Basic
96
+ #### Basic
52
97
 
53
98
  ```ts
54
99
  import { fatcher } from 'fatcher';
55
- import { aborter } from '@fatcherjs/middleware-aborter';
56
-
57
- fatcher('https://foo.bar', {
58
- onAbort: () => console.log('aborted'),
59
- middlewares: [aborter()],
100
+ import { timeout } from '@fatcherjs/middleware-aborter';
101
+
102
+ const response = await fatcher('xxx', {
103
+ middlewares: [timeout],
104
+ timeout: 30 * 1000,
105
+ onTimeout: () => {
106
+ console.log('timeout!');
107
+ },
60
108
  });
61
109
  ```
62
110
 
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
77
-
78
- ```ts
79
- import { fatcher } from 'fatcher';
80
- import { aborter } from '@fatcherjs/middleware-aborter';
81
-
82
- const abortController = new AbortController();
83
-
84
- fatcher('https://foo.bar', {
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
111
+ ### isTimeoutError
96
112
 
97
113
  ```ts
98
114
  import { fatcher } from 'fatcher';
99
- import { aborter, isAbortError } from '@fatcherjs/middleware-aborter';
100
-
101
- const abortController = new AbortController();
115
+ import { isTimeoutError, timeout } from '@fatcherjs/middleware-aborter';
102
116
 
103
117
  fatcher('https://foo.bar', {
104
- onAbort: () => console.log('aborted'),
105
- abortController,
106
- middlewares: [aborter],
118
+ timeout: 30 * 1000,
119
+ middlewares: [timeout],
107
120
  }).catch(error => {
108
- if (isAbortError(error)) {
121
+ if (isTimeoutError(error)) {
109
122
  // do something..
110
123
  return;
111
124
  }
112
125
  // other error
113
126
  });
114
-
115
- abortController.abort();
116
127
  ```
117
128
 
118
129
  ## License
119
130
 
120
- [MIT](https://github.com/fanhaoyuan/fatcher/blob/master/LICENSE)
131
+ [MIT](https://github.com/fatcherjs/middleware-aborter/blob/master/LICENSE)
@@ -0,0 +1 @@
1
+ export declare function abortSignalAny(signals: AbortSignal[]): AbortSignal;
@@ -0,0 +1 @@
1
+ export declare function abortSignalTimeout(timeout: number): AbortSignal;
package/dist/index.d.ts CHANGED
@@ -1,25 +1,3 @@
1
- import { FatcherMiddleware } from 'fatcher';
2
-
3
- declare const aborter: FatcherMiddleware;
4
-
5
- /**
6
- * Confirm an error whether is DOMException
7
- * @param error
8
- * @returns
9
- */
10
- declare function isAbortError(error: unknown): error is DOMException;
11
-
12
- declare const timeout: FatcherMiddleware;
13
-
14
- declare module 'fatcher' {
15
- interface FatcherOptions {
16
- onAbort?: (reason?: string) => void;
17
- abortController?: AbortController;
18
- timeout?: number;
19
- }
20
- interface FatcherContext {
21
- abort: (reason?: string) => void;
22
- }
23
- }
24
-
25
- export { aborter, isAbortError, timeout };
1
+ export * from './isTimeoutError';
2
+ export * from './timeout';
3
+ export * from './types';
package/dist/index.esm.js CHANGED
@@ -1,52 +1,47 @@
1
- import 'fatcher';
2
-
3
- const aborter = {
4
- name: "fatcher-middleware-aborter",
5
- use: async (request, next) => {
6
- const { onAbort, abortController = new AbortController() } = request;
7
- const handler = () => {
8
- onAbort?.(abortController.signal.reason);
9
- abortController.signal.removeEventListener("abort", handler);
10
- };
11
- if (onAbort) {
12
- abortController.signal.addEventListener("abort", handler);
13
- }
14
- const response = await next({
15
- abort: abortController.abort.bind(abortController),
16
- signal: abortController.signal
17
- });
18
- if (onAbort) {
19
- abortController.signal.removeEventListener("abort", handler);
1
+ function c(t) {
2
+ return t instanceof DOMException && t.name === "TimeoutError";
3
+ }
4
+ function u(t) {
5
+ if (typeof AbortSignal.any == "function")
6
+ return AbortSignal.any(t);
7
+ const o = new AbortController(), e = () => {
8
+ o.abort(), r();
9
+ }, r = () => {
10
+ for (const n of t)
11
+ n.removeEventListener("abort", e);
12
+ };
13
+ for (const n of t) {
14
+ if (n.aborted) {
15
+ e();
16
+ break;
20
17
  }
21
- return response;
18
+ n.addEventListener("abort", e);
22
19
  }
23
- };
24
-
25
- function isAbortError(error) {
26
- return error instanceof DOMException && error.name === "AbortError";
20
+ return o.signal;
21
+ }
22
+ function s(t) {
23
+ if (typeof AbortSignal.timeout == "function")
24
+ return AbortSignal.timeout(t);
25
+ const o = new AbortController(), e = setTimeout(() => {
26
+ o.abort(new DOMException("The operation timed out.", "TimeoutError"));
27
+ }, t);
28
+ return o.signal.addEventListener("abort", () => clearTimeout(e)), o.signal;
27
29
  }
28
-
29
- const timeout = {
30
+ const l = {
30
31
  name: "fatcher-middleware-timeout",
31
- use: async (context, next) => {
32
- const { abort, timeout: _timeout } = context;
33
- let timer = null;
34
- if (_timeout) {
35
- timer = setTimeout(() => {
36
- abort();
37
- }, _timeout);
38
- }
39
- let response;
40
- try {
41
- response = await next();
42
- } catch (error) {
43
- if (timer) {
44
- clearTimeout(timer);
32
+ use: async (t, o) => {
33
+ const { timeout: e = 6e4, signal: r, onTimeout: n } = t, i = s(e), a = r ? u([i, r]) : i;
34
+ return a.addEventListener("abort", () => {
35
+ if (i.aborted) {
36
+ n?.();
37
+ return;
45
38
  }
46
- throw error;
47
- }
48
- return response;
39
+ }), o({
40
+ request: new Request(t.request, { signal: a })
41
+ });
49
42
  }
50
43
  };
51
-
52
- export { aborter, isAbortError, timeout };
44
+ export {
45
+ c as isTimeoutError,
46
+ l as timeout
47
+ };
package/dist/index.js CHANGED
@@ -1,58 +1 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- require('fatcher');
6
-
7
- const aborter = {
8
- name: "fatcher-middleware-aborter",
9
- use: async (request, next) => {
10
- const { onAbort, abortController = new AbortController() } = request;
11
- const handler = () => {
12
- onAbort?.(abortController.signal.reason);
13
- abortController.signal.removeEventListener("abort", handler);
14
- };
15
- if (onAbort) {
16
- abortController.signal.addEventListener("abort", handler);
17
- }
18
- const response = await next({
19
- abort: abortController.abort.bind(abortController),
20
- signal: abortController.signal
21
- });
22
- if (onAbort) {
23
- abortController.signal.removeEventListener("abort", handler);
24
- }
25
- return response;
26
- }
27
- };
28
-
29
- function isAbortError(error) {
30
- return error instanceof DOMException && error.name === "AbortError";
31
- }
32
-
33
- const timeout = {
34
- name: "fatcher-middleware-timeout",
35
- use: async (context, next) => {
36
- const { abort, timeout: _timeout } = context;
37
- let timer = null;
38
- if (_timeout) {
39
- timer = setTimeout(() => {
40
- abort();
41
- }, _timeout);
42
- }
43
- let response;
44
- try {
45
- response = await next();
46
- } catch (error) {
47
- if (timer) {
48
- clearTimeout(timer);
49
- }
50
- throw error;
51
- }
52
- return response;
53
- }
54
- };
55
-
56
- exports.aborter = aborter;
57
- exports.isAbortError = isAbortError;
58
- exports.timeout = timeout;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function u(t){return t instanceof DOMException&&t.name==="TimeoutError"}function s(t){if(typeof AbortSignal.any=="function")return AbortSignal.any(t);const o=new AbortController,e=()=>{o.abort(),r()},r=()=>{for(const n of t)n.removeEventListener("abort",e)};for(const n of t){if(n.aborted){e();break}n.addEventListener("abort",e)}return o.signal}function l(t){if(typeof AbortSignal.timeout=="function")return AbortSignal.timeout(t);const o=new AbortController,e=setTimeout(()=>{o.abort(new DOMException("The operation timed out.","TimeoutError"))},t);return o.signal.addEventListener("abort",()=>clearTimeout(e)),o.signal}const c={name:"fatcher-middleware-timeout",use:async(t,o)=>{const{timeout:e=6e4,signal:r,onTimeout:n}=t,i=l(e),a=r?s([i,r]):i;return a.addEventListener("abort",()=>{if(i.aborted){n?.();return}}),o({request:new Request(t.request,{signal:a})})}};exports.isTimeoutError=u;exports.timeout=c;
package/dist/index.min.js CHANGED
@@ -1 +1 @@
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={name:"fatcher-middleware-aborter",use:async(o,s)=>{const{onAbort:r,abortController:e=new AbortController}=o,n=()=>{r==null||r(e.signal.reason),e.signal.removeEventListener("abort",n)};r&&e.signal.addEventListener("abort",n);const a=await s({abort:e.abort.bind(e),signal:e.signal});return r&&e.signal.removeEventListener("abort",n),a}};function u(o){return o instanceof DOMException&&o.name==="AbortError"}const l={name:"fatcher-middleware-timeout",use:async(o,s)=>{const{abort:r,timeout:e}=o;let n=null;e&&(n=setTimeout(()=>{r()},e));let a;try{a=await s()}catch(c){throw n&&clearTimeout(n),c}return a}};t.aborter=i,t.isAbortError=u,t.timeout=l,Object.defineProperty(t,"__esModule",{value:!0})});
1
+ (function(o,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(o=typeof globalThis<"u"?globalThis:o||self,i(o.FatcherMiddlewareAborter={}))})(this,(function(o){"use strict";function i(t){return t instanceof DOMException&&t.name==="TimeoutError"}function f(t){if(typeof AbortSignal.any=="function")return AbortSignal.any(t);const e=new AbortController,n=()=>{e.abort(),a()},a=()=>{for(const r of t)r.removeEventListener("abort",n)};for(const r of t){if(r.aborted){n();break}r.addEventListener("abort",n)}return e.signal}function l(t){if(typeof AbortSignal.timeout=="function")return AbortSignal.timeout(t);const e=new AbortController,n=setTimeout(()=>{e.abort(new DOMException("The operation timed out.","TimeoutError"))},t);return e.signal.addEventListener("abort",()=>clearTimeout(n)),e.signal}const c={name:"fatcher-middleware-timeout",use:async(t,e)=>{const{timeout:n=6e4,signal:a,onTimeout:r}=t,u=l(n),s=a?f([u,a]):u;return s.addEventListener("abort",()=>{if(u.aborted){r?.();return}}),e({request:new Request(t.request,{signal:s})})}};o.isTimeoutError=i,o.timeout=c,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
@@ -0,0 +1 @@
1
+ export declare function isTimeoutError(value: unknown): value is DOMException;
@@ -0,0 +1,2 @@
1
+ import { FatcherMiddleware } from 'fatcher';
2
+ export declare const timeout: FatcherMiddleware;
@@ -0,0 +1,7 @@
1
+ declare module 'fatcher' {
2
+ interface FatcherOptions {
3
+ timeout?: number;
4
+ onTimeout?: () => void;
5
+ }
6
+ }
7
+ export {};
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@fatcherjs/middleware-aborter",
3
- "homepage": "https://github.com/fanhaoyuan/fatcher",
3
+ "version": "3.0.0",
4
+ "homepage": "https://github.com/fatcherjs/middleware-aborter",
4
5
  "repository": {
5
6
  "type": "git",
6
- "url": "git+https://github.com/fanhaoyuan/fatcher.git"
7
+ "url": "git+https://github.com/fatcherjs/middleware-aborter.git"
7
8
  },
8
9
  "main": "dist/index.js",
9
10
  "module": "dist/index.esm.js",
@@ -13,16 +14,32 @@
13
14
  "dist"
14
15
  ],
15
16
  "peerDependencies": {
16
- "fatcher": "3.0.0-alpha-11"
17
+ "fatcher": "^3.0.0"
17
18
  },
18
19
  "devDependencies": {
19
- "fatcher": "3.0.0-alpha-11"
20
+ "@fansy/eslint-config": "^1.4.2",
21
+ "@fansy/prettier-config": "^1.1.0",
22
+ "@types/node": "^24.3.1",
23
+ "@typescript-eslint/eslint-plugin": "^8.11.0",
24
+ "@typescript-eslint/parser": "^8.11.0",
25
+ "@vitest/coverage-v8": "3.2.4",
26
+ "eslint": "^8.57.1",
27
+ "eslint-plugin-prettier": "^5.2.1",
28
+ "fatcher": "^3.0.0",
29
+ "husky": "^9.1.7",
30
+ "lint-staged": "^16.1.6",
31
+ "msw": "^2.11.1",
32
+ "prettier": "^3.3.3",
33
+ "prettier-plugin-organize-imports": "^4.1.0",
34
+ "typescript": "^5.9.2",
35
+ "vite": "^7.1.5",
36
+ "vite-plugin-dts": "^4.5.4",
37
+ "vitest": "^3.2.4"
20
38
  },
21
- "version": "3.0.0-alpha-11",
22
39
  "scripts": {
23
- "clean": "rimraf dist",
24
- "build": "npm run clean && rollup -c rollup.config.ts",
25
- "test": "jest",
26
- "test:cov": "jest --coverage"
40
+ "build": "vite build",
41
+ "test": "vitest run",
42
+ "test:cov": "vitest run --coverage",
43
+ "eslint": "eslint ."
27
44
  }
28
45
  }