@fatcherjs/middleware-aborter 3.0.0-alpha-4 → 3.0.0-alpha-5

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.
@@ -0,0 +1,24 @@
1
+ import { FatcherMiddleware } from 'fatcher';
2
+
3
+ /**
4
+ * Confirm an error whether is DOMException
5
+ * @param error
6
+ * @returns
7
+ */
8
+ declare function isAbortError(error: unknown): error is DOMException;
9
+
10
+ interface AborterOptions {
11
+ timeout?: number;
12
+ signal?: AbortSignal;
13
+ abort?: () => void;
14
+ onAbort?: () => void;
15
+ }
16
+ declare module 'fatcher' {
17
+ interface FatcherRequest {
18
+ abort: () => void;
19
+ }
20
+ }
21
+
22
+ declare const aborter: (options?: AborterOptions) => FatcherMiddleware;
23
+
24
+ export { AborterOptions, aborter, isAbortError };
@@ -0,0 +1,32 @@
1
+ import 'fatcher';
2
+
3
+ function isAbortError(error) {
4
+ return error instanceof DOMException && error.name === "AbortError";
5
+ }
6
+
7
+ const aborter = (options = {}) => {
8
+ return async (req, next) => {
9
+ const { onAbort, timeout } = options;
10
+ const abortController = new AbortController();
11
+ if (onAbort) {
12
+ abortController.signal.addEventListener("abort", () => onAbort());
13
+ }
14
+ const promise = next({
15
+ abort: abortController.abort,
16
+ signal: abortController.signal
17
+ });
18
+ let timer = null;
19
+ if (timeout) {
20
+ timer = setTimeout(() => {
21
+ abortController.abort();
22
+ }, timeout);
23
+ }
24
+ const response = await promise;
25
+ if (timer) {
26
+ clearTimeout(timer);
27
+ }
28
+ return response;
29
+ };
30
+ };
31
+
32
+ export { aborter, isAbortError };
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ require('fatcher');
6
+
7
+ function isAbortError(error) {
8
+ return error instanceof DOMException && error.name === "AbortError";
9
+ }
10
+
11
+ const aborter = (options = {}) => {
12
+ return async (req, next) => {
13
+ const { onAbort, timeout } = options;
14
+ const abortController = new AbortController();
15
+ if (onAbort) {
16
+ abortController.signal.addEventListener("abort", () => onAbort());
17
+ }
18
+ const promise = next({
19
+ abort: abortController.abort,
20
+ signal: abortController.signal
21
+ });
22
+ let timer = null;
23
+ if (timeout) {
24
+ timer = setTimeout(() => {
25
+ abortController.abort();
26
+ }, timeout);
27
+ }
28
+ const response = await promise;
29
+ if (timer) {
30
+ clearTimeout(timer);
31
+ }
32
+ return response;
33
+ };
34
+ };
35
+
36
+ exports.aborter = aborter;
37
+ exports.isAbortError = isAbortError;
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports=="object"&&typeof module!="undefined"?t(exports):typeof define=="function"&&define.amd?define(["exports"],t):(e=typeof globalThis!="undefined"?globalThis:e||self,t(e.FatcherMiddlewareAborter={}))})(this,function(e){"use strict";function t(o){return o instanceof DOMException&&o.name==="AbortError"}const a=(o={})=>async(b,u)=>{const{onAbort:i,timeout:s}=o,r=new AbortController;i&&r.signal.addEventListener("abort",()=>i());const f=u({abort:r.abort,signal:r.signal});let n=null;s&&(n=setTimeout(()=>{r.abort()},s));const l=await f;return n&&clearTimeout(n),l};e.aborter=a,e.isAbortError=t,Object.defineProperty(e,"__esModule",{value:!0})});
package/package.json CHANGED
@@ -13,12 +13,12 @@
13
13
  "dist"
14
14
  ],
15
15
  "peerDependencies": {
16
- "fatcher": "3.0.0-alpha-4"
16
+ "fatcher": "3.0.0-alpha-5"
17
17
  },
18
18
  "devDependencies": {
19
- "fatcher": "3.0.0-alpha-4"
19
+ "fatcher": "3.0.0-alpha-5"
20
20
  },
21
- "version": "3.0.0-alpha-4",
21
+ "version": "3.0.0-alpha-5",
22
22
  "scripts": {
23
23
  "clean": "rimraf dist",
24
24
  "build": "npm run clean && rollup -c rollup.config.ts",