api-def 0.9.0 → 0.9.2
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.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 +202 -197
- 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 -126
- 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 +12 -32
package/esm/Requester.js
CHANGED
|
@@ -1,253 +1,252 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
12
|
-
import
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
import
|
|
17
|
-
const locks = {};
|
|
18
|
-
const runningOperations = {};
|
|
19
|
-
const MOCK_REQUEST_BACKEND = new MockRequestBackend();
|
|
20
|
-
export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
-
const computedConfig = host.computeConfig(config);
|
|
22
|
-
const backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
23
|
-
const context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
24
|
-
const { key } = context;
|
|
25
|
-
// don't do this -- should only be for GET requests anyway and should be opt-in
|
|
26
|
-
/*
|
|
27
|
-
// if we are already running this request just return the same promise, no need to do it again
|
|
28
|
-
const sameRequest = runningOperations[key];
|
|
29
|
-
if (sameRequest) {
|
|
30
|
-
return sameRequest;
|
|
31
|
-
}
|
|
32
|
-
*/
|
|
33
|
-
const { lock } = context.computedConfig || {};
|
|
34
|
-
if (typeof lock === "string") {
|
|
35
|
-
const lockedContext = locks[lock];
|
|
36
|
-
if (lockedContext && lockedContext.id !== context.id) {
|
|
37
|
-
lockedContext.cancel();
|
|
38
|
-
}
|
|
39
|
-
locks[lock] = context;
|
|
40
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
let response = yield (runningOperations[key] = makeRequest(context));
|
|
43
|
-
const successEventResult = yield context.triggerEvent(RequestEvent.Success);
|
|
44
|
-
if (successEventResult &&
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
const
|
|
115
|
-
context
|
|
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
|
-
const contentType = parsedResponse.headers["content-type"];
|
|
189
|
-
const inferredResponseType = inferResponseType(contentType);
|
|
190
|
-
if (!error) {
|
|
191
|
-
// expand to array buffer once we support that in inferResponseType
|
|
192
|
-
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
193
|
-
throw convertToRequestError({
|
|
194
|
-
error: new Error(`[api-def] Expected '${context.responseType}' response, got '${inferredResponseType}' (from 'Content-Type' of '${contentType}')`),
|
|
195
|
-
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
196
|
-
response: parsedResponse,
|
|
197
|
-
context: context,
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
// transform arrayBuffer to json
|
|
201
|
-
if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
|
|
202
|
-
if (parsedResponse.data &&
|
|
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
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
});
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { EventResultType, RequestEvent } from "./ApiConstants";
|
|
11
|
+
import { inferResponseType, isAcceptableStatus, isNetworkError } from "./ApiUtils";
|
|
12
|
+
import RequestContext from "./RequestContext";
|
|
13
|
+
import { RequestErrorCode, convertToRequestError, isRequestError } from "./RequestError";
|
|
14
|
+
import { textDecode } from "./TextDecoding";
|
|
15
|
+
import MockRequestBackend from "./backend/MockRequestBackend";
|
|
16
|
+
import retry from "./util/retry";
|
|
17
|
+
const locks = {};
|
|
18
|
+
const runningOperations = {};
|
|
19
|
+
const MOCK_REQUEST_BACKEND = new MockRequestBackend();
|
|
20
|
+
export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
const computedConfig = host.computeConfig(config);
|
|
22
|
+
const backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
23
|
+
const context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
24
|
+
const { key } = context;
|
|
25
|
+
// don't do this -- should only be for GET requests anyway and should be opt-in
|
|
26
|
+
/*
|
|
27
|
+
// if we are already running this request just return the same promise, no need to do it again
|
|
28
|
+
const sameRequest = runningOperations[key];
|
|
29
|
+
if (sameRequest) {
|
|
30
|
+
return sameRequest;
|
|
31
|
+
}
|
|
32
|
+
*/
|
|
33
|
+
const { lock } = context.computedConfig || {};
|
|
34
|
+
if (typeof lock === "string") {
|
|
35
|
+
const lockedContext = locks[lock];
|
|
36
|
+
if (lockedContext && lockedContext.id !== context.id) {
|
|
37
|
+
lockedContext.cancel();
|
|
38
|
+
}
|
|
39
|
+
locks[lock] = context;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
let response = yield (runningOperations[key] = makeRequest(context));
|
|
43
|
+
const successEventResult = yield context.triggerEvent(RequestEvent.Success);
|
|
44
|
+
if (successEventResult && successEventResult.type === EventResultType.Respond) {
|
|
45
|
+
context.response = response = successEventResult.response;
|
|
46
|
+
}
|
|
47
|
+
delete runningOperations[key];
|
|
48
|
+
return response;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
delete runningOperations[key];
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
if (typeof lock === "string") {
|
|
56
|
+
delete locks[lock];
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
const parseRetryOptions = (retryConfig) => {
|
|
61
|
+
if (retryConfig && typeof retryConfig === "object") {
|
|
62
|
+
return retryConfig;
|
|
63
|
+
}
|
|
64
|
+
if (typeof retryConfig === "number") {
|
|
65
|
+
return { maxAttempts: retryConfig };
|
|
66
|
+
}
|
|
67
|
+
return { maxAttempts: 0 };
|
|
68
|
+
};
|
|
69
|
+
const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* () {
|
|
70
|
+
var _a, _b, _c;
|
|
71
|
+
const beforeSendEventResult = yield context.triggerEvent(RequestEvent.BeforeSend);
|
|
72
|
+
if (beforeSendEventResult && beforeSendEventResult.type === EventResultType.Respond) {
|
|
73
|
+
return (context.response = beforeSendEventResult.response);
|
|
74
|
+
}
|
|
75
|
+
// validation
|
|
76
|
+
if (context.validation.query) {
|
|
77
|
+
try {
|
|
78
|
+
context.computedConfig.queryObject = context.validation.query.parse(context.computedConfig.queryObject);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
throw convertToRequestError({
|
|
82
|
+
error: error,
|
|
83
|
+
code: RequestErrorCode.VALIDATION_QUERY_VALIDATE_ERROR,
|
|
84
|
+
context: context,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (context.validation.body) {
|
|
89
|
+
try {
|
|
90
|
+
context.computedConfig.body = context.validation.body.parse(context.computedConfig.body);
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
throw convertToRequestError({
|
|
94
|
+
error: error,
|
|
95
|
+
code: RequestErrorCode.VALIDATION_BODY_VALIDATE_ERROR,
|
|
96
|
+
context: context,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const retryOptions = parseRetryOptions((_a = context.computedConfig) === null || _a === void 0 ? void 0 : _a.retry);
|
|
101
|
+
const internalRetryOptions = {
|
|
102
|
+
retries: retryOptions.maxAttempts,
|
|
103
|
+
// assume most users won't want to tune the delay between retries
|
|
104
|
+
minTimeout: (_b = retryOptions.minDelay) !== null && _b !== void 0 ? _b : 200,
|
|
105
|
+
maxTimeout: (_c = retryOptions.maxDelay) !== null && _c !== void 0 ? _c : 1000,
|
|
106
|
+
randomize: true,
|
|
107
|
+
};
|
|
108
|
+
context.stats.attempt = 0;
|
|
109
|
+
const performRequest = (fnBail, attemptCount) => __awaiter(void 0, void 0, void 0, function* () {
|
|
110
|
+
context.stats.attempt++;
|
|
111
|
+
try {
|
|
112
|
+
const { promise, canceler } = context.backend.makeRequest(context);
|
|
113
|
+
context.addCanceller(canceler);
|
|
114
|
+
const response = yield promise;
|
|
115
|
+
const parsedResponse = (yield parseResponse(context, response));
|
|
116
|
+
if (!isAcceptableStatus(parsedResponse.status, context.computedConfig.acceptableStatus)) {
|
|
117
|
+
throw convertToRequestError({
|
|
118
|
+
error: new Error(`[api-def] Invalid response status code '${parsedResponse.status}'`),
|
|
119
|
+
response: parsedResponse,
|
|
120
|
+
code: RequestErrorCode.REQUEST_INVALID_STATUS,
|
|
121
|
+
context: context,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
context.response = parsedResponse;
|
|
125
|
+
return parsedResponse;
|
|
126
|
+
}
|
|
127
|
+
catch (rawError) {
|
|
128
|
+
if (context.cancelled) {
|
|
129
|
+
rawError.isCancelledRequest = true;
|
|
130
|
+
}
|
|
131
|
+
const error = yield parseError(context, rawError);
|
|
132
|
+
context.error = error;
|
|
133
|
+
context.response = error.response;
|
|
134
|
+
const errorEventResult = yield context.triggerEvent(RequestEvent.Error);
|
|
135
|
+
if ((errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Respond) {
|
|
136
|
+
return errorEventResult.response;
|
|
137
|
+
}
|
|
138
|
+
// allow retry logic to handle network errors
|
|
139
|
+
//const shouldNaturallyRetry = ApiUtils.isNetworkError(error);
|
|
140
|
+
//if (shouldNaturallyRetry) {
|
|
141
|
+
// throw error;
|
|
142
|
+
//}
|
|
143
|
+
// if we have an event that tells us to retry, we must do it
|
|
144
|
+
const forceRetry = (errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Retry;
|
|
145
|
+
if (forceRetry) {
|
|
146
|
+
return performRequest(fnBail, attemptCount);
|
|
147
|
+
}
|
|
148
|
+
// allow retry logic to handle
|
|
149
|
+
if (!retryOptions.shouldRetry || retryOptions.shouldRetry(error)) {
|
|
150
|
+
throw error;
|
|
151
|
+
}
|
|
152
|
+
// error is unrecoverable, bail
|
|
153
|
+
const unrecoverableErrorEventResult = yield context.triggerEvent(RequestEvent.UnrecoverableError);
|
|
154
|
+
if (unrecoverableErrorEventResult) {
|
|
155
|
+
if (unrecoverableErrorEventResult.type === EventResultType.Respond) {
|
|
156
|
+
return unrecoverableErrorEventResult.response;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
fnBail(error);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const response = yield retry(performRequest, internalRetryOptions);
|
|
163
|
+
if (context.validation.response) {
|
|
164
|
+
try {
|
|
165
|
+
response.data = context.validation.response.parse(response.data);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
throw convertToRequestError({
|
|
169
|
+
error: error,
|
|
170
|
+
code: RequestErrorCode.VALIDATION_RESPONSE_VALIDATE_ERROR,
|
|
171
|
+
context: context,
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return response;
|
|
176
|
+
});
|
|
177
|
+
const parseResponse = (context, response, error) => __awaiter(void 0, void 0, void 0, function* () {
|
|
178
|
+
var _d;
|
|
179
|
+
if (response) {
|
|
180
|
+
const parsedResponse = yield context.backend.convertResponse(context, response);
|
|
181
|
+
// lowercase all header names
|
|
182
|
+
parsedResponse.headers =
|
|
183
|
+
parsedResponse.__lowercaseHeaders ||
|
|
184
|
+
Object.keys(parsedResponse.headers).reduce((headers, header) => {
|
|
185
|
+
headers[header.toLowerCase()] = parsedResponse.headers[header];
|
|
186
|
+
return headers;
|
|
187
|
+
}, {});
|
|
188
|
+
const contentType = parsedResponse.headers["content-type"];
|
|
189
|
+
const inferredResponseType = inferResponseType(contentType);
|
|
190
|
+
if (!error) {
|
|
191
|
+
// expand to array buffer once we support that in inferResponseType
|
|
192
|
+
if (inferredResponseType === "text" && context.responseType === "json") {
|
|
193
|
+
throw convertToRequestError({
|
|
194
|
+
error: new Error(`[api-def] Expected '${context.responseType}' response, got '${inferredResponseType}' (from 'Content-Type' of '${contentType}')`),
|
|
195
|
+
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
196
|
+
response: parsedResponse,
|
|
197
|
+
context: context,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
// transform arrayBuffer to json
|
|
201
|
+
if (inferredResponseType === "arraybuffer" && context.responseType === "json") {
|
|
202
|
+
if (parsedResponse.data && typeof parsedResponse.data === "object") {
|
|
203
|
+
const data = response.data;
|
|
204
|
+
if (((_d = data.constructor) === null || _d === void 0 ? void 0 : _d.name) === "ArrayBuffer") {
|
|
205
|
+
try {
|
|
206
|
+
const decodedData = (response.data = textDecode(data));
|
|
207
|
+
response.data = JSON.parse(decodedData);
|
|
208
|
+
}
|
|
209
|
+
catch (e) {
|
|
210
|
+
throw convertToRequestError({
|
|
211
|
+
error: new Error(`[api-def] Expected '${context.responseType}' response, got '${inferredResponseType}' (from 'Content-Type' of '${contentType}')`),
|
|
212
|
+
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
213
|
+
response: parsedResponse,
|
|
214
|
+
context: context,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return parsedResponse;
|
|
222
|
+
}
|
|
223
|
+
return response;
|
|
224
|
+
});
|
|
225
|
+
const parseError = (context, rawError) => __awaiter(void 0, void 0, void 0, function* () {
|
|
226
|
+
var _e;
|
|
227
|
+
let error;
|
|
228
|
+
if (isRequestError(rawError)) {
|
|
229
|
+
error = rawError;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
const extractedResponse = yield context.backend.extractResponseFromError(rawError);
|
|
233
|
+
let errorResponse = undefined;
|
|
234
|
+
if (extractedResponse !== undefined) {
|
|
235
|
+
errorResponse = yield parseResponse(context, extractedResponse, true);
|
|
236
|
+
}
|
|
237
|
+
let code = isNetworkError(rawError) ? RequestErrorCode.REQUEST_NETWORK_ERROR : RequestErrorCode.MISC_UNKNOWN_ERROR;
|
|
238
|
+
if (errorResponse) {
|
|
239
|
+
if (!isAcceptableStatus(errorResponse.status, context.computedConfig.acceptableStatus)) {
|
|
240
|
+
code = RequestErrorCode.REQUEST_INVALID_STATUS;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
if (rawError.code === "ENOTFOUND" || ((_e = rawError.cause) === null || _e === void 0 ? void 0 : _e.code) === "ENOTFOUND") {
|
|
245
|
+
code = RequestErrorCode.REQUEST_HOST_NAME_NOT_FOUND;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
const errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
|
|
249
|
+
error = convertToRequestError(Object.assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
|
|
250
|
+
}
|
|
251
|
+
return error;
|
|
252
|
+
});
|
package/esm/TextDecoding.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const textDecode: (inputArrayOrBuffer: any, options?: any) => string;
|
|
1
|
+
export declare const textDecode: (inputArrayOrBuffer: any, options?: any) => string;
|