api-def 0.7.4 → 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/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/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/cjs/Api.js
CHANGED
|
@@ -42,6 +42,7 @@ var EndpointBuilder_1 = require("./EndpointBuilder");
|
|
|
42
42
|
var Utils = require("./Utils");
|
|
43
43
|
var FetchRequestBackend_1 = require("./backend/FetchRequestBackend");
|
|
44
44
|
var ApiConstants_1 = require("./ApiConstants");
|
|
45
|
+
var RequestConfig_1 = require("./RequestConfig");
|
|
45
46
|
// use fetch as default if it is present
|
|
46
47
|
var requestBackend = Utils.getGlobalFetch() ? new FetchRequestBackend_1.default() : null;
|
|
47
48
|
var requestBackendIsDefault = true;
|
|
@@ -60,7 +61,7 @@ var setRequestBackend = function (backend) {
|
|
|
60
61
|
exports.setRequestBackend = setRequestBackend;
|
|
61
62
|
var HotRequestHost = /** @class */ (function () {
|
|
62
63
|
function HotRequestHost(api, path, method) {
|
|
63
|
-
this.responseType =
|
|
64
|
+
this.responseType = undefined;
|
|
64
65
|
this.api = api;
|
|
65
66
|
this.method = method;
|
|
66
67
|
this.path = path;
|
|
@@ -74,7 +75,10 @@ var HotRequestHost = /** @class */ (function () {
|
|
|
74
75
|
});
|
|
75
76
|
HotRequestHost.prototype.computeConfig = function (config) {
|
|
76
77
|
var apiDefaults = this.api.getConfig();
|
|
77
|
-
return
|
|
78
|
+
return (0, RequestConfig_1.computeRequestConfig)([
|
|
79
|
+
apiDefaults,
|
|
80
|
+
config,
|
|
81
|
+
]);
|
|
78
82
|
};
|
|
79
83
|
HotRequestHost.prototype.computePath = function (path, config) {
|
|
80
84
|
return path.startsWith("/") ? path : "/".concat(path);
|
package/cjs/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/cjs/ApiTypes.js
CHANGED
package/cjs/ApiUtils.js
CHANGED
|
@@ -32,7 +32,7 @@ var isAcceptableStatus = function (status, acceptableStatus) {
|
|
|
32
32
|
return (false);
|
|
33
33
|
};
|
|
34
34
|
exports.isAcceptableStatus = isAcceptableStatus;
|
|
35
|
-
var TEXT_CONTENT_TYPES = ["text/plain"];
|
|
35
|
+
var TEXT_CONTENT_TYPES = ["text/plain", "text/html", "text/xml", "application/xml"];
|
|
36
36
|
var JSON_CONTENT_TYPES = ["text/json", "application/json"];
|
|
37
37
|
var ARRAY_BUFFER_CONTENT_TYPES = ["application/octet-stream"];
|
|
38
38
|
var inferResponseType = function (contentType) {
|
package/cjs/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/cjs/Endpoint.js
CHANGED
|
@@ -37,8 +37,9 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
var Requester = require("./Requester");
|
|
40
|
+
var ApiTypes_1 = require("./ApiTypes");
|
|
40
41
|
var Utils = require("./Utils");
|
|
41
|
-
var
|
|
42
|
+
var RequestConfig_1 = require("./RequestConfig");
|
|
42
43
|
var Endpoint = /** @class */ (function () {
|
|
43
44
|
function Endpoint(api, info) {
|
|
44
45
|
this.api = api;
|
|
@@ -48,7 +49,7 @@ var Endpoint = /** @class */ (function () {
|
|
|
48
49
|
this.description = info.description;
|
|
49
50
|
this.path = info.path;
|
|
50
51
|
this.config = info.config;
|
|
51
|
-
this.responseType = info.responseType
|
|
52
|
+
this.responseType = info.responseType;
|
|
52
53
|
this.mocking = info.mocking;
|
|
53
54
|
}
|
|
54
55
|
Endpoint.prototype.submit = function (config) {
|
|
@@ -93,13 +94,17 @@ var Endpoint = /** @class */ (function () {
|
|
|
93
94
|
configurable: true
|
|
94
95
|
});
|
|
95
96
|
Endpoint.prototype.computeConfig = function (config) {
|
|
97
|
+
var _a;
|
|
96
98
|
var apiDefaults = this.api.getConfig();
|
|
97
|
-
var computedConfig = Utils.assign({},
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
var computedConfig = Utils.assign((_a = {},
|
|
100
|
+
_a[ApiTypes_1.COMPUTED_CONFIG_SYMBOL] = true,
|
|
101
|
+
_a), apiDefaults, this.config, config);
|
|
102
|
+
return (0, RequestConfig_1.computeRequestConfig)([
|
|
103
|
+
apiDefaults,
|
|
104
|
+
this.config,
|
|
105
|
+
config,
|
|
106
|
+
]);
|
|
107
|
+
delete computedConfig.queryParser;
|
|
103
108
|
return computedConfig;
|
|
104
109
|
};
|
|
105
110
|
return Endpoint;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_QUERY_PARSE = exports.DEFAULT_QUERY_STRINGIFY = void 0;
|
|
4
|
+
var DEFAULT_QUERY_STRINGIFY = function (query) {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
var queryStrings = [];
|
|
7
|
+
var queryKeys = Object.keys(query);
|
|
8
|
+
for (var i = 0; i < queryKeys.length; i++) {
|
|
9
|
+
var key = queryKeys[i];
|
|
10
|
+
queryStrings.push("".concat(key, "=").concat((_b = (_a = query[key]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : ""));
|
|
11
|
+
}
|
|
12
|
+
return queryStrings.join("&");
|
|
13
|
+
};
|
|
14
|
+
exports.DEFAULT_QUERY_STRINGIFY = DEFAULT_QUERY_STRINGIFY;
|
|
15
|
+
var DEFAULT_QUERY_PARSE = function (queryString) {
|
|
16
|
+
var query = {};
|
|
17
|
+
var queryStrings = queryString.split("&");
|
|
18
|
+
for (var i = 0; i < queryStrings.length; i++) {
|
|
19
|
+
var _a = queryStrings[i].split("="), key = _a[0], value = _a[1];
|
|
20
|
+
query[key] = !(value === null || value === void 0 ? void 0 : value.length) ? true : value;
|
|
21
|
+
}
|
|
22
|
+
return query;
|
|
23
|
+
};
|
|
24
|
+
exports.DEFAULT_QUERY_PARSE = DEFAULT_QUERY_PARSE;
|
|
@@ -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,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.computeRequestConfig = void 0;
|
|
13
|
+
var ApiTypes_1 = require("./ApiTypes");
|
|
14
|
+
var Utils = require("./Utils");
|
|
15
|
+
var QueryHandling_1 = require("./QueryHandling");
|
|
16
|
+
var MERGED_CONFIG_KEYS = ["headers"];
|
|
17
|
+
var computeRequestConfig = function (configs) {
|
|
18
|
+
var _a;
|
|
19
|
+
var _b, _c;
|
|
20
|
+
var computedConfig = Utils.assign.apply(Utils, __spreadArray([(_a = {},
|
|
21
|
+
_a[ApiTypes_1.COMPUTED_CONFIG_SYMBOL] = true,
|
|
22
|
+
_a)], configs, false));
|
|
23
|
+
var _loop_1 = function (key) {
|
|
24
|
+
computedConfig[key] = Utils.assign.apply(Utils, __spreadArray([{}], configs.reduce(function (acc, config) {
|
|
25
|
+
if (config === null || config === void 0 ? void 0 : config[key]) {
|
|
26
|
+
acc.push(config[key]);
|
|
27
|
+
}
|
|
28
|
+
return acc;
|
|
29
|
+
}, []), false));
|
|
30
|
+
};
|
|
31
|
+
// merge other values
|
|
32
|
+
for (var _i = 0, MERGED_CONFIG_KEYS_1 = MERGED_CONFIG_KEYS; _i < MERGED_CONFIG_KEYS_1.length; _i++) {
|
|
33
|
+
var key = MERGED_CONFIG_KEYS_1[_i];
|
|
34
|
+
_loop_1(key);
|
|
35
|
+
}
|
|
36
|
+
computedConfig.queryHandling = {
|
|
37
|
+
parse: ((_b = computedConfig.queryHandling) === null || _b === void 0 ? void 0 : _b.parse) || QueryHandling_1.DEFAULT_QUERY_PARSE,
|
|
38
|
+
stringify: ((_c = computedConfig.queryHandling) === null || _c === void 0 ? void 0 : _c.stringify) || computedConfig.queryParser || QueryHandling_1.DEFAULT_QUERY_STRINGIFY,
|
|
39
|
+
};
|
|
40
|
+
delete computedConfig.queryParser;
|
|
41
|
+
var query = computedConfig.query;
|
|
42
|
+
var queryString;
|
|
43
|
+
var queryObject;
|
|
44
|
+
if (query) {
|
|
45
|
+
Object.defineProperty(computedConfig, "queryString", {
|
|
46
|
+
get: function () {
|
|
47
|
+
if (queryString) {
|
|
48
|
+
return queryString;
|
|
49
|
+
}
|
|
50
|
+
if (typeof query === "string") {
|
|
51
|
+
return queryString = query;
|
|
52
|
+
}
|
|
53
|
+
return queryString = computedConfig.queryHandling.stringify(query);
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(computedConfig, "queryObject", {
|
|
57
|
+
get: function () {
|
|
58
|
+
if (queryObject) {
|
|
59
|
+
return queryObject;
|
|
60
|
+
}
|
|
61
|
+
if (typeof query === "string") {
|
|
62
|
+
return queryObject = computedConfig.queryHandling.parse(query);
|
|
63
|
+
}
|
|
64
|
+
return queryObject = query;
|
|
65
|
+
},
|
|
66
|
+
set: function (value) {
|
|
67
|
+
queryObject = value;
|
|
68
|
+
queryString = computedConfig.queryHandling.stringify(value);
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return computedConfig;
|
|
73
|
+
};
|
|
74
|
+
exports.computeRequestConfig = computeRequestConfig;
|
package/cjs/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;
|
package/cjs/RequestContext.js
CHANGED
|
@@ -105,18 +105,9 @@ var RequestContext = /** @class */ (function () {
|
|
|
105
105
|
}
|
|
106
106
|
};
|
|
107
107
|
RequestContext.prototype.generateKey = function () {
|
|
108
|
-
var computedConfig = this.computedConfig;
|
|
109
108
|
var key = this.computedPath.trim();
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
var queryKeys = Object.keys(computedConfig.query);
|
|
113
|
-
for (var i = 0; i < queryKeys.length; i++) {
|
|
114
|
-
var queryKey = queryKeys[i];
|
|
115
|
-
queryStrings.push("".concat(queryKey, "=").concat(computedConfig.query[queryKey]));
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (queryStrings.length > 0) {
|
|
119
|
-
key += "?" + queryStrings.join("&");
|
|
109
|
+
if (this.computedConfig.queryString) {
|
|
110
|
+
key += "?" + this.computedConfig.queryString;
|
|
120
111
|
}
|
|
121
112
|
return key;
|
|
122
113
|
};
|
|
@@ -151,7 +142,7 @@ var RequestContext = /** @class */ (function () {
|
|
|
151
142
|
return this.parsedBody;
|
|
152
143
|
};
|
|
153
144
|
RequestContext.prototype.updateQuery = function (newQuery) {
|
|
154
|
-
this.computedConfig.
|
|
145
|
+
this.computedConfig.queryObject = Utils.assign({}, this.computedConfig.queryObject, newQuery);
|
|
155
146
|
return this;
|
|
156
147
|
};
|
|
157
148
|
RequestContext.prototype.triggerEvent = function (eventType) {
|
|
@@ -192,7 +183,6 @@ var RequestContext = /** @class */ (function () {
|
|
|
192
183
|
}
|
|
193
184
|
};
|
|
194
185
|
RequestContext.prototype.getRequestUrl = function () {
|
|
195
|
-
var _a;
|
|
196
186
|
var path = !this.baseUrl.endsWith("/")
|
|
197
187
|
? this.baseUrl + "/"
|
|
198
188
|
: this.baseUrl;
|
|
@@ -209,17 +199,8 @@ var RequestContext = /** @class */ (function () {
|
|
|
209
199
|
}
|
|
210
200
|
}
|
|
211
201
|
var url = new URL(path, origin);
|
|
212
|
-
if (this.computedConfig.
|
|
213
|
-
|
|
214
|
-
url.search = this.computedConfig.queryParser(this.computedConfig.query);
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
var queryKeys = Object.keys(this.computedConfig.query);
|
|
218
|
-
for (var i = 0; i < queryKeys.length; i++) {
|
|
219
|
-
var key = queryKeys[i];
|
|
220
|
-
url.searchParams.append(key, ((_a = this.computedConfig.query[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "");
|
|
221
|
-
}
|
|
222
|
-
}
|
|
202
|
+
if (this.computedConfig.queryString) {
|
|
203
|
+
url.search = this.computedConfig.queryString;
|
|
223
204
|
}
|
|
224
205
|
return url;
|
|
225
206
|
};
|
package/cjs/RequestError.js
CHANGED
|
@@ -22,7 +22,7 @@ var convertToRequestError = function (config) {
|
|
|
22
22
|
isRequestError: true,
|
|
23
23
|
request: {
|
|
24
24
|
url: context.getRequestUrl().href,
|
|
25
|
-
query: context.computedConfig.
|
|
25
|
+
query: context.computedConfig.queryObject,
|
|
26
26
|
headers: context.computedConfig.headers,
|
|
27
27
|
body: body,
|
|
28
28
|
},
|
package/cjs/Requester.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { ApiResponse, Body, Query, RequestConfig, RequestHost } from "./ApiTypes";
|
|
2
2
|
import { EndpointMockingConfig } from "./MockingTypes";
|
|
3
|
-
export declare const submit: <R, P extends string | undefined, Q extends Query
|
|
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
|
@@ -73,7 +73,7 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
|
|
|
73
73
|
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
74
74
|
key = context.key;
|
|
75
75
|
lock = (context.computedConfig || {}).lock;
|
|
76
|
-
if (lock) {
|
|
76
|
+
if (typeof lock === "string") {
|
|
77
77
|
lockedContext = locks[lock];
|
|
78
78
|
if (lockedContext && lockedContext.id !== context.id) {
|
|
79
79
|
lockedContext.cancel();
|
|
@@ -100,7 +100,7 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
|
|
|
100
100
|
delete runningOperations[key];
|
|
101
101
|
throw error_1;
|
|
102
102
|
case 5:
|
|
103
|
-
if (lock) {
|
|
103
|
+
if (typeof lock === "string") {
|
|
104
104
|
delete locks[lock];
|
|
105
105
|
}
|
|
106
106
|
return [7 /*endfinally*/];
|
|
@@ -62,6 +62,8 @@ var AxiosRequestBackend = /** @class */ (function () {
|
|
|
62
62
|
return __awaiter(this, void 0, void 0, function () {
|
|
63
63
|
return __generator(this, function (_a) {
|
|
64
64
|
return [2 /*return*/, {
|
|
65
|
+
method: context.method,
|
|
66
|
+
url: response.request.res.responseUrl,
|
|
65
67
|
data: response.data,
|
|
66
68
|
headers: response.headers,
|
|
67
69
|
status: response.status,
|
|
@@ -72,19 +74,17 @@ var AxiosRequestBackend = /** @class */ (function () {
|
|
|
72
74
|
};
|
|
73
75
|
AxiosRequestBackend.prototype.makeRequest = function (context) {
|
|
74
76
|
var computedConfig = context.computedConfig;
|
|
77
|
+
var url = context.getRequestUrl();
|
|
75
78
|
var canceler = null;
|
|
76
79
|
var promise = axios({
|
|
77
80
|
method: context.method,
|
|
78
|
-
|
|
79
|
-
url: context.computedPath,
|
|
81
|
+
url: url.href,
|
|
80
82
|
data: context.getParsedBody(),
|
|
81
|
-
params: computedConfig.query || {},
|
|
82
83
|
headers: computedConfig.headers || {},
|
|
83
84
|
responseType: context.responseType,
|
|
84
85
|
cancelToken: new axios.CancelToken(function (cancellerFunc) {
|
|
85
86
|
canceler = cancellerFunc;
|
|
86
87
|
}),
|
|
87
|
-
paramsSerializer: context.computedConfig.queryParser,
|
|
88
88
|
});
|
|
89
89
|
return {
|
|
90
90
|
promise: promise,
|
|
@@ -53,7 +53,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
53
53
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
54
|
var Utils = require("../Utils");
|
|
55
55
|
var Utils_1 = require("../Utils");
|
|
56
|
-
var ApiConstants_1 = require("../ApiConstants");
|
|
57
56
|
var ApiUtils_1 = require("../ApiUtils");
|
|
58
57
|
var FetchError = /** @class */ (function (_super) {
|
|
59
58
|
__extends(FetchError, _super);
|
|
@@ -102,13 +101,13 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
102
101
|
_b.label = 3;
|
|
103
102
|
case 3:
|
|
104
103
|
_b.trys.push([3, 7, , 8]);
|
|
105
|
-
if (!(responseType ===
|
|
104
|
+
if (!(responseType === "arraybuffer")) return [3 /*break*/, 5];
|
|
106
105
|
return [4 /*yield*/, response.clone().arrayBuffer()];
|
|
107
106
|
case 4:
|
|
108
107
|
data = _b.sent();
|
|
109
108
|
return [3 /*break*/, 6];
|
|
110
109
|
case 5:
|
|
111
|
-
if (responseType ===
|
|
110
|
+
if (responseType === "json") {
|
|
112
111
|
data = JSON.parse(response.__text);
|
|
113
112
|
}
|
|
114
113
|
_b.label = 6;
|
|
@@ -125,6 +124,8 @@ var FetchRequestBackend = /** @class */ (function () {
|
|
|
125
124
|
});
|
|
126
125
|
return [2 /*return*/, {
|
|
127
126
|
__lowercaseHeaders: processedHeaders,
|
|
127
|
+
method: context.method,
|
|
128
|
+
url: response.url,
|
|
128
129
|
data: data,
|
|
129
130
|
status: status,
|
|
130
131
|
headers: processedHeaders,
|
|
@@ -80,7 +80,7 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
80
80
|
req = {
|
|
81
81
|
body: context.getParsedBody(),
|
|
82
82
|
params: (_b = context.computedConfig.params) !== null && _b !== void 0 ? _b : {},
|
|
83
|
-
query: context.computedConfig.
|
|
83
|
+
query: context.computedConfig.queryObject,
|
|
84
84
|
headers: (_c = context.computedConfig.headers) !== null && _c !== void 0 ? _c : {},
|
|
85
85
|
url: context.getRequestUrl().toString(),
|
|
86
86
|
};
|
|
@@ -140,6 +140,8 @@ var MockRequestBackend = /** @class */ (function () {
|
|
|
140
140
|
return parsedHeaders;
|
|
141
141
|
}, {});
|
|
142
142
|
return [2 /*return*/, {
|
|
143
|
+
url: context.getRequestUrl().href,
|
|
144
|
+
method: context.method,
|
|
143
145
|
headers: parsedHeaders,
|
|
144
146
|
data: res.response,
|
|
145
147
|
status: res.statusCode,
|
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
|
+
}
|