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.
Files changed (122) hide show
  1. package/README.md +1 -1
  2. package/cjs/Api.d.ts +35 -35
  3. package/cjs/Api.js +140 -144
  4. package/cjs/ApiConstants.d.ts +58 -58
  5. package/cjs/ApiConstants.js +55 -55
  6. package/cjs/ApiTypes.d.ts +103 -103
  7. package/cjs/ApiTypes.js +4 -4
  8. package/cjs/ApiUtils.d.ts +6 -6
  9. package/cjs/ApiUtils.js +51 -53
  10. package/cjs/Endpoint.d.ts +52 -52
  11. package/cjs/Endpoint.js +104 -108
  12. package/cjs/EndpointBuilder.d.ts +14 -14
  13. package/cjs/EndpointBuilder.js +46 -46
  14. package/cjs/MockingTypes.d.ts +30 -30
  15. package/cjs/MockingTypes.js +2 -2
  16. package/cjs/QueryHandling.d.ts +3 -3
  17. package/cjs/QueryHandling.js +24 -24
  18. package/cjs/RequestConfig.d.ts +2 -2
  19. package/cjs/RequestConfig.js +74 -74
  20. package/cjs/RequestContext.d.ts +48 -48
  21. package/cjs/RequestContext.js +235 -238
  22. package/cjs/RequestError.d.ts +30 -30
  23. package/cjs/RequestError.js +47 -47
  24. package/cjs/Requester.d.ts +3 -3
  25. package/cjs/Requester.js +335 -336
  26. package/cjs/TextDecoding.d.ts +1 -1
  27. package/cjs/TextDecoding.js +152 -139
  28. package/cjs/UtilTypes.d.ts +5 -5
  29. package/cjs/UtilTypes.js +2 -2
  30. package/cjs/Utils.d.ts +15 -15
  31. package/cjs/Utils.js +79 -77
  32. package/cjs/Validation.d.ts +8 -8
  33. package/cjs/Validation.js +2 -2
  34. package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
  35. package/cjs/backend/AxiosRequestBackend.js +99 -99
  36. package/cjs/backend/FetchRequestBackend.d.ts +15 -15
  37. package/cjs/backend/FetchRequestBackend.js +203 -202
  38. package/cjs/backend/MockRequestBackend.d.ts +11 -11
  39. package/cjs/backend/MockRequestBackend.js +164 -164
  40. package/cjs/backend/RequestBackend.d.ts +19 -19
  41. package/cjs/backend/RequestBackend.js +2 -2
  42. package/cjs/cache/CacheBackend.d.ts +6 -6
  43. package/cjs/cache/CacheBackend.js +2 -2
  44. package/cjs/cache/Caching.d.ts +10 -10
  45. package/cjs/cache/Caching.js +88 -88
  46. package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
  47. package/cjs/cache/LocalForageCacheBackend.js +24 -24
  48. package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
  49. package/cjs/cache/LocalStorageCacheBackend.js +77 -77
  50. package/cjs/index.d.ts +13 -13
  51. package/cjs/index.js +41 -41
  52. package/cjs/middleware/CacheMiddleware.d.ts +7 -7
  53. package/cjs/middleware/CacheMiddleware.js +107 -107
  54. package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
  55. package/cjs/middleware/LoggingMiddleware.js +82 -88
  56. package/cjs/util/retry/index.d.ts +3 -3
  57. package/cjs/util/retry/index.js +52 -52
  58. package/cjs/util/retry/lib/retry.d.ts +3 -3
  59. package/cjs/util/retry/lib/retry.js +50 -54
  60. package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
  61. package/cjs/util/retry/lib/retryOperation.js +130 -134
  62. package/esm/Api.d.ts +35 -35
  63. package/esm/Api.js +92 -98
  64. package/esm/ApiConstants.d.ts +58 -58
  65. package/esm/ApiConstants.js +52 -52
  66. package/esm/ApiTypes.d.ts +103 -103
  67. package/esm/ApiTypes.js +1 -1
  68. package/esm/ApiUtils.d.ts +6 -6
  69. package/esm/ApiUtils.js +43 -45
  70. package/esm/Endpoint.d.ts +52 -52
  71. package/esm/Endpoint.js +66 -70
  72. package/esm/EndpointBuilder.d.ts +14 -14
  73. package/esm/EndpointBuilder.js +31 -31
  74. package/esm/MockingTypes.d.ts +30 -30
  75. package/esm/MockingTypes.js +1 -1
  76. package/esm/QueryHandling.d.ts +3 -3
  77. package/esm/QueryHandling.js +19 -19
  78. package/esm/RequestConfig.d.ts +2 -2
  79. package/esm/RequestConfig.js +56 -56
  80. package/esm/RequestContext.d.ts +48 -48
  81. package/esm/RequestContext.js +167 -170
  82. package/esm/RequestError.d.ts +30 -30
  83. package/esm/RequestError.js +42 -42
  84. package/esm/Requester.d.ts +3 -3
  85. package/esm/Requester.js +252 -253
  86. package/esm/TextDecoding.d.ts +1 -1
  87. package/esm/TextDecoding.js +148 -135
  88. package/esm/UtilTypes.d.ts +5 -5
  89. package/esm/UtilTypes.js +1 -1
  90. package/esm/Utils.d.ts +15 -15
  91. package/esm/Utils.js +66 -68
  92. package/esm/Validation.d.ts +8 -8
  93. package/esm/Validation.js +1 -1
  94. package/esm/backend/AxiosRequestBackend.d.ts +13 -13
  95. package/esm/backend/AxiosRequestBackend.js +62 -62
  96. package/esm/backend/FetchRequestBackend.d.ts +15 -15
  97. package/esm/backend/FetchRequestBackend.js +136 -136
  98. package/esm/backend/MockRequestBackend.d.ts +11 -11
  99. package/esm/backend/MockRequestBackend.js +116 -116
  100. package/esm/backend/RequestBackend.d.ts +19 -19
  101. package/esm/backend/RequestBackend.js +1 -1
  102. package/esm/cache/CacheBackend.d.ts +6 -6
  103. package/esm/cache/CacheBackend.js +1 -1
  104. package/esm/cache/Caching.d.ts +10 -10
  105. package/esm/cache/Caching.js +39 -39
  106. package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
  107. package/esm/cache/LocalForageCacheBackend.js +20 -20
  108. package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
  109. package/esm/cache/LocalStorageCacheBackend.js +32 -32
  110. package/esm/index.d.ts +13 -13
  111. package/esm/index.js +14 -15
  112. package/esm/middleware/CacheMiddleware.d.ts +7 -7
  113. package/esm/middleware/CacheMiddleware.js +58 -58
  114. package/esm/middleware/LoggingMiddleware.d.ts +6 -6
  115. package/esm/middleware/LoggingMiddleware.js +77 -83
  116. package/esm/util/retry/index.d.ts +3 -3
  117. package/esm/util/retry/index.js +50 -50
  118. package/esm/util/retry/lib/retry.d.ts +3 -3
  119. package/esm/util/retry/lib/retry.js +35 -39
  120. package/esm/util/retry/lib/retryOperation.d.ts +27 -27
  121. package/esm/util/retry/lib/retryOperation.js +126 -132
  122. package/package.json +19 -34
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 { inferResponseType, isAcceptableStatus, isNetworkError } from "./ApiUtils";
11
- import RequestContext from "./RequestContext";
12
- import { EventResultType, RequestEvent } from "./ApiConstants";
13
- import retry from "./util/retry";
14
- import MockRequestBackend from "./backend/MockRequestBackend";
15
- import { convertToRequestError, isRequestError, RequestErrorCode } from "./RequestError";
16
- import { textDecode } from "./TextDecoding";
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
- successEventResult.type === EventResultType.Respond) {
46
- context.response = response = successEventResult.response;
47
- }
48
- delete runningOperations[key];
49
- return response;
50
- }
51
- catch (error) {
52
- delete runningOperations[key];
53
- throw error;
54
- }
55
- finally {
56
- if (typeof lock === "string") {
57
- delete locks[lock];
58
- }
59
- }
60
- });
61
- const parseRetryOptions = (retryConfig) => {
62
- if (retryConfig && typeof retryConfig === "object") {
63
- return retryConfig;
64
- }
65
- if (typeof retryConfig === "number") {
66
- return { maxAttempts: retryConfig };
67
- }
68
- return { maxAttempts: 0 };
69
- };
70
- const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* () {
71
- var _a, _b, _c;
72
- const beforeSendEventResult = yield context.triggerEvent(RequestEvent.BeforeSend);
73
- if (beforeSendEventResult &&
74
- beforeSendEventResult.type === EventResultType.Respond) {
75
- return (context.response = beforeSendEventResult.response);
76
- }
77
- // validation
78
- if (context.validation.query) {
79
- try {
80
- context.computedConfig.queryObject = context.validation.query.parse(context.computedConfig.queryObject);
81
- }
82
- catch (error) {
83
- throw convertToRequestError({
84
- error: error,
85
- code: RequestErrorCode.VALIDATION_QUERY_VALIDATE_ERROR,
86
- context: context,
87
- });
88
- }
89
- }
90
- if (context.validation.body) {
91
- try {
92
- context.computedConfig.body = context.validation.body.parse(context.computedConfig.body);
93
- }
94
- catch (error) {
95
- throw convertToRequestError({
96
- error: error,
97
- code: RequestErrorCode.VALIDATION_BODY_VALIDATE_ERROR,
98
- context: context,
99
- });
100
- }
101
- }
102
- const retryOptions = parseRetryOptions((_a = context.computedConfig) === null || _a === void 0 ? void 0 : _a.retry);
103
- const internalRetryOptions = {
104
- retries: retryOptions.maxAttempts,
105
- // assume most users won't want to tune the delay between retries
106
- minTimeout: (_b = retryOptions.minDelay) !== null && _b !== void 0 ? _b : 200,
107
- maxTimeout: (_c = retryOptions.maxDelay) !== null && _c !== void 0 ? _c : 1000,
108
- randomize: true,
109
- };
110
- context.stats.attempt = 0;
111
- const performRequest = (fnBail, attemptCount) => __awaiter(void 0, void 0, void 0, function* () {
112
- context.stats.attempt++;
113
- try {
114
- const { promise, canceler } = context.backend.makeRequest(context);
115
- context.addCanceller(canceler);
116
- const response = yield promise;
117
- const parsedResponse = (yield parseResponse(context, response));
118
- if (!isAcceptableStatus(parsedResponse.status, context.computedConfig.acceptableStatus)) {
119
- throw convertToRequestError({
120
- error: new Error(`[api-def] Invalid response status code '${parsedResponse.status}'`),
121
- response: parsedResponse,
122
- code: RequestErrorCode.REQUEST_INVALID_STATUS,
123
- context: context,
124
- });
125
- }
126
- context.response = parsedResponse;
127
- return (parsedResponse);
128
- }
129
- catch (rawError) {
130
- if (context.cancelled) {
131
- rawError.isCancelledRequest = true;
132
- }
133
- const error = yield parseError(context, rawError);
134
- context.error = error;
135
- context.response = error.response;
136
- const errorEventResult = yield context.triggerEvent(RequestEvent.Error);
137
- if ((errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Respond) {
138
- return errorEventResult.response;
139
- }
140
- // allow retry logic to handle network errors
141
- //const shouldNaturallyRetry = ApiUtils.isNetworkError(error);
142
- //if (shouldNaturallyRetry) {
143
- // throw error;
144
- //}
145
- // if we have an event that tells us to retry, we must do it
146
- const forceRetry = (errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.Retry;
147
- if (forceRetry) {
148
- return performRequest(fnBail, attemptCount);
149
- }
150
- // allow retry logic to handle
151
- if (!retryOptions.shouldRetry || retryOptions.shouldRetry(error)) {
152
- throw error;
153
- }
154
- // error is unrecoverable, bail
155
- const unrecoverableErrorEventResult = yield context.triggerEvent(RequestEvent.UnrecoverableError);
156
- if (unrecoverableErrorEventResult) {
157
- if (unrecoverableErrorEventResult.type === EventResultType.Respond) {
158
- return unrecoverableErrorEventResult.response;
159
- }
160
- }
161
- fnBail(error);
162
- }
163
- });
164
- const response = yield retry(performRequest, internalRetryOptions);
165
- if (context.validation.response) {
166
- try {
167
- response.data = context.validation.response.parse(response.data);
168
- }
169
- catch (error) {
170
- throw convertToRequestError({
171
- error: error,
172
- code: RequestErrorCode.VALIDATION_RESPONSE_VALIDATE_ERROR,
173
- context: context,
174
- });
175
- }
176
- }
177
- return (response);
178
- });
179
- const parseResponse = (context, response, error) => __awaiter(void 0, void 0, void 0, function* () {
180
- var _d;
181
- if (response) {
182
- const parsedResponse = yield context.backend.convertResponse(context, response);
183
- // lowercase all header names
184
- parsedResponse.headers = parsedResponse.__lowercaseHeaders || 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 &&
203
- typeof parsedResponse.data === "object") {
204
- const data = response.data;
205
- if (((_d = data.constructor) === null || _d === void 0 ? void 0 : _d.name) === "ArrayBuffer") {
206
- try {
207
- const decodedData = (response.data = textDecode(data));
208
- response.data = JSON.parse(decodedData);
209
- }
210
- catch (e) {
211
- throw convertToRequestError({
212
- error: new Error(`[api-def] Expected '${context.responseType}' response, got '${inferredResponseType}' (from 'Content-Type' of '${contentType}')`),
213
- code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
214
- response: parsedResponse,
215
- context: context,
216
- });
217
- }
218
- }
219
- }
220
- }
221
- }
222
- return parsedResponse;
223
- }
224
- return response;
225
- });
226
- const parseError = (context, rawError) => __awaiter(void 0, void 0, void 0, function* () {
227
- var _e;
228
- let error;
229
- if (isRequestError(rawError)) {
230
- error = rawError;
231
- }
232
- else {
233
- const extractedResponse = yield context.backend.extractResponseFromError(rawError);
234
- let errorResponse = undefined;
235
- if (extractedResponse !== undefined) {
236
- errorResponse = yield parseResponse(context, extractedResponse, true);
237
- }
238
- let code = isNetworkError(rawError) ? RequestErrorCode.REQUEST_NETWORK_ERROR : RequestErrorCode.MISC_UNKNOWN_ERROR;
239
- if (errorResponse) {
240
- if (!isAcceptableStatus(errorResponse.status, context.computedConfig.acceptableStatus)) {
241
- code = RequestErrorCode.REQUEST_INVALID_STATUS;
242
- }
243
- }
244
- else {
245
- if (rawError.code === "ENOTFOUND" || ((_e = rawError.cause) === null || _e === void 0 ? void 0 : _e.code) === "ENOTFOUND") {
246
- code = RequestErrorCode.REQUEST_HOST_NAME_NOT_FOUND;
247
- }
248
- }
249
- const errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
250
- error = convertToRequestError(Object.assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
251
- }
252
- return error;
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
+ });
@@ -1 +1 @@
1
- export declare const textDecode: (inputArrayOrBuffer: any, options?: any) => string;
1
+ export declare const textDecode: (inputArrayOrBuffer: any, options?: any) => string;