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