api-def 0.6.2-alpha.1 → 0.6.2-alpha.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/RequestContext.d.ts +1 -0
- package/cjs/RequestContext.js +32 -0
- package/cjs/RequestError.d.ts +2 -0
- package/cjs/RequestError.js +11 -5
- package/cjs/Requester.js +4 -1
- package/cjs/backend/FetchRequestBackend.js +1 -24
- package/cjs/backend/MockRequestBackend.js +3 -0
- package/esm/RequestContext.d.ts +1 -0
- package/esm/RequestContext.js +32 -0
- package/esm/RequestError.d.ts +2 -0
- package/esm/RequestError.js +11 -5
- package/esm/Requester.js +4 -1
- package/esm/backend/FetchRequestBackend.js +1 -24
- package/esm/backend/MockRequestBackend.js +3 -0
- package/package.json +1 -1
package/cjs/RequestContext.d.ts
CHANGED
package/cjs/RequestContext.js
CHANGED
|
@@ -191,6 +191,38 @@ var RequestContext = /** @class */ (function () {
|
|
|
191
191
|
this.canceler();
|
|
192
192
|
}
|
|
193
193
|
};
|
|
194
|
+
RequestContext.prototype.getRequestUrl = function () {
|
|
195
|
+
var _a;
|
|
196
|
+
var path = !this.baseUrl.endsWith("/")
|
|
197
|
+
? this.baseUrl + "/"
|
|
198
|
+
: this.baseUrl;
|
|
199
|
+
path += this.computedPath.startsWith("/")
|
|
200
|
+
? this.computedPath.substring(1)
|
|
201
|
+
: this.computedPath;
|
|
202
|
+
var origin = undefined;
|
|
203
|
+
if (typeof window !== "undefined") {
|
|
204
|
+
origin = window.origin;
|
|
205
|
+
}
|
|
206
|
+
if (!origin) {
|
|
207
|
+
if (path.indexOf("://") === -1) {
|
|
208
|
+
path = "https://".concat(path);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
var url = new URL(path, origin);
|
|
212
|
+
if (this.computedConfig.query) {
|
|
213
|
+
if (this.computedConfig.queryParser) {
|
|
214
|
+
url.search = this.computedConfig.queryParser(this.computedConfig.query);
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
var queryKeys = Object.keys(this.computedConfig.query);
|
|
218
|
+
for (var i = 0; i < queryKeys.length; i++) {
|
|
219
|
+
var key = queryKeys[i];
|
|
220
|
+
url.searchParams.append(key, ((_a = this.computedConfig.query[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "");
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return url;
|
|
225
|
+
};
|
|
194
226
|
return RequestContext;
|
|
195
227
|
}());
|
|
196
228
|
exports.default = RequestContext;
|
package/cjs/RequestError.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ApiResponse } from "./ApiTypes";
|
|
2
2
|
import { EnumOf } from "./Utils";
|
|
3
|
+
import RequestContext from "./RequestContext";
|
|
3
4
|
export declare const RequestErrorCode: {
|
|
4
5
|
readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
|
|
5
6
|
readonly REQUEST_NETWORK_ERROR: "request/network-error";
|
|
@@ -17,6 +18,7 @@ export declare const isRequestError: (error: Error) => error is RequestError;
|
|
|
17
18
|
export interface RequestErrorConfig {
|
|
18
19
|
error: Error;
|
|
19
20
|
code: string;
|
|
21
|
+
context: RequestContext;
|
|
20
22
|
response?: ApiResponse | null;
|
|
21
23
|
}
|
|
22
24
|
export declare const convertToRequestError: (config: RequestErrorConfig) => RequestError;
|
package/cjs/RequestError.js
CHANGED
|
@@ -13,15 +13,21 @@ var isRequestError = function (error) {
|
|
|
13
13
|
};
|
|
14
14
|
exports.isRequestError = isRequestError;
|
|
15
15
|
var convertToRequestError = function (config) {
|
|
16
|
-
var error = config.error, response = config.response, code = config.code;
|
|
17
|
-
|
|
16
|
+
var error = config.error, context = config.context, response = config.response, code = config.code;
|
|
17
|
+
var resultError = Object.assign(error, {
|
|
18
18
|
name: "RequestError",
|
|
19
19
|
response: response,
|
|
20
20
|
code: code,
|
|
21
21
|
isRequestError: true,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
request: {
|
|
23
|
+
url: context.getRequestUrl().href,
|
|
24
|
+
query: context.computedConfig.query,
|
|
25
|
+
headers: context.computedConfig.headers,
|
|
26
|
+
body: context.getParsedBody(),
|
|
27
|
+
},
|
|
25
28
|
});
|
|
29
|
+
delete resultError.config;
|
|
30
|
+
delete resultError.toJSON;
|
|
31
|
+
return resultError;
|
|
26
32
|
};
|
|
27
33
|
exports.convertToRequestError = convertToRequestError;
|
package/cjs/Requester.js
CHANGED
|
@@ -161,6 +161,7 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
|
|
|
161
161
|
error: new Error("[api-def] Invalid response status code '".concat(parsedResponse.status, "'")),
|
|
162
162
|
response: parsedResponse,
|
|
163
163
|
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_STATUS,
|
|
164
|
+
context: context,
|
|
164
165
|
});
|
|
165
166
|
}
|
|
166
167
|
context.response = parsedResponse;
|
|
@@ -234,6 +235,7 @@ var parseResponse = function (context, response, error) { return __awaiter(void
|
|
|
234
235
|
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
235
236
|
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
236
237
|
response: parsedResponse_1,
|
|
238
|
+
context: context,
|
|
237
239
|
});
|
|
238
240
|
}
|
|
239
241
|
// transform arrayBuffer to json
|
|
@@ -251,6 +253,7 @@ var parseResponse = function (context, response, error) { return __awaiter(void
|
|
|
251
253
|
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
252
254
|
code: RequestError_1.RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
253
255
|
response: parsedResponse_1,
|
|
256
|
+
context: context,
|
|
254
257
|
});
|
|
255
258
|
}
|
|
256
259
|
}
|
|
@@ -287,7 +290,7 @@ var parseError = function (context, rawError) { return __awaiter(void 0, void 0,
|
|
|
287
290
|
}
|
|
288
291
|
}
|
|
289
292
|
errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
|
|
290
|
-
error = (0, RequestError_1.convertToRequestError)(__assign({ error: rawError, response: errorResponse, code: code }, errorInfo));
|
|
293
|
+
error = (0, RequestError_1.convertToRequestError)(__assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
|
|
291
294
|
_a.label = 5;
|
|
292
295
|
case 5: return [2 /*return*/, error];
|
|
293
296
|
}
|
|
@@ -128,34 +128,10 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
128
128
|
});
|
|
129
129
|
};
|
|
130
130
|
FetchRequestBackend.prototype.makeRequest = function (context) {
|
|
131
|
-
var _a;
|
|
132
131
|
if (!this.fetch) {
|
|
133
132
|
throw new Error("[api-def] No fetch impl was provided to FetchRequestBackend");
|
|
134
133
|
}
|
|
135
134
|
var computedConfig = context.computedConfig;
|
|
136
|
-
var path = !context.baseUrl.endsWith("/")
|
|
137
|
-
? context.baseUrl + "/"
|
|
138
|
-
: context.baseUrl;
|
|
139
|
-
path += context.computedPath.startsWith("/")
|
|
140
|
-
? context.computedPath.substring(1)
|
|
141
|
-
: context.computedPath;
|
|
142
|
-
var origin = undefined;
|
|
143
|
-
if (typeof window !== "undefined") {
|
|
144
|
-
origin = window.origin;
|
|
145
|
-
}
|
|
146
|
-
var url = new URL(path, origin);
|
|
147
|
-
if (computedConfig.query) {
|
|
148
|
-
if (context.computedConfig.queryParser) {
|
|
149
|
-
url.search = context.computedConfig.queryParser(computedConfig.query);
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
var queryKeys = Object.keys(computedConfig.query);
|
|
153
|
-
for (var i = 0; i < queryKeys.length; i++) {
|
|
154
|
-
var key = queryKeys[i];
|
|
155
|
-
url.searchParams.append(key, ((_a = computedConfig.query[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "");
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
135
|
// abort controller is a newer feature than fetch
|
|
160
136
|
var abortController = AbortController && new AbortController();
|
|
161
137
|
var abortSignal = abortController ? abortController.signal : undefined;
|
|
@@ -174,6 +150,7 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
174
150
|
}
|
|
175
151
|
return parsedHeaders;
|
|
176
152
|
}, {});
|
|
153
|
+
var url = context.getRequestUrl();
|
|
177
154
|
var promise = this.fetch(url.href, {
|
|
178
155
|
method: context.method,
|
|
179
156
|
body: bodyJsonify ? JSON.stringify(body) : body,
|
|
@@ -74,6 +74,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
74
74
|
throw (0, RequestError_1.convertToRequestError)({
|
|
75
75
|
error: new Error("[api-def] Attempted to run mocked request without mocking function"),
|
|
76
76
|
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
77
|
+
context: context,
|
|
77
78
|
});
|
|
78
79
|
}
|
|
79
80
|
req = {
|
|
@@ -110,6 +111,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
110
111
|
throw (0, RequestError_1.convertToRequestError)({
|
|
111
112
|
error: new Error("[api-def] Min delay cannot be greater than max delay"),
|
|
112
113
|
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
114
|
+
context: context,
|
|
113
115
|
});
|
|
114
116
|
}
|
|
115
117
|
delayMs = (0, Utils_1.randInt)(min, max);
|
|
@@ -129,6 +131,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
129
131
|
throw (0, RequestError_1.convertToRequestError)({
|
|
130
132
|
error: new Error("[api-def] Mocked API did not respond"),
|
|
131
133
|
code: RequestError_1.RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
134
|
+
context: context,
|
|
132
135
|
});
|
|
133
136
|
}
|
|
134
137
|
parsedHeaders = Object.keys(res.headers).reduce(function (parsedHeaders, key) {
|
package/esm/RequestContext.d.ts
CHANGED
package/esm/RequestContext.js
CHANGED
|
@@ -189,6 +189,38 @@ var RequestContext = /** @class */ (function () {
|
|
|
189
189
|
this.canceler();
|
|
190
190
|
}
|
|
191
191
|
};
|
|
192
|
+
RequestContext.prototype.getRequestUrl = function () {
|
|
193
|
+
var _a;
|
|
194
|
+
var path = !this.baseUrl.endsWith("/")
|
|
195
|
+
? this.baseUrl + "/"
|
|
196
|
+
: this.baseUrl;
|
|
197
|
+
path += this.computedPath.startsWith("/")
|
|
198
|
+
? this.computedPath.substring(1)
|
|
199
|
+
: this.computedPath;
|
|
200
|
+
var origin = undefined;
|
|
201
|
+
if (typeof window !== "undefined") {
|
|
202
|
+
origin = window.origin;
|
|
203
|
+
}
|
|
204
|
+
if (!origin) {
|
|
205
|
+
if (path.indexOf("://") === -1) {
|
|
206
|
+
path = "https://".concat(path);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
var url = new URL(path, origin);
|
|
210
|
+
if (this.computedConfig.query) {
|
|
211
|
+
if (this.computedConfig.queryParser) {
|
|
212
|
+
url.search = this.computedConfig.queryParser(this.computedConfig.query);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
var queryKeys = Object.keys(this.computedConfig.query);
|
|
216
|
+
for (var i = 0; i < queryKeys.length; i++) {
|
|
217
|
+
var key = queryKeys[i];
|
|
218
|
+
url.searchParams.append(key, ((_a = this.computedConfig.query[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "");
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return url;
|
|
223
|
+
};
|
|
192
224
|
return RequestContext;
|
|
193
225
|
}());
|
|
194
226
|
export default RequestContext;
|
package/esm/RequestError.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ApiResponse } from "./ApiTypes";
|
|
2
2
|
import { EnumOf } from "./Utils";
|
|
3
|
+
import RequestContext from "./RequestContext";
|
|
3
4
|
export declare const RequestErrorCode: {
|
|
4
5
|
readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
|
|
5
6
|
readonly REQUEST_NETWORK_ERROR: "request/network-error";
|
|
@@ -17,6 +18,7 @@ export declare const isRequestError: (error: Error) => error is RequestError;
|
|
|
17
18
|
export interface RequestErrorConfig {
|
|
18
19
|
error: Error;
|
|
19
20
|
code: string;
|
|
21
|
+
context: RequestContext;
|
|
20
22
|
response?: ApiResponse | null;
|
|
21
23
|
}
|
|
22
24
|
export declare const convertToRequestError: (config: RequestErrorConfig) => RequestError;
|
package/esm/RequestError.js
CHANGED
|
@@ -9,14 +9,20 @@ export var isRequestError = function (error) {
|
|
|
9
9
|
return "isRequestError" in error;
|
|
10
10
|
};
|
|
11
11
|
export var convertToRequestError = function (config) {
|
|
12
|
-
var error = config.error, response = config.response, code = config.code;
|
|
13
|
-
|
|
12
|
+
var error = config.error, context = config.context, response = config.response, code = config.code;
|
|
13
|
+
var resultError = Object.assign(error, {
|
|
14
14
|
name: "RequestError",
|
|
15
15
|
response: response,
|
|
16
16
|
code: code,
|
|
17
17
|
isRequestError: true,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
request: {
|
|
19
|
+
url: context.getRequestUrl().href,
|
|
20
|
+
query: context.computedConfig.query,
|
|
21
|
+
headers: context.computedConfig.headers,
|
|
22
|
+
body: context.getParsedBody(),
|
|
23
|
+
},
|
|
21
24
|
});
|
|
25
|
+
delete resultError.config;
|
|
26
|
+
delete resultError.toJSON;
|
|
27
|
+
return resultError;
|
|
22
28
|
};
|
package/esm/Requester.js
CHANGED
|
@@ -157,6 +157,7 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
|
|
|
157
157
|
error: new Error("[api-def] Invalid response status code '".concat(parsedResponse.status, "'")),
|
|
158
158
|
response: parsedResponse,
|
|
159
159
|
code: RequestErrorCode.REQUEST_INVALID_STATUS,
|
|
160
|
+
context: context,
|
|
160
161
|
});
|
|
161
162
|
}
|
|
162
163
|
context.response = parsedResponse;
|
|
@@ -230,6 +231,7 @@ var parseResponse = function (context, response, error) { return __awaiter(void
|
|
|
230
231
|
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
231
232
|
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
232
233
|
response: parsedResponse_1,
|
|
234
|
+
context: context,
|
|
233
235
|
});
|
|
234
236
|
}
|
|
235
237
|
// transform arrayBuffer to json
|
|
@@ -247,6 +249,7 @@ var parseResponse = function (context, response, error) { return __awaiter(void
|
|
|
247
249
|
error: new Error("[api-def] Expected '".concat(context.responseType, "' response, got '").concat(inferredResponseType, "' (from 'Content-Type' of '").concat(contentType, "')")),
|
|
248
250
|
code: RequestErrorCode.REQUEST_MISMATCH_RESPONSE_TYPE,
|
|
249
251
|
response: parsedResponse_1,
|
|
252
|
+
context: context,
|
|
250
253
|
});
|
|
251
254
|
}
|
|
252
255
|
}
|
|
@@ -283,7 +286,7 @@ var parseError = function (context, rawError) { return __awaiter(void 0, void 0,
|
|
|
283
286
|
}
|
|
284
287
|
}
|
|
285
288
|
errorInfo = context.backend.getErrorInfo(rawError, errorResponse);
|
|
286
|
-
error = convertToRequestError(__assign({ error: rawError, response: errorResponse, code: code }, errorInfo));
|
|
289
|
+
error = convertToRequestError(__assign({ error: rawError, response: errorResponse, code: code, context: context }, errorInfo));
|
|
287
290
|
_a.label = 5;
|
|
288
291
|
case 5: return [2 /*return*/, error];
|
|
289
292
|
}
|
|
@@ -126,34 +126,10 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
126
126
|
});
|
|
127
127
|
};
|
|
128
128
|
FetchRequestBackend.prototype.makeRequest = function (context) {
|
|
129
|
-
var _a;
|
|
130
129
|
if (!this.fetch) {
|
|
131
130
|
throw new Error("[api-def] No fetch impl was provided to FetchRequestBackend");
|
|
132
131
|
}
|
|
133
132
|
var computedConfig = context.computedConfig;
|
|
134
|
-
var path = !context.baseUrl.endsWith("/")
|
|
135
|
-
? context.baseUrl + "/"
|
|
136
|
-
: context.baseUrl;
|
|
137
|
-
path += context.computedPath.startsWith("/")
|
|
138
|
-
? context.computedPath.substring(1)
|
|
139
|
-
: context.computedPath;
|
|
140
|
-
var origin = undefined;
|
|
141
|
-
if (typeof window !== "undefined") {
|
|
142
|
-
origin = window.origin;
|
|
143
|
-
}
|
|
144
|
-
var url = new URL(path, origin);
|
|
145
|
-
if (computedConfig.query) {
|
|
146
|
-
if (context.computedConfig.queryParser) {
|
|
147
|
-
url.search = context.computedConfig.queryParser(computedConfig.query);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
var queryKeys = Object.keys(computedConfig.query);
|
|
151
|
-
for (var i = 0; i < queryKeys.length; i++) {
|
|
152
|
-
var key = queryKeys[i];
|
|
153
|
-
url.searchParams.append(key, ((_a = computedConfig.query[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "");
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
133
|
// abort controller is a newer feature than fetch
|
|
158
134
|
var abortController = AbortController && new AbortController();
|
|
159
135
|
var abortSignal = abortController ? abortController.signal : undefined;
|
|
@@ -172,6 +148,7 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
172
148
|
}
|
|
173
149
|
return parsedHeaders;
|
|
174
150
|
}, {});
|
|
151
|
+
var url = context.getRequestUrl();
|
|
175
152
|
var promise = this.fetch(url.href, {
|
|
176
153
|
method: context.method,
|
|
177
154
|
body: bodyJsonify ? JSON.stringify(body) : body,
|
|
@@ -72,6 +72,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
72
72
|
throw convertToRequestError({
|
|
73
73
|
error: new Error("[api-def] Attempted to run mocked request without mocking function"),
|
|
74
74
|
code: RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
75
|
+
context: context,
|
|
75
76
|
});
|
|
76
77
|
}
|
|
77
78
|
req = {
|
|
@@ -108,6 +109,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
108
109
|
throw convertToRequestError({
|
|
109
110
|
error: new Error("[api-def] Min delay cannot be greater than max delay"),
|
|
110
111
|
code: RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
112
|
+
context: context,
|
|
111
113
|
});
|
|
112
114
|
}
|
|
113
115
|
delayMs = randInt(min, max);
|
|
@@ -127,6 +129,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
127
129
|
throw convertToRequestError({
|
|
128
130
|
error: new Error("[api-def] Mocked API did not respond"),
|
|
129
131
|
code: RequestErrorCode.REQUEST_INVALID_CONFIG,
|
|
132
|
+
context: context,
|
|
130
133
|
});
|
|
131
134
|
}
|
|
132
135
|
parsedHeaders = Object.keys(res.headers).reduce(function (parsedHeaders, key) {
|