apollo-link-timeout 5.0.1 → 5.0.3
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/lib/cjs/TimeoutError.js +30 -0
- package/lib/cjs/TimeoutError.js.map +1 -0
- package/lib/cjs/cjs.js +6 -0
- package/lib/cjs/cjs.js.map +1 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/timeoutLink.js +125 -0
- package/lib/cjs/timeoutLink.js.map +1 -0
- package/lib/esm/TimeoutError.js +28 -0
- package/lib/esm/TimeoutError.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/timeoutLink.js +123 -0
- package/lib/esm/timeoutLink.js.map +1 -0
- package/lib/types/TimeoutError.d.ts +5 -0
- package/lib/types/cjs.d.ts +1 -0
- package/lib/types/timeoutLink.d.ts +18 -0
- package/package.json +9 -5
- package/src/timeoutLink.ts +11 -11
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
var TimeoutError = /** @class */ (function (_super) {
|
|
19
|
+
__extends(TimeoutError, _super);
|
|
20
|
+
function TimeoutError(message, timeout, statusCode) {
|
|
21
|
+
if (statusCode === void 0) { statusCode = 408; }
|
|
22
|
+
var _this = _super.call(this, message) || this;
|
|
23
|
+
_this.timeout = timeout;
|
|
24
|
+
_this.statusCode = statusCode;
|
|
25
|
+
return _this;
|
|
26
|
+
}
|
|
27
|
+
return TimeoutError;
|
|
28
|
+
}(Error));
|
|
29
|
+
exports.default = TimeoutError;
|
|
30
|
+
//# sourceMappingURL=TimeoutError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
package/lib/cjs/cjs.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cjs.js","sourceRoot":"","sources":["../../src/cjs.ts"],"names":[],"mappings":";;AAAA,mDAA2C;AAE3C,MAAM,CAAC,OAAO,GAAG,wBAAW,CAAC;AAC7B,MAAM,CAAC,OAAO,CAAC,OAAO,GAAG,wBAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "commonjs"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
// note, this import is modified when building for ESM via `script/fix_apollo_import.mjs`
|
|
30
|
+
var core_1 = require("@apollo/client/core");
|
|
31
|
+
var TimeoutError_js_1 = require("./TimeoutError.js");
|
|
32
|
+
var DEFAULT_TIMEOUT = 15000;
|
|
33
|
+
/**
|
|
34
|
+
* Aborts the request if the timeout expires before the response is received.
|
|
35
|
+
*/
|
|
36
|
+
var TimeoutLink = /** @class */ (function (_super) {
|
|
37
|
+
__extends(TimeoutLink, _super);
|
|
38
|
+
/**
|
|
39
|
+
* Creates a new TimeoutLink instance.
|
|
40
|
+
* Aborts the request if the timeout expires before the response is received.
|
|
41
|
+
*
|
|
42
|
+
* @param timeout - The timeout in milliseconds for the request. Default is 15000ms (15 seconds).
|
|
43
|
+
* @param statusCode - The HTTP status code to return when a timeout occurs. Default is 408 (Request Timeout).
|
|
44
|
+
*/
|
|
45
|
+
function TimeoutLink(timeout, statusCode) {
|
|
46
|
+
var _this = _super.call(this) || this;
|
|
47
|
+
_this.timeout = timeout || DEFAULT_TIMEOUT;
|
|
48
|
+
_this.statusCode = statusCode;
|
|
49
|
+
return _this;
|
|
50
|
+
}
|
|
51
|
+
TimeoutLink.prototype.request = function (operation, forward) {
|
|
52
|
+
var _this = this;
|
|
53
|
+
var controller;
|
|
54
|
+
var ourController;
|
|
55
|
+
// override timeout from query context
|
|
56
|
+
var requestTimeout = operation.getContext().timeout || this.timeout;
|
|
57
|
+
var operationType = operation.query.definitions.find(function (def) { return def.kind === 'OperationDefinition'; }).operation;
|
|
58
|
+
if (requestTimeout <= 0 || operationType === 'subscription') {
|
|
59
|
+
return forward(operation); // skip this link if timeout is zero or it's a subscription request
|
|
60
|
+
}
|
|
61
|
+
// add abort controller and signal object to fetchOptions if they don't already exist
|
|
62
|
+
if (typeof AbortController !== 'undefined') {
|
|
63
|
+
var context = operation.getContext();
|
|
64
|
+
var fetchOptions = context.fetchOptions || {};
|
|
65
|
+
ourController = new AbortController();
|
|
66
|
+
controller = fetchOptions.controller || ourController;
|
|
67
|
+
fetchOptions = __assign(__assign({}, fetchOptions), { controller: controller, signal: controller.signal });
|
|
68
|
+
operation.setContext({ fetchOptions: fetchOptions });
|
|
69
|
+
}
|
|
70
|
+
var chainObservable = forward(operation); // observable for remaining link chain
|
|
71
|
+
// create local observable with timeout functionality (unsubscibe from chain observable and
|
|
72
|
+
// return an error if the timeout expires before chain observable resolves)
|
|
73
|
+
var localObservable = new core_1.Observable(function (observer) {
|
|
74
|
+
var timer;
|
|
75
|
+
// listen to chainObservable for result and pass to localObservable if received before timeout
|
|
76
|
+
var subscription = chainObservable.subscribe(function (result) {
|
|
77
|
+
clearTimeout(timer);
|
|
78
|
+
observer.next(result);
|
|
79
|
+
observer.complete();
|
|
80
|
+
}, function (error) {
|
|
81
|
+
clearTimeout(timer);
|
|
82
|
+
observer.error(error);
|
|
83
|
+
observer.complete();
|
|
84
|
+
});
|
|
85
|
+
// if timeout expires before observable completes, abort call, unsubscribe, and return error
|
|
86
|
+
timer = setTimeout(function () {
|
|
87
|
+
if (controller) {
|
|
88
|
+
if (controller.signal.aborted) {
|
|
89
|
+
// already aborted from somewhere else
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
controller.abort(); // abort fetch operation
|
|
93
|
+
// if the AbortController in the operation context is one we created,
|
|
94
|
+
// it's now "used up", so we need to remove it to avoid blocking any
|
|
95
|
+
// future retry of the operation.
|
|
96
|
+
var context = operation.getContext();
|
|
97
|
+
var fetchOptions = context.fetchOptions || {};
|
|
98
|
+
if (fetchOptions.controller === ourController && fetchOptions.signal === ourController.signal) {
|
|
99
|
+
operation.setContext(__assign(__assign({}, fetchOptions), { controller: undefined, signal: undefined }));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
observer.error(new TimeoutError_js_1.default('Timeout exceeded', requestTimeout, _this.statusCode));
|
|
103
|
+
subscription.unsubscribe();
|
|
104
|
+
}, requestTimeout);
|
|
105
|
+
var cancelTimeout = function () {
|
|
106
|
+
clearTimeout(timer);
|
|
107
|
+
subscription.unsubscribe();
|
|
108
|
+
};
|
|
109
|
+
var ctxRef = operation.getContext().timeoutRef;
|
|
110
|
+
if (ctxRef) {
|
|
111
|
+
ctxRef({ unsubscribe: cancelTimeout });
|
|
112
|
+
}
|
|
113
|
+
// cancel timeout if aborted from somewhere else
|
|
114
|
+
controller.signal.addEventListener("abort", function () {
|
|
115
|
+
cancelTimeout();
|
|
116
|
+
}, { once: true });
|
|
117
|
+
// this function is called when a client unsubscribes from localObservable
|
|
118
|
+
return cancelTimeout;
|
|
119
|
+
});
|
|
120
|
+
return localObservable;
|
|
121
|
+
};
|
|
122
|
+
return TimeoutLink;
|
|
123
|
+
}(core_1.ApolloLink));
|
|
124
|
+
exports.default = TimeoutLink;
|
|
125
|
+
//# sourceMappingURL=timeoutLink.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+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AAM,EAAE,SAAS,IAAG,CAAC;qBACtF;iBACF;gBAED,QAAQ,CAAC,KAAK,CAAC,IAAI,yBAAY,CAAC,kBAAkB,EAAE,cAAc,EAAE,KAAI,CAAC,UAAU,CAAC,CAAC,CAAC;gBACtF,YAAY,CAAC,WAAW,EAAE,CAAC;YAC7B,CAAC,EAAE,cAAc,CAAC,CAAC;YAEnB,IAAM,aAAa,GAAG;gBACpB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,YAAY,CAAC,WAAW,EAAE,CAAC;YAC7B,CAAC,CAAC;YAEF,IAAM,MAAM,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC,UAAU,CAAC;YACjD,IAAI,MAAM,EAAE;gBACV,MAAM,CAAC,EAAE,WAAW,EAAE,aAAa,EAAE,CAAC,CAAC;aACxC;YAED,gDAAgD;YAChD,UAAU,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE;gBAC1C,aAAa,EAAE,CAAC;YAClB,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAEnB,0EAA0E;YAC1E,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QAEH,OAAO,eAAe,CAAC;IACzB,CAAC;IACH,kBAAC;AAAD,CAAC,AA9GD,CAAyC,iBAAU,GA8GlD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var TimeoutError = /** @class */ (function (_super) {
|
|
17
|
+
__extends(TimeoutError, _super);
|
|
18
|
+
function TimeoutError(message, timeout, statusCode) {
|
|
19
|
+
if (statusCode === void 0) { statusCode = 408; }
|
|
20
|
+
var _this = _super.call(this, message) || this;
|
|
21
|
+
_this.timeout = timeout;
|
|
22
|
+
_this.statusCode = statusCode;
|
|
23
|
+
return _this;
|
|
24
|
+
}
|
|
25
|
+
return TimeoutError;
|
|
26
|
+
}(Error));
|
|
27
|
+
export default TimeoutError;
|
|
28
|
+
//# sourceMappingURL=TimeoutError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type": "module"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var __assign = (this && this.__assign) || function () {
|
|
17
|
+
__assign = Object.assign || function(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
21
|
+
t[p] = s[p];
|
|
22
|
+
}
|
|
23
|
+
return t;
|
|
24
|
+
};
|
|
25
|
+
return __assign.apply(this, arguments);
|
|
26
|
+
};
|
|
27
|
+
// note, this import is modified when building for ESM via `script/fix_apollo_import.mjs`
|
|
28
|
+
import { ApolloLink, Observable } from '@apollo/client/core/index.js';
|
|
29
|
+
import TimeoutError from './TimeoutError.js';
|
|
30
|
+
var DEFAULT_TIMEOUT = 15000;
|
|
31
|
+
/**
|
|
32
|
+
* Aborts the request if the timeout expires before the response is received.
|
|
33
|
+
*/
|
|
34
|
+
var TimeoutLink = /** @class */ (function (_super) {
|
|
35
|
+
__extends(TimeoutLink, _super);
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new TimeoutLink instance.
|
|
38
|
+
* Aborts the request if the timeout expires before the response is received.
|
|
39
|
+
*
|
|
40
|
+
* @param timeout - The timeout in milliseconds for the request. Default is 15000ms (15 seconds).
|
|
41
|
+
* @param statusCode - The HTTP status code to return when a timeout occurs. Default is 408 (Request Timeout).
|
|
42
|
+
*/
|
|
43
|
+
function TimeoutLink(timeout, statusCode) {
|
|
44
|
+
var _this = _super.call(this) || this;
|
|
45
|
+
_this.timeout = timeout || DEFAULT_TIMEOUT;
|
|
46
|
+
_this.statusCode = statusCode;
|
|
47
|
+
return _this;
|
|
48
|
+
}
|
|
49
|
+
TimeoutLink.prototype.request = function (operation, forward) {
|
|
50
|
+
var _this = this;
|
|
51
|
+
var controller;
|
|
52
|
+
var ourController;
|
|
53
|
+
// override timeout from query context
|
|
54
|
+
var requestTimeout = operation.getContext().timeout || this.timeout;
|
|
55
|
+
var operationType = operation.query.definitions.find(function (def) { return def.kind === 'OperationDefinition'; }).operation;
|
|
56
|
+
if (requestTimeout <= 0 || operationType === 'subscription') {
|
|
57
|
+
return forward(operation); // skip this link if timeout is zero or it's a subscription request
|
|
58
|
+
}
|
|
59
|
+
// add abort controller and signal object to fetchOptions if they don't already exist
|
|
60
|
+
if (typeof AbortController !== 'undefined') {
|
|
61
|
+
var context = operation.getContext();
|
|
62
|
+
var fetchOptions = context.fetchOptions || {};
|
|
63
|
+
ourController = new AbortController();
|
|
64
|
+
controller = fetchOptions.controller || ourController;
|
|
65
|
+
fetchOptions = __assign(__assign({}, fetchOptions), { controller: controller, signal: controller.signal });
|
|
66
|
+
operation.setContext({ fetchOptions: fetchOptions });
|
|
67
|
+
}
|
|
68
|
+
var chainObservable = forward(operation); // observable for remaining link chain
|
|
69
|
+
// create local observable with timeout functionality (unsubscibe from chain observable and
|
|
70
|
+
// return an error if the timeout expires before chain observable resolves)
|
|
71
|
+
var localObservable = new Observable(function (observer) {
|
|
72
|
+
var timer;
|
|
73
|
+
// listen to chainObservable for result and pass to localObservable if received before timeout
|
|
74
|
+
var subscription = chainObservable.subscribe(function (result) {
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
observer.next(result);
|
|
77
|
+
observer.complete();
|
|
78
|
+
}, function (error) {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
observer.error(error);
|
|
81
|
+
observer.complete();
|
|
82
|
+
});
|
|
83
|
+
// if timeout expires before observable completes, abort call, unsubscribe, and return error
|
|
84
|
+
timer = setTimeout(function () {
|
|
85
|
+
if (controller) {
|
|
86
|
+
if (controller.signal.aborted) {
|
|
87
|
+
// already aborted from somewhere else
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
controller.abort(); // abort fetch operation
|
|
91
|
+
// if the AbortController in the operation context is one we created,
|
|
92
|
+
// it's now "used up", so we need to remove it to avoid blocking any
|
|
93
|
+
// future retry of the operation.
|
|
94
|
+
var context = operation.getContext();
|
|
95
|
+
var fetchOptions = context.fetchOptions || {};
|
|
96
|
+
if (fetchOptions.controller === ourController && fetchOptions.signal === ourController.signal) {
|
|
97
|
+
operation.setContext(__assign(__assign({}, fetchOptions), { controller: undefined, signal: undefined }));
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
observer.error(new TimeoutError('Timeout exceeded', requestTimeout, _this.statusCode));
|
|
101
|
+
subscription.unsubscribe();
|
|
102
|
+
}, requestTimeout);
|
|
103
|
+
var cancelTimeout = function () {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
subscription.unsubscribe();
|
|
106
|
+
};
|
|
107
|
+
var ctxRef = operation.getContext().timeoutRef;
|
|
108
|
+
if (ctxRef) {
|
|
109
|
+
ctxRef({ unsubscribe: cancelTimeout });
|
|
110
|
+
}
|
|
111
|
+
// cancel timeout if aborted from somewhere else
|
|
112
|
+
controller.signal.addEventListener("abort", function () {
|
|
113
|
+
cancelTimeout();
|
|
114
|
+
}, { once: true });
|
|
115
|
+
// this function is called when a client unsubscribes from localObservable
|
|
116
|
+
return cancelTimeout;
|
|
117
|
+
});
|
|
118
|
+
return localObservable;
|
|
119
|
+
};
|
|
120
|
+
return TimeoutLink;
|
|
121
|
+
}(ApolloLink));
|
|
122
|
+
export default TimeoutLink;
|
|
123
|
+
//# sourceMappingURL=timeoutLink.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeoutLink.js","sourceRoot":"","sources":["../../src/timeoutLink.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yFAAyF;AACzF,OAAO,EAAE,UA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,CAAC,IAAI,YAAY,CAAC,kBAAkB,EAAE,cAAc,EAAE,KAAI,CAAC,UAAU,CAAC,CAAC,CAAC;gBACtF,YAAY,CAAC,WAAW,EAAE,CAAC;YAC7B,CAAC,EAAE,cAAc,CAAC,CAAC;YAEnB,IAAM,aAAa,GAAG;gBACpB,YAAY,CAAC,KAAK,CAAC,CAAC;gBACpB,YAAY,CAAC,WAAW,EAAE,CAAC;YAC7B,CAAC,CAAC;YAEF,IAAM,MAAM,GAAG,SAAS,CAAC,UAAU,EAAE,CAAC,UAAU,CAAC;YACjD,IAAI,MAAM,EAAE;gBACV,MAAM,CAAC,EAAE,WAAW,EAAE,aAAa,EAAE,CAAC,CAAC;aACxC;YAED,gDAAgD;YAChD,UAAU,CAAC,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE;gBAC1C,aAAa,EAAE,CAAC;YAClB,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAEnB,0EAA0E;YAC1E,OAAO,aAAa,CAAC;QACvB,CAAC,CAAC,CAAC;QAEH,OAAO,eAAe,CAAC;IACzB,CAAC;IACH,kBAAC;AAAD,CAAC,AA9GD,CAAyC,UAAU,GA8GlD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/// <reference types="zen-observable" />
|
|
2
|
+
import { ApolloLink, Observable, type Operation, type NextLink, type FetchResult } 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
|
+
/**
|
|
10
|
+
* Creates a new TimeoutLink instance.
|
|
11
|
+
* Aborts the request if the timeout expires before the response is received.
|
|
12
|
+
*
|
|
13
|
+
* @param timeout - The timeout in milliseconds for the request. Default is 15000ms (15 seconds).
|
|
14
|
+
* @param statusCode - The HTTP status code to return when a timeout occurs. Default is 408 (Request Timeout).
|
|
15
|
+
*/
|
|
16
|
+
constructor(timeout?: number, statusCode?: number);
|
|
17
|
+
request(operation: Operation, forward: NextLink): Observable<FetchResult>;
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "apollo-link-timeout",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.3",
|
|
4
4
|
"description": "Abort requests that take longer than a specified timeout period",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"@apollo/client": "^3.0.0"
|
|
7
7
|
},
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@apollo/client": "^3.
|
|
9
|
+
"@apollo/client": "^3.13.9",
|
|
10
10
|
"@types/jest": "^23.1.1",
|
|
11
11
|
"@types/node": "^10.3.4",
|
|
12
12
|
"graphql": "^15.3.0",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"jest": {
|
|
61
61
|
"globals": {
|
|
62
62
|
"ts-jest": {
|
|
63
|
-
|
|
63
|
+
"useESM": true
|
|
64
64
|
}
|
|
65
65
|
},
|
|
66
66
|
"moduleFileExtensions": [
|
|
@@ -72,13 +72,17 @@
|
|
|
72
72
|
"moduleNameMapper": {
|
|
73
73
|
"(.+)\\.js": "$1"
|
|
74
74
|
},
|
|
75
|
-
"testEnvironment": "
|
|
75
|
+
"testEnvironment": "./__tests__/fixedEnvironment.js",
|
|
76
|
+
"transformIgnorePatterns": [
|
|
77
|
+
"__tests__/fixedEnvironment.js",
|
|
78
|
+
"/node_modules/"
|
|
79
|
+
],
|
|
76
80
|
"testPathIgnorePatterns": [
|
|
77
81
|
"__tests__/integration/",
|
|
78
82
|
"/node_modules/",
|
|
79
83
|
"/lib/"
|
|
80
84
|
],
|
|
81
|
-
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx
|
|
85
|
+
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
|
|
82
86
|
"transform": {
|
|
83
87
|
".(ts|tsx)": "ts-jest"
|
|
84
88
|
}
|
package/src/timeoutLink.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
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';
|
|
2
|
+
import { ApolloLink, Observable, type Operation, type NextLink, type FetchResult } from '@apollo/client/core';
|
|
3
3
|
import type { DefinitionNode } from 'graphql';
|
|
4
4
|
import TimeoutError from './TimeoutError.js';
|
|
5
5
|
|
|
@@ -25,13 +25,21 @@ export default class TimeoutLink extends ApolloLink {
|
|
|
25
25
|
this.statusCode = statusCode;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
public request(operation: Operation, forward: NextLink) {
|
|
28
|
+
public request(operation: Operation, forward: NextLink): Observable<FetchResult> {
|
|
29
29
|
let controller: AbortController;
|
|
30
30
|
let ourController: AbortController;
|
|
31
31
|
|
|
32
32
|
// override timeout from query context
|
|
33
33
|
const requestTimeout = operation.getContext().timeout || this.timeout;
|
|
34
34
|
|
|
35
|
+
const operationType = (operation.query.definitions as any).find(
|
|
36
|
+
(def: DefinitionNode) => def.kind === 'OperationDefinition'
|
|
37
|
+
).operation;
|
|
38
|
+
|
|
39
|
+
if (requestTimeout <= 0 || operationType === 'subscription') {
|
|
40
|
+
return forward(operation); // skip this link if timeout is zero or it's a subscription request
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
// add abort controller and signal object to fetchOptions if they don't already exist
|
|
36
44
|
if (typeof AbortController !== 'undefined') {
|
|
37
45
|
const context = operation.getContext();
|
|
@@ -46,17 +54,9 @@ export default class TimeoutLink extends ApolloLink {
|
|
|
46
54
|
|
|
47
55
|
const chainObservable = forward(operation); // observable for remaining link chain
|
|
48
56
|
|
|
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
57
|
// create local observable with timeout functionality (unsubscibe from chain observable and
|
|
58
58
|
// return an error if the timeout expires before chain observable resolves)
|
|
59
|
-
const localObservable = new Observable(observer => {
|
|
59
|
+
const localObservable = new Observable<FetchResult>(observer => {
|
|
60
60
|
let timer: any;
|
|
61
61
|
|
|
62
62
|
// listen to chainObservable for result and pass to localObservable if received before timeout
|