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/RequestContext.js
CHANGED
|
@@ -1,55 +1,43 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
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);
|
|
13
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
-
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;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
39
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
40
|
-
if (ar || !(i in from)) {
|
|
41
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
42
|
-
ar[i] = from[i];
|
|
43
|
-
}
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
44
7
|
}
|
|
45
|
-
|
|
46
|
-
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
47
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
36
|
+
const ApiUtils_1 = require("./ApiUtils");
|
|
37
|
+
const Utils = __importStar(require("./Utils"));
|
|
38
|
+
let contextIdCounter = 0;
|
|
39
|
+
class RequestContext {
|
|
40
|
+
constructor(backend, host, config, rawPath, mocking) {
|
|
53
41
|
this.canceler = null;
|
|
54
42
|
this.response = undefined;
|
|
55
43
|
this.error = null;
|
|
@@ -78,96 +66,92 @@ var RequestContext = /** @class */ (function () {
|
|
|
78
66
|
this.parseRequestBody();
|
|
79
67
|
this.computedRequestUrl = this.resolveRequestUrl();
|
|
80
68
|
}
|
|
81
|
-
|
|
69
|
+
validatePath() {
|
|
82
70
|
this.computedPath = (0, ApiUtils_1.resolvePathParams)(this.rawPath, this.requestConfig.params);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
return this.host.api;
|
|
104
|
-
},
|
|
105
|
-
enumerable: false,
|
|
106
|
-
configurable: true
|
|
107
|
-
});
|
|
108
|
-
Object.defineProperty(RequestContext.prototype, "baseUrl", {
|
|
109
|
-
get: function () {
|
|
110
|
-
return this.computedBaseUrl;
|
|
111
|
-
},
|
|
112
|
-
enumerable: false,
|
|
113
|
-
configurable: true
|
|
114
|
-
});
|
|
115
|
-
Object.defineProperty(RequestContext.prototype, "responseType", {
|
|
116
|
-
get: function () {
|
|
117
|
-
return this.host.responseType;
|
|
118
|
-
},
|
|
119
|
-
enumerable: false,
|
|
120
|
-
configurable: true
|
|
121
|
-
});
|
|
122
|
-
RequestContext.prototype.initMiddleware = function () {
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* @deprecated Use `requestConfig` instead
|
|
74
|
+
*/
|
|
75
|
+
get computedConfig() {
|
|
76
|
+
return this.requestConfig;
|
|
77
|
+
}
|
|
78
|
+
get method() {
|
|
79
|
+
return this.computedMethod;
|
|
80
|
+
}
|
|
81
|
+
get api() {
|
|
82
|
+
return this.host.api;
|
|
83
|
+
}
|
|
84
|
+
get baseUrl() {
|
|
85
|
+
return this.computedBaseUrl;
|
|
86
|
+
}
|
|
87
|
+
get responseType() {
|
|
88
|
+
return this.host.responseType;
|
|
89
|
+
}
|
|
90
|
+
initMiddleware() {
|
|
123
91
|
// Merge API-level and endpoint-level middleware
|
|
124
|
-
|
|
92
|
+
const allMiddleware = [
|
|
93
|
+
...this.api.middleware,
|
|
94
|
+
...("middleware" in this.host && Array.isArray(this.host.middleware) ? this.host.middleware : []),
|
|
95
|
+
];
|
|
125
96
|
// Process all middleware in one pass
|
|
126
|
-
for (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
for (
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
97
|
+
for (let i = 0; i < allMiddleware.length; i++) {
|
|
98
|
+
const events = allMiddleware[i];
|
|
99
|
+
const eventTypes = Object.keys(events);
|
|
100
|
+
for (let n = 0; n < eventTypes.length; n++) {
|
|
101
|
+
const eventType = eventTypes[n];
|
|
102
|
+
const eventHandlersForType = this.eventHandlers[eventType] || (this.eventHandlers[eventType] = []);
|
|
103
|
+
const middlewareEventHandlers = events[eventType];
|
|
133
104
|
if (middlewareEventHandlers) {
|
|
134
105
|
eventHandlersForType.push(middlewareEventHandlers);
|
|
135
106
|
}
|
|
136
107
|
}
|
|
137
108
|
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
|
|
109
|
+
}
|
|
110
|
+
generateKey() {
|
|
111
|
+
let key = this.computedPath.trim();
|
|
141
112
|
if (this.requestConfig.queryString) {
|
|
142
|
-
key +=
|
|
113
|
+
key += `?${this.requestConfig.queryString}`;
|
|
143
114
|
}
|
|
144
115
|
return key;
|
|
145
|
-
}
|
|
146
|
-
|
|
116
|
+
}
|
|
117
|
+
updateHeaders(newHeaders) {
|
|
147
118
|
this.requestConfig.headers = Utils.assign({}, this.requestConfig.headers, newHeaders);
|
|
148
119
|
this.parseRequestBody();
|
|
149
120
|
return this;
|
|
150
|
-
}
|
|
151
|
-
|
|
121
|
+
}
|
|
122
|
+
updateParams(params) {
|
|
152
123
|
this.requestConfig.params = Utils.assign({}, this.requestConfig.params, params);
|
|
153
124
|
this.computedPath = (0, ApiUtils_1.resolvePathParams)(this.rawPath, this.requestConfig.params);
|
|
154
125
|
this.computedRequestUrl = this.resolveRequestUrl();
|
|
155
126
|
return this;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
var _a;
|
|
127
|
+
}
|
|
128
|
+
parseRequestBody() {
|
|
159
129
|
if (this.requestConfig.body && this.requestConfig.headers) {
|
|
160
|
-
|
|
161
|
-
|
|
130
|
+
const contentTypeKey = Object.keys(this.requestConfig.headers).find((key) => key.toLowerCase() === "content-type");
|
|
131
|
+
const contentType = contentTypeKey && this.requestConfig.headers[contentTypeKey]?.toString().split(";")[0].trim();
|
|
132
|
+
const bodyEncoding = this.validation.bodyEncoding ?? "application/json";
|
|
133
|
+
if (!contentTypeKey && bodyEncoding === "application/x-www-form-urlencoded") {
|
|
134
|
+
this.requestConfig.headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
135
|
+
}
|
|
162
136
|
if (contentType === "application/x-www-form-urlencoded") {
|
|
163
|
-
|
|
164
|
-
for (
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
137
|
+
const searchParams = new URLSearchParams();
|
|
138
|
+
for (const key of Object.keys(this.requestConfig.body)) {
|
|
139
|
+
const value = this.requestConfig.body[key];
|
|
140
|
+
searchParams.append(key, value?.toString());
|
|
141
|
+
}
|
|
142
|
+
this.parsedBody = searchParams;
|
|
143
|
+
}
|
|
144
|
+
else if (bodyEncoding === "application/x-www-form-urlencoded") {
|
|
145
|
+
const searchParams = new URLSearchParams();
|
|
146
|
+
for (const key of Object.keys(this.requestConfig.body)) {
|
|
147
|
+
const value = this.requestConfig.body[key];
|
|
148
|
+
searchParams.append(key, value?.toString());
|
|
168
149
|
}
|
|
169
150
|
this.parsedBody = searchParams;
|
|
170
151
|
}
|
|
152
|
+
else if (bodyEncoding === "multipart/form-data") {
|
|
153
|
+
this.parsedBody = Utils.toFormData(this.requestConfig.body);
|
|
154
|
+
}
|
|
171
155
|
else {
|
|
172
156
|
this.parsedBody = this.requestConfig.body;
|
|
173
157
|
}
|
|
@@ -175,89 +159,68 @@ var RequestContext = /** @class */ (function () {
|
|
|
175
159
|
else {
|
|
176
160
|
this.parsedBody = undefined;
|
|
177
161
|
}
|
|
178
|
-
}
|
|
179
|
-
|
|
162
|
+
}
|
|
163
|
+
getParsedBody() {
|
|
180
164
|
return this.parsedBody;
|
|
181
|
-
}
|
|
182
|
-
|
|
165
|
+
}
|
|
166
|
+
parseBody() {
|
|
167
|
+
this.parseRequestBody();
|
|
168
|
+
}
|
|
169
|
+
updateQuery(newQuery) {
|
|
183
170
|
this.requestConfig.queryObject = Utils.assign({}, this.requestConfig.queryObject, newQuery);
|
|
184
171
|
this.computedRequestUrl = this.resolveRequestUrl();
|
|
185
172
|
return this;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
i = 0;
|
|
196
|
-
_a.label = 1;
|
|
197
|
-
case 1:
|
|
198
|
-
if (!(i < eventHandlers.length)) return [3 /*break*/, 4];
|
|
199
|
-
eventHandler = eventHandlers[i];
|
|
200
|
-
return [4 /*yield*/, eventHandler(this)];
|
|
201
|
-
case 2:
|
|
202
|
-
eventResult = _a.sent();
|
|
203
|
-
if (eventResult) {
|
|
204
|
-
return [2 /*return*/, eventResult];
|
|
205
|
-
}
|
|
206
|
-
_a.label = 3;
|
|
207
|
-
case 3:
|
|
208
|
-
i++;
|
|
209
|
-
return [3 /*break*/, 1];
|
|
210
|
-
case 4: return [2 /*return*/, undefined];
|
|
173
|
+
}
|
|
174
|
+
async triggerEvent(eventType) {
|
|
175
|
+
const eventHandlers = this.eventHandlers[eventType];
|
|
176
|
+
if (eventHandlers) {
|
|
177
|
+
for (let i = 0; i < eventHandlers.length; i++) {
|
|
178
|
+
const eventHandler = eventHandlers[i];
|
|
179
|
+
const eventResult = await eventHandler(this);
|
|
180
|
+
if (eventResult) {
|
|
181
|
+
return eventResult;
|
|
211
182
|
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
addCanceller(canceler) {
|
|
216
188
|
this.canceler = canceler;
|
|
217
|
-
}
|
|
218
|
-
|
|
189
|
+
}
|
|
190
|
+
cancel() {
|
|
219
191
|
this.cancelled = true;
|
|
220
192
|
if (this.canceler) {
|
|
221
193
|
this.canceler();
|
|
222
194
|
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
Object.defineProperty(RequestContext.prototype, "path", {
|
|
232
|
-
get: function () {
|
|
233
|
-
return this.computedPath;
|
|
234
|
-
},
|
|
235
|
-
enumerable: false,
|
|
236
|
-
configurable: true
|
|
237
|
-
});
|
|
238
|
-
RequestContext.prototype.updatePath = function (path) {
|
|
195
|
+
}
|
|
196
|
+
get requestUrl() {
|
|
197
|
+
return this.computedRequestUrl;
|
|
198
|
+
}
|
|
199
|
+
get path() {
|
|
200
|
+
return this.computedPath;
|
|
201
|
+
}
|
|
202
|
+
updatePath(path) {
|
|
239
203
|
this.computedPath = path;
|
|
240
204
|
this.computedRequestUrl = this.resolveRequestUrl();
|
|
241
|
-
}
|
|
242
|
-
|
|
205
|
+
}
|
|
206
|
+
updateBaseUrl(baseUrl) {
|
|
243
207
|
this.computedBaseUrl = baseUrl;
|
|
244
208
|
this.computedRequestUrl = this.resolveRequestUrl();
|
|
245
|
-
}
|
|
246
|
-
|
|
209
|
+
}
|
|
210
|
+
updateMethod(method) {
|
|
247
211
|
this.computedMethod = method;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// @ts-
|
|
212
|
+
}
|
|
213
|
+
updateBody(body) {
|
|
214
|
+
// @ts-expect-error
|
|
251
215
|
this.requestConfig.body = body;
|
|
252
216
|
this.parseRequestBody();
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
217
|
+
}
|
|
218
|
+
resolveRequestUrl() {
|
|
219
|
+
const url = (0, ApiUtils_1.resolveUrl)({ baseUrl: this.baseUrl, path: this.computedPath });
|
|
256
220
|
if (this.requestConfig.queryString) {
|
|
257
221
|
url.search = this.requestConfig.queryString;
|
|
258
222
|
}
|
|
259
223
|
return url;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
}());
|
|
224
|
+
}
|
|
225
|
+
}
|
|
263
226
|
exports.default = RequestContext;
|
package/cjs/RequestError.js
CHANGED
|
@@ -13,8 +13,8 @@ exports.RequestErrorCode = {
|
|
|
13
13
|
VALIDATION_BODY_VALIDATE_ERROR: "validation/body-validate-error",
|
|
14
14
|
VALIDATION_RESPONSE_VALIDATE_ERROR: "validation/response-validate-error",
|
|
15
15
|
};
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const REQUEST_ERROR_SYMBOL = "isApiDefError";
|
|
17
|
+
const isRequestError = (error) => {
|
|
18
18
|
if (!error) {
|
|
19
19
|
return false;
|
|
20
20
|
}
|
|
@@ -24,10 +24,9 @@ var isRequestError = function (error) {
|
|
|
24
24
|
return error[REQUEST_ERROR_SYMBOL] === true;
|
|
25
25
|
};
|
|
26
26
|
exports.isRequestError = isRequestError;
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var body = context.getParsedBody();
|
|
27
|
+
const convertToRequestError = (config) => {
|
|
28
|
+
const { error, context, response, code } = config;
|
|
29
|
+
const body = context.getParsedBody();
|
|
31
30
|
if (error.constructor.name === "TypeError") {
|
|
32
31
|
error.constructor = TypeError;
|
|
33
32
|
Object.setPrototypeOf(error, TypeError.prototype);
|
|
@@ -36,23 +35,22 @@ var convertToRequestError = function (config) {
|
|
|
36
35
|
error.constructor = Error;
|
|
37
36
|
Object.setPrototypeOf(error, Error.prototype);
|
|
38
37
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
_a.request = {
|
|
38
|
+
const resultError = Object.assign(error, {
|
|
39
|
+
name: "RequestError",
|
|
40
|
+
response: response,
|
|
41
|
+
code: code,
|
|
42
|
+
[REQUEST_ERROR_SYMBOL]: true,
|
|
43
|
+
attempts: context.stats.attempt,
|
|
44
|
+
request: {
|
|
47
45
|
url: context.requestUrl.href,
|
|
48
46
|
query: context.requestConfig.queryObject,
|
|
49
47
|
headers: context.requestConfig.headers,
|
|
50
48
|
body: body,
|
|
51
49
|
},
|
|
52
|
-
|
|
50
|
+
});
|
|
53
51
|
try {
|
|
54
52
|
Object.defineProperty(resultError, "message", {
|
|
55
|
-
value:
|
|
53
|
+
value: `A ${context.method.toUpperCase()} request to '${context.requestUrl.href}' failed${response?.status ? ` with status code ${response.status}` : ""} [${code}]: ${resultError.message}`,
|
|
56
54
|
});
|
|
57
55
|
}
|
|
58
56
|
catch (e) {
|
|
@@ -63,10 +61,9 @@ var convertToRequestError = function (config) {
|
|
|
63
61
|
return resultError;
|
|
64
62
|
};
|
|
65
63
|
exports.convertToRequestError = convertToRequestError;
|
|
66
|
-
|
|
67
|
-
var _a;
|
|
64
|
+
const getErrorResponse = (error) => {
|
|
68
65
|
if ((0, exports.isRequestError)(error)) {
|
|
69
|
-
return
|
|
66
|
+
return error.response ?? undefined;
|
|
70
67
|
}
|
|
71
68
|
return undefined;
|
|
72
69
|
};
|