api-def 0.7.3 → 0.8.0
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.js +6 -2
- package/cjs/ApiTypes.d.ts +30 -10
- package/cjs/ApiTypes.js +2 -0
- package/cjs/ApiUtils.js +1 -1
- package/cjs/Endpoint.d.ts +3 -3
- package/cjs/Endpoint.js +13 -8
- package/cjs/QueryHandling.d.ts +3 -0
- package/cjs/QueryHandling.js +24 -0
- package/cjs/RequestConfig.d.ts +2 -0
- package/cjs/RequestConfig.js +74 -0
- package/cjs/RequestContext.d.ts +4 -4
- package/cjs/RequestContext.js +5 -24
- package/cjs/RequestError.js +1 -1
- package/cjs/Requester.d.ts +1 -1
- package/cjs/Requester.js +2 -2
- package/cjs/backend/AxiosRequestBackend.js +4 -4
- package/cjs/backend/FetchRequestBackend.js +4 -3
- package/cjs/backend/MockRequestBackend.js +3 -1
- package/cjs/index.d.ts +1 -0
- package/cjs/index.js +3 -1
- package/esm/Api.js +34 -70
- package/esm/ApiConstants.js +5 -5
- package/esm/ApiTypes.d.ts +30 -10
- package/esm/ApiTypes.js +1 -1
- package/esm/ApiUtils.js +12 -13
- package/esm/Endpoint.d.ts +3 -3
- package/esm/Endpoint.js +42 -74
- package/esm/EndpointBuilder.js +14 -16
- package/esm/QueryHandling.d.ts +3 -0
- package/esm/QueryHandling.js +19 -0
- package/esm/RequestConfig.d.ts +2 -0
- package/esm/RequestConfig.js +56 -0
- package/esm/RequestContext.d.ts +4 -4
- package/esm/RequestContext.js +71 -148
- package/esm/RequestError.js +7 -7
- package/esm/Requester.d.ts +1 -1
- package/esm/Requester.js +182 -266
- package/esm/TextDecoding.js +20 -20
- package/esm/Utils.js +15 -15
- package/esm/backend/AxiosRequestBackend.js +34 -67
- package/esm/backend/FetchRequestBackend.js +66 -134
- package/esm/backend/MockRequestBackend.js +92 -136
- package/esm/cache/Caching.js +24 -66
- package/esm/cache/LocalForageCacheBackend.js +11 -13
- package/esm/cache/LocalStorageCacheBackend.js +19 -62
- package/esm/index.d.ts +1 -0
- package/esm/index.js +1 -0
- package/esm/middleware/CacheMiddleware.js +44 -91
- package/esm/middleware/LoggingMiddleware.js +36 -39
- package/esm/util/retry/index.js +8 -8
- package/esm/util/retry/lib/retry.js +11 -22
- package/package.json +4 -2
package/esm/Api.js
CHANGED
|
@@ -7,87 +7,53 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (_) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
10
|
import * as Requester from "./Requester";
|
|
38
11
|
import EndpointBuilder from "./EndpointBuilder";
|
|
39
12
|
import * as Utils from "./Utils";
|
|
40
13
|
import FetchRequestBackend from "./backend/FetchRequestBackend";
|
|
41
|
-
import { RequestMethod,
|
|
14
|
+
import { RequestMethod, } from "./ApiConstants";
|
|
15
|
+
import { computeRequestConfig } from "./RequestConfig";
|
|
42
16
|
// use fetch as default if it is present
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
export
|
|
17
|
+
let requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend() : null;
|
|
18
|
+
let requestBackendIsDefault = true;
|
|
19
|
+
export const getRequestBackend = () => {
|
|
46
20
|
return requestBackend;
|
|
47
21
|
};
|
|
48
|
-
export
|
|
22
|
+
export const isRequestBackendDefault = () => {
|
|
49
23
|
return requestBackendIsDefault;
|
|
50
24
|
};
|
|
51
|
-
export
|
|
25
|
+
export const setRequestBackend = (backend) => {
|
|
52
26
|
requestBackendIsDefault = false;
|
|
53
27
|
requestBackend = backend;
|
|
54
28
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.responseType =
|
|
29
|
+
class HotRequestHost {
|
|
30
|
+
constructor(api, path, method) {
|
|
31
|
+
this.responseType = undefined;
|
|
58
32
|
this.api = api;
|
|
59
33
|
this.method = method;
|
|
60
34
|
this.path = path;
|
|
61
35
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
var Api = /** @class */ (function () {
|
|
79
|
-
function Api(info) {
|
|
80
|
-
var _this = this;
|
|
36
|
+
get baseUrl() {
|
|
37
|
+
return this.api.baseUrl;
|
|
38
|
+
}
|
|
39
|
+
computeConfig(config) {
|
|
40
|
+
const apiDefaults = this.api.getConfig();
|
|
41
|
+
return computeRequestConfig([
|
|
42
|
+
apiDefaults,
|
|
43
|
+
config,
|
|
44
|
+
]);
|
|
45
|
+
}
|
|
46
|
+
computePath(path, config) {
|
|
47
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export class Api {
|
|
51
|
+
constructor(info) {
|
|
81
52
|
var _a;
|
|
82
53
|
this.endpoints = {};
|
|
83
|
-
this.hotRequest =
|
|
84
|
-
return
|
|
85
|
-
|
|
86
|
-
case 0: return [4 /*yield*/, Requester.submit(new HotRequestHost(this, path, requestMethod), config, null)];
|
|
87
|
-
case 1: return [2 /*return*/, (_a.sent())];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}); }; };
|
|
54
|
+
this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () {
|
|
55
|
+
return (yield Requester.submit(new HotRequestHost(this, path, requestMethod), config, null));
|
|
56
|
+
});
|
|
91
57
|
this.get = this.hotRequest(RequestMethod.GET);
|
|
92
58
|
this.post = this.hotRequest(RequestMethod.POST);
|
|
93
59
|
this.put = this.hotRequest(RequestMethod.PUT);
|
|
@@ -100,13 +66,11 @@ var Api = /** @class */ (function () {
|
|
|
100
66
|
this.config = info.config;
|
|
101
67
|
this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
|
|
102
68
|
}
|
|
103
|
-
|
|
69
|
+
endpoint() {
|
|
104
70
|
return new EndpointBuilder(this);
|
|
105
|
-
}
|
|
106
|
-
|
|
71
|
+
}
|
|
72
|
+
getConfig() {
|
|
107
73
|
return ((typeof this.config === "function" ? this.config() : this.config) ||
|
|
108
74
|
{});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
}());
|
|
112
|
-
export { Api };
|
|
75
|
+
}
|
|
76
|
+
}
|
package/esm/ApiConstants.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const RequestMethod = {
|
|
2
2
|
/** @deprecated use 'POST' */
|
|
3
3
|
Post: "post",
|
|
4
4
|
/** @deprecated use 'GET' */
|
|
@@ -9,7 +9,7 @@ export var RequestMethod = {
|
|
|
9
9
|
DELETE: "delete",
|
|
10
10
|
PATCH: "patch",
|
|
11
11
|
};
|
|
12
|
-
export
|
|
12
|
+
export const RequestEvent = {
|
|
13
13
|
/** @deprecated use 'BEFORE_SEND' */
|
|
14
14
|
BeforeSend: "beforeSend",
|
|
15
15
|
/** @deprecated use 'SUCCESS' */
|
|
@@ -23,7 +23,7 @@ export var RequestEvent = {
|
|
|
23
23
|
ERROR: "error",
|
|
24
24
|
UNRECOVERABLE_ERROR: "unrecoverableError",
|
|
25
25
|
};
|
|
26
|
-
export
|
|
26
|
+
export const EventResultType = {
|
|
27
27
|
/** @deprecated use 'RESPOND' */
|
|
28
28
|
Respond: "respond",
|
|
29
29
|
/** @deprecated use 'RETRY' */
|
|
@@ -31,7 +31,7 @@ export var EventResultType = {
|
|
|
31
31
|
RESPOND: "respond",
|
|
32
32
|
RETRY: "retry",
|
|
33
33
|
};
|
|
34
|
-
export
|
|
34
|
+
export const CacheSource = {
|
|
35
35
|
/** @deprecated use 'API' */
|
|
36
36
|
Api: "api",
|
|
37
37
|
/** @deprecated use 'LOCAL' */
|
|
@@ -39,7 +39,7 @@ export var CacheSource = {
|
|
|
39
39
|
API: "api",
|
|
40
40
|
LOCAL: "local",
|
|
41
41
|
};
|
|
42
|
-
export
|
|
42
|
+
export const ResponseType = {
|
|
43
43
|
/** @deprecated use 'JSON' */
|
|
44
44
|
Json: "json",
|
|
45
45
|
/** @deprecated use 'TEXT' */
|
package/esm/ApiTypes.d.ts
CHANGED
|
@@ -4,20 +4,33 @@ import { CacheSource, EventResultType, RequestEvent, RequestMethod, ResponseType
|
|
|
4
4
|
export declare type AcceptableStatus = number | [min: number, max: number];
|
|
5
5
|
export declare type Headers = Record<string, string | number | boolean | null | undefined>;
|
|
6
6
|
export declare type Params = string;
|
|
7
|
-
export declare type Query = Record<string, any>;
|
|
7
|
+
export declare type Query = string | undefined | Record<string, any>;
|
|
8
8
|
export declare type Body = string | number | Record<string, any>;
|
|
9
9
|
export interface ApiResponse<T = any> {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
readonly method: RequestMethod;
|
|
11
|
+
readonly url: string;
|
|
12
|
+
readonly status: number;
|
|
13
|
+
readonly data: T;
|
|
14
|
+
readonly headers: Record<string, string>;
|
|
15
|
+
}
|
|
16
|
+
export declare type RequestLock = string | false;
|
|
17
|
+
export declare type QueryStringify = (query: any) => string;
|
|
18
|
+
export declare type QueryParse = (query: string) => any;
|
|
19
|
+
interface QueryHandling {
|
|
20
|
+
parse: QueryParse;
|
|
21
|
+
stringify: QueryStringify;
|
|
13
22
|
}
|
|
14
23
|
export interface BaseRequestConfig {
|
|
15
24
|
cache?: number | boolean;
|
|
16
|
-
lock?:
|
|
25
|
+
lock?: RequestLock;
|
|
17
26
|
retry?: number | false;
|
|
18
27
|
headers?: Readonly<Headers>;
|
|
19
28
|
acceptableStatus?: AcceptableStatus[];
|
|
20
|
-
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated use `queryHandling.stringify` instead
|
|
31
|
+
**/
|
|
32
|
+
queryParser?: QueryStringify;
|
|
33
|
+
queryHandling?: Partial<QueryHandling>;
|
|
21
34
|
}
|
|
22
35
|
export declare type RequestConfig<P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = (P extends undefined ? {
|
|
23
36
|
params?: never;
|
|
@@ -32,13 +45,20 @@ export declare type RequestConfig<P extends Params | undefined = Params | undefi
|
|
|
32
45
|
} : {
|
|
33
46
|
body: B;
|
|
34
47
|
}) & BaseRequestConfig;
|
|
48
|
+
export declare const COMPUTED_CONFIG_SYMBOL: unique symbol;
|
|
49
|
+
export declare type ComputedRequestConfig<P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = Omit<RequestConfig<P, Q, B>, "queryParser" | "query" | "queryHandling"> & {
|
|
50
|
+
[COMPUTED_CONFIG_SYMBOL]: true;
|
|
51
|
+
queryObject: Record<string, any> | undefined;
|
|
52
|
+
queryString: string | undefined;
|
|
53
|
+
queryHandling: QueryHandling;
|
|
54
|
+
};
|
|
35
55
|
interface BaseEventResult<T extends EventResultType> {
|
|
36
56
|
type: T;
|
|
37
57
|
}
|
|
38
|
-
export declare type ResponseEventResult<R> = BaseEventResult<
|
|
58
|
+
export declare type ResponseEventResult<R> = BaseEventResult<"respond"> & {
|
|
39
59
|
response: ApiResponse<R>;
|
|
40
60
|
};
|
|
41
|
-
export declare type RetryEventResult<R> = BaseEventResult<
|
|
61
|
+
export declare type RetryEventResult<R> = BaseEventResult<"retry">;
|
|
42
62
|
export declare type EventResult<R> = ResponseEventResult<R> | RetryEventResult<R>;
|
|
43
63
|
export declare type RequestEventHandler<R> = (context: RequestContext<R>) => EventResult<R> | void | Promise<EventResult<R> | void>;
|
|
44
64
|
export declare type RequestEventHandlers<R> = {
|
|
@@ -59,8 +79,8 @@ export interface RequestHost {
|
|
|
59
79
|
readonly api: Api;
|
|
60
80
|
readonly baseUrl: string;
|
|
61
81
|
readonly path: string;
|
|
62
|
-
readonly responseType: ResponseType;
|
|
63
|
-
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>):
|
|
82
|
+
readonly responseType: ResponseType | undefined;
|
|
83
|
+
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
|
|
64
84
|
computePath(path: string, config: RequestConfig): string;
|
|
65
85
|
}
|
|
66
86
|
export interface CancelledRequestError extends Error {
|
package/esm/ApiTypes.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const COMPUTED_CONFIG_SYMBOL = Symbol("computed");
|
package/esm/ApiUtils.js
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const isCancelledError = (error) => {
|
|
2
2
|
return "isCancelledRequest" in error;
|
|
3
3
|
};
|
|
4
|
-
export
|
|
4
|
+
export const isNetworkError = (error) => {
|
|
5
5
|
var _a;
|
|
6
6
|
return (error.name === "NetworkError" ||
|
|
7
7
|
error.message === "Network Error" ||
|
|
8
8
|
((_a = error.constructor) === null || _a === void 0 ? void 0 : _a.name) === "NetworkError");
|
|
9
9
|
};
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
for (
|
|
14
|
-
var cmpStatus = acceptable_1[_i];
|
|
10
|
+
const DEFAULT_ACCEPTABLE_STATUS = [[200, 299], 304];
|
|
11
|
+
export const isAcceptableStatus = (status, acceptableStatus) => {
|
|
12
|
+
const acceptable = acceptableStatus !== null && acceptableStatus !== void 0 ? acceptableStatus : DEFAULT_ACCEPTABLE_STATUS;
|
|
13
|
+
for (const cmpStatus of acceptable) {
|
|
15
14
|
if (Array.isArray(cmpStatus)) {
|
|
16
|
-
|
|
15
|
+
const [min, max] = cmpStatus;
|
|
17
16
|
if (status >= min && status <= max) {
|
|
18
17
|
return (true);
|
|
19
18
|
}
|
|
@@ -26,11 +25,11 @@ export var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
26
25
|
}
|
|
27
26
|
return (false);
|
|
28
27
|
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
export
|
|
33
|
-
|
|
28
|
+
const TEXT_CONTENT_TYPES = ["text/plain", "text/html", "text/xml", "application/xml"];
|
|
29
|
+
const JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
30
|
+
const ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
|
|
31
|
+
export const inferResponseType = (contentType) => {
|
|
32
|
+
const contentTypePart = contentType === null || contentType === void 0 ? void 0 : contentType.split(";")[0].trim();
|
|
34
33
|
if (contentTypePart) {
|
|
35
34
|
if (TEXT_CONTENT_TYPES.includes(contentTypePart)) {
|
|
36
35
|
return "text";
|
package/esm/Endpoint.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Api } from "./Api";
|
|
2
|
-
import { ApiResponse, BaseRequestConfig, Body, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
|
+
import { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
3
3
|
import * as Mocking from "./MockingTypes";
|
|
4
4
|
import { RequestMethod, ResponseType } from "./ApiConstants";
|
|
5
5
|
export interface EndpointConfig<R, P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined> {
|
|
@@ -37,11 +37,11 @@ export default class Endpoint<R = any, P extends Params | undefined = Params | u
|
|
|
37
37
|
readonly description?: string;
|
|
38
38
|
readonly path: string;
|
|
39
39
|
readonly config?: BaseRequestConfig;
|
|
40
|
-
readonly responseType: ResponseType;
|
|
40
|
+
readonly responseType: ResponseType | undefined;
|
|
41
41
|
readonly mocking?: Mocking.EndpointMockingConfig<R, P, Q, B>;
|
|
42
42
|
constructor(api: Api, info: EndpointConfig<R, P, Q, B>);
|
|
43
43
|
submit(config: RequestConfig<P, Q, B>): Promise<ApiResponse<R>>;
|
|
44
44
|
computePath(path: string, request: RequestConfig): string;
|
|
45
45
|
get baseUrl(): string;
|
|
46
|
-
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>):
|
|
46
|
+
computeConfig<P extends Params | undefined, Q extends Query | undefined, B extends Body | undefined>(config: RequestConfig<P, Q, B>): ComputedRequestConfig<P, Q, B>;
|
|
47
47
|
}
|
package/esm/Endpoint.js
CHANGED
|
@@ -7,38 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
11
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
12
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
13
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
14
|
-
function step(op) {
|
|
15
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
16
|
-
while (_) try {
|
|
17
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
18
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
19
|
-
switch (op[0]) {
|
|
20
|
-
case 0: case 1: t = op; break;
|
|
21
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
22
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
23
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
24
|
-
default:
|
|
25
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
26
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
27
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
28
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
29
|
-
if (t[2]) _.ops.pop();
|
|
30
|
-
_.trys.pop(); continue;
|
|
31
|
-
}
|
|
32
|
-
op = body.call(thisArg, _);
|
|
33
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
34
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
10
|
import * as Requester from "./Requester";
|
|
11
|
+
import { COMPUTED_CONFIG_SYMBOL, } from "./ApiTypes";
|
|
38
12
|
import * as Utils from "./Utils";
|
|
39
|
-
import {
|
|
40
|
-
|
|
41
|
-
|
|
13
|
+
import { computeRequestConfig } from "./RequestConfig";
|
|
14
|
+
export default class Endpoint {
|
|
15
|
+
constructor(api, info) {
|
|
42
16
|
this.api = api;
|
|
43
17
|
this.id = info.id;
|
|
44
18
|
this.method = info.method;
|
|
@@ -46,60 +20,54 @@ var Endpoint = /** @class */ (function () {
|
|
|
46
20
|
this.description = info.description;
|
|
47
21
|
this.path = info.path;
|
|
48
22
|
this.config = info.config;
|
|
49
|
-
this.responseType = info.responseType
|
|
23
|
+
this.responseType = info.responseType;
|
|
50
24
|
this.mocking = info.mocking;
|
|
51
25
|
}
|
|
52
|
-
|
|
26
|
+
submit(config) {
|
|
53
27
|
var _a, _b;
|
|
54
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!((_b = this.mocking) === null || _b === void 0 ? void 0 : _b.handler)) {
|
|
63
|
-
throw new Error("[api-def] Endpoint for '".concat(this.path, "' has no mocking"));
|
|
64
|
-
}
|
|
65
|
-
mock = true;
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
let mock = false;
|
|
30
|
+
const apiMocking = this.api.mocking;
|
|
31
|
+
if (apiMocking) {
|
|
32
|
+
const mockingEnabled = (_a = (typeof apiMocking.enabled === "function" ? apiMocking.enabled() : apiMocking.enabled)) !== null && _a !== void 0 ? _a : false;
|
|
33
|
+
if (mockingEnabled) {
|
|
34
|
+
if (!((_b = this.mocking) === null || _b === void 0 ? void 0 : _b.handler)) {
|
|
35
|
+
throw new Error(`[api-def] Endpoint for '${this.path}' has no mocking`);
|
|
66
36
|
}
|
|
37
|
+
mock = true;
|
|
67
38
|
}
|
|
68
|
-
|
|
69
|
-
|
|
39
|
+
}
|
|
40
|
+
return Requester.submit(this, config, mock ? this.mocking : null);
|
|
70
41
|
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
42
|
+
}
|
|
43
|
+
computePath(path, request) {
|
|
44
|
+
let computedPath = path.startsWith("/") ? path : `/${path}`;
|
|
74
45
|
if (request.params) {
|
|
75
|
-
|
|
76
|
-
for (
|
|
77
|
-
|
|
78
|
-
computedPath = computedPath.replace(
|
|
46
|
+
const keys = Object.keys(request.params);
|
|
47
|
+
for (let i = 0; i < keys.length; i++) {
|
|
48
|
+
const argName = keys[i];
|
|
49
|
+
computedPath = computedPath.replace(`:${argName}`, request.params[argName]);
|
|
79
50
|
}
|
|
80
51
|
}
|
|
81
52
|
if (computedPath.includes(":")) {
|
|
82
|
-
throw new Error(
|
|
53
|
+
throw new Error(`[api-def] Not all path params have been resolved: '${computedPath}'`);
|
|
83
54
|
}
|
|
84
55
|
return computedPath;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}
|
|
56
|
+
}
|
|
57
|
+
get baseUrl() {
|
|
58
|
+
return this.api.baseUrl;
|
|
59
|
+
}
|
|
60
|
+
computeConfig(config) {
|
|
61
|
+
const apiDefaults = this.api.getConfig();
|
|
62
|
+
const computedConfig = Utils.assign({
|
|
63
|
+
[COMPUTED_CONFIG_SYMBOL]: true,
|
|
64
|
+
}, apiDefaults, this.config, config);
|
|
65
|
+
return computeRequestConfig([
|
|
66
|
+
apiDefaults,
|
|
67
|
+
this.config,
|
|
68
|
+
config,
|
|
69
|
+
]);
|
|
70
|
+
delete computedConfig.queryParser;
|
|
101
71
|
return computedConfig;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
}());
|
|
105
|
-
export default Endpoint;
|
|
72
|
+
}
|
|
73
|
+
}
|
package/esm/EndpointBuilder.js
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
import Endpoint from "./Endpoint";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
export default class EndpointBuilder {
|
|
3
|
+
constructor(api) {
|
|
4
4
|
this.api = api;
|
|
5
5
|
}
|
|
6
|
-
|
|
6
|
+
queryOf() {
|
|
7
7
|
return this;
|
|
8
|
-
}
|
|
9
|
-
|
|
8
|
+
}
|
|
9
|
+
paramsOf() {
|
|
10
10
|
return this;
|
|
11
|
-
}
|
|
12
|
-
|
|
11
|
+
}
|
|
12
|
+
bodyOf() {
|
|
13
13
|
return this;
|
|
14
|
-
}
|
|
15
|
-
|
|
14
|
+
}
|
|
15
|
+
responseOf() {
|
|
16
16
|
return this;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
}
|
|
18
|
+
build(config) {
|
|
19
|
+
const endpoint = new Endpoint(this.api, config);
|
|
20
20
|
this.api.endpoints[endpoint.id] = endpoint;
|
|
21
21
|
return endpoint;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
}());
|
|
25
|
-
export default EndpointBuilder;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const DEFAULT_QUERY_STRINGIFY = (query) => {
|
|
2
|
+
var _a, _b;
|
|
3
|
+
const queryStrings = [];
|
|
4
|
+
const queryKeys = Object.keys(query);
|
|
5
|
+
for (let i = 0; i < queryKeys.length; i++) {
|
|
6
|
+
const key = queryKeys[i];
|
|
7
|
+
queryStrings.push(`${key}=${(_b = (_a = query[key]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ""}`);
|
|
8
|
+
}
|
|
9
|
+
return queryStrings.join("&");
|
|
10
|
+
};
|
|
11
|
+
export const DEFAULT_QUERY_PARSE = (queryString) => {
|
|
12
|
+
const query = {};
|
|
13
|
+
const queryStrings = queryString.split("&");
|
|
14
|
+
for (let i = 0; i < queryStrings.length; i++) {
|
|
15
|
+
const [key, value] = queryStrings[i].split("=");
|
|
16
|
+
query[key] = !(value === null || value === void 0 ? void 0 : value.length) ? true : value;
|
|
17
|
+
}
|
|
18
|
+
return query;
|
|
19
|
+
};
|
|
@@ -0,0 +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: (BaseRequestConfig | RequestConfig<P, Q, B> | undefined)[]) => ComputedRequestConfig<P, Q, B>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { COMPUTED_CONFIG_SYMBOL, } from "./ApiTypes";
|
|
2
|
+
import * as Utils from "./Utils";
|
|
3
|
+
import { DEFAULT_QUERY_PARSE, DEFAULT_QUERY_STRINGIFY } from "./QueryHandling";
|
|
4
|
+
const MERGED_CONFIG_KEYS = ["headers"];
|
|
5
|
+
export const computeRequestConfig = (configs) => {
|
|
6
|
+
var _a, _b;
|
|
7
|
+
const computedConfig = Utils.assign({
|
|
8
|
+
[COMPUTED_CONFIG_SYMBOL]: true,
|
|
9
|
+
}, ...configs);
|
|
10
|
+
// merge other values
|
|
11
|
+
for (const key of MERGED_CONFIG_KEYS) {
|
|
12
|
+
computedConfig[key] = Utils.assign({}, ...configs.reduce((acc, config) => {
|
|
13
|
+
if (config === null || config === void 0 ? void 0 : config[key]) {
|
|
14
|
+
acc.push(config[key]);
|
|
15
|
+
}
|
|
16
|
+
return acc;
|
|
17
|
+
}, []));
|
|
18
|
+
}
|
|
19
|
+
computedConfig.queryHandling = {
|
|
20
|
+
parse: ((_a = computedConfig.queryHandling) === null || _a === void 0 ? void 0 : _a.parse) || DEFAULT_QUERY_PARSE,
|
|
21
|
+
stringify: ((_b = computedConfig.queryHandling) === null || _b === void 0 ? void 0 : _b.stringify) || computedConfig.queryParser || DEFAULT_QUERY_STRINGIFY,
|
|
22
|
+
};
|
|
23
|
+
delete computedConfig.queryParser;
|
|
24
|
+
const query = computedConfig.query;
|
|
25
|
+
let queryString;
|
|
26
|
+
let queryObject;
|
|
27
|
+
if (query) {
|
|
28
|
+
Object.defineProperty(computedConfig, "queryString", {
|
|
29
|
+
get() {
|
|
30
|
+
if (queryString) {
|
|
31
|
+
return queryString;
|
|
32
|
+
}
|
|
33
|
+
if (typeof query === "string") {
|
|
34
|
+
return queryString = query;
|
|
35
|
+
}
|
|
36
|
+
return queryString = computedConfig.queryHandling.stringify(query);
|
|
37
|
+
},
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(computedConfig, "queryObject", {
|
|
40
|
+
get() {
|
|
41
|
+
if (queryObject) {
|
|
42
|
+
return queryObject;
|
|
43
|
+
}
|
|
44
|
+
if (typeof query === "string") {
|
|
45
|
+
return queryObject = computedConfig.queryHandling.parse(query);
|
|
46
|
+
}
|
|
47
|
+
return queryObject = query;
|
|
48
|
+
},
|
|
49
|
+
set(value) {
|
|
50
|
+
queryObject = value;
|
|
51
|
+
queryString = computedConfig.queryHandling.stringify(value);
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return computedConfig;
|
|
56
|
+
};
|
package/esm/RequestContext.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ApiResponse, Body, EventResult, Headers, Params, Query, RequestCacheInfo,
|
|
1
|
+
import { ApiResponse, Body, ComputedRequestConfig, EventResult, Headers, Params, Query, RequestCacheInfo, RequestContextStats, RequestEventHandlers, RequestHost } from "./ApiTypes";
|
|
2
2
|
import { Api } from "./Api";
|
|
3
3
|
import { RequestEvent, RequestMethod, ResponseType } from "./ApiConstants";
|
|
4
4
|
import { EndpointMockingConfig } from "./MockingTypes";
|
|
@@ -17,14 +17,14 @@ export default class RequestContext<R = any, P extends Params | undefined = Para
|
|
|
17
17
|
error: RequestError | null;
|
|
18
18
|
readonly cacheInfo: RequestCacheInfo;
|
|
19
19
|
cancelled: boolean;
|
|
20
|
-
readonly computedConfig:
|
|
20
|
+
readonly computedConfig: ComputedRequestConfig<P, Q, B>;
|
|
21
21
|
readonly mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined;
|
|
22
22
|
private parsedBody;
|
|
23
|
-
constructor(backend: RequestBackend, host: RequestHost, config:
|
|
23
|
+
constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<P, Q, B>, computedPath: string, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined);
|
|
24
24
|
get method(): RequestMethod;
|
|
25
25
|
get api(): Api;
|
|
26
26
|
get baseUrl(): string;
|
|
27
|
-
get responseType(): ResponseType;
|
|
27
|
+
get responseType(): ResponseType | undefined;
|
|
28
28
|
private initMiddleware;
|
|
29
29
|
private generateKey;
|
|
30
30
|
updateHeaders(newHeaders: Headers): this;
|