api-def 0.12.0-alpha.47 → 0.12.0-alpha.48
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 +5 -1
- package/cjs/Api.js +6 -2
- package/cjs/ApiTypes.d.ts +1 -1
- package/cjs/ApiUtils.d.ts +5 -1
- package/cjs/ApiUtils.js +2 -1
- package/cjs/Endpoint.js +4 -1
- package/cjs/RequestContext.js +2 -1
- package/cjs/RequestError.js +8 -1
- package/cjs/Requester.js +2 -6
- package/esm/Api.d.ts +5 -1
- package/esm/Api.js +6 -2
- package/esm/ApiTypes.d.ts +1 -1
- package/esm/ApiUtils.d.ts +5 -1
- package/esm/ApiUtils.js +2 -1
- package/esm/Endpoint.js +4 -1
- package/esm/RequestContext.js +2 -1
- package/esm/RequestError.js +8 -1
- package/esm/Requester.js +2 -6
- package/package.json +1 -1
package/cjs/Api.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
|
+
import { type ResolveUrlOptions } from "./ApiUtils";
|
|
2
3
|
import type Endpoint from "./Endpoint";
|
|
3
4
|
import EndpointBuilder from "./EndpointBuilder";
|
|
4
5
|
import type { ApiMockingConfig } from "./MockingTypes";
|
|
@@ -16,6 +17,9 @@ export interface ApiOptions {
|
|
|
16
17
|
readonly mocking?: ApiMockingConfig;
|
|
17
18
|
readonly requestBackend?: RequestBackend;
|
|
18
19
|
}
|
|
20
|
+
export interface ApiResolveUrlOptions extends Omit<ResolveUrlOptions, "baseUrl"> {
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
}
|
|
19
23
|
export type ApiInfo = Omit<ApiOptions, "config"> & Required<Pick<ApiOptions, "middleware" | "requestBackend">>;
|
|
20
24
|
export declare class Api implements ApiInfo {
|
|
21
25
|
private readonly info;
|
|
@@ -49,5 +53,5 @@ export declare class Api implements ApiInfo {
|
|
|
49
53
|
put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
50
54
|
delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
51
55
|
patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
52
|
-
resolveUrl(
|
|
56
|
+
resolveUrl(options: ApiResolveUrlOptions): URL;
|
|
53
57
|
}
|
package/cjs/Api.js
CHANGED
|
@@ -200,8 +200,12 @@ var Api = /** @class */ (function () {
|
|
|
200
200
|
Api.prototype.computeRequestConfig = function () {
|
|
201
201
|
return ((typeof this.defaultRequestConfig === "function" ? this.defaultRequestConfig() : this.defaultRequestConfig) || {});
|
|
202
202
|
};
|
|
203
|
-
Api.prototype.resolveUrl = function (
|
|
204
|
-
|
|
203
|
+
Api.prototype.resolveUrl = function (options) {
|
|
204
|
+
var _a;
|
|
205
|
+
return (0, ApiUtils_1.resolveUrl)({
|
|
206
|
+
baseUrl: (_a = options.baseUrl) !== null && _a !== void 0 ? _a : this.baseUrl,
|
|
207
|
+
path: options.path,
|
|
208
|
+
});
|
|
205
209
|
};
|
|
206
210
|
return Api;
|
|
207
211
|
}());
|
package/cjs/ApiTypes.d.ts
CHANGED
package/cjs/ApiUtils.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { ResponseType } from "./ApiConstants";
|
|
2
2
|
import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
3
|
+
export interface ResolveUrlOptions {
|
|
4
|
+
path: string;
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
}
|
|
3
7
|
export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
|
|
4
8
|
export declare const isNetworkError: (error: Error) => boolean;
|
|
5
9
|
export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
|
|
6
10
|
export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
|
|
7
11
|
export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined) => string;
|
|
8
|
-
export declare const resolveUrl: (
|
|
12
|
+
export declare const resolveUrl: (options: ResolveUrlOptions) => URL;
|
package/cjs/ApiUtils.js
CHANGED
|
@@ -71,7 +71,8 @@ var resolvePathParams = function (path, params) {
|
|
|
71
71
|
return computedPath;
|
|
72
72
|
};
|
|
73
73
|
exports.resolvePathParams = resolvePathParams;
|
|
74
|
-
var resolveUrl = function (
|
|
74
|
+
var resolveUrl = function (options) {
|
|
75
|
+
var baseUrl = options.baseUrl, path = options.path;
|
|
75
76
|
var result = !baseUrl.endsWith("/") ? "".concat(baseUrl, "/") : baseUrl;
|
|
76
77
|
result += path.startsWith("/") ? path.substring(1) : path;
|
|
77
78
|
var origin = undefined;
|
package/cjs/Endpoint.js
CHANGED
|
@@ -156,7 +156,10 @@ var Endpoint = /** @class */ (function () {
|
|
|
156
156
|
};
|
|
157
157
|
Endpoint.prototype.resolveUrl = function (options) {
|
|
158
158
|
var query = options.query;
|
|
159
|
-
var url = this.api.resolveUrl(
|
|
159
|
+
var url = this.api.resolveUrl({
|
|
160
|
+
path: this.resolvePath(options),
|
|
161
|
+
baseUrl: options.baseUrl,
|
|
162
|
+
});
|
|
160
163
|
if (query) {
|
|
161
164
|
url.search = new URLSearchParams(query).toString();
|
|
162
165
|
}
|
package/cjs/RequestContext.js
CHANGED
|
@@ -68,6 +68,7 @@ var RequestContext = /** @class */ (function () {
|
|
|
68
68
|
attempt: 0,
|
|
69
69
|
cached: false,
|
|
70
70
|
startTimestamp: Date.now(),
|
|
71
|
+
endTimestamp: undefined,
|
|
71
72
|
};
|
|
72
73
|
this.eventHandlers = {};
|
|
73
74
|
this.mocking = mocking;
|
|
@@ -241,7 +242,7 @@ var RequestContext = /** @class */ (function () {
|
|
|
241
242
|
this.parseRequestBody();
|
|
242
243
|
};
|
|
243
244
|
RequestContext.prototype.resolveRequestUrl = function () {
|
|
244
|
-
var url = (0, ApiUtils_1.resolveUrl)(this.baseUrl, this.computedPath);
|
|
245
|
+
var url = (0, ApiUtils_1.resolveUrl)({ baseUrl: this.baseUrl, path: this.computedPath });
|
|
245
246
|
if (this.requestConfig.queryString) {
|
|
246
247
|
url.search = this.requestConfig.queryString;
|
|
247
248
|
}
|
package/cjs/RequestError.js
CHANGED
|
@@ -28,6 +28,14 @@ var convertToRequestError = function (config) {
|
|
|
28
28
|
var _a;
|
|
29
29
|
var error = config.error, context = config.context, response = config.response, code = config.code;
|
|
30
30
|
var body = context.getParsedBody();
|
|
31
|
+
if (error.constructor.name === "TypeError") {
|
|
32
|
+
error.constructor = TypeError;
|
|
33
|
+
Object.setPrototypeOf(error, TypeError.prototype);
|
|
34
|
+
}
|
|
35
|
+
else if (error.constructor.name === "Error") {
|
|
36
|
+
error.constructor = Error;
|
|
37
|
+
Object.setPrototypeOf(error, Error.prototype);
|
|
38
|
+
}
|
|
31
39
|
var resultError = Object.assign(error, (_a = {
|
|
32
40
|
name: "RequestError",
|
|
33
41
|
response: response,
|
|
@@ -52,7 +60,6 @@ var convertToRequestError = function (config) {
|
|
|
52
60
|
}
|
|
53
61
|
resultError.config = undefined;
|
|
54
62
|
resultError.toJSON = undefined;
|
|
55
|
-
Object.setPrototypeOf(error, Error);
|
|
56
63
|
return resultError;
|
|
57
64
|
};
|
|
58
65
|
exports.convertToRequestError = convertToRequestError;
|
package/cjs/Requester.js
CHANGED
|
@@ -121,8 +121,6 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
|
|
|
121
121
|
case 1:
|
|
122
122
|
beforeSendEventResult = _d.sent();
|
|
123
123
|
if (beforeSendEventResult && beforeSendEventResult.type === ApiConstants_1.EventResultType.RESPOND) {
|
|
124
|
-
// Calculate duration for early responses
|
|
125
|
-
context.stats.durationMs = Date.now() - context.stats.startTimestamp;
|
|
126
124
|
return [2 /*return*/, (context.response = beforeSendEventResult.response)];
|
|
127
125
|
}
|
|
128
126
|
// validation
|
|
@@ -185,13 +183,10 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
|
|
|
185
183
|
});
|
|
186
184
|
}
|
|
187
185
|
context.response = parsedResponse;
|
|
188
|
-
|
|
189
|
-
context.stats.durationMs = Date.now() - context.stats.startTimestamp;
|
|
186
|
+
context.stats.endTimestamp = Date.now();
|
|
190
187
|
return [2 /*return*/, parsedResponse];
|
|
191
188
|
case 4:
|
|
192
189
|
rawError_1 = _b.sent();
|
|
193
|
-
// Calculate duration for failed requests
|
|
194
|
-
context.stats.durationMs = Date.now() - context.stats.startTimestamp;
|
|
195
190
|
if (context.cancelled) {
|
|
196
191
|
rawError_1.isCancelledRequest = true;
|
|
197
192
|
}
|
|
@@ -200,6 +195,7 @@ var makeRequest = function (context) { return __awaiter(void 0, void 0, void 0,
|
|
|
200
195
|
error = _b.sent();
|
|
201
196
|
context.error = error;
|
|
202
197
|
context.response = error.response;
|
|
198
|
+
context.stats.endTimestamp = Date.now();
|
|
203
199
|
return [4 /*yield*/, context.triggerEvent(ApiConstants_1.RequestEvent.ERROR)];
|
|
204
200
|
case 6:
|
|
205
201
|
errorEventResult = _b.sent();
|
package/esm/Api.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
|
+
import { type ResolveUrlOptions } from "./ApiUtils";
|
|
2
3
|
import type Endpoint from "./Endpoint";
|
|
3
4
|
import EndpointBuilder from "./EndpointBuilder";
|
|
4
5
|
import type { ApiMockingConfig } from "./MockingTypes";
|
|
@@ -16,6 +17,9 @@ export interface ApiOptions {
|
|
|
16
17
|
readonly mocking?: ApiMockingConfig;
|
|
17
18
|
readonly requestBackend?: RequestBackend;
|
|
18
19
|
}
|
|
20
|
+
export interface ApiResolveUrlOptions extends Omit<ResolveUrlOptions, "baseUrl"> {
|
|
21
|
+
baseUrl?: string;
|
|
22
|
+
}
|
|
19
23
|
export type ApiInfo = Omit<ApiOptions, "config"> & Required<Pick<ApiOptions, "middleware" | "requestBackend">>;
|
|
20
24
|
export declare class Api implements ApiInfo {
|
|
21
25
|
private readonly info;
|
|
@@ -49,5 +53,5 @@ export declare class Api implements ApiInfo {
|
|
|
49
53
|
put: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
50
54
|
delete: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
51
55
|
patch: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
52
|
-
resolveUrl(
|
|
56
|
+
resolveUrl(options: ApiResolveUrlOptions): URL;
|
|
53
57
|
}
|
package/esm/Api.js
CHANGED
|
@@ -126,7 +126,11 @@ export class Api {
|
|
|
126
126
|
computeRequestConfig() {
|
|
127
127
|
return ((typeof this.defaultRequestConfig === "function" ? this.defaultRequestConfig() : this.defaultRequestConfig) || {});
|
|
128
128
|
}
|
|
129
|
-
resolveUrl(
|
|
130
|
-
|
|
129
|
+
resolveUrl(options) {
|
|
130
|
+
var _a;
|
|
131
|
+
return resolveUrl({
|
|
132
|
+
baseUrl: (_a = options.baseUrl) !== null && _a !== void 0 ? _a : this.baseUrl,
|
|
133
|
+
path: options.path,
|
|
134
|
+
});
|
|
131
135
|
}
|
|
132
136
|
}
|
package/esm/ApiTypes.d.ts
CHANGED
package/esm/ApiUtils.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import type { ResponseType } from "./ApiConstants";
|
|
2
2
|
import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
3
|
+
export interface ResolveUrlOptions {
|
|
4
|
+
path: string;
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
}
|
|
3
7
|
export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
|
|
4
8
|
export declare const isNetworkError: (error: Error) => boolean;
|
|
5
9
|
export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
|
|
6
10
|
export declare const inferResponseType: (contentType: string | null | undefined) => ResponseType;
|
|
7
11
|
export declare const resolvePathParams: (path: string, params: Record<string, string> | undefined) => string;
|
|
8
|
-
export declare const resolveUrl: (
|
|
12
|
+
export declare const resolveUrl: (options: ResolveUrlOptions) => URL;
|
package/esm/ApiUtils.js
CHANGED
|
@@ -62,7 +62,8 @@ export const resolvePathParams = (path, params) => {
|
|
|
62
62
|
}
|
|
63
63
|
return computedPath;
|
|
64
64
|
};
|
|
65
|
-
export const resolveUrl = (
|
|
65
|
+
export const resolveUrl = (options) => {
|
|
66
|
+
const { baseUrl, path } = options;
|
|
66
67
|
let result = !baseUrl.endsWith("/") ? `${baseUrl}/` : baseUrl;
|
|
67
68
|
result += path.startsWith("/") ? path.substring(1) : path;
|
|
68
69
|
let origin = undefined;
|
package/esm/Endpoint.js
CHANGED
|
@@ -69,7 +69,10 @@ export default class Endpoint {
|
|
|
69
69
|
}
|
|
70
70
|
resolveUrl(options) {
|
|
71
71
|
const { query } = options;
|
|
72
|
-
const url = this.api.resolveUrl(
|
|
72
|
+
const url = this.api.resolveUrl({
|
|
73
|
+
path: this.resolvePath(options),
|
|
74
|
+
baseUrl: options.baseUrl,
|
|
75
|
+
});
|
|
73
76
|
if (query) {
|
|
74
77
|
url.search = new URLSearchParams(query).toString();
|
|
75
78
|
}
|
package/esm/RequestContext.js
CHANGED
|
@@ -30,6 +30,7 @@ export default class RequestContext {
|
|
|
30
30
|
attempt: 0,
|
|
31
31
|
cached: false,
|
|
32
32
|
startTimestamp: Date.now(),
|
|
33
|
+
endTimestamp: undefined,
|
|
33
34
|
};
|
|
34
35
|
this.eventHandlers = {};
|
|
35
36
|
this.mocking = mocking;
|
|
@@ -164,7 +165,7 @@ export default class RequestContext {
|
|
|
164
165
|
this.parseRequestBody();
|
|
165
166
|
}
|
|
166
167
|
resolveRequestUrl() {
|
|
167
|
-
const url = resolveUrl(this.baseUrl, this.computedPath);
|
|
168
|
+
const url = resolveUrl({ baseUrl: this.baseUrl, path: this.computedPath });
|
|
168
169
|
if (this.requestConfig.queryString) {
|
|
169
170
|
url.search = this.requestConfig.queryString;
|
|
170
171
|
}
|
package/esm/RequestError.js
CHANGED
|
@@ -23,6 +23,14 @@ export const isRequestError = (error) => {
|
|
|
23
23
|
export const convertToRequestError = (config) => {
|
|
24
24
|
const { error, context, response, code } = config;
|
|
25
25
|
const body = context.getParsedBody();
|
|
26
|
+
if (error.constructor.name === "TypeError") {
|
|
27
|
+
error.constructor = TypeError;
|
|
28
|
+
Object.setPrototypeOf(error, TypeError.prototype);
|
|
29
|
+
}
|
|
30
|
+
else if (error.constructor.name === "Error") {
|
|
31
|
+
error.constructor = Error;
|
|
32
|
+
Object.setPrototypeOf(error, Error.prototype);
|
|
33
|
+
}
|
|
26
34
|
const resultError = Object.assign(error, {
|
|
27
35
|
name: "RequestError",
|
|
28
36
|
response: response,
|
|
@@ -46,6 +54,5 @@ export const convertToRequestError = (config) => {
|
|
|
46
54
|
}
|
|
47
55
|
resultError.config = undefined;
|
|
48
56
|
resultError.toJSON = undefined;
|
|
49
|
-
Object.setPrototypeOf(error, Error);
|
|
50
57
|
return resultError;
|
|
51
58
|
};
|
package/esm/Requester.js
CHANGED
|
@@ -70,8 +70,6 @@ const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
70
70
|
var _a, _b, _c;
|
|
71
71
|
const beforeSendEventResult = yield context.triggerEvent(RequestEvent.BEFORE_SEND);
|
|
72
72
|
if (beforeSendEventResult && beforeSendEventResult.type === EventResultType.RESPOND) {
|
|
73
|
-
// Calculate duration for early responses
|
|
74
|
-
context.stats.durationMs = Date.now() - context.stats.startTimestamp;
|
|
75
73
|
return (context.response = beforeSendEventResult.response);
|
|
76
74
|
}
|
|
77
75
|
// validation
|
|
@@ -124,19 +122,17 @@ const makeRequest = (context) => __awaiter(void 0, void 0, void 0, function* ()
|
|
|
124
122
|
});
|
|
125
123
|
}
|
|
126
124
|
context.response = parsedResponse;
|
|
127
|
-
|
|
128
|
-
context.stats.durationMs = Date.now() - context.stats.startTimestamp;
|
|
125
|
+
context.stats.endTimestamp = Date.now();
|
|
129
126
|
return parsedResponse;
|
|
130
127
|
}
|
|
131
128
|
catch (rawError) {
|
|
132
|
-
// Calculate duration for failed requests
|
|
133
|
-
context.stats.durationMs = Date.now() - context.stats.startTimestamp;
|
|
134
129
|
if (context.cancelled) {
|
|
135
130
|
rawError.isCancelledRequest = true;
|
|
136
131
|
}
|
|
137
132
|
const error = yield parseError(context, rawError);
|
|
138
133
|
context.error = error;
|
|
139
134
|
context.response = error.response;
|
|
135
|
+
context.stats.endTimestamp = Date.now();
|
|
140
136
|
const errorEventResult = yield context.triggerEvent(RequestEvent.ERROR);
|
|
141
137
|
if ((errorEventResult === null || errorEventResult === void 0 ? void 0 : errorEventResult.type) === EventResultType.RESPOND) {
|
|
142
138
|
return errorEventResult.response;
|