apollo-link-timeout 4.0.0 → 5.0.1

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 CHANGED
@@ -12,7 +12,7 @@ yarn add apollo-link-timeout
12
12
  ```
13
13
 
14
14
  ## Usage
15
- ```
15
+ ```javascript
16
16
  import ApolloLinkTimeout from 'apollo-link-timeout';
17
17
  import { createHttpLink } from 'apollo-link-http';
18
18
  import { ApolloClient } from 'apollo-client';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apollo-link-timeout",
3
- "version": "4.0.0",
3
+ "version": "5.0.1",
4
4
  "description": "Abort requests that take longer than a specified timeout period",
5
5
  "peerDependencies": {
6
6
  "@apollo/client": "^3.0.0"
@@ -11,15 +11,21 @@
11
11
  "@types/node": "^10.3.4",
12
12
  "graphql": "^15.3.0",
13
13
  "graphql-tag": "^2.11.0",
14
- "jest": "^23.1.0",
15
- "react": "^16.13.1",
16
- "ts-jest": "^22.4.6",
17
- "ts-loader": "^4.4.1",
14
+ "jest": "^28.1.3",
15
+ "jest-environment-jsdom": "^28.1.3",
16
+ "ts-jest": "^28.0.8",
18
17
  "tslint": "^5.8.0",
19
- "typescript": "^2.9.2"
18
+ "typescript": "^4.9.5"
19
+ },
20
+ "main": "./lib/cjs/cjs.js",
21
+ "types": "./lib/types/timeoutLink.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "import": "./lib/esm/timeoutLink.js",
25
+ "require": "./lib/cjs/cjs.js",
26
+ "types": "./lib/types/timeoutLink.d.ts"
27
+ }
20
28
  },
21
- "main": "lib/timeoutLink.js",
22
- "types": "lib/timeoutLink.d.ts",
23
29
  "repository": {
24
30
  "type": "git",
25
31
  "url": "https://github.com/drcallaway/apollo-link-timeout"
@@ -28,10 +34,15 @@
28
34
  "registry": "https://registry.npmjs.org/"
29
35
  },
30
36
  "scripts": {
31
- "build": "tsc",
37
+ "build": "yarn build:cjs && yarn build:esm && yarn build:types",
38
+ "build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
39
+ "build:esm": "tsc -p tsconfig.esm.json && echo '{\"type\": \"module\"}' > lib/esm/package.json && node scripts/fix_apollo_import.mjs",
40
+ "build:types": "tsc -p tsconfig.types.json",
32
41
  "lint": "tslint src/*.ts* src/**/*.ts*",
33
- "test": "jest",
34
- "prepublish": "yarn lint && yarn test && yarn build",
42
+ "test": "yarn test:unit && yarn test:integration",
43
+ "test:integration": "node __tests__/integration/run.mjs",
44
+ "test:unit": "jest",
45
+ "prepublish": "yarn lint && yarn test:unit && yarn build",
35
46
  "deploy": "yarn publish && git push"
36
47
  },
37
48
  "keywords": [
@@ -43,22 +54,33 @@
43
54
  "author": "Dustin Callaway <drcallaway@gmail.com>",
44
55
  "license": "MIT",
45
56
  "files": [
46
- "lib"
57
+ "lib",
58
+ "src"
47
59
  ],
48
60
  "jest": {
49
- "transform": {
50
- ".(ts|tsx)": "ts-jest"
61
+ "globals": {
62
+ "ts-jest": {
63
+ "useESM": true
64
+ }
51
65
  },
52
- "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
53
66
  "moduleFileExtensions": [
54
67
  "ts",
55
68
  "tsx",
56
69
  "js",
57
70
  "json"
58
71
  ],
72
+ "moduleNameMapper": {
73
+ "(.+)\\.js": "$1"
74
+ },
75
+ "testEnvironment": "jsdom",
59
76
  "testPathIgnorePatterns": [
77
+ "__tests__/integration/",
60
78
  "/node_modules/",
61
79
  "/lib/"
62
- ]
80
+ ],
81
+ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
82
+ "transform": {
83
+ ".(ts|tsx)": "ts-jest"
84
+ }
63
85
  }
64
86
  }
@@ -0,0 +1,10 @@
1
+ export default class TimeoutError extends Error {
2
+ public timeout: number;
3
+ public statusCode: number;
4
+
5
+ constructor(message: string, timeout: number, statusCode: number = 408) {
6
+ super(message);
7
+ this.timeout = timeout;
8
+ this.statusCode = statusCode;
9
+ }
10
+ }
package/src/cjs.ts ADDED
@@ -0,0 +1,4 @@
1
+ import TimeoutLink from "./timeoutLink.js";
2
+
3
+ module.exports = TimeoutLink;
4
+ module.exports.default = TimeoutLink;
@@ -0,0 +1,121 @@
1
+ // note, this import is modified when building for ESM via `script/fix_apollo_import.mjs`
2
+ import { ApolloLink, Observable, type Operation, type NextLink } from '@apollo/client/core';
3
+ import type { DefinitionNode } from 'graphql';
4
+ import TimeoutError from './TimeoutError.js';
5
+
6
+ const DEFAULT_TIMEOUT: number = 15000;
7
+
8
+ /**
9
+ * Aborts the request if the timeout expires before the response is received.
10
+ */
11
+ export default class TimeoutLink extends ApolloLink {
12
+ private timeout: number;
13
+ private statusCode?: number;
14
+
15
+ /**
16
+ * Creates a new TimeoutLink instance.
17
+ * Aborts the request if the timeout expires before the response is received.
18
+ *
19
+ * @param timeout - The timeout in milliseconds for the request. Default is 15000ms (15 seconds).
20
+ * @param statusCode - The HTTP status code to return when a timeout occurs. Default is 408 (Request Timeout).
21
+ */
22
+ constructor(timeout?: number, statusCode?: number) {
23
+ super();
24
+ this.timeout = timeout || DEFAULT_TIMEOUT;
25
+ this.statusCode = statusCode;
26
+ }
27
+
28
+ public request(operation: Operation, forward: NextLink) {
29
+ let controller: AbortController;
30
+ let ourController: AbortController;
31
+
32
+ // override timeout from query context
33
+ const requestTimeout = operation.getContext().timeout || this.timeout;
34
+
35
+ // add abort controller and signal object to fetchOptions if they don't already exist
36
+ if (typeof AbortController !== 'undefined') {
37
+ const context = operation.getContext();
38
+ let fetchOptions = context.fetchOptions || {};
39
+
40
+ ourController = new AbortController();
41
+ controller = fetchOptions.controller || ourController;
42
+
43
+ fetchOptions = { ...fetchOptions, controller, signal: controller.signal };
44
+ operation.setContext({ fetchOptions });
45
+ }
46
+
47
+ const chainObservable = forward(operation); // observable for remaining link chain
48
+
49
+ const operationType = (operation.query.definitions as any).find(
50
+ (def: DefinitionNode) => def.kind === 'OperationDefinition'
51
+ ).operation;
52
+
53
+ if (requestTimeout <= 0 || operationType === 'subscription') {
54
+ return chainObservable; // skip this link if timeout is zero or it's a subscription request
55
+ }
56
+
57
+ // create local observable with timeout functionality (unsubscibe from chain observable and
58
+ // return an error if the timeout expires before chain observable resolves)
59
+ const localObservable = new Observable(observer => {
60
+ let timer: any;
61
+
62
+ // listen to chainObservable for result and pass to localObservable if received before timeout
63
+ const subscription = chainObservable.subscribe(
64
+ result => {
65
+ clearTimeout(timer);
66
+ observer.next(result);
67
+ observer.complete();
68
+ },
69
+ error => {
70
+ clearTimeout(timer);
71
+ observer.error(error);
72
+ observer.complete();
73
+ }
74
+ );
75
+
76
+ // if timeout expires before observable completes, abort call, unsubscribe, and return error
77
+ timer = setTimeout(() => {
78
+ if (controller) {
79
+ if (controller.signal.aborted) {
80
+ // already aborted from somewhere else
81
+ return;
82
+ }
83
+
84
+ controller.abort(); // abort fetch operation
85
+
86
+ // if the AbortController in the operation context is one we created,
87
+ // it's now "used up", so we need to remove it to avoid blocking any
88
+ // future retry of the operation.
89
+ const context = operation.getContext();
90
+ const fetchOptions = context.fetchOptions || {};
91
+ if(fetchOptions.controller === ourController && fetchOptions.signal === ourController.signal) {
92
+ operation.setContext({ ...fetchOptions, controller: undefined, signal: undefined });
93
+ }
94
+ }
95
+
96
+ observer.error(new TimeoutError('Timeout exceeded', requestTimeout, this.statusCode));
97
+ subscription.unsubscribe();
98
+ }, requestTimeout);
99
+
100
+ const cancelTimeout = () => {
101
+ clearTimeout(timer);
102
+ subscription.unsubscribe();
103
+ };
104
+
105
+ const ctxRef = operation.getContext().timeoutRef;
106
+ if (ctxRef) {
107
+ ctxRef({ unsubscribe: cancelTimeout });
108
+ }
109
+
110
+ // cancel timeout if aborted from somewhere else
111
+ controller.signal.addEventListener("abort", () => {
112
+ cancelTimeout();
113
+ }, { once: true });
114
+
115
+ // this function is called when a client unsubscribes from localObservable
116
+ return cancelTimeout;
117
+ });
118
+
119
+ return localObservable;
120
+ }
121
+ }
@@ -1,5 +0,0 @@
1
- export default class TimeoutError extends Error {
2
- timeout: number;
3
- statusCode: number;
4
- constructor(message: string, timeout: number, statusCode?: number);
5
- }
@@ -1,25 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6
- return function (d, b) {
7
- extendStatics(d, b);
8
- function __() { this.constructor = d; }
9
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10
- };
11
- })();
12
- Object.defineProperty(exports, "__esModule", { value: true });
13
- var TimeoutError = /** @class */ (function (_super) {
14
- __extends(TimeoutError, _super);
15
- function TimeoutError(message, timeout, statusCode) {
16
- if (statusCode === void 0) { statusCode = 408; }
17
- var _this = _super.call(this, message) || this;
18
- _this.timeout = timeout;
19
- _this.statusCode = statusCode;
20
- return _this;
21
- }
22
- return TimeoutError;
23
- }(Error));
24
- exports.default = TimeoutError;
25
- //# sourceMappingURL=TimeoutError.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"TimeoutError.js","sourceRoot":"","sources":["../src/TimeoutError.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;IAA0C,gCAAK;IAI7C,sBAAY,OAAe,EAAE,OAAe,EAAE,UAAwB;QAAxB,2BAAA,EAAA,gBAAwB;QAAtE,YACE,kBAAM,OAAO,CAAC,SAGf;QAFC,KAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,KAAI,CAAC,UAAU,GAAG,UAAU,CAAC;;IAC/B,CAAC;IACH,mBAAC;AAAD,CAAC,AATD,CAA0C,KAAK,GAS9C"}
@@ -1,11 +0,0 @@
1
- /// <reference types="zen-observable" />
2
- import { ApolloLink, Observable, Operation, NextLink } from '@apollo/client/core';
3
- /**
4
- * Aborts the request if the timeout expires before the response is received.
5
- */
6
- export default class TimeoutLink extends ApolloLink {
7
- private timeout;
8
- private statusCode?;
9
- constructor(timeout: number, statusCode?: number);
10
- request(operation: Operation, forward: NextLink): Observable<{}>;
11
- }
@@ -1,104 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = Object.setPrototypeOf ||
4
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6
- return function (d, b) {
7
- extendStatics(d, b);
8
- function __() { this.constructor = d; }
9
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10
- };
11
- })();
12
- var __assign = (this && this.__assign) || Object.assign || function(t) {
13
- for (var s, i = 1, n = arguments.length; i < n; i++) {
14
- s = arguments[i];
15
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
16
- t[p] = s[p];
17
- }
18
- return t;
19
- };
20
- Object.defineProperty(exports, "__esModule", { value: true });
21
- var core_1 = require("@apollo/client/core");
22
- var TimeoutError_1 = require("./TimeoutError");
23
- var DEFAULT_TIMEOUT = 15000;
24
- /**
25
- * Aborts the request if the timeout expires before the response is received.
26
- */
27
- var TimeoutLink = /** @class */ (function (_super) {
28
- __extends(TimeoutLink, _super);
29
- function TimeoutLink(timeout, statusCode) {
30
- var _this = _super.call(this) || this;
31
- _this.timeout = timeout || DEFAULT_TIMEOUT;
32
- _this.statusCode = statusCode;
33
- return _this;
34
- }
35
- TimeoutLink.prototype.request = function (operation, forward) {
36
- var _this = this;
37
- var controller;
38
- // override timeout from query context
39
- var requestTimeout = operation.getContext().timeout || this.timeout;
40
- // add abort controller and signal object to fetchOptions if they don't already exist
41
- if (typeof AbortController !== 'undefined') {
42
- var context = operation.getContext();
43
- var fetchOptions = context.fetchOptions || {};
44
- controller = fetchOptions.controller || new AbortController();
45
- fetchOptions = __assign({}, fetchOptions, { controller: controller, signal: controller.signal });
46
- operation.setContext({ fetchOptions: fetchOptions });
47
- }
48
- var chainObservable = forward(operation); // observable for remaining link chain
49
- var operationType = operation.query.definitions.find(function (def) { return def.kind === 'OperationDefinition'; }).operation;
50
- if (requestTimeout <= 0 || operationType === 'subscription') {
51
- return chainObservable; // skip this link if timeout is zero or it's a subscription request
52
- }
53
- // create local observable with timeout functionality (unsubscibe from chain observable and
54
- // return an error if the timeout expires before chain observable resolves)
55
- var localObservable = new core_1.Observable(function (observer) {
56
- var timer;
57
- // listen to chainObservable for result and pass to localObservable if received before timeout
58
- var subscription = chainObservable.subscribe(function (result) {
59
- clearTimeout(timer);
60
- observer.next(result);
61
- observer.complete();
62
- }, function (error) {
63
- clearTimeout(timer);
64
- observer.error(error);
65
- observer.complete();
66
- });
67
- // if timeout expires before observable completes, abort call, unsubscribe, and return error
68
- timer = setTimeout(function () {
69
- if (controller) {
70
- controller.abort(); // abort fetch operation
71
- // if the AbortController in the operation context is one we created,
72
- // it's now "used up", so we need to remove it to avoid blocking any
73
- // future retry of the operation.
74
- var context = operation.getContext();
75
- var fetchOptions = context.fetchOptions || {};
76
- if (fetchOptions.controller === controller && fetchOptions.signal === controller.signal) {
77
- fetchOptions = __assign({}, fetchOptions, { controller: null, signal: null });
78
- operation.setContext({ fetchOptions: fetchOptions });
79
- }
80
- }
81
- observer.error(new TimeoutError_1.default('Timeout exceeded', requestTimeout, _this.statusCode));
82
- subscription.unsubscribe();
83
- }, requestTimeout);
84
- var ctxRef = operation.getContext().timeoutRef;
85
- if (ctxRef) {
86
- ctxRef({
87
- unsubscribe: function () {
88
- clearTimeout(timer);
89
- subscription.unsubscribe();
90
- }
91
- });
92
- }
93
- // this function is called when a client unsubscribes from localObservable
94
- return function () {
95
- clearTimeout(timer);
96
- subscription.unsubscribe();
97
- };
98
- });
99
- return localObservable;
100
- };
101
- return TimeoutLink;
102
- }(core_1.ApolloLink));
103
- exports.default = TimeoutLink;
104
- //# sourceMappingURL=timeoutLink.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"timeoutLink.js","source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