api-def 0.9.2 → 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/README.md +1 -1
- 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 +18 -13
- 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 +9 -5
- 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 +22 -32
package/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# [api-def](https://github.com/Censkh/api-def/) · [](https://github.com/Censkh/api-def/blob/master/LICENSE) [](https://www.npmjs.com/package/api-def) [ · [](https://github.com/Censkh/api-def/blob/master/LICENSE) [](https://www.npmjs.com/package/api-def) [](https://github.com/Censkh/api-def/actions)
|
|
2
2
|
|
|
3
3
|
Typed APIs with middleware support
|
|
4
4
|
|
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:
|
|
@@ -109,24 +109,25 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
109
109
|
responseType = (_a = context.responseType) !== null && _a !== void 0 ? _a : (0, ApiUtils_1.inferResponseType)(response.headers.get("Content-Type"));
|
|
110
110
|
_b.label = 1;
|
|
111
111
|
case 1:
|
|
112
|
-
_b.trys.push([1,
|
|
112
|
+
_b.trys.push([1, 8, , 9]);
|
|
113
113
|
if (!(responseType === "arraybuffer")) return [3 /*break*/, 3];
|
|
114
114
|
return [4 /*yield*/, response.arrayBuffer()];
|
|
115
115
|
case 2:
|
|
116
116
|
data = _b.sent();
|
|
117
|
-
return [3 /*break*/,
|
|
117
|
+
return [3 /*break*/, 7];
|
|
118
118
|
case 3:
|
|
119
119
|
if (!(responseType === "json")) return [3 /*break*/, 5];
|
|
120
120
|
return [4 /*yield*/, response.text()];
|
|
121
121
|
case 4:
|
|
122
122
|
text = _b.sent();
|
|
123
123
|
data = JSON.parse(text);
|
|
124
|
-
return [3 /*break*/,
|
|
125
|
-
case 5:
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
case 7:
|
|
124
|
+
return [3 /*break*/, 7];
|
|
125
|
+
case 5: return [4 /*yield*/, response.text()];
|
|
126
|
+
case 6:
|
|
127
|
+
data = _b.sent();
|
|
128
|
+
_b.label = 7;
|
|
129
|
+
case 7: return [3 /*break*/, 9];
|
|
130
|
+
case 8:
|
|
130
131
|
error_1 = _b.sent();
|
|
131
132
|
throw (0, RequestError_1.convertToRequestError)({
|
|
132
133
|
error: Object.assign(new Error("[api-def] Failed to parse response as '".concat(responseType, "'").concat(text ? ", got: ".concat(text) : "")), {
|
|
@@ -136,7 +137,7 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
136
137
|
context: context,
|
|
137
138
|
response: convertedResponse,
|
|
138
139
|
});
|
|
139
|
-
case
|
|
140
|
+
case 9:
|
|
140
141
|
convertedResponse.data = data;
|
|
141
142
|
return [2 /*return*/, convertedResponse];
|
|
142
143
|
}
|
|
@@ -167,13 +168,17 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
167
168
|
return parsedHeaders;
|
|
168
169
|
}, {});
|
|
169
170
|
var url = context.requestUrl;
|
|
170
|
-
var
|
|
171
|
+
var fetchOptions = {
|
|
171
172
|
method: context.method.toUpperCase(),
|
|
172
173
|
body: bodyJsonify ? JSON.stringify(body) : body,
|
|
173
174
|
headers: parsedHeaders,
|
|
174
|
-
mode: "cors",
|
|
175
175
|
signal: abortSignal,
|
|
176
|
-
}
|
|
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) {
|
|
177
182
|
responded = true;
|
|
178
183
|
if (!response.ok) {
|
|
179
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) => {
|
|
@@ -62,7 +62,7 @@ export default class FetchRequestBackend {
|
|
|
62
62
|
data = JSON.parse(text);
|
|
63
63
|
}
|
|
64
64
|
else {
|
|
65
|
-
data = response.text();
|
|
65
|
+
data = yield response.text();
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
catch (error) {
|
|
@@ -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,18 +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 test && npm run build",
|
|
11
|
-
"test": "
|
|
12
|
-
"test:unit": "bun test --timeout 10000",
|
|
13
|
-
"test:types": "tsc --noEmit -p .",
|
|
10
|
+
"prepublishOnly": "npm run check:fix && npm run test && npm run build",
|
|
11
|
+
"test": "jest",
|
|
14
12
|
"example:start": "cd example && npm run start",
|
|
15
|
-
"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 .",
|
|
16
14
|
"check": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check .",
|
|
17
15
|
"cleanup": "rimraf esm && rimraf cjs",
|
|
18
16
|
"build": "npm run cleanup && npm run build:esm && npm run build:cjs",
|
|
@@ -21,39 +19,31 @@
|
|
|
21
19
|
"website:dev": "cd website && npm run start",
|
|
22
20
|
"website:deploy": "cd website && npm run deploy"
|
|
23
21
|
},
|
|
24
|
-
"keywords": [
|
|
25
|
-
"typescript",
|
|
26
|
-
"javascript",
|
|
27
|
-
"node",
|
|
28
|
-
"web",
|
|
29
|
-
"api",
|
|
30
|
-
"typed",
|
|
31
|
-
"cache",
|
|
32
|
-
"fetch",
|
|
33
|
-
"retry",
|
|
34
|
-
"middleware"
|
|
35
|
-
],
|
|
22
|
+
"keywords": ["typescript", "javascript", "node", "web", "api", "typed", "cache", "fetch", "retry", "middleware"],
|
|
36
23
|
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
37
24
|
"license": "MIT",
|
|
38
|
-
"files": [
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
25
|
+
"files": ["LICENSE", "README.md", "esm/", "cjs/"],
|
|
26
|
+
"jest": {
|
|
27
|
+
"transform": {
|
|
28
|
+
"^.+\\.(t|j)sx?$": "@swc/jest"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
44
31
|
"repository": "https://github.com/Censkh/api-def",
|
|
45
32
|
"devDependencies": {
|
|
46
|
-
"@biomejs/biome": "
|
|
33
|
+
"@biomejs/biome": "latest",
|
|
34
|
+
"@swc/core": "latest",
|
|
35
|
+
"@swc/jest": "latest",
|
|
47
36
|
"@types/axios": "0.14.0",
|
|
48
|
-
"@types/jest": "^29.5.
|
|
49
|
-
"@types/node": "20.
|
|
50
|
-
"@types/qs": "6.9.
|
|
51
|
-
"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",
|
|
52
41
|
"cross-env": "7.0.3",
|
|
42
|
+
"jest": "latest",
|
|
53
43
|
"npm-run-all": "4.1.5",
|
|
54
|
-
"qs": "6.
|
|
44
|
+
"qs": "6.12.1",
|
|
55
45
|
"rimraf": "5.0.5",
|
|
56
|
-
"typescript": "5.
|
|
57
|
-
"zod": "3.22.
|
|
46
|
+
"typescript": "5.4.5",
|
|
47
|
+
"zod": "3.22.5"
|
|
58
48
|
}
|
|
59
49
|
}
|