api-def 0.7.4 → 0.8.0
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/cjs/Api.js +6 -2
- package/cjs/ApiTypes.d.ts +30 -10
- package/cjs/ApiTypes.js +2 -0
- package/cjs/ApiUtils.js +1 -1
- package/cjs/Endpoint.d.ts +3 -3
- package/cjs/Endpoint.js +13 -8
- package/cjs/QueryHandling.d.ts +3 -0
- package/cjs/QueryHandling.js +24 -0
- package/cjs/RequestConfig.d.ts +2 -0
- package/cjs/RequestConfig.js +74 -0
- package/cjs/RequestContext.d.ts +4 -4
- package/cjs/RequestContext.js +5 -24
- package/cjs/RequestError.js +1 -1
- package/cjs/Requester.d.ts +1 -1
- package/cjs/Requester.js +2 -2
- package/cjs/backend/AxiosRequestBackend.js +4 -4
- package/cjs/backend/FetchRequestBackend.js +4 -3
- package/cjs/backend/MockRequestBackend.js +3 -1
- package/esm/Api.js +34 -70
- package/esm/ApiConstants.js +5 -5
- package/esm/ApiTypes.d.ts +30 -10
- package/esm/ApiTypes.js +1 -1
- package/esm/ApiUtils.js +12 -13
- package/esm/Endpoint.d.ts +3 -3
- package/esm/Endpoint.js +42 -74
- package/esm/EndpointBuilder.js +14 -16
- package/esm/QueryHandling.d.ts +3 -0
- package/esm/QueryHandling.js +19 -0
- package/esm/RequestConfig.d.ts +2 -0
- package/esm/RequestConfig.js +56 -0
- package/esm/RequestContext.d.ts +4 -4
- package/esm/RequestContext.js +71 -148
- package/esm/RequestError.js +7 -7
- package/esm/Requester.d.ts +1 -1
- package/esm/Requester.js +182 -266
- package/esm/TextDecoding.js +20 -20
- package/esm/Utils.js +15 -15
- package/esm/backend/AxiosRequestBackend.js +34 -67
- package/esm/backend/FetchRequestBackend.js +66 -134
- package/esm/backend/MockRequestBackend.js +92 -136
- package/esm/cache/Caching.js +24 -66
- package/esm/cache/LocalForageCacheBackend.js +11 -13
- package/esm/cache/LocalStorageCacheBackend.js +19 -62
- package/esm/middleware/CacheMiddleware.js +44 -91
- package/esm/middleware/LoggingMiddleware.js +36 -39
- package/esm/util/retry/index.js +8 -8
- package/esm/util/retry/lib/retry.js +11 -22
- package/package.json +4 -2
package/esm/Requester.js
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -18,33 +7,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
18
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
19
8
|
});
|
|
20
9
|
};
|
|
21
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
22
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
23
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
24
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
25
|
-
function step(op) {
|
|
26
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
27
|
-
while (_) try {
|
|
28
|
-
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;
|
|
29
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
30
|
-
switch (op[0]) {
|
|
31
|
-
case 0: case 1: t = op; break;
|
|
32
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
33
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
34
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
35
|
-
default:
|
|
36
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
37
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
38
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
39
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
40
|
-
if (t[2]) _.ops.pop();
|
|
41
|
-
_.trys.pop(); continue;
|
|
42
|
-
}
|
|
43
|
-
op = body.call(thisArg, _);
|
|
44
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
45
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
10
|
import * as ApiUtils from "./ApiUtils";
|
|
49
11
|
import { inferResponseType, isAcceptableStatus, isNetworkError } from "./ApiUtils";
|
|
50
12
|
import RequestContext from "./RequestContext";
|
|
@@ -54,241 +16,195 @@ import retry from "./util/retry";
|
|
|
54
16
|
import MockRequestBackend from "./backend/MockRequestBackend";
|
|
55
17
|
import { convertToRequestError, isRequestError, RequestErrorCode } from "./RequestError";
|
|
56
18
|
import { textDecode } from "./TextDecoding";
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
export
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
case 1:
|
|
82
|
-
_a.trys.push([1, 4, 5, 6]);
|
|
83
|
-
return [4 /*yield*/, (runningOperations[key] = makeRequest(context))];
|
|
84
|
-
case 2:
|
|
85
|
-
response = _a.sent();
|
|
86
|
-
return [4 /*yield*/, context.triggerEvent(RequestEvent.Success)];
|
|
87
|
-
case 3:
|
|
88
|
-
successEventResult = _a.sent();
|
|
89
|
-
if (successEventResult &&
|
|
90
|
-
successEventResult.type === EventResultType.Respond) {
|
|
91
|
-
context.response = response = successEventResult.response;
|
|
92
|
-
}
|
|
93
|
-
delete runningOperations[key];
|
|
94
|
-
return [2 /*return*/, response];
|
|
95
|
-
case 4:
|
|
96
|
-
error_1 = _a.sent();
|
|
97
|
-
delete runningOperations[key];
|
|
98
|
-
throw error_1;
|
|
99
|
-
case 5:
|
|
100
|
-
if (lock) {
|
|
101
|
-
delete locks[lock];
|
|
102
|
-
}
|
|
103
|
-
return [7 /*endfinally*/];
|
|
104
|
-
case 6: return [2 /*return*/];
|
|
19
|
+
const locks = {};
|
|
20
|
+
const runningOperations = {};
|
|
21
|
+
const MOCK_REQUEST_BACKEND = new MockRequestBackend();
|
|
22
|
+
export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void 0, function* () {
|
|
23
|
+
const computedConfig = host.computeConfig(config);
|
|
24
|
+
const backend = mocking ? MOCK_REQUEST_BACKEND : Api.getRequestBackend();
|
|
25
|
+
if (!backend) {
|
|
26
|
+
throw new Error("[api-def] Please specify a backend you wish to use, this can be done either with 'setRequestBackend()'");
|
|
27
|
+
}
|
|
28
|
+
const context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
29
|
+
const { key } = context;
|
|
30
|
+
// don't do this -- should only be for GET requests anyway and should be opt-in
|
|
31
|
+
/*
|
|
32
|
+
// if we are already running this request just return the same promise, no need to do it again
|
|
33
|
+
const sameRequest = runningOperations[key];
|
|
34
|
+
if (sameRequest) {
|
|
35
|
+
return sameRequest;
|
|
36
|
+
}
|
|
37
|
+
*/
|
|
38
|
+
const { lock } = context.computedConfig || {};
|
|
39
|
+
if (typeof lock === "string") {
|
|
40
|
+
const lockedContext = locks[lock];
|
|
41
|
+
if (lockedContext && lockedContext.id !== context.id) {
|
|
42
|
+
lockedContext.cancel();
|
|
105
43
|
}
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
44
|
+
locks[lock] = context;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
let response = yield (runningOperations[key] = makeRequest(context));
|
|
48
|
+
const successEventResult = yield context.triggerEvent(RequestEvent.Success);
|
|
49
|
+
if (successEventResult &&
|
|
50
|
+
successEventResult.type === EventResultType.Respond) {
|
|
51
|
+
context.response = response = successEventResult.response;
|
|
52
|
+
}
|
|
53
|
+
delete runningOperations[key];
|
|
54
|
+
return response;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
delete runningOperations[key];
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
if (typeof lock === "string") {
|
|
62
|
+
delete locks[lock];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
let defaultBackendMessageShown = false;
|
|
67
|
+
const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
111
68
|
var _a;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
69
|
+
if (process.env.NODE_ENV === "development") {
|
|
70
|
+
if (Api.isRequestBackendDefault() && !defaultBackendMessageShown) {
|
|
71
|
+
defaultBackendMessageShown = true;
|
|
72
|
+
// eslint-disable-next-line
|
|
73
|
+
console.warn("[api-def] Using default fetch backend, you can use a different one with 'setRequestBackend()' (dev only message)");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const beforeSendEventResult = yield context.triggerEvent(RequestEvent.BeforeSend);
|
|
77
|
+
if (beforeSendEventResult &&
|
|
78
|
+
beforeSendEventResult.type === EventResultType.Respond) {
|
|
79
|
+
return (context.response = beforeSendEventResult.response);
|
|
80
|
+
}
|
|
81
|
+
const maxRetries = ((_a = context.computedConfig) === null || _a === void 0 ? void 0 : _a.retry) || 0;
|
|
82
|
+
const retryOpts = {
|
|
83
|
+
retries: maxRetries,
|
|
84
|
+
// assume most users won't want to tune the delay between retries
|
|
85
|
+
minTimeout: 1 * 1000,
|
|
86
|
+
maxTimeout: 5 * 1000,
|
|
87
|
+
randomize: true,
|
|
88
|
+
};
|
|
89
|
+
context.stats.attempt = 0;
|
|
90
|
+
const performRequest = (fnBail, attemptCount) => __awaiter(void 0, void 0, void 0, function* () {
|
|
91
|
+
context.stats.attempt++;
|
|
92
|
+
try {
|
|
93
|
+
const { promise, canceler } = context.backend.makeRequest(context);
|
|
94
|
+
context.addCanceller(canceler);
|
|
95
|
+
const response = yield promise;
|
|
96
|
+
const parsedResponse = (yield parseResponse(context, response));
|
|
97
|
+
if (!isAcceptableStatus(parsedResponse.status, context.computedConfig.acceptableStatus)) {
|
|
98
|
+
throw convertToRequestError({
|
|
99
|
+
error: new Error(`[api-def] Invalid response status code '${parsedResponse.status}'`),
|
|
100
|
+
response: parsedResponse,
|
|
101
|
+
code: RequestErrorCode.REQUEST_INVALID_STATUS,
|
|
102
|
+
context: context,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
context.response = parsedResponse;
|
|
106
|
+
return (parsedResponse);
|
|
107
|
+
}
|
|
108
|
+
catch (rawError) {
|
|
109
|
+
if (context.cancelled) {
|
|
110
|
+
rawError.isCancelledRequest = true;
|
|
111
|
+
}
|
|
112
|
+
const error = yield parseError(context, rawError);
|
|
113
|
+
context.error = error;
|
|
114
|
+
context.response = error.response;
|
|
115
|
+
const errorEventResult = yield context.triggerEvent(RequestEvent.Error);
|
|
116
|
+
if ((errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Respond) {
|
|
117
|
+
return errorEventResult.response;
|
|
118
|
+
}
|
|
119
|
+
// allow retry logic to handle network errors
|
|
120
|
+
const shouldNaturallyRetry = ApiUtils.isNetworkError(error);
|
|
121
|
+
if (shouldNaturallyRetry) {
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
// if we have an event that tells us to retry, we must do it
|
|
125
|
+
const forceRetry = (errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Retry;
|
|
126
|
+
if (forceRetry) {
|
|
127
|
+
return performRequest(fnBail, attemptCount);
|
|
128
|
+
}
|
|
129
|
+
// error is unrecoverable, bail
|
|
130
|
+
const unrecoverableErrorEventResult = yield context.triggerEvent(RequestEvent.UnrecoverableError);
|
|
131
|
+
if (unrecoverableErrorEventResult) {
|
|
132
|
+
if (unrecoverableErrorEventResult.type === EventResultType.Respond) {
|
|
133
|
+
return unrecoverableErrorEventResult.response;
|
|
128
134
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
retries: maxRetries,
|
|
132
|
-
// assume most users won't want to tune the delay between retries
|
|
133
|
-
minTimeout: 1 * 1000,
|
|
134
|
-
maxTimeout: 5 * 1000,
|
|
135
|
-
randomize: true,
|
|
136
|
-
};
|
|
137
|
-
context.stats.attempt = 0;
|
|
138
|
-
performRequest = function (fnBail, attemptCount) { return __awaiter(void 0, void 0, void 0, function () {
|
|
139
|
-
var _a, promise, canceler, response_1, parsedResponse, rawError_1, error, errorEventResult, shouldNaturallyRetry, forceRetry, unrecoverableErrorEventResult;
|
|
140
|
-
return __generator(this, function (_b) {
|
|
141
|
-
switch (_b.label) {
|
|
142
|
-
case 0:
|
|
143
|
-
context.stats.attempt++;
|
|
144
|
-
_b.label = 1;
|
|
145
|
-
case 1:
|
|
146
|
-
_b.trys.push([1, 4, , 8]);
|
|
147
|
-
_a = context.backend.makeRequest(context), promise = _a.promise, canceler = _a.canceler;
|
|
148
|
-
context.addCanceller(canceler);
|
|
149
|
-
return [4 /*yield*/, promise];
|
|
150
|
-
case 2:
|
|
151
|
-
response_1 = _b.sent();
|
|
152
|
-
return [4 /*yield*/, parseResponse(context, response_1)];
|
|
153
|
-
case 3:
|
|
154
|
-
parsedResponse = (_b.sent());
|
|
155
|
-
if (!isAcceptableStatus(parsedResponse.status, context.computedConfig.acceptableStatus)) {
|
|
156
|
-
throw convertToRequestError({
|
|
157
|
-
error: new Error("[api-def] Invalid response status code '".concat(parsedResponse.status, "'")),
|
|
158
|
-
response: parsedResponse,
|
|
159
|
-
code: RequestErrorCode.REQUEST_INVALID_STATUS,
|
|
160
|
-
context: context,
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
context.response = parsedResponse;
|
|
164
|
-
return [2 /*return*/, (parsedResponse)];
|
|
165
|
-
case 4:
|
|
166
|
-
rawError_1 = _b.sent();
|
|
167
|
-
if (context.cancelled) {
|
|
168
|
-
rawError_1.isCancelledRequest = true;
|
|
169
|
-
}
|
|
170
|
-
return [4 /*yield*/, parseError(context, rawError_1)];
|
|
171
|
-
case 5:
|
|
172
|
-
error = _b.sent();
|
|
173
|
-
context.error = error;
|
|
174
|
-
context.response = error.response;
|
|
175
|
-
return [4 /*yield*/, context.triggerEvent(RequestEvent.Error)];
|
|
176
|
-
case 6:
|
|
177
|
-
errorEventResult = _b.sent();
|
|
178
|
-
if ((errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Respond) {
|
|
179
|
-
return [2 /*return*/, errorEventResult.response];
|
|
180
|
-
}
|
|
181
|
-
shouldNaturallyRetry = ApiUtils.isNetworkError(error);
|
|
182
|
-
if (shouldNaturallyRetry) {
|
|
183
|
-
throw error;
|
|
184
|
-
}
|
|
185
|
-
forceRetry = (errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Retry;
|
|
186
|
-
if (forceRetry) {
|
|
187
|
-
return [2 /*return*/, performRequest(fnBail, attemptCount)];
|
|
188
|
-
}
|
|
189
|
-
return [4 /*yield*/, context.triggerEvent(RequestEvent.UnrecoverableError)];
|
|
190
|
-
case 7:
|
|
191
|
-
unrecoverableErrorEventResult = _b.sent();
|
|
192
|
-
if (unrecoverableErrorEventResult) {
|
|
193
|
-
if (unrecoverableErrorEventResult.type === EventResultType.Respond) {
|
|
194
|
-
return [2 /*return*/, unrecoverableErrorEventResult.response];
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
fnBail(error);
|
|
198
|
-
return [3 /*break*/, 8];
|
|
199
|
-
case 8: return [2 /*return*/];
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
}); };
|
|
203
|
-
return [4 /*yield*/, retry(performRequest, retryOpts)];
|
|
204
|
-
case 2:
|
|
205
|
-
response = _b.sent();
|
|
206
|
-
return [2 /*return*/, (response)];
|
|
135
|
+
}
|
|
136
|
+
fnBail(error);
|
|
207
137
|
}
|
|
208
138
|
});
|
|
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
|
-
if (
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
data =
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
251
|
-
response: parsedResponse_1,
|
|
252
|
-
context: context,
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
}
|
|
139
|
+
const response = yield retry(performRequest, retryOpts);
|
|
140
|
+
return (response);
|
|
141
|
+
});
|
|
142
|
+
const parseResponse = (context, response, error) => __awaiter(void 0, void 0, void 0, function* () {
|
|
143
|
+
var _b;
|
|
144
|
+
if (response) {
|
|
145
|
+
const parsedResponse = yield context.backend.convertResponse(context, response);
|
|
146
|
+
// lowercase all header names
|
|
147
|
+
parsedResponse.headers = parsedResponse.__lowercaseHeaders || Object.keys(parsedResponse.headers).reduce((headers, header) => {
|
|
148
|
+
headers[header.toLowerCase()] = parsedResponse.headers[header];
|
|
149
|
+
return headers;
|
|
150
|
+
}, {});
|
|
151
|
+
const contentType = parsedResponse.headers["content-type"];
|
|
152
|
+
const inferredResponseType = inferResponseType(contentType);
|
|
153
|
+
if (!error) {
|
|
154
|
+
// expand to array buffer once we support that in inferResponseType
|
|
155
|
+
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
156
|
+
throw convertToRequestError({
|
|
157
|
+
error: new Error(`[api-def] Expected '${context.responseType}' response, got '${inferredResponseType}' (from 'Content-Type' of '${contentType}')`),
|
|
158
|
+
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
159
|
+
response: parsedResponse,
|
|
160
|
+
context: context,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
// transform arrayBuffer to json
|
|
164
|
+
if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
|
|
165
|
+
if (parsedResponse.data &&
|
|
166
|
+
typeof parsedResponse.data === "object") {
|
|
167
|
+
const data = response.data;
|
|
168
|
+
if (((_b = data.constructor) === null || _b === void 0 ? void 0 : _b.name) === "ArrayBuffer") {
|
|
169
|
+
try {
|
|
170
|
+
const decodedData = (response.data = textDecode(data));
|
|
171
|
+
response.data = JSON.parse(decodedData);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
throw convertToRequestError({
|
|
175
|
+
error: new Error(`[api-def] Expected '${context.responseType}' response, got '${inferredResponseType}' (from 'Content-Type' of '${contentType}')`),
|
|
176
|
+
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
177
|
+
response: parsedResponse,
|
|
178
|
+
context: context,
|
|
179
|
+
});
|
|
256
180
|
}
|
|
257
181
|
}
|
|
258
182
|
}
|
|
259
|
-
|
|
260
|
-
case 2: return [2 /*return*/, response];
|
|
183
|
+
}
|
|
261
184
|
}
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
if (!(extractedResponse !== undefined)) return [3 /*break*/, 4];
|
|
277
|
-
return [4 /*yield*/, parseResponse(context, extractedResponse, true)];
|
|
278
|
-
case 3:
|
|
279
|
-
errorResponse = _a.sent();
|
|
280
|
-
_a.label = 4;
|
|
281
|
-
case 4:
|
|
282
|
-
code = isNetworkError(rawError) ? RequestErrorCode.REQUEST_NETWORK_ERROR : RequestErrorCode.MISC_UNKNOWN_ERROR;
|
|
283
|
-
if (errorResponse) {
|
|
284
|
-
if (!isAcceptableStatus(errorResponse.status, context.computedConfig.acceptableStatus)) {
|
|
285
|
-
code = RequestErrorCode.REQUEST_INVALID_STATUS;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
|
|
289
|
-
error = convertToRequestError(__assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
|
|
290
|
-
_a.label = 5;
|
|
291
|
-
case 5: return [2 /*return*/, error];
|
|
185
|
+
return parsedResponse;
|
|
186
|
+
}
|
|
187
|
+
return response;
|
|
188
|
+
});
|
|
189
|
+
const parseError = (context, rawError) => __awaiter(void 0, void 0, void 0, function* () {
|
|
190
|
+
let error;
|
|
191
|
+
if (isRequestError(rawError)) {
|
|
192
|
+
error = rawError;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
const extractedResponse = yield context.backend.extractResponseFromError(rawError);
|
|
196
|
+
let errorResponse = undefined;
|
|
197
|
+
if (extractedResponse !== undefined) {
|
|
198
|
+
errorResponse = yield parseResponse(context, extractedResponse, true);
|
|
292
199
|
}
|
|
293
|
-
|
|
294
|
-
|
|
200
|
+
let code = isNetworkError(rawError) ? RequestErrorCode.REQUEST_NETWORK_ERROR : RequestErrorCode.MISC_UNKNOWN_ERROR;
|
|
201
|
+
if (errorResponse) {
|
|
202
|
+
if (!isAcceptableStatus(errorResponse.status, context.computedConfig.acceptableStatus)) {
|
|
203
|
+
code = RequestErrorCode.REQUEST_INVALID_STATUS;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
|
|
207
|
+
error = convertToRequestError(Object.assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
|
|
208
|
+
}
|
|
209
|
+
return error;
|
|
210
|
+
});
|
package/esm/TextDecoding.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
1
|
/* eslint-disable prefer-const */
|
|
2
2
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
3
3
|
// polyfill from https://github.com/anonyco/FastestSmallestTextEncoderDecoder/blob/master/EncoderDecoderTogether.src.js
|
|
4
|
-
export
|
|
4
|
+
export const textDecode = (inputArrayOrBuffer, options) => {
|
|
5
5
|
if (typeof TextDecoder !== "undefined") {
|
|
6
6
|
return new TextDecoder("utf-8").decode(inputArrayOrBuffer);
|
|
7
7
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
const fromCharCode = String.fromCharCode;
|
|
9
|
+
const Object_prototype_toString = ({}).toString;
|
|
10
|
+
const sharedArrayBufferString = Object_prototype_toString.call(window["SharedArrayBuffer"]);
|
|
11
|
+
const undefinedObjectString = Object_prototype_toString();
|
|
12
|
+
const NativeUint8Array = window.Uint8Array;
|
|
13
|
+
const patchedU8Array = NativeUint8Array || Array;
|
|
14
|
+
const nativeArrayBuffer = NativeUint8Array ? ArrayBuffer : patchedU8Array;
|
|
15
|
+
const arrayBuffer_isView = nativeArrayBuffer.isView || function (x) {
|
|
16
16
|
return x && "length" in x;
|
|
17
17
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const arrayBufferString = Object_prototype_toString.call(nativeArrayBuffer.prototype);
|
|
19
|
+
const tmpBufferU16 = new (NativeUint8Array ? Uint16Array : patchedU8Array)(32);
|
|
20
|
+
let inputAs8 = inputArrayOrBuffer;
|
|
21
|
+
let asObjectString;
|
|
22
22
|
if (!arrayBuffer_isView(inputAs8)) {
|
|
23
23
|
asObjectString = Object_prototype_toString.call(inputAs8);
|
|
24
24
|
if (asObjectString !== arrayBufferString && asObjectString !== sharedArrayBufferString && asObjectString !== undefinedObjectString)
|
|
25
25
|
throw TypeError("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
|
|
26
26
|
inputAs8 = NativeUint8Array ? new patchedU8Array(inputAs8) : inputAs8 || [];
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
let resultingString = "", tmpStr = "", index = 0, len = inputAs8.length | 0, lenMinus32 = len - 32 | 0, nextEnd = 0, cp0 = 0, codePoint = 0, minBits = 0, cp1 = 0, pos = 0, tmp = -1;
|
|
29
29
|
// Note that tmp represents the 2nd half of a surrogate pair incase a surrogate gets divided between blocks
|
|
30
30
|
for (; index < len;) {
|
|
31
31
|
nextEnd = index <= lenMinus32 ? 32 : len - index | 0;
|
|
@@ -34,29 +34,29 @@ export var textDecode = function (inputArrayOrBuffer, options) {
|
|
|
34
34
|
switch (cp0 >> 4) {
|
|
35
35
|
case 15:
|
|
36
36
|
cp1 = inputAs8[index = index + 1 | 0] & 0xff;
|
|
37
|
-
if ((cp1 >> 6) !==
|
|
37
|
+
if ((cp1 >> 6) !== 0b10 || 0b11110111 < cp0) {
|
|
38
38
|
index = index - 1 | 0;
|
|
39
39
|
break;
|
|
40
40
|
}
|
|
41
|
-
codePoint = ((cp0 &
|
|
41
|
+
codePoint = ((cp0 & 0b111) << 6) | (cp1 & 0b00111111);
|
|
42
42
|
minBits = 5; // 20 ensures it never passes -> all invalid replacements
|
|
43
43
|
cp0 = 0x100; // keep track of th bit size
|
|
44
44
|
// eslint-disable-next-line no-fallthrough
|
|
45
45
|
case 14:
|
|
46
46
|
cp1 = inputAs8[index = index + 1 | 0] & 0xff;
|
|
47
47
|
codePoint <<= 6;
|
|
48
|
-
codePoint |= ((cp0 &
|
|
49
|
-
minBits = (cp1 >> 6) ===
|
|
48
|
+
codePoint |= ((cp0 & 0b1111) << 6) | (cp1 & 0b00111111);
|
|
49
|
+
minBits = (cp1 >> 6) === 0b10 ? minBits + 4 | 0 : 24; // 24 ensures it never passes -> all invalid replacements
|
|
50
50
|
cp0 = (cp0 + 0x100) & 0x300; // keep track of th bit size
|
|
51
51
|
// eslint-disable-next-line no-fallthrough
|
|
52
52
|
case 13:
|
|
53
53
|
case 12:
|
|
54
54
|
cp1 = inputAs8[index = index + 1 | 0] & 0xff;
|
|
55
55
|
codePoint <<= 6;
|
|
56
|
-
codePoint |= ((cp0 &
|
|
56
|
+
codePoint |= ((cp0 & 0b11111) << 6) | cp1 & 0b00111111;
|
|
57
57
|
minBits = minBits + 7 | 0;
|
|
58
58
|
// Now, process the code point
|
|
59
|
-
if (index < len && (cp1 >> 6) ===
|
|
59
|
+
if (index < len && (cp1 >> 6) === 0b10 && (codePoint >> minBits) && codePoint < 0x110000) {
|
|
60
60
|
cp0 = codePoint;
|
|
61
61
|
codePoint = codePoint - 0x10000 | 0;
|
|
62
62
|
if (0 <= codePoint /*0xffff < codePoint*/) { // BMP code point
|
package/esm/Utils.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
|
|
2
|
-
export
|
|
2
|
+
export const assign = Object.assign || function (target, varArgs) {
|
|
3
3
|
if (target === null || target === undefined) {
|
|
4
4
|
throw new TypeError("Cannot convert undefined or null to object");
|
|
5
5
|
}
|
|
6
|
-
|
|
7
|
-
for (
|
|
6
|
+
const to = Object(target);
|
|
7
|
+
for (let index = 1; index < arguments.length; index++) {
|
|
8
8
|
// eslint-disable-next-line prefer-rest-params
|
|
9
|
-
|
|
9
|
+
const nextSource = arguments[index];
|
|
10
10
|
if (nextSource !== null && nextSource !== undefined) {
|
|
11
|
-
for (
|
|
11
|
+
for (const nextKey in nextSource) {
|
|
12
12
|
// Avoid bugs when hasOwnProperty is shadowed
|
|
13
13
|
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
|
14
14
|
to[nextKey] = nextSource[nextKey];
|
|
@@ -18,13 +18,13 @@ export var assign = Object.assign || function (target, varArgs) {
|
|
|
18
18
|
}
|
|
19
19
|
return to;
|
|
20
20
|
};
|
|
21
|
-
export
|
|
22
|
-
|
|
21
|
+
export const padNumber = (stringOrNumber, maxLength) => {
|
|
22
|
+
const string = stringOrNumber.toString();
|
|
23
23
|
return string.length >= maxLength
|
|
24
24
|
? string
|
|
25
25
|
: "0".repeat(maxLength - string.length) + string;
|
|
26
26
|
};
|
|
27
|
-
export
|
|
27
|
+
export const getGlobalFetch = () => {
|
|
28
28
|
if (typeof global !== "undefined" && typeof global.fetch === "function") {
|
|
29
29
|
return global.fetch;
|
|
30
30
|
}
|
|
@@ -34,7 +34,7 @@ export var getGlobalFetch = function () {
|
|
|
34
34
|
return window.fetch.bind(window);
|
|
35
35
|
};
|
|
36
36
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
37
|
-
export
|
|
37
|
+
export const noop = () => {
|
|
38
38
|
};
|
|
39
39
|
/**
|
|
40
40
|
* Just used to simulate lag, or loading times.
|
|
@@ -42,10 +42,10 @@ export var noop = function () {
|
|
|
42
42
|
* @param delayMs The delay in ms
|
|
43
43
|
* @returns The `value` param as a Promise
|
|
44
44
|
*/
|
|
45
|
-
export
|
|
46
|
-
return (new Promise(
|
|
45
|
+
export const delayThenReturn = (value, delayMs) => {
|
|
46
|
+
return (new Promise((resolve) => {
|
|
47
47
|
if (delayMs > 0) {
|
|
48
|
-
setTimeout(
|
|
48
|
+
setTimeout(() => {
|
|
49
49
|
resolve(value);
|
|
50
50
|
}, delayMs);
|
|
51
51
|
}
|
|
@@ -54,8 +54,8 @@ export var delayThenReturn = function (value, delayMs) {
|
|
|
54
54
|
}
|
|
55
55
|
}));
|
|
56
56
|
};
|
|
57
|
-
export
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
export const randInt = (min, max) => {
|
|
58
|
+
const minI = Math.ceil(min);
|
|
59
|
+
const maxI = Math.floor(max);
|
|
60
60
|
return (Math.floor(Math.random() * (maxI - minI + 1)) + minI);
|
|
61
61
|
};
|