api-def 0.9.1 → 0.9.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/README.md +1 -1
- package/cjs/Api.d.ts +35 -35
- package/cjs/Api.js +140 -144
- package/cjs/ApiConstants.d.ts +58 -58
- package/cjs/ApiConstants.js +55 -55
- package/cjs/ApiTypes.d.ts +103 -103
- package/cjs/ApiTypes.js +4 -4
- package/cjs/ApiUtils.d.ts +6 -6
- package/cjs/ApiUtils.js +51 -53
- package/cjs/Endpoint.d.ts +52 -52
- package/cjs/Endpoint.js +104 -108
- package/cjs/EndpointBuilder.d.ts +14 -14
- package/cjs/EndpointBuilder.js +46 -46
- package/cjs/MockingTypes.d.ts +30 -30
- package/cjs/MockingTypes.js +2 -2
- package/cjs/QueryHandling.d.ts +3 -3
- package/cjs/QueryHandling.js +24 -24
- package/cjs/RequestConfig.d.ts +2 -2
- package/cjs/RequestConfig.js +74 -74
- package/cjs/RequestContext.d.ts +48 -48
- package/cjs/RequestContext.js +235 -238
- package/cjs/RequestError.d.ts +30 -30
- package/cjs/RequestError.js +47 -47
- package/cjs/Requester.d.ts +3 -3
- package/cjs/Requester.js +335 -336
- package/cjs/TextDecoding.d.ts +1 -1
- package/cjs/TextDecoding.js +152 -139
- package/cjs/UtilTypes.d.ts +5 -5
- package/cjs/UtilTypes.js +2 -2
- package/cjs/Utils.d.ts +15 -15
- package/cjs/Utils.js +79 -77
- package/cjs/Validation.d.ts +8 -8
- package/cjs/Validation.js +2 -2
- package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
- package/cjs/backend/AxiosRequestBackend.js +99 -99
- package/cjs/backend/FetchRequestBackend.d.ts +15 -15
- package/cjs/backend/FetchRequestBackend.js +203 -202
- package/cjs/backend/MockRequestBackend.d.ts +11 -11
- package/cjs/backend/MockRequestBackend.js +164 -164
- package/cjs/backend/RequestBackend.d.ts +19 -19
- package/cjs/backend/RequestBackend.js +2 -2
- package/cjs/cache/CacheBackend.d.ts +6 -6
- package/cjs/cache/CacheBackend.js +2 -2
- package/cjs/cache/Caching.d.ts +10 -10
- package/cjs/cache/Caching.js +88 -88
- package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
- package/cjs/cache/LocalForageCacheBackend.js +24 -24
- package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
- package/cjs/cache/LocalStorageCacheBackend.js +77 -77
- package/cjs/index.d.ts +13 -13
- package/cjs/index.js +41 -41
- package/cjs/middleware/CacheMiddleware.d.ts +7 -7
- package/cjs/middleware/CacheMiddleware.js +107 -107
- package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
- package/cjs/middleware/LoggingMiddleware.js +82 -88
- package/cjs/util/retry/index.d.ts +3 -3
- package/cjs/util/retry/index.js +52 -52
- package/cjs/util/retry/lib/retry.d.ts +3 -3
- package/cjs/util/retry/lib/retry.js +50 -54
- package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
- package/cjs/util/retry/lib/retryOperation.js +130 -134
- package/esm/Api.d.ts +35 -35
- package/esm/Api.js +92 -98
- package/esm/ApiConstants.d.ts +58 -58
- package/esm/ApiConstants.js +52 -52
- package/esm/ApiTypes.d.ts +103 -103
- package/esm/ApiTypes.js +1 -1
- package/esm/ApiUtils.d.ts +6 -6
- package/esm/ApiUtils.js +43 -45
- package/esm/Endpoint.d.ts +52 -52
- package/esm/Endpoint.js +66 -70
- package/esm/EndpointBuilder.d.ts +14 -14
- package/esm/EndpointBuilder.js +31 -31
- package/esm/MockingTypes.d.ts +30 -30
- package/esm/MockingTypes.js +1 -1
- package/esm/QueryHandling.d.ts +3 -3
- package/esm/QueryHandling.js +19 -19
- package/esm/RequestConfig.d.ts +2 -2
- package/esm/RequestConfig.js +56 -56
- package/esm/RequestContext.d.ts +48 -48
- package/esm/RequestContext.js +167 -170
- package/esm/RequestError.d.ts +30 -30
- package/esm/RequestError.js +42 -42
- package/esm/Requester.d.ts +3 -3
- package/esm/Requester.js +252 -253
- package/esm/TextDecoding.d.ts +1 -1
- package/esm/TextDecoding.js +148 -135
- package/esm/UtilTypes.d.ts +5 -5
- package/esm/UtilTypes.js +1 -1
- package/esm/Utils.d.ts +15 -15
- package/esm/Utils.js +66 -68
- package/esm/Validation.d.ts +8 -8
- package/esm/Validation.js +1 -1
- package/esm/backend/AxiosRequestBackend.d.ts +13 -13
- package/esm/backend/AxiosRequestBackend.js +62 -62
- package/esm/backend/FetchRequestBackend.d.ts +15 -15
- package/esm/backend/FetchRequestBackend.js +136 -136
- package/esm/backend/MockRequestBackend.d.ts +11 -11
- package/esm/backend/MockRequestBackend.js +116 -116
- package/esm/backend/RequestBackend.d.ts +19 -19
- package/esm/backend/RequestBackend.js +1 -1
- package/esm/cache/CacheBackend.d.ts +6 -6
- package/esm/cache/CacheBackend.js +1 -1
- package/esm/cache/Caching.d.ts +10 -10
- package/esm/cache/Caching.js +39 -39
- package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
- package/esm/cache/LocalForageCacheBackend.js +20 -20
- package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
- package/esm/cache/LocalStorageCacheBackend.js +32 -32
- package/esm/index.d.ts +13 -13
- package/esm/index.js +14 -15
- package/esm/middleware/CacheMiddleware.d.ts +7 -7
- package/esm/middleware/CacheMiddleware.js +58 -58
- package/esm/middleware/LoggingMiddleware.d.ts +6 -6
- package/esm/middleware/LoggingMiddleware.js +77 -83
- package/esm/util/retry/index.d.ts +3 -3
- package/esm/util/retry/index.js +50 -50
- package/esm/util/retry/lib/retry.d.ts +3 -3
- package/esm/util/retry/lib/retry.js +35 -39
- package/esm/util/retry/lib/retryOperation.d.ts +27 -27
- package/esm/util/retry/lib/retryOperation.js +126 -132
- package/package.json +19 -34
package/cjs/Requester.js
CHANGED
|
@@ -1,336 +1,335 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
-
});
|
|
21
|
-
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.submit = void 0;
|
|
51
|
-
var
|
|
52
|
-
var
|
|
53
|
-
var
|
|
54
|
-
var
|
|
55
|
-
var
|
|
56
|
-
var
|
|
57
|
-
var
|
|
58
|
-
var locks = {};
|
|
59
|
-
var runningOperations = {};
|
|
60
|
-
var MOCK_REQUEST_BACKEND = new MockRequestBackend_1.default();
|
|
61
|
-
var submit = function (host, config, mocking) { return __awaiter(void 0, void 0, void 0, function () {
|
|
62
|
-
var computedConfig, backend, context, key, lock, lockedContext, response, successEventResult, error_1;
|
|
63
|
-
return __generator(this, function (_a) {
|
|
64
|
-
switch (_a.label) {
|
|
65
|
-
case 0:
|
|
66
|
-
computedConfig = host.computeConfig(config);
|
|
67
|
-
backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
68
|
-
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
69
|
-
key = context.key;
|
|
70
|
-
lock = (context.computedConfig || {}).lock;
|
|
71
|
-
if (typeof lock === "string") {
|
|
72
|
-
lockedContext = locks[lock];
|
|
73
|
-
if (lockedContext && lockedContext.id !== context.id) {
|
|
74
|
-
lockedContext.cancel();
|
|
75
|
-
}
|
|
76
|
-
locks[lock] = context;
|
|
77
|
-
}
|
|
78
|
-
_a.label = 1;
|
|
79
|
-
case 1:
|
|
80
|
-
_a.trys.push([1, 4, 5, 6]);
|
|
81
|
-
return [4 /*yield*/, (runningOperations[key] = makeRequest(context))];
|
|
82
|
-
case 2:
|
|
83
|
-
response = _a.sent();
|
|
84
|
-
return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.Success)];
|
|
85
|
-
case 3:
|
|
86
|
-
successEventResult = _a.sent();
|
|
87
|
-
if (successEventResult &&
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
var
|
|
117
|
-
var
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
case
|
|
122
|
-
|
|
123
|
-
beforeSendEventResult
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
_b.
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
context.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
var
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
parsedResponse_1 =
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
contentType = parsedResponse_1.headers["content-type"];
|
|
262
|
-
inferredResponseType = (0, ApiUtils_1.inferResponseType)(contentType);
|
|
263
|
-
if (!error) {
|
|
264
|
-
// expand to array buffer once we support that in inferResponseType
|
|
265
|
-
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
266
|
-
throw (0, RequestError_1.convertToRequestError)({
|
|
267
|
-
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
268
|
-
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
269
|
-
response: parsedResponse_1,
|
|
270
|
-
context: context,
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
// transform arrayBuffer to json
|
|
274
|
-
if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
|
|
275
|
-
if (parsedResponse_1.data &&
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
var
|
|
301
|
-
var
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
case
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
}); };
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
16
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
17
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
18
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
19
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
+
function step(op) {
|
|
27
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
29
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
+
switch (op[0]) {
|
|
32
|
+
case 0: case 1: t = op; break;
|
|
33
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
+
default:
|
|
37
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
+
if (t[2]) _.ops.pop();
|
|
42
|
+
_.trys.pop(); continue;
|
|
43
|
+
}
|
|
44
|
+
op = body.call(thisArg, _);
|
|
45
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
+
exports.submit = void 0;
|
|
51
|
+
var ApiConstants_1 = require("./ApiConstants");
|
|
52
|
+
var ApiUtils_1 = require("./ApiUtils");
|
|
53
|
+
var RequestContext_1 = require("./RequestContext");
|
|
54
|
+
var RequestError_1 = require("./RequestError");
|
|
55
|
+
var TextDecoding_1 = require("./TextDecoding");
|
|
56
|
+
var MockRequestBackend_1 = require("./backend/MockRequestBackend");
|
|
57
|
+
var retry_1 = require("./util/retry");
|
|
58
|
+
var locks = {};
|
|
59
|
+
var runningOperations = {};
|
|
60
|
+
var MOCK_REQUEST_BACKEND = new MockRequestBackend_1.default();
|
|
61
|
+
var submit = function (host, config, mocking) { return __awaiter(void 0, void 0, void 0, function () {
|
|
62
|
+
var computedConfig, backend, context, key, lock, lockedContext, response, successEventResult, error_1;
|
|
63
|
+
return __generator(this, function (_a) {
|
|
64
|
+
switch (_a.label) {
|
|
65
|
+
case 0:
|
|
66
|
+
computedConfig = host.computeConfig(config);
|
|
67
|
+
backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
68
|
+
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
69
|
+
key = context.key;
|
|
70
|
+
lock = (context.computedConfig || {}).lock;
|
|
71
|
+
if (typeof lock === "string") {
|
|
72
|
+
lockedContext = locks[lock];
|
|
73
|
+
if (lockedContext && lockedContext.id !== context.id) {
|
|
74
|
+
lockedContext.cancel();
|
|
75
|
+
}
|
|
76
|
+
locks[lock] = context;
|
|
77
|
+
}
|
|
78
|
+
_a.label = 1;
|
|
79
|
+
case 1:
|
|
80
|
+
_a.trys.push([1, 4, 5, 6]);
|
|
81
|
+
return [4 /*yield*/, (runningOperations[key] = makeRequest(context))];
|
|
82
|
+
case 2:
|
|
83
|
+
response = _a.sent();
|
|
84
|
+
return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.Success)];
|
|
85
|
+
case 3:
|
|
86
|
+
successEventResult = _a.sent();
|
|
87
|
+
if (successEventResult && successEventResult.type === ApiConstants_1.EventResultType.Respond) {
|
|
88
|
+
context.response = response = successEventResult.response;
|
|
89
|
+
}
|
|
90
|
+
delete runningOperations[key];
|
|
91
|
+
return [2 /*return*/, response];
|
|
92
|
+
case 4:
|
|
93
|
+
error_1 = _a.sent();
|
|
94
|
+
delete runningOperations[key];
|
|
95
|
+
throw error_1;
|
|
96
|
+
case 5:
|
|
97
|
+
if (typeof lock === "string") {
|
|
98
|
+
delete locks[lock];
|
|
99
|
+
}
|
|
100
|
+
return [7 /*endfinally*/];
|
|
101
|
+
case 6: return [2 /*return*/];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}); };
|
|
105
|
+
exports.submit = submit;
|
|
106
|
+
var parseRetryOptions = function (retryConfig) {
|
|
107
|
+
if (retryConfig && typeof retryConfig === "object") {
|
|
108
|
+
return retryConfig;
|
|
109
|
+
}
|
|
110
|
+
if (typeof retryConfig === "number") {
|
|
111
|
+
return { maxAttempts: retryConfig };
|
|
112
|
+
}
|
|
113
|
+
return { maxAttempts: 0 };
|
|
114
|
+
};
|
|
115
|
+
var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0, function () {
|
|
116
|
+
var beforeSendEventResult, retryOptions, internalRetryOptions, performRequest, response;
|
|
117
|
+
var _a, _b, _c;
|
|
118
|
+
return __generator(this, function (_d) {
|
|
119
|
+
switch (_d.label) {
|
|
120
|
+
case 0: return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.BeforeSend)];
|
|
121
|
+
case 1:
|
|
122
|
+
beforeSendEventResult = _d.sent();
|
|
123
|
+
if (beforeSendEventResult && beforeSendEventResult.type === ApiConstants_1.EventResultType.Respond) {
|
|
124
|
+
return [2 /*return*/, (context.response = beforeSendEventResult.response)];
|
|
125
|
+
}
|
|
126
|
+
// validation
|
|
127
|
+
if (context.validation.query) {
|
|
128
|
+
try {
|
|
129
|
+
context.computedConfig.queryObject = context.validation.query.parse(context.computedConfig.queryObject);
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
133
|
+
error: error,
|
|
134
|
+
code: RequestError_1.RequestErrorCode.VALIDATION_QUERY_VALIDATE_ERROR,
|
|
135
|
+
context: context,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (context.validation.body) {
|
|
140
|
+
try {
|
|
141
|
+
context.computedConfig.body = context.validation.body.parse(context.computedConfig.body);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
145
|
+
error: error,
|
|
146
|
+
code: RequestError_1.RequestErrorCode.VALIDATION_BODY_VALIDATE_ERROR,
|
|
147
|
+
context: context,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
retryOptions = parseRetryOptions((_a = context.computedConfig) === null || _a === void 0 ? void 0 : _a.retry);
|
|
152
|
+
internalRetryOptions = {
|
|
153
|
+
retries: retryOptions.maxAttempts,
|
|
154
|
+
// assume most users won't want to tune the delay between retries
|
|
155
|
+
minTimeout: (_b = retryOptions.minDelay) !== null && _b !== void 0 ? _b : 200,
|
|
156
|
+
maxTimeout: (_c = retryOptions.maxDelay) !== null && _c !== void 0 ? _c : 1000,
|
|
157
|
+
randomize: true,
|
|
158
|
+
};
|
|
159
|
+
context.stats.attempt = 0;
|
|
160
|
+
performRequest = function (fnBail, attemptCount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
161
|
+
var _a, promise, canceler, response_1, parsedResponse, rawError_1, error, errorEventResult, forceRetry, unrecoverableErrorEventResult;
|
|
162
|
+
return __generator(this, function (_b) {
|
|
163
|
+
switch (_b.label) {
|
|
164
|
+
case 0:
|
|
165
|
+
context.stats.attempt++;
|
|
166
|
+
_b.label = 1;
|
|
167
|
+
case 1:
|
|
168
|
+
_b.trys.push([1, 4, , 8]);
|
|
169
|
+
_a = context.backend.makeRequest(context), promise = _a.promise, canceler = _a.canceler;
|
|
170
|
+
context.addCanceller(canceler);
|
|
171
|
+
return [4 /*yield*/, promise];
|
|
172
|
+
case 2:
|
|
173
|
+
response_1 = _b.sent();
|
|
174
|
+
return [4 /*yield*/, parseResponse(context, response_1)];
|
|
175
|
+
case 3:
|
|
176
|
+
parsedResponse = (_b.sent());
|
|
177
|
+
if (!(0, ApiUtils_1.isAcceptableStatus)(parsedResponse.status, context.computedConfig.acceptableStatus)) {
|
|
178
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
179
|
+
error: new Error("[api-def] Invalid response status code '".concat(parsedResponse.status, "'")),
|
|
180
|
+
response: parsedResponse,
|
|
181
|
+
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_STATUS,
|
|
182
|
+
context: context,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
context.response = parsedResponse;
|
|
186
|
+
return [2 /*return*/, parsedResponse];
|
|
187
|
+
case 4:
|
|
188
|
+
rawError_1 = _b.sent();
|
|
189
|
+
if (context.cancelled) {
|
|
190
|
+
rawError_1.isCancelledRequest = true;
|
|
191
|
+
}
|
|
192
|
+
return [4 /*yield*/, parseError(context, rawError_1)];
|
|
193
|
+
case 5:
|
|
194
|
+
error = _b.sent();
|
|
195
|
+
context.error = error;
|
|
196
|
+
context.response = error.response;
|
|
197
|
+
return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.Error)];
|
|
198
|
+
case 6:
|
|
199
|
+
errorEventResult = _b.sent();
|
|
200
|
+
if ((errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === ApiConstants_1.EventResultType.Respond) {
|
|
201
|
+
return [2 /*return*/, errorEventResult.response];
|
|
202
|
+
}
|
|
203
|
+
forceRetry = (errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === ApiConstants_1.EventResultType.Retry;
|
|
204
|
+
if (forceRetry) {
|
|
205
|
+
return [2 /*return*/, performRequest(fnBail, attemptCount)];
|
|
206
|
+
}
|
|
207
|
+
// allow retry logic to handle
|
|
208
|
+
if (!retryOptions.shouldRetry || retryOptions.shouldRetry(error)) {
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.UnrecoverableError)];
|
|
212
|
+
case 7:
|
|
213
|
+
unrecoverableErrorEventResult = _b.sent();
|
|
214
|
+
if (unrecoverableErrorEventResult) {
|
|
215
|
+
if (unrecoverableErrorEventResult.type === ApiConstants_1.EventResultType.Respond) {
|
|
216
|
+
return [2 /*return*/, unrecoverableErrorEventResult.response];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
fnBail(error);
|
|
220
|
+
return [3 /*break*/, 8];
|
|
221
|
+
case 8: return [2 /*return*/];
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}); };
|
|
225
|
+
return [4 /*yield*/, (0, retry_1.default)(performRequest, internalRetryOptions)];
|
|
226
|
+
case 2:
|
|
227
|
+
response = _d.sent();
|
|
228
|
+
if (context.validation.response) {
|
|
229
|
+
try {
|
|
230
|
+
response.data = context.validation.response.parse(response.data);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
234
|
+
error: error,
|
|
235
|
+
code: RequestError_1.RequestErrorCode.VALIDATION_RESPONSE_VALIDATE_ERROR,
|
|
236
|
+
context: context,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return [2 /*return*/, response];
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}); };
|
|
244
|
+
var parseResponse = function (context, response, error) { return __awaiter(void 0, void 0, void 0, function () {
|
|
245
|
+
var parsedResponse_1, contentType, inferredResponseType, data, decodedData;
|
|
246
|
+
var _a;
|
|
247
|
+
return __generator(this, function (_b) {
|
|
248
|
+
switch (_b.label) {
|
|
249
|
+
case 0:
|
|
250
|
+
if (!response) return [3 /*break*/, 2];
|
|
251
|
+
return [4 /*yield*/, context.backend.convertResponse(context, response)];
|
|
252
|
+
case 1:
|
|
253
|
+
parsedResponse_1 = _b.sent();
|
|
254
|
+
// lowercase all header names
|
|
255
|
+
parsedResponse_1.headers =
|
|
256
|
+
parsedResponse_1.__lowercaseHeaders ||
|
|
257
|
+
Object.keys(parsedResponse_1.headers).reduce(function (headers, header) {
|
|
258
|
+
headers[header.toLowerCase()] = parsedResponse_1.headers[header];
|
|
259
|
+
return headers;
|
|
260
|
+
}, {});
|
|
261
|
+
contentType = parsedResponse_1.headers["content-type"];
|
|
262
|
+
inferredResponseType = (0, ApiUtils_1.inferResponseType)(contentType);
|
|
263
|
+
if (!error) {
|
|
264
|
+
// expand to array buffer once we support that in inferResponseType
|
|
265
|
+
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
266
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
267
|
+
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
268
|
+
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
269
|
+
response: parsedResponse_1,
|
|
270
|
+
context: context,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
// transform arrayBuffer to json
|
|
274
|
+
if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
|
|
275
|
+
if (parsedResponse_1.data && typeof parsedResponse_1.data === "object") {
|
|
276
|
+
data = response.data;
|
|
277
|
+
if (((_a = data.constructor) === null || _a === void 0 ? void 0 : _a.name) === "ArrayBuffer") {
|
|
278
|
+
try {
|
|
279
|
+
decodedData = (response.data = (0, TextDecoding_1.textDecode)(data));
|
|
280
|
+
response.data = JSON.parse(decodedData);
|
|
281
|
+
}
|
|
282
|
+
catch (e) {
|
|
283
|
+
throw (0, RequestError_1.convertToRequestError)({
|
|
284
|
+
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
285
|
+
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
286
|
+
response: parsedResponse_1,
|
|
287
|
+
context: context,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return [2 /*return*/, parsedResponse_1];
|
|
295
|
+
case 2: return [2 /*return*/, response];
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}); };
|
|
299
|
+
var parseError = function (context, rawError) { return __awaiter(void 0, void 0, void 0, function () {
|
|
300
|
+
var error, extractedResponse, errorResponse, code, errorInfo;
|
|
301
|
+
var _a;
|
|
302
|
+
return __generator(this, function (_b) {
|
|
303
|
+
switch (_b.label) {
|
|
304
|
+
case 0:
|
|
305
|
+
if (!(0, RequestError_1.isRequestError)(rawError)) return [3 /*break*/, 1];
|
|
306
|
+
error = rawError;
|
|
307
|
+
return [3 /*break*/, 5];
|
|
308
|
+
case 1: return [4 /*yield*/, context.backend.extractResponseFromError(rawError)];
|
|
309
|
+
case 2:
|
|
310
|
+
extractedResponse = _b.sent();
|
|
311
|
+
errorResponse = undefined;
|
|
312
|
+
if (!(extractedResponse !== undefined)) return [3 /*break*/, 4];
|
|
313
|
+
return [4 /*yield*/, parseResponse(context, extractedResponse, true)];
|
|
314
|
+
case 3:
|
|
315
|
+
errorResponse = _b.sent();
|
|
316
|
+
_b.label = 4;
|
|
317
|
+
case 4:
|
|
318
|
+
code = (0, ApiUtils_1.isNetworkError)(rawError) ? RequestError_1.RequestErrorCode.REQUEST_NETWORK_ERROR : RequestError_1.RequestErrorCode.MISC_UNKNOWN_ERROR;
|
|
319
|
+
if (errorResponse) {
|
|
320
|
+
if (!(0, ApiUtils_1.isAcceptableStatus)(errorResponse.status, context.computedConfig.acceptableStatus)) {
|
|
321
|
+
code = RequestError_1.RequestErrorCode.REQUEST_INVALID_STATUS;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
if (rawError.code === "ENOTFOUND" || ((_a = rawError.cause) === null || _a === void 0 ? void 0 : _a.code) === "ENOTFOUND") {
|
|
326
|
+
code = RequestError_1.RequestErrorCode.REQUEST_HOST_NAME_NOT_FOUND;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
|
|
330
|
+
error = (0, RequestError_1.convertToRequestError)(__assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
|
|
331
|
+
_b.label = 5;
|
|
332
|
+
case 5: return [2 /*return*/, error];
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}); };
|