api-def 0.9.3 → 0.9.4-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/cjs/Api.d.ts +4 -4
- package/cjs/ApiConstants.d.ts +1 -1
- package/cjs/ApiTypes.d.ts +5 -5
- package/cjs/ApiUtils.d.ts +2 -2
- package/cjs/ApiUtils.js +3 -1
- package/cjs/Endpoint.d.ts +6 -6
- package/cjs/Endpoint.js +1 -1
- package/cjs/EndpointBuilder.d.ts +4 -4
- package/cjs/MockingTypes.d.ts +1 -1
- package/cjs/QueryHandling.d.ts +1 -1
- package/cjs/RequestConfig.d.ts +2 -2
- package/cjs/RequestContext.d.ts +7 -7
- package/cjs/RequestError.d.ts +3 -3
- package/cjs/RequestError.js +3 -1
- package/cjs/Requester.d.ts +2 -2
- package/cjs/Requester.js +3 -1
- package/cjs/TextDecoding.js +3 -1
- package/cjs/UtilTypes.d.ts +1 -1
- package/cjs/Validation.d.ts +2 -2
- package/cjs/backend/AxiosRequestBackend.d.ts +4 -3
- package/cjs/backend/FetchRequestBackend.d.ts +5 -4
- package/cjs/backend/FetchRequestBackend.js +8 -4
- package/cjs/backend/MockRequestBackend.d.ts +4 -3
- package/cjs/backend/MockRequestBackend.js +8 -8
- package/cjs/backend/RequestBackend.d.ts +2 -2
- package/cjs/cache/Caching.d.ts +1 -1
- package/cjs/cache/LocalForageCacheBackend.d.ts +1 -1
- package/cjs/cache/LocalStorageCacheBackend.d.ts +1 -1
- package/cjs/middleware/CacheMiddleware.d.ts +1 -1
- package/cjs/middleware/LoggingMiddleware.d.ts +1 -1
- package/cjs/middleware/LoggingMiddleware.js +7 -1
- package/cjs/util/retry/index.d.ts +1 -1
- package/cjs/util/retry/lib/retry.d.ts +1 -1
- package/cjs/util/retry/lib/retry.js +2 -2
- package/cjs/util/retry/lib/retryOperation.js +1 -1
- package/esm/Api.d.ts +4 -4
- package/esm/ApiConstants.d.ts +1 -1
- package/esm/ApiTypes.d.ts +5 -5
- package/esm/ApiUtils.d.ts +2 -2
- package/esm/ApiUtils.js +3 -1
- package/esm/Endpoint.d.ts +6 -6
- package/esm/Endpoint.js +1 -1
- package/esm/EndpointBuilder.d.ts +4 -4
- package/esm/MockingTypes.d.ts +1 -1
- package/esm/QueryHandling.d.ts +1 -1
- package/esm/RequestConfig.d.ts +2 -2
- package/esm/RequestConfig.js +1 -1
- package/esm/RequestContext.d.ts +7 -7
- package/esm/RequestError.d.ts +3 -3
- package/esm/RequestError.js +3 -1
- package/esm/Requester.d.ts +2 -2
- package/esm/Requester.js +3 -1
- package/esm/TextDecoding.js +3 -1
- package/esm/UtilTypes.d.ts +1 -1
- package/esm/Validation.d.ts +2 -2
- package/esm/backend/AxiosRequestBackend.d.ts +4 -3
- package/esm/backend/FetchRequestBackend.d.ts +5 -4
- package/esm/backend/FetchRequestBackend.js +8 -4
- package/esm/backend/MockRequestBackend.d.ts +4 -3
- package/esm/backend/MockRequestBackend.js +1 -1
- package/esm/backend/RequestBackend.d.ts +2 -2
- package/esm/cache/Caching.d.ts +1 -1
- package/esm/cache/LocalForageCacheBackend.d.ts +1 -1
- package/esm/cache/LocalStorageCacheBackend.d.ts +1 -1
- package/esm/middleware/CacheMiddleware.d.ts +1 -1
- package/esm/middleware/LoggingMiddleware.d.ts +1 -1
- package/esm/middleware/LoggingMiddleware.js +7 -1
- package/esm/util/retry/index.d.ts +1 -1
- package/esm/util/retry/lib/retry.d.ts +1 -1
- package/esm/util/retry/lib/retry.js +2 -2
- package/esm/util/retry/lib/retryOperation.js +1 -1
- package/package.json +16 -31
package/cjs/Api.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
|
-
import Endpoint from "./Endpoint";
|
|
1
|
+
import type { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
|
+
import type Endpoint from "./Endpoint";
|
|
3
3
|
import EndpointBuilder from "./EndpointBuilder";
|
|
4
|
-
import { ApiMockingConfig } from "./MockingTypes";
|
|
5
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
4
|
+
import type { ApiMockingConfig } from "./MockingTypes";
|
|
5
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
6
6
|
export declare const getRequestBackend: () => RequestBackend | null;
|
|
7
7
|
export declare const isRequestBackendDefault: () => boolean;
|
|
8
8
|
export declare const setRequestBackend: (backend: RequestBackend) => void;
|
package/cjs/ApiConstants.d.ts
CHANGED
package/cjs/ApiTypes.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Api } from "./Api";
|
|
2
|
-
import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import RequestContext from "./RequestContext";
|
|
4
|
-
import { Validation } from "./Validation";
|
|
5
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
1
|
+
import type { Api } from "./Api";
|
|
2
|
+
import type { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type RequestContext from "./RequestContext";
|
|
4
|
+
import type { Validation } from "./Validation";
|
|
5
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
6
6
|
export type AcceptableStatus = number | [min: number, max: number];
|
|
7
7
|
export type Headers = Record<string, string | number | boolean | null | undefined>;
|
|
8
8
|
export type Params = string;
|
package/cjs/ApiUtils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ResponseType } from "./ApiConstants";
|
|
2
|
-
import { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
1
|
+
import type { ResponseType } from "./ApiConstants";
|
|
2
|
+
import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
3
3
|
export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
|
|
4
4
|
export declare const isNetworkError: (error: Error) => boolean;
|
|
5
5
|
export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
|
package/cjs/ApiUtils.js
CHANGED
|
@@ -7,7 +7,9 @@ var isCancelledError = function (error) {
|
|
|
7
7
|
exports.isCancelledError = isCancelledError;
|
|
8
8
|
var isNetworkError = function (error) {
|
|
9
9
|
var _a;
|
|
10
|
-
return error.name === "NetworkError" ||
|
|
10
|
+
return (error.name === "NetworkError" ||
|
|
11
|
+
error.message === "Network Error" ||
|
|
12
|
+
((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
|
|
11
13
|
};
|
|
12
14
|
exports.isNetworkError = isNetworkError;
|
|
13
15
|
var DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
|
package/cjs/Endpoint.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Api } from "./Api";
|
|
2
|
-
import { RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
4
|
-
import * as Mocking from "./MockingTypes";
|
|
5
|
-
import { Validation } from "./Validation";
|
|
6
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
1
|
+
import type { Api } from "./Api";
|
|
2
|
+
import type { RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
4
|
+
import type * as Mocking from "./MockingTypes";
|
|
5
|
+
import type { Validation } from "./Validation";
|
|
6
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
7
7
|
export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined, Path extends string = string> {
|
|
8
8
|
readonly id: string;
|
|
9
9
|
readonly method: RequestMethod;
|
package/cjs/Endpoint.js
CHANGED
|
@@ -52,9 +52,9 @@ var Endpoint = /** @class */ (function () {
|
|
|
52
52
|
this.validation = info.validation || {};
|
|
53
53
|
}
|
|
54
54
|
Endpoint.prototype.submit = function (config) {
|
|
55
|
-
var _a, _b;
|
|
56
55
|
return __awaiter(this, void 0, void 0, function () {
|
|
57
56
|
var mock, apiMocking, mockingEnabled;
|
|
57
|
+
var _a, _b;
|
|
58
58
|
return __generator(this, function (_c) {
|
|
59
59
|
mock = false;
|
|
60
60
|
apiMocking = this.api.mocking;
|
package/cjs/EndpointBuilder.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as zod from "zod";
|
|
2
|
-
import { Api } from "./Api";
|
|
3
|
-
import { Body, Params, Query } from "./ApiTypes";
|
|
4
|
-
import Endpoint, { EndpointConfig } from "./Endpoint";
|
|
1
|
+
import type * as zod from "zod";
|
|
2
|
+
import type { Api } from "./Api";
|
|
3
|
+
import type { Body, Params, Query } from "./ApiTypes";
|
|
4
|
+
import Endpoint, { type EndpointConfig } from "./Endpoint";
|
|
5
5
|
export default class EndpointBuilder<R = any, P extends Params | undefined = undefined, Q extends Query | undefined = undefined, B extends Body | undefined = undefined> {
|
|
6
6
|
private api;
|
|
7
7
|
private readonly validation;
|
package/cjs/MockingTypes.d.ts
CHANGED
package/cjs/QueryHandling.d.ts
CHANGED
package/cjs/RequestConfig.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { BaseRequestConfig, Body, ComputedRequestConfig, Query, RequestConfig } from "./ApiTypes";
|
|
2
|
-
export declare const computeRequestConfig: <P extends string | undefined, Q extends Query, B extends Body | undefined>(configs: (
|
|
1
|
+
import { type BaseRequestConfig, type Body, type ComputedRequestConfig, type Query, type RequestConfig } from "./ApiTypes";
|
|
2
|
+
export declare const computeRequestConfig: <P extends string | undefined, Q extends Query, B extends Body | undefined>(configs: (RequestConfig<P, Q, B> | BaseRequestConfig | undefined)[]) => ComputedRequestConfig<P, Q, B>;
|
package/cjs/RequestContext.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Api } from "./Api";
|
|
2
|
-
import { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
|
|
4
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
-
import { RequestError } from "./RequestError";
|
|
6
|
-
import { Validation } from "./Validation";
|
|
7
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
1
|
+
import type { Api } from "./Api";
|
|
2
|
+
import type { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
|
|
4
|
+
import type { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
+
import type { RequestError } from "./RequestError";
|
|
6
|
+
import type { Validation } from "./Validation";
|
|
7
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
8
8
|
export default class RequestContext<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
9
9
|
readonly id: number;
|
|
10
10
|
readonly key: string;
|
package/cjs/RequestError.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ApiResponse } from "./ApiTypes";
|
|
2
|
-
import RequestContext from "./RequestContext";
|
|
3
|
-
import { EnumOf } from "./Utils";
|
|
1
|
+
import type { ApiResponse } from "./ApiTypes";
|
|
2
|
+
import type RequestContext from "./RequestContext";
|
|
3
|
+
import type { EnumOf } from "./Utils";
|
|
4
4
|
export declare const RequestErrorCode: {
|
|
5
5
|
readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
|
|
6
6
|
readonly REQUEST_NETWORK_ERROR: "request/network-error";
|
package/cjs/RequestError.js
CHANGED
|
@@ -34,7 +34,9 @@ var convertToRequestError = function (config) {
|
|
|
34
34
|
},
|
|
35
35
|
});
|
|
36
36
|
try {
|
|
37
|
-
Object.defineProperty(resultError, "message", {
|
|
37
|
+
Object.defineProperty(resultError, "message", {
|
|
38
|
+
value: "Request failed".concat((response === null || response === void 0 ? void 0 : response.status) ? " with status code ".concat(response.status) : "", " [").concat(code, "]: ").concat(resultError.message),
|
|
39
|
+
});
|
|
38
40
|
}
|
|
39
41
|
catch (e) {
|
|
40
42
|
// ignore
|
package/cjs/Requester.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
1
|
+
import type { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
+
import type { EndpointMockingConfig } from "./MockingTypes";
|
|
3
3
|
export declare const submit: <R, P extends string | undefined, Q extends Query, B extends Body | undefined>(host: RequestHost, config: RequestConfig<P, Q, B>, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined) => Promise<ApiResponse<R>>;
|
package/cjs/Requester.js
CHANGED
|
@@ -315,7 +315,9 @@ var parseError = function (context, rawError) { return __awaiter(void 0, void 0,
|
|
|
315
315
|
errorResponse = _b.sent();
|
|
316
316
|
_b.label = 4;
|
|
317
317
|
case 4:
|
|
318
|
-
code = (0, ApiUtils_1.isNetworkError)(rawError)
|
|
318
|
+
code = (0, ApiUtils_1.isNetworkError)(rawError)
|
|
319
|
+
? RequestError_1.RequestErrorCode.REQUEST_NETWORK_ERROR
|
|
320
|
+
: RequestError_1.RequestErrorCode.MISC_UNKNOWN_ERROR;
|
|
319
321
|
if (errorResponse) {
|
|
320
322
|
if (!(0, ApiUtils_1.isAcceptableStatus)(errorResponse.status, context.computedConfig.acceptableStatus)) {
|
|
321
323
|
code = RequestError_1.RequestErrorCode.REQUEST_INVALID_STATUS;
|
package/cjs/TextDecoding.js
CHANGED
|
@@ -22,7 +22,9 @@ var textDecode = function (inputArrayOrBuffer, options) {
|
|
|
22
22
|
var asObjectString;
|
|
23
23
|
if (!arrayBuffer_isView(inputAs8)) {
|
|
24
24
|
asObjectString = Object_prototype_toString.call(inputAs8);
|
|
25
|
-
if (asObjectString !== arrayBufferString &&
|
|
25
|
+
if (asObjectString !== arrayBufferString &&
|
|
26
|
+
asObjectString !== sharedArrayBufferString &&
|
|
27
|
+
asObjectString !== undefinedObjectString)
|
|
26
28
|
throw TypeError("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
|
|
27
29
|
inputAs8 = NativeUint8Array ? new patchedU8Array(inputAs8) : inputAs8 || [];
|
|
28
30
|
}
|
package/cjs/UtilTypes.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Endpoint from "./Endpoint";
|
|
1
|
+
import type Endpoint from "./Endpoint";
|
|
2
2
|
export type ResponseOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<infer R, any, any, any> ? R : never;
|
|
3
3
|
export type ParamsOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, infer P, any, any> ? P : never;
|
|
4
4
|
export type QueryOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, any, infer Q, any> ? Q : never;
|
package/cjs/Validation.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as zod from "zod";
|
|
2
|
-
import { Body, Params, Query } from "./ApiTypes";
|
|
1
|
+
import type * as zod from "zod";
|
|
2
|
+
import type { Body, Params, Query } from "./ApiTypes";
|
|
3
3
|
export interface Validation<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
4
4
|
query?: zod.Schema<Q>;
|
|
5
5
|
params?: zod.Schema<P>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { AxiosError, AxiosResponse } from "axios";
|
|
2
|
-
import { ApiResponse } from "../ApiTypes";
|
|
3
|
-
import RequestContext from "../RequestContext";
|
|
4
|
-
import RequestBackend
|
|
2
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
3
|
+
import type RequestContext from "../RequestContext";
|
|
4
|
+
import type RequestBackend from "./RequestBackend";
|
|
5
|
+
import type { ConvertedApiResponse, RequestBackendErrorInfo, RequestOperation } from "./RequestBackend";
|
|
5
6
|
export declare const isAxiosError: (error: Error) => error is AxiosError<unknown, any>;
|
|
6
7
|
export default class AxiosRequestBackend implements RequestBackend<AxiosResponse> {
|
|
7
8
|
readonly id = "axios";
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ApiResponse } from "../ApiTypes";
|
|
2
|
-
import RequestContext from "../RequestContext";
|
|
3
|
-
import { Fetch } from "../Utils";
|
|
4
|
-
import RequestBackend
|
|
1
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
2
|
+
import type RequestContext from "../RequestContext";
|
|
3
|
+
import { type Fetch } from "../Utils";
|
|
4
|
+
import type RequestBackend from "./RequestBackend";
|
|
5
|
+
import type { ConvertedApiResponse, RequestBackendErrorInfo, RequestOperation } from "./RequestBackend";
|
|
5
6
|
export default class FetchRequestBackend implements RequestBackend<Response> {
|
|
6
7
|
fetch: (((input: RequestInfo | URL, init?: RequestInit | undefined) => Promise<Response>) & typeof fetch) | undefined;
|
|
7
8
|
readonly id = "fetch";
|
|
@@ -87,9 +87,9 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
87
87
|
});
|
|
88
88
|
};
|
|
89
89
|
FetchRequestBackend.prototype.convertResponse = function (context, response) {
|
|
90
|
-
var _a;
|
|
91
90
|
return __awaiter(this, void 0, void 0, function () {
|
|
92
91
|
var status, headers, processedHeaders, convertedResponse, responseType, text, data, error_1;
|
|
92
|
+
var _a;
|
|
93
93
|
return __generator(this, function (_b) {
|
|
94
94
|
switch (_b.label) {
|
|
95
95
|
case 0:
|
|
@@ -168,13 +168,17 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
168
168
|
return parsedHeaders;
|
|
169
169
|
}, {});
|
|
170
170
|
var url = context.requestUrl;
|
|
171
|
-
var
|
|
171
|
+
var fetchOptions = {
|
|
172
172
|
method: context.method.toUpperCase(),
|
|
173
173
|
body: bodyJsonify ? JSON.stringify(body) : body,
|
|
174
174
|
headers: parsedHeaders,
|
|
175
|
-
mode: "cors",
|
|
176
175
|
signal: abortSignal,
|
|
177
|
-
}
|
|
176
|
+
};
|
|
177
|
+
// edge doesn't support mode
|
|
178
|
+
if ("mode" in Request.prototype) {
|
|
179
|
+
fetchOptions.mode = "cors";
|
|
180
|
+
}
|
|
181
|
+
var promise = this.fetch(url.href, fetchOptions).then(function (response) {
|
|
178
182
|
responded = true;
|
|
179
183
|
if (!response.ok) {
|
|
180
184
|
var error = new FetchError("Fetch failed");
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ApiResponse } from "../ApiTypes";
|
|
2
|
-
import RequestContext from "../RequestContext";
|
|
3
|
-
import RequestBackend
|
|
1
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
2
|
+
import type RequestContext from "../RequestContext";
|
|
3
|
+
import type RequestBackend from "./RequestBackend";
|
|
4
|
+
import type { RequestBackendErrorInfo, RequestOperation } from "./RequestBackend";
|
|
4
5
|
export default class MockRequestBackend implements RequestBackend<ApiResponse> {
|
|
5
6
|
readonly id = "mock";
|
|
6
7
|
convertResponse<T>(context: RequestContext, response: ApiResponse, error?: boolean): Promise<ApiResponse<T>>;
|
|
@@ -63,13 +63,13 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
63
63
|
});
|
|
64
64
|
};
|
|
65
65
|
MockRequestBackend.prototype.runRequest = function (context) {
|
|
66
|
-
var _a, _b, _c, _d;
|
|
67
66
|
return __awaiter(this, void 0, void 0, function () {
|
|
68
|
-
var mockingFunc, req, res, delay, delayMs, min, max,
|
|
67
|
+
var mockingFunc, req, res, delay, delayMs, min, max, _a, parsedHeaders;
|
|
68
|
+
var _b, _c, _d, _e;
|
|
69
69
|
return __generator(this, function (_f) {
|
|
70
70
|
switch (_f.label) {
|
|
71
71
|
case 0:
|
|
72
|
-
mockingFunc = (
|
|
72
|
+
mockingFunc = (_b = context.mocking) === null || _b === void 0 ? void 0 : _b.handler;
|
|
73
73
|
if (!mockingFunc) {
|
|
74
74
|
throw (0, RequestError_1.convertToRequestError)({
|
|
75
75
|
error: new Error("[api-def] Attempted to run mocked request without mocking function"),
|
|
@@ -79,9 +79,9 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
79
79
|
}
|
|
80
80
|
req = {
|
|
81
81
|
body: context.getParsedBody(),
|
|
82
|
-
params: (
|
|
82
|
+
params: (_c = context.computedConfig.params) !== null && _c !== void 0 ? _c : {},
|
|
83
83
|
query: context.computedConfig.queryObject,
|
|
84
|
-
headers: (
|
|
84
|
+
headers: (_d = context.computedConfig.headers) !== null && _d !== void 0 ? _d : {},
|
|
85
85
|
url: context.requestUrl.toString(),
|
|
86
86
|
};
|
|
87
87
|
res = {
|
|
@@ -100,7 +100,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
100
100
|
return res;
|
|
101
101
|
},
|
|
102
102
|
};
|
|
103
|
-
if (!((
|
|
103
|
+
if (!((_e = context.mocking) === null || _e === void 0 ? void 0 : _e.delay)) return [3 /*break*/, 3];
|
|
104
104
|
delay = context.mocking.delay;
|
|
105
105
|
delayMs = void 0;
|
|
106
106
|
if (typeof delay === "number") {
|
|
@@ -117,9 +117,9 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
117
117
|
}
|
|
118
118
|
delayMs = (0, Utils_1.randInt)(min, max);
|
|
119
119
|
}
|
|
120
|
-
|
|
120
|
+
_a = Utils_1.delayThenReturn;
|
|
121
121
|
return [4 /*yield*/, mockingFunc(req, res)];
|
|
122
|
-
case 1: return [4 /*yield*/,
|
|
122
|
+
case 1: return [4 /*yield*/, _a.apply(void 0, [_f.sent(), delayMs])];
|
|
123
123
|
case 2:
|
|
124
124
|
_f.sent();
|
|
125
125
|
return [3 /*break*/, 5];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ApiResponse } from "../ApiTypes";
|
|
2
|
-
import RequestContext from "../RequestContext";
|
|
1
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
2
|
+
import type RequestContext from "../RequestContext";
|
|
3
3
|
export interface RequestOperation<R> {
|
|
4
4
|
promise: Promise<R>;
|
|
5
5
|
canceler: () => void;
|
package/cjs/cache/Caching.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CacheBackend } from "./CacheBackend";
|
|
1
|
+
import type { CacheBackend } from "./CacheBackend";
|
|
2
2
|
export declare const setCacheBackend: (backend: CacheBackend) => void;
|
|
3
3
|
export declare const DEFAULT_CACHE_EXPIRY: number;
|
|
4
4
|
export declare const clearCache: () => Promise<void>;
|
|
@@ -43,7 +43,13 @@ var log = function (context, type, message, config, objects) {
|
|
|
43
43
|
}
|
|
44
44
|
var color = COLOR_MAP[type];
|
|
45
45
|
var timestamp = formatTime(new Date());
|
|
46
|
-
var args = [
|
|
46
|
+
var args = [
|
|
47
|
+
"%cnetwork %c[".concat(context.api.name, "] ").concat(context.method.toUpperCase(), " ").concat(context.path, " %c").concat(message, " %c@ ").concat(timestamp),
|
|
48
|
+
"color:gray",
|
|
49
|
+
"color:auto",
|
|
50
|
+
"color:".concat(color),
|
|
51
|
+
"color:gray",
|
|
52
|
+
];
|
|
47
53
|
/* eslint-disable-next-line no-console */
|
|
48
54
|
console.groupCollapsed.apply(console, args);
|
|
49
55
|
/* eslint-disable-next-line no-console */
|
|
@@ -16,7 +16,7 @@ var retryOperation_1 = require("./retryOperation");
|
|
|
16
16
|
var operation = function (options) {
|
|
17
17
|
var timeouts = _timeouts(options);
|
|
18
18
|
return new retryOperation_1.default(timeouts, {
|
|
19
|
-
forever: options && (options.forever || options.retries ===
|
|
19
|
+
forever: options && (options.forever || options.retries === Number.POSITIVE_INFINITY),
|
|
20
20
|
unref: options === null || options === void 0 ? void 0 : options.unref,
|
|
21
21
|
maxRetryTime: options === null || options === void 0 ? void 0 : options.maxRetryTime,
|
|
22
22
|
});
|
|
@@ -31,7 +31,7 @@ var _timeouts = function (options) {
|
|
|
31
31
|
return timeout;
|
|
32
32
|
};
|
|
33
33
|
var defaultRetries = 10;
|
|
34
|
-
var opts = __assign({ retries: defaultRetries, factor: 2, minTimeout: 1 * 1000, maxTimeout:
|
|
34
|
+
var opts = __assign({ retries: defaultRetries, factor: 2, minTimeout: 1 * 1000, maxTimeout: Number.POSITIVE_INFINITY, randomize: false }, options);
|
|
35
35
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
36
36
|
if (opts.minTimeout > opts.maxTimeout) {
|
|
37
37
|
throw new Error("minTimeout is greater than maxTimeout");
|
|
@@ -4,7 +4,7 @@ function RetryOperation(timeouts, options) {
|
|
|
4
4
|
this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
|
|
5
5
|
this._timeouts = timeouts;
|
|
6
6
|
this._options = options || {};
|
|
7
|
-
this._maxRetryTime = (options === null || options === void 0 ? void 0 : options.maxRetryTime) ||
|
|
7
|
+
this._maxRetryTime = (options === null || options === void 0 ? void 0 : options.maxRetryTime) || Number.POSITIVE_INFINITY;
|
|
8
8
|
this._fn = null;
|
|
9
9
|
this._errors = [];
|
|
10
10
|
this._attempts = 1;
|
package/esm/Api.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
|
-
import Endpoint from "./Endpoint";
|
|
1
|
+
import type { ApiResponse, BaseRequestConfig, RequestConfig, RequestMiddleware } from "./ApiTypes";
|
|
2
|
+
import type Endpoint from "./Endpoint";
|
|
3
3
|
import EndpointBuilder from "./EndpointBuilder";
|
|
4
|
-
import { ApiMockingConfig } from "./MockingTypes";
|
|
5
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
4
|
+
import type { ApiMockingConfig } from "./MockingTypes";
|
|
5
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
6
6
|
export declare const getRequestBackend: () => RequestBackend | null;
|
|
7
7
|
export declare const isRequestBackendDefault: () => boolean;
|
|
8
8
|
export declare const setRequestBackend: (backend: RequestBackend) => void;
|
package/esm/ApiConstants.d.ts
CHANGED
package/esm/ApiTypes.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Api } from "./Api";
|
|
2
|
-
import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import RequestContext from "./RequestContext";
|
|
4
|
-
import { Validation } from "./Validation";
|
|
5
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
1
|
+
import type { Api } from "./Api";
|
|
2
|
+
import type { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type RequestContext from "./RequestContext";
|
|
4
|
+
import type { Validation } from "./Validation";
|
|
5
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
6
6
|
export type AcceptableStatus = number | [min: number, max: number];
|
|
7
7
|
export type Headers = Record<string, string | number | boolean | null | undefined>;
|
|
8
8
|
export type Params = string;
|
package/esm/ApiUtils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ResponseType } from "./ApiConstants";
|
|
2
|
-
import { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
1
|
+
import type { ResponseType } from "./ApiConstants";
|
|
2
|
+
import type { AcceptableStatus, CancelledRequestError } from "./ApiTypes";
|
|
3
3
|
export declare const isCancelledError: (error: Error) => error is CancelledRequestError;
|
|
4
4
|
export declare const isNetworkError: (error: Error) => boolean;
|
|
5
5
|
export declare const isAcceptableStatus: (status: number, acceptableStatus?: AcceptableStatus[]) => boolean;
|
package/esm/ApiUtils.js
CHANGED
|
@@ -3,7 +3,9 @@ export const isCancelledError = (error) => {
|
|
|
3
3
|
};
|
|
4
4
|
export const isNetworkError = (error) => {
|
|
5
5
|
var _a;
|
|
6
|
-
return error.name === "NetworkError" ||
|
|
6
|
+
return (error.name === "NetworkError" ||
|
|
7
|
+
error.message === "Network Error" ||
|
|
8
|
+
((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
|
|
7
9
|
};
|
|
8
10
|
const DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
|
|
9
11
|
export const isAcceptableStatus = (status, acceptableStatus) => {
|
package/esm/Endpoint.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Api } from "./Api";
|
|
2
|
-
import { RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
4
|
-
import * as Mocking from "./MockingTypes";
|
|
5
|
-
import { Validation } from "./Validation";
|
|
6
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
1
|
+
import type { Api } from "./Api";
|
|
2
|
+
import type { RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
4
|
+
import type * as Mocking from "./MockingTypes";
|
|
5
|
+
import type { Validation } from "./Validation";
|
|
6
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
7
7
|
export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined, Path extends string = string> {
|
|
8
8
|
readonly id: string;
|
|
9
9
|
readonly method: RequestMethod;
|
package/esm/Endpoint.js
CHANGED
|
@@ -23,8 +23,8 @@ export default class Endpoint {
|
|
|
23
23
|
this.validation = info.validation || {};
|
|
24
24
|
}
|
|
25
25
|
submit(config) {
|
|
26
|
-
var _a, _b;
|
|
27
26
|
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
var _a, _b;
|
|
28
28
|
let mock = false;
|
|
29
29
|
const apiMocking = this.api.mocking;
|
|
30
30
|
if (apiMocking) {
|
package/esm/EndpointBuilder.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as zod from "zod";
|
|
2
|
-
import { Api } from "./Api";
|
|
3
|
-
import { Body, Params, Query } from "./ApiTypes";
|
|
4
|
-
import Endpoint, { EndpointConfig } from "./Endpoint";
|
|
1
|
+
import type * as zod from "zod";
|
|
2
|
+
import type { Api } from "./Api";
|
|
3
|
+
import type { Body, Params, Query } from "./ApiTypes";
|
|
4
|
+
import Endpoint, { type EndpointConfig } from "./Endpoint";
|
|
5
5
|
export default class EndpointBuilder<R = any, P extends Params | undefined = undefined, Q extends Query | undefined = undefined, B extends Body | undefined = undefined> {
|
|
6
6
|
private api;
|
|
7
7
|
private readonly validation;
|
package/esm/MockingTypes.d.ts
CHANGED
package/esm/QueryHandling.d.ts
CHANGED
package/esm/RequestConfig.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { BaseRequestConfig, Body, ComputedRequestConfig, Query, RequestConfig } from "./ApiTypes";
|
|
2
|
-
export declare const computeRequestConfig: <P extends string | undefined, Q extends Query, B extends Body | undefined>(configs: (
|
|
1
|
+
import { type BaseRequestConfig, type Body, type ComputedRequestConfig, type Query, type RequestConfig } from "./ApiTypes";
|
|
2
|
+
export declare const computeRequestConfig: <P extends string | undefined, Q extends Query, B extends Body | undefined>(configs: (RequestConfig<P, Q, B> | BaseRequestConfig | undefined)[]) => ComputedRequestConfig<P, Q, B>;
|
package/esm/RequestConfig.js
CHANGED
package/esm/RequestContext.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Api } from "./Api";
|
|
2
|
-
import { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
|
|
4
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
-
import { RequestError } from "./RequestError";
|
|
6
|
-
import { Validation } from "./Validation";
|
|
7
|
-
import RequestBackend from "./backend/RequestBackend";
|
|
1
|
+
import type { Api } from "./Api";
|
|
2
|
+
import type { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
+
import type { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
|
|
4
|
+
import type { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
+
import type { RequestError } from "./RequestError";
|
|
6
|
+
import type { Validation } from "./Validation";
|
|
7
|
+
import type RequestBackend from "./backend/RequestBackend";
|
|
8
8
|
export default class RequestContext<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
9
9
|
readonly id: number;
|
|
10
10
|
readonly key: string;
|
package/esm/RequestError.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ApiResponse } from "./ApiTypes";
|
|
2
|
-
import RequestContext from "./RequestContext";
|
|
3
|
-
import { EnumOf } from "./Utils";
|
|
1
|
+
import type { ApiResponse } from "./ApiTypes";
|
|
2
|
+
import type RequestContext from "./RequestContext";
|
|
3
|
+
import type { EnumOf } from "./Utils";
|
|
4
4
|
export declare const RequestErrorCode: {
|
|
5
5
|
readonly MISC_UNKNOWN_ERROR: "misc/unknown-error";
|
|
6
6
|
readonly REQUEST_NETWORK_ERROR: "request/network-error";
|
package/esm/RequestError.js
CHANGED
|
@@ -30,7 +30,9 @@ export const convertToRequestError = (config) => {
|
|
|
30
30
|
},
|
|
31
31
|
});
|
|
32
32
|
try {
|
|
33
|
-
Object.defineProperty(resultError, "message", {
|
|
33
|
+
Object.defineProperty(resultError, "message", {
|
|
34
|
+
value: `Request failed${(response === null || response === void 0 ? void 0 : response.status) ? ` with status code ${response.status}` : ""} [${code}]: ${resultError.message}`,
|
|
35
|
+
});
|
|
34
36
|
}
|
|
35
37
|
catch (e) {
|
|
36
38
|
// ignore
|
package/esm/Requester.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
1
|
+
import type { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
+
import type { EndpointMockingConfig } from "./MockingTypes";
|
|
3
3
|
export declare const submit: <R, P extends string | undefined, Q extends Query, B extends Body | undefined>(host: RequestHost, config: RequestConfig<P, Q, B>, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined) => Promise<ApiResponse<R>>;
|
package/esm/Requester.js
CHANGED
|
@@ -234,7 +234,9 @@ const parseError = (context, rawError) => __awaiter(void 0, void 0, void 0, func
|
|
|
234
234
|
if (extractedResponse !== undefined) {
|
|
235
235
|
errorResponse = yield parseResponse(context, extractedResponse, true);
|
|
236
236
|
}
|
|
237
|
-
let code = isNetworkError(rawError)
|
|
237
|
+
let code = isNetworkError(rawError)
|
|
238
|
+
? RequestErrorCode.REQUEST_NETWORK_ERROR
|
|
239
|
+
: RequestErrorCode.MISC_UNKNOWN_ERROR;
|
|
238
240
|
if (errorResponse) {
|
|
239
241
|
if (!isAcceptableStatus(errorResponse.status, context.computedConfig.acceptableStatus)) {
|
|
240
242
|
code = RequestErrorCode.REQUEST_INVALID_STATUS;
|
package/esm/TextDecoding.js
CHANGED
|
@@ -19,7 +19,9 @@ export const textDecode = (inputArrayOrBuffer, options) => {
|
|
|
19
19
|
let asObjectString;
|
|
20
20
|
if (!arrayBuffer_isView(inputAs8)) {
|
|
21
21
|
asObjectString = Object_prototype_toString.call(inputAs8);
|
|
22
|
-
if (asObjectString !== arrayBufferString &&
|
|
22
|
+
if (asObjectString !== arrayBufferString &&
|
|
23
|
+
asObjectString !== sharedArrayBufferString &&
|
|
24
|
+
asObjectString !== undefinedObjectString)
|
|
23
25
|
throw TypeError("Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'");
|
|
24
26
|
inputAs8 = NativeUint8Array ? new patchedU8Array(inputAs8) : inputAs8 || [];
|
|
25
27
|
}
|
package/esm/UtilTypes.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Endpoint from "./Endpoint";
|
|
1
|
+
import type Endpoint from "./Endpoint";
|
|
2
2
|
export type ResponseOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<infer R, any, any, any> ? R : never;
|
|
3
3
|
export type ParamsOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, infer P, any, any> ? P : never;
|
|
4
4
|
export type QueryOf<E extends Endpoint<any, any, any, any>> = E extends Endpoint<any, any, infer Q, any> ? Q : never;
|
package/esm/Validation.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import * as zod from "zod";
|
|
2
|
-
import { Body, Params, Query } from "./ApiTypes";
|
|
1
|
+
import type * as zod from "zod";
|
|
2
|
+
import type { Body, Params, Query } from "./ApiTypes";
|
|
3
3
|
export interface Validation<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
4
4
|
query?: zod.Schema<Q>;
|
|
5
5
|
params?: zod.Schema<P>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { AxiosError, AxiosResponse } from "axios";
|
|
2
|
-
import { ApiResponse } from "../ApiTypes";
|
|
3
|
-
import RequestContext from "../RequestContext";
|
|
4
|
-
import RequestBackend
|
|
2
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
3
|
+
import type RequestContext from "../RequestContext";
|
|
4
|
+
import type RequestBackend from "./RequestBackend";
|
|
5
|
+
import type { ConvertedApiResponse, RequestBackendErrorInfo, RequestOperation } from "./RequestBackend";
|
|
5
6
|
export declare const isAxiosError: (error: Error) => error is AxiosError<unknown, any>;
|
|
6
7
|
export default class AxiosRequestBackend implements RequestBackend<AxiosResponse> {
|
|
7
8
|
readonly id = "axios";
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ApiResponse } from "../ApiTypes";
|
|
2
|
-
import RequestContext from "../RequestContext";
|
|
3
|
-
import { Fetch } from "../Utils";
|
|
4
|
-
import RequestBackend
|
|
1
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
2
|
+
import type RequestContext from "../RequestContext";
|
|
3
|
+
import { type Fetch } from "../Utils";
|
|
4
|
+
import type RequestBackend from "./RequestBackend";
|
|
5
|
+
import type { ConvertedApiResponse, RequestBackendErrorInfo, RequestOperation } from "./RequestBackend";
|
|
5
6
|
export default class FetchRequestBackend implements RequestBackend<Response> {
|
|
6
7
|
fetch: (((input: RequestInfo | URL, init?: RequestInit | undefined) => Promise<Response>) & typeof fetch) | undefined;
|
|
7
8
|
readonly id = "fetch";
|
|
@@ -35,8 +35,8 @@ export default class FetchRequestBackend {
|
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
convertResponse(context, response) {
|
|
38
|
-
var _a;
|
|
39
38
|
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
var _a;
|
|
40
40
|
const { status, headers } = response;
|
|
41
41
|
const processedHeaders = {};
|
|
42
42
|
headers.forEach((value, key) => {
|
|
@@ -103,13 +103,17 @@ export default class FetchRequestBackend {
|
|
|
103
103
|
return parsedHeaders;
|
|
104
104
|
}, {});
|
|
105
105
|
const url = context.requestUrl;
|
|
106
|
-
const
|
|
106
|
+
const fetchOptions = {
|
|
107
107
|
method: context.method.toUpperCase(),
|
|
108
108
|
body: bodyJsonify ? JSON.stringify(body) : body,
|
|
109
109
|
headers: parsedHeaders,
|
|
110
|
-
mode: "cors",
|
|
111
110
|
signal: abortSignal,
|
|
112
|
-
}
|
|
111
|
+
};
|
|
112
|
+
// edge doesn't support mode
|
|
113
|
+
if ("mode" in Request.prototype) {
|
|
114
|
+
fetchOptions.mode = "cors";
|
|
115
|
+
}
|
|
116
|
+
const promise = this.fetch(url.href, fetchOptions).then((response) => {
|
|
113
117
|
responded = true;
|
|
114
118
|
if (!response.ok) {
|
|
115
119
|
const error = new FetchError("Fetch failed");
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ApiResponse } from "../ApiTypes";
|
|
2
|
-
import RequestContext from "../RequestContext";
|
|
3
|
-
import RequestBackend
|
|
1
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
2
|
+
import type RequestContext from "../RequestContext";
|
|
3
|
+
import type RequestBackend from "./RequestBackend";
|
|
4
|
+
import type { RequestBackendErrorInfo, RequestOperation } from "./RequestBackend";
|
|
4
5
|
export default class MockRequestBackend implements RequestBackend<ApiResponse> {
|
|
5
6
|
readonly id = "mock";
|
|
6
7
|
convertResponse<T>(context: RequestContext, response: ApiResponse, error?: boolean): Promise<ApiResponse<T>>;
|
|
@@ -29,8 +29,8 @@ export default class MockRequestBackend {
|
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
31
|
runRequest(context) {
|
|
32
|
-
var _a, _b, _c, _d;
|
|
33
32
|
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
var _a, _b, _c, _d;
|
|
34
34
|
const mockingFunc = (_a = context.mocking) === null || _a === void 0 ? void 0 : _a.handler;
|
|
35
35
|
if (!mockingFunc) {
|
|
36
36
|
throw convertToRequestError({
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ApiResponse } from "../ApiTypes";
|
|
2
|
-
import RequestContext from "../RequestContext";
|
|
1
|
+
import type { ApiResponse } from "../ApiTypes";
|
|
2
|
+
import type RequestContext from "../RequestContext";
|
|
3
3
|
export interface RequestOperation<R> {
|
|
4
4
|
promise: Promise<R>;
|
|
5
5
|
canceler: () => void;
|
package/esm/cache/Caching.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CacheBackend } from "./CacheBackend";
|
|
1
|
+
import type { CacheBackend } from "./CacheBackend";
|
|
2
2
|
export declare const setCacheBackend: (backend: CacheBackend) => void;
|
|
3
3
|
export declare const DEFAULT_CACHE_EXPIRY: number;
|
|
4
4
|
export declare const clearCache: () => Promise<void>;
|
|
@@ -40,7 +40,13 @@ const log = (context, type, message, config, objects) => {
|
|
|
40
40
|
}
|
|
41
41
|
const color = COLOR_MAP[type];
|
|
42
42
|
const timestamp = formatTime(new Date());
|
|
43
|
-
const args = [
|
|
43
|
+
const args = [
|
|
44
|
+
`%cnetwork %c[${context.api.name}] ${context.method.toUpperCase()} ${context.path} %c${message} %c@ ${timestamp}`,
|
|
45
|
+
"color:gray",
|
|
46
|
+
"color:auto",
|
|
47
|
+
`color:${color}`,
|
|
48
|
+
"color:gray",
|
|
49
|
+
];
|
|
44
50
|
/* eslint-disable-next-line no-console */
|
|
45
51
|
console.groupCollapsed(...args);
|
|
46
52
|
/* eslint-disable-next-line no-console */
|
|
@@ -2,7 +2,7 @@ import RetryOperation from "./retryOperation";
|
|
|
2
2
|
export const operation = (options) => {
|
|
3
3
|
const timeouts = _timeouts(options);
|
|
4
4
|
return new RetryOperation(timeouts, {
|
|
5
|
-
forever: options && (options.forever || options.retries ===
|
|
5
|
+
forever: options && (options.forever || options.retries === Number.POSITIVE_INFINITY),
|
|
6
6
|
unref: options === null || options === void 0 ? void 0 : options.unref,
|
|
7
7
|
maxRetryTime: options === null || options === void 0 ? void 0 : options.maxRetryTime,
|
|
8
8
|
});
|
|
@@ -16,7 +16,7 @@ const _timeouts = (options) => {
|
|
|
16
16
|
return timeout;
|
|
17
17
|
};
|
|
18
18
|
const defaultRetries = 10;
|
|
19
|
-
const opts = Object.assign({ retries: defaultRetries, factor: 2, minTimeout: 1 * 1000, maxTimeout:
|
|
19
|
+
const opts = Object.assign({ retries: defaultRetries, factor: 2, minTimeout: 1 * 1000, maxTimeout: Number.POSITIVE_INFINITY, randomize: false }, options);
|
|
20
20
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
21
21
|
if (opts.minTimeout > opts.maxTimeout) {
|
|
22
22
|
throw new Error("minTimeout is greater than maxTimeout");
|
|
@@ -2,7 +2,7 @@ function RetryOperation(timeouts, options) {
|
|
|
2
2
|
this._originalTimeouts = JSON.parse(JSON.stringify(timeouts));
|
|
3
3
|
this._timeouts = timeouts;
|
|
4
4
|
this._options = options || {};
|
|
5
|
-
this._maxRetryTime = (options === null || options === void 0 ? void 0 : options.maxRetryTime) ||
|
|
5
|
+
this._maxRetryTime = (options === null || options === void 0 ? void 0 : options.maxRetryTime) || Number.POSITIVE_INFINITY;
|
|
6
6
|
this._fn = null;
|
|
7
7
|
this._errors = [];
|
|
8
8
|
this._attempts = 1;
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-def",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4-alpha.1",
|
|
4
4
|
"description": "Typed API definitions with middleware support",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"types": "esm/index.d.ts",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"scripts": {
|
|
10
|
-
"prepublishOnly": "npm run check && npm run test && npm run build",
|
|
10
|
+
"prepublishOnly": "npm run check:fix && npm run test && npm run build",
|
|
11
11
|
"test": "jest",
|
|
12
12
|
"example:start": "cd example && npm run start",
|
|
13
|
-
"check:fix": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check --apply
|
|
13
|
+
"check:fix": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check --apply .",
|
|
14
14
|
"check": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check .",
|
|
15
15
|
"cleanup": "rimraf esm && rimraf cjs",
|
|
16
16
|
"build": "npm run cleanup && npm run build:esm && npm run build:cjs",
|
|
@@ -19,26 +19,10 @@
|
|
|
19
19
|
"website:dev": "cd website && npm run start",
|
|
20
20
|
"website:deploy": "cd website && npm run deploy"
|
|
21
21
|
},
|
|
22
|
-
"keywords": [
|
|
23
|
-
"typescript",
|
|
24
|
-
"javascript",
|
|
25
|
-
"node",
|
|
26
|
-
"web",
|
|
27
|
-
"api",
|
|
28
|
-
"typed",
|
|
29
|
-
"cache",
|
|
30
|
-
"fetch",
|
|
31
|
-
"retry",
|
|
32
|
-
"middleware"
|
|
33
|
-
],
|
|
22
|
+
"keywords": ["typescript", "javascript", "node", "web", "api", "typed", "cache", "fetch", "retry", "middleware"],
|
|
34
23
|
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
35
24
|
"license": "MIT",
|
|
36
|
-
"files": [
|
|
37
|
-
"LICENSE",
|
|
38
|
-
"README.md",
|
|
39
|
-
"esm/",
|
|
40
|
-
"cjs/"
|
|
41
|
-
],
|
|
25
|
+
"files": ["LICENSE", "README.md", "esm/", "cjs/"],
|
|
42
26
|
"jest": {
|
|
43
27
|
"transform": {
|
|
44
28
|
"^.+\\.(t|j)sx?$": "@swc/jest"
|
|
@@ -46,19 +30,20 @@
|
|
|
46
30
|
},
|
|
47
31
|
"repository": "https://github.com/Censkh/api-def",
|
|
48
32
|
"devDependencies": {
|
|
49
|
-
"@biomejs/biome": "
|
|
50
|
-
"@swc/
|
|
33
|
+
"@biomejs/biome": "latest",
|
|
34
|
+
"@swc/core": "latest",
|
|
35
|
+
"@swc/jest": "latest",
|
|
51
36
|
"@types/axios": "0.14.0",
|
|
52
|
-
"@types/jest": "^29.5.
|
|
53
|
-
"@types/node": "20.
|
|
54
|
-
"@types/qs": "6.9.
|
|
55
|
-
"axios": "1.6.
|
|
37
|
+
"@types/jest": "^29.5.12",
|
|
38
|
+
"@types/node": "20.12.7",
|
|
39
|
+
"@types/qs": "6.9.15",
|
|
40
|
+
"axios": "1.6.8",
|
|
56
41
|
"cross-env": "7.0.3",
|
|
57
|
-
"jest": "
|
|
42
|
+
"jest": "latest",
|
|
58
43
|
"npm-run-all": "4.1.5",
|
|
59
|
-
"qs": "6.
|
|
44
|
+
"qs": "6.12.1",
|
|
60
45
|
"rimraf": "5.0.5",
|
|
61
|
-
"typescript": "5.
|
|
62
|
-
"zod": "3.22.
|
|
46
|
+
"typescript": "5.4.5",
|
|
47
|
+
"zod": "3.22.5"
|
|
63
48
|
}
|
|
64
49
|
}
|