api-def 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/Api.d.ts +35 -35
- package/cjs/Api.js +140 -144
- package/cjs/ApiConstants.d.ts +58 -58
- package/cjs/ApiConstants.js +55 -55
- package/cjs/ApiTypes.d.ts +103 -103
- package/cjs/ApiTypes.js +4 -4
- package/cjs/ApiUtils.d.ts +6 -6
- package/cjs/ApiUtils.js +51 -53
- package/cjs/Endpoint.d.ts +52 -52
- package/cjs/Endpoint.js +104 -108
- package/cjs/EndpointBuilder.d.ts +14 -14
- package/cjs/EndpointBuilder.js +46 -46
- package/cjs/MockingTypes.d.ts +30 -30
- package/cjs/MockingTypes.js +2 -2
- package/cjs/QueryHandling.d.ts +3 -3
- package/cjs/QueryHandling.js +24 -24
- package/cjs/RequestConfig.d.ts +2 -2
- package/cjs/RequestConfig.js +74 -74
- package/cjs/RequestContext.d.ts +48 -48
- package/cjs/RequestContext.js +235 -238
- package/cjs/RequestError.d.ts +30 -30
- package/cjs/RequestError.js +47 -47
- package/cjs/Requester.d.ts +3 -3
- package/cjs/Requester.js +335 -336
- package/cjs/TextDecoding.d.ts +1 -1
- package/cjs/TextDecoding.js +152 -139
- package/cjs/UtilTypes.d.ts +5 -5
- package/cjs/UtilTypes.js +2 -2
- package/cjs/Utils.d.ts +15 -15
- package/cjs/Utils.js +79 -77
- package/cjs/Validation.d.ts +8 -8
- package/cjs/Validation.js +2 -2
- package/cjs/backend/AxiosRequestBackend.d.ts +13 -13
- package/cjs/backend/AxiosRequestBackend.js +99 -99
- package/cjs/backend/FetchRequestBackend.d.ts +15 -15
- package/cjs/backend/FetchRequestBackend.js +202 -202
- package/cjs/backend/MockRequestBackend.d.ts +11 -11
- package/cjs/backend/MockRequestBackend.js +164 -164
- package/cjs/backend/RequestBackend.d.ts +19 -19
- package/cjs/backend/RequestBackend.js +2 -2
- package/cjs/cache/CacheBackend.d.ts +6 -6
- package/cjs/cache/CacheBackend.js +2 -2
- package/cjs/cache/Caching.d.ts +10 -10
- package/cjs/cache/Caching.js +88 -88
- package/cjs/cache/LocalForageCacheBackend.d.ts +9 -9
- package/cjs/cache/LocalForageCacheBackend.js +24 -24
- package/cjs/cache/LocalStorageCacheBackend.d.ts +7 -7
- package/cjs/cache/LocalStorageCacheBackend.js +77 -77
- package/cjs/index.d.ts +13 -13
- package/cjs/index.js +41 -41
- package/cjs/middleware/CacheMiddleware.d.ts +7 -7
- package/cjs/middleware/CacheMiddleware.js +107 -107
- package/cjs/middleware/LoggingMiddleware.d.ts +6 -6
- package/cjs/middleware/LoggingMiddleware.js +82 -88
- package/cjs/util/retry/index.d.ts +3 -3
- package/cjs/util/retry/index.js +52 -52
- package/cjs/util/retry/lib/retry.d.ts +3 -3
- package/cjs/util/retry/lib/retry.js +50 -54
- package/cjs/util/retry/lib/retryOperation.d.ts +27 -27
- package/cjs/util/retry/lib/retryOperation.js +130 -134
- package/esm/Api.d.ts +35 -35
- package/esm/Api.js +92 -98
- package/esm/ApiConstants.d.ts +58 -58
- package/esm/ApiConstants.js +52 -52
- package/esm/ApiTypes.d.ts +103 -103
- package/esm/ApiTypes.js +1 -1
- package/esm/ApiUtils.d.ts +6 -6
- package/esm/ApiUtils.js +43 -45
- package/esm/Endpoint.d.ts +52 -52
- package/esm/Endpoint.js +66 -70
- package/esm/EndpointBuilder.d.ts +14 -14
- package/esm/EndpointBuilder.js +31 -31
- package/esm/MockingTypes.d.ts +30 -30
- package/esm/MockingTypes.js +1 -1
- package/esm/QueryHandling.d.ts +3 -3
- package/esm/QueryHandling.js +19 -19
- package/esm/RequestConfig.d.ts +2 -2
- package/esm/RequestConfig.js +56 -56
- package/esm/RequestContext.d.ts +48 -48
- package/esm/RequestContext.js +167 -170
- package/esm/RequestError.d.ts +30 -30
- package/esm/RequestError.js +42 -42
- package/esm/Requester.d.ts +3 -3
- package/esm/Requester.js +252 -253
- package/esm/TextDecoding.d.ts +1 -1
- package/esm/TextDecoding.js +148 -135
- package/esm/UtilTypes.d.ts +5 -5
- package/esm/UtilTypes.js +1 -1
- package/esm/Utils.d.ts +15 -15
- package/esm/Utils.js +66 -68
- package/esm/Validation.d.ts +8 -8
- package/esm/Validation.js +1 -1
- package/esm/backend/AxiosRequestBackend.d.ts +13 -13
- package/esm/backend/AxiosRequestBackend.js +62 -62
- package/esm/backend/FetchRequestBackend.d.ts +15 -15
- package/esm/backend/FetchRequestBackend.js +136 -136
- package/esm/backend/MockRequestBackend.d.ts +11 -11
- package/esm/backend/MockRequestBackend.js +116 -116
- package/esm/backend/RequestBackend.d.ts +19 -19
- package/esm/backend/RequestBackend.js +1 -1
- package/esm/cache/CacheBackend.d.ts +6 -6
- package/esm/cache/CacheBackend.js +1 -1
- package/esm/cache/Caching.d.ts +10 -10
- package/esm/cache/Caching.js +39 -39
- package/esm/cache/LocalForageCacheBackend.d.ts +9 -9
- package/esm/cache/LocalForageCacheBackend.js +20 -20
- package/esm/cache/LocalStorageCacheBackend.d.ts +7 -7
- package/esm/cache/LocalStorageCacheBackend.js +32 -32
- package/esm/index.d.ts +13 -13
- package/esm/index.js +14 -15
- package/esm/middleware/CacheMiddleware.d.ts +7 -7
- package/esm/middleware/CacheMiddleware.js +58 -58
- package/esm/middleware/LoggingMiddleware.d.ts +6 -6
- package/esm/middleware/LoggingMiddleware.js +77 -83
- package/esm/util/retry/index.d.ts +3 -3
- package/esm/util/retry/index.js +50 -50
- package/esm/util/retry/lib/retry.d.ts +3 -3
- package/esm/util/retry/lib/retry.js +35 -39
- package/esm/util/retry/lib/retryOperation.d.ts +27 -27
- package/esm/util/retry/lib/retryOperation.js +126 -132
- package/package.json +12 -32
package/esm/Endpoint.js
CHANGED
|
@@ -1,70 +1,66 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import * as Requester from "./Requester";
|
|
11
|
-
import { computeRequestConfig } from "./RequestConfig";
|
|
12
|
-
export default class Endpoint {
|
|
13
|
-
constructor(api, info) {
|
|
14
|
-
this.api = api;
|
|
15
|
-
this.id = info.id;
|
|
16
|
-
this.method = info.method;
|
|
17
|
-
this.name = info.name || info.id;
|
|
18
|
-
this.description = info.description;
|
|
19
|
-
this.path = info.path;
|
|
20
|
-
this.config = info.config;
|
|
21
|
-
this.responseType = info.responseType;
|
|
22
|
-
this.mocking = info.mocking;
|
|
23
|
-
this.validation = info.validation || {};
|
|
24
|
-
}
|
|
25
|
-
submit(config) {
|
|
26
|
-
var _a, _b;
|
|
27
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
-
let mock = false;
|
|
29
|
-
const apiMocking = this.api.mocking;
|
|
30
|
-
if (apiMocking) {
|
|
31
|
-
const mockingEnabled = (_a = (typeof apiMocking.enabled === "function" ? apiMocking.enabled() : apiMocking.enabled)) !== null && _a !== void 0 ? _a : false;
|
|
32
|
-
if (mockingEnabled) {
|
|
33
|
-
if (!((_b = this.mocking) === null || _b === void 0 ? void 0 : _b.handler)) {
|
|
34
|
-
throw new Error(`[api-def] Endpoint for '${this.path}' has no mocking`);
|
|
35
|
-
}
|
|
36
|
-
mock = true;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return Requester.submit(this, config, mock ? this.mocking : null);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
computePath(path, request) {
|
|
43
|
-
let computedPath = path.startsWith("/") ? path : `/${path}`;
|
|
44
|
-
if (request.params) {
|
|
45
|
-
const keys = Object.keys(request.params);
|
|
46
|
-
for (let i = 0; i < keys.length; i++) {
|
|
47
|
-
const argName = keys[i];
|
|
48
|
-
computedPath = computedPath.replace(`:${argName}`, request.params[argName]);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (computedPath.includes(":")) {
|
|
52
|
-
throw new Error(`[api-def] Not all path params have been resolved: '${computedPath}'`);
|
|
53
|
-
}
|
|
54
|
-
return computedPath;
|
|
55
|
-
}
|
|
56
|
-
get baseUrl() {
|
|
57
|
-
return this.api.baseUrl;
|
|
58
|
-
}
|
|
59
|
-
computeConfig(config) {
|
|
60
|
-
const apiDefaults = this.api.getConfig();
|
|
61
|
-
return computeRequestConfig([
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
getRequestBackend() {
|
|
68
|
-
return this.api.getRequestBackend();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import * as Requester from "./Requester";
|
|
11
|
+
import { computeRequestConfig } from "./RequestConfig";
|
|
12
|
+
export default class Endpoint {
|
|
13
|
+
constructor(api, info) {
|
|
14
|
+
this.api = api;
|
|
15
|
+
this.id = info.id;
|
|
16
|
+
this.method = info.method;
|
|
17
|
+
this.name = info.name || info.id;
|
|
18
|
+
this.description = info.description;
|
|
19
|
+
this.path = info.path;
|
|
20
|
+
this.config = info.config;
|
|
21
|
+
this.responseType = info.responseType;
|
|
22
|
+
this.mocking = info.mocking;
|
|
23
|
+
this.validation = info.validation || {};
|
|
24
|
+
}
|
|
25
|
+
submit(config) {
|
|
26
|
+
var _a, _b;
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
let mock = false;
|
|
29
|
+
const apiMocking = this.api.mocking;
|
|
30
|
+
if (apiMocking) {
|
|
31
|
+
const mockingEnabled = (_a = (typeof apiMocking.enabled === "function" ? apiMocking.enabled() : apiMocking.enabled)) !== null && _a !== void 0 ? _a : false;
|
|
32
|
+
if (mockingEnabled) {
|
|
33
|
+
if (!((_b = this.mocking) === null || _b === void 0 ? void 0 : _b.handler)) {
|
|
34
|
+
throw new Error(`[api-def] Endpoint for '${this.path}' has no mocking`);
|
|
35
|
+
}
|
|
36
|
+
mock = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return Requester.submit(this, config, mock ? this.mocking : null);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
computePath(path, request) {
|
|
43
|
+
let computedPath = path.startsWith("/") ? path : `/${path}`;
|
|
44
|
+
if (request.params) {
|
|
45
|
+
const keys = Object.keys(request.params);
|
|
46
|
+
for (let i = 0; i < keys.length; i++) {
|
|
47
|
+
const argName = keys[i];
|
|
48
|
+
computedPath = computedPath.replace(`:${argName}`, request.params[argName]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (computedPath.includes(":")) {
|
|
52
|
+
throw new Error(`[api-def] Not all path params have been resolved: '${computedPath}'`);
|
|
53
|
+
}
|
|
54
|
+
return computedPath;
|
|
55
|
+
}
|
|
56
|
+
get baseUrl() {
|
|
57
|
+
return this.api.baseUrl;
|
|
58
|
+
}
|
|
59
|
+
computeConfig(config) {
|
|
60
|
+
const apiDefaults = this.api.getConfig();
|
|
61
|
+
return computeRequestConfig([apiDefaults, this.config, config]);
|
|
62
|
+
}
|
|
63
|
+
getRequestBackend() {
|
|
64
|
+
return this.api.getRequestBackend();
|
|
65
|
+
}
|
|
66
|
+
}
|
package/esm/EndpointBuilder.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
export default class EndpointBuilder<R = any, P extends Params | undefined = undefined, Q extends Query | undefined = undefined, B extends Body | undefined = undefined> {
|
|
6
|
-
private api;
|
|
7
|
-
private readonly validation;
|
|
8
|
-
constructor(api: Api);
|
|
9
|
-
queryOf<Q extends Query>(schema?: zod.Schema<Q>): EndpointBuilder<R, P, Q, B>;
|
|
10
|
-
paramsOf<P extends Params>(): EndpointBuilder<R, P, Q, B>;
|
|
11
|
-
bodyOf<B extends Body>(schema?: zod.Schema<B>): EndpointBuilder<R, P, Q, B>;
|
|
12
|
-
responseOf<R>(schema?: zod.Schema<R>): EndpointBuilder<R, P, Q, B>;
|
|
13
|
-
build<Path extends string>(config: Omit<EndpointConfig<R, P, Q, B, Path>, "validation">): Endpoint<R, P, Q, B>;
|
|
14
|
-
}
|
|
1
|
+
import * as zod from "zod";
|
|
2
|
+
import { Api } from "./Api";
|
|
3
|
+
import { Body, Params, Query } from "./ApiTypes";
|
|
4
|
+
import Endpoint, { EndpointConfig } from "./Endpoint";
|
|
5
|
+
export default class EndpointBuilder<R = any, P extends Params | undefined = undefined, Q extends Query | undefined = undefined, B extends Body | undefined = undefined> {
|
|
6
|
+
private api;
|
|
7
|
+
private readonly validation;
|
|
8
|
+
constructor(api: Api);
|
|
9
|
+
queryOf<Q extends Query>(schema?: zod.Schema<Q>): EndpointBuilder<R, P, Q, B>;
|
|
10
|
+
paramsOf<P extends Params>(): EndpointBuilder<R, P, Q, B>;
|
|
11
|
+
bodyOf<B extends Body>(schema?: zod.Schema<B>): EndpointBuilder<R, P, Q, B>;
|
|
12
|
+
responseOf<R>(schema?: zod.Schema<R>): EndpointBuilder<R, P, Q, B>;
|
|
13
|
+
build<Path extends string>(config: Omit<EndpointConfig<R, P, Q, B, Path>, "validation">): Endpoint<R, P, Q, B>;
|
|
14
|
+
}
|
package/esm/EndpointBuilder.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import Endpoint from "./Endpoint";
|
|
2
|
-
/*type ExtractParams<Path> = Path extends `${infer Segment}/${infer Rest}`
|
|
3
|
-
? Segment extends `:${infer Param}` ? Param | ExtractParams<Rest> : ExtractParams<Rest>
|
|
4
|
-
: Path extends `:${infer Param}` ? Param : undefined;
|
|
5
|
-
*/
|
|
6
|
-
export default class EndpointBuilder {
|
|
7
|
-
constructor(api) {
|
|
8
|
-
this.validation = {};
|
|
9
|
-
this.api = api;
|
|
10
|
-
}
|
|
11
|
-
queryOf(schema) {
|
|
12
|
-
this.validation.query = schema;
|
|
13
|
-
return this;
|
|
14
|
-
}
|
|
15
|
-
paramsOf() {
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
bodyOf(schema) {
|
|
19
|
-
this.validation.body = schema;
|
|
20
|
-
return this;
|
|
21
|
-
}
|
|
22
|
-
responseOf(schema) {
|
|
23
|
-
this.validation.response = schema;
|
|
24
|
-
return this;
|
|
25
|
-
}
|
|
26
|
-
build(config) {
|
|
27
|
-
const endpoint = new Endpoint(this.api, Object.assign(Object.assign({}, config), { validation: this.validation }));
|
|
28
|
-
this.api.endpoints[endpoint.id] = endpoint;
|
|
29
|
-
return endpoint;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
import Endpoint from "./Endpoint";
|
|
2
|
+
/*type ExtractParams<Path> = Path extends `${infer Segment}/${infer Rest}`
|
|
3
|
+
? Segment extends `:${infer Param}` ? Param | ExtractParams<Rest> : ExtractParams<Rest>
|
|
4
|
+
: Path extends `:${infer Param}` ? Param : undefined;
|
|
5
|
+
*/
|
|
6
|
+
export default class EndpointBuilder {
|
|
7
|
+
constructor(api) {
|
|
8
|
+
this.validation = {};
|
|
9
|
+
this.api = api;
|
|
10
|
+
}
|
|
11
|
+
queryOf(schema) {
|
|
12
|
+
this.validation.query = schema;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
paramsOf() {
|
|
16
|
+
return this;
|
|
17
|
+
}
|
|
18
|
+
bodyOf(schema) {
|
|
19
|
+
this.validation.body = schema;
|
|
20
|
+
return this;
|
|
21
|
+
}
|
|
22
|
+
responseOf(schema) {
|
|
23
|
+
this.validation.response = schema;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
build(config) {
|
|
27
|
+
const endpoint = new Endpoint(this.api, Object.assign(Object.assign({}, config), { validation: this.validation }));
|
|
28
|
+
this.api.endpoints[endpoint.id] = endpoint;
|
|
29
|
+
return endpoint;
|
|
30
|
+
}
|
|
31
|
+
}
|
package/esm/MockingTypes.d.ts
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { ApiResponse, Body, Headers, Params, Query } from "./ApiTypes";
|
|
2
|
-
export interface ApiMockingConfig {
|
|
3
|
-
enabled: boolean | (() => boolean);
|
|
4
|
-
}
|
|
5
|
-
export interface MockRequest<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
6
|
-
params: P extends Params ? Record<P, string> : {};
|
|
7
|
-
body: B;
|
|
8
|
-
query: Q;
|
|
9
|
-
headers: Readonly<Headers>;
|
|
10
|
-
url: string;
|
|
11
|
-
}
|
|
12
|
-
export interface MockResponse<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
13
|
-
statusCode: number;
|
|
14
|
-
response: R | undefined;
|
|
15
|
-
headers: Headers;
|
|
16
|
-
status(statusCode: number): this;
|
|
17
|
-
send(response: R): this;
|
|
18
|
-
}
|
|
19
|
-
export
|
|
20
|
-
response?: ApiResponse;
|
|
21
|
-
};
|
|
22
|
-
export
|
|
23
|
-
export interface EndpointMockingConfig<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
24
|
-
/**-
|
|
25
|
-
* The range supplied will be used to simulate the lag in obtaining a response
|
|
26
|
-
* your endpoint. If no values are supplied, a response will be returned immediately
|
|
27
|
-
*/
|
|
28
|
-
delay?: number | [minMs: number, maxMs: number];
|
|
29
|
-
handler: EndpointMockingFunction<R, P, Q, B>;
|
|
30
|
-
}
|
|
1
|
+
import { ApiResponse, Body, Headers, Params, Query } from "./ApiTypes";
|
|
2
|
+
export interface ApiMockingConfig {
|
|
3
|
+
enabled: boolean | (() => boolean);
|
|
4
|
+
}
|
|
5
|
+
export interface MockRequest<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
6
|
+
params: P extends Params ? Record<P, string> : {};
|
|
7
|
+
body: B;
|
|
8
|
+
query: Q;
|
|
9
|
+
headers: Readonly<Headers>;
|
|
10
|
+
url: string;
|
|
11
|
+
}
|
|
12
|
+
export interface MockResponse<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
13
|
+
statusCode: number;
|
|
14
|
+
response: R | undefined;
|
|
15
|
+
headers: Headers;
|
|
16
|
+
status(statusCode: number): this;
|
|
17
|
+
send(response: R): this;
|
|
18
|
+
}
|
|
19
|
+
export type MockRequestError = Error & {
|
|
20
|
+
response?: ApiResponse;
|
|
21
|
+
};
|
|
22
|
+
export type EndpointMockingFunction<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> = (req: MockRequest<R, P, Q, B>, res: MockResponse<R, P, Q, B>) => Promise<MockResponse<R, P, Q, B>> | MockResponse<R, P, Q, B>;
|
|
23
|
+
export interface EndpointMockingConfig<R = any, P extends Params | undefined = Params | undefined, Q extends Query | undefined = Query | undefined, B extends Body | undefined = Body | undefined> {
|
|
24
|
+
/**-
|
|
25
|
+
* The range supplied will be used to simulate the lag in obtaining a response
|
|
26
|
+
* your endpoint. If no values are supplied, a response will be returned immediately
|
|
27
|
+
*/
|
|
28
|
+
delay?: number | [minMs: number, maxMs: number];
|
|
29
|
+
handler: EndpointMockingFunction<R, P, Q, B>;
|
|
30
|
+
}
|
package/esm/MockingTypes.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export {};
|
|
1
|
+
export {};
|
package/esm/QueryHandling.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { QueryStringify } from "./ApiTypes";
|
|
2
|
-
export declare const DEFAULT_QUERY_STRINGIFY: QueryStringify;
|
|
3
|
-
export declare const DEFAULT_QUERY_PARSE: (queryString: string) => Record<string, any>;
|
|
1
|
+
import { QueryStringify } from "./ApiTypes";
|
|
2
|
+
export declare const DEFAULT_QUERY_STRINGIFY: QueryStringify;
|
|
3
|
+
export declare const DEFAULT_QUERY_PARSE: (queryString: string) => Record<string, any>;
|
package/esm/QueryHandling.js
CHANGED
|
@@ -1,19 +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
|
-
};
|
|
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
|
+
};
|
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: (BaseRequestConfig | RequestConfig<P, Q, B> | undefined)[]) => ComputedRequestConfig<P, Q, B>;
|
|
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>;
|
package/esm/RequestConfig.js
CHANGED
|
@@ -1,56 +1,56 @@
|
|
|
1
|
-
import { COMPUTED_CONFIG_SYMBOL
|
|
2
|
-
import
|
|
3
|
-
import
|
|
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
|
-
|
|
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
|
-
};
|
|
1
|
+
import { COMPUTED_CONFIG_SYMBOL } from "./ApiTypes";
|
|
2
|
+
import { DEFAULT_QUERY_PARSE, DEFAULT_QUERY_STRINGIFY } from "./QueryHandling";
|
|
3
|
+
import * as Utils from "./Utils";
|
|
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
|
+
computedConfig.queryParser = undefined;
|
|
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,48 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { EndpointMockingConfig } from "./MockingTypes";
|
|
5
|
-
import { RequestError } from "./RequestError";
|
|
6
|
-
import
|
|
7
|
-
import
|
|
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
|
-
readonly id: number;
|
|
10
|
-
readonly key: string;
|
|
11
|
-
private computedPath;
|
|
12
|
-
private computedBaseUrl;
|
|
13
|
-
private computedMethod;
|
|
14
|
-
readonly stats: RequestContextStats;
|
|
15
|
-
private readonly host;
|
|
16
|
-
readonly eventHandlers: RequestEventHandlers<R>;
|
|
17
|
-
readonly backend: RequestBackend;
|
|
18
|
-
private canceler;
|
|
19
|
-
response: ApiResponse<R> | null | undefined;
|
|
20
|
-
error: RequestError | null;
|
|
21
|
-
readonly cacheInfo: RequestCacheInfo;
|
|
22
|
-
cancelled: boolean;
|
|
23
|
-
readonly computedConfig: ComputedRequestConfig<P, Q, B>;
|
|
24
|
-
private computedRequestUrl;
|
|
25
|
-
readonly mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined;
|
|
26
|
-
private parsedBody;
|
|
27
|
-
readonly validation: Validation<R, P, Q, B>;
|
|
28
|
-
constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<P, Q, B>, computedPath: string, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined);
|
|
29
|
-
get method(): RequestMethod;
|
|
30
|
-
get api(): Api;
|
|
31
|
-
get baseUrl(): string;
|
|
32
|
-
get responseType(): ResponseType | undefined;
|
|
33
|
-
private initMiddleware;
|
|
34
|
-
private generateKey;
|
|
35
|
-
updateHeaders(newHeaders: Headers): this;
|
|
36
|
-
private parseRequestBody;
|
|
37
|
-
getParsedBody(): any;
|
|
38
|
-
updateQuery(newQuery: Partial<Q>): this;
|
|
39
|
-
triggerEvent(eventType: RequestEvent): Promise<EventResult<R> | undefined>;
|
|
40
|
-
addCanceller(canceler: () => void): void;
|
|
41
|
-
cancel(): void;
|
|
42
|
-
get requestUrl(): URL;
|
|
43
|
-
get path(): string;
|
|
44
|
-
updatePath(path: string): void;
|
|
45
|
-
updateBaseUrl(baseUrl: string): void;
|
|
46
|
-
updateMethod(method: RequestMethod): void;
|
|
47
|
-
private generateRequestUrl;
|
|
48
|
-
}
|
|
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";
|
|
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
|
+
readonly id: number;
|
|
10
|
+
readonly key: string;
|
|
11
|
+
private computedPath;
|
|
12
|
+
private computedBaseUrl;
|
|
13
|
+
private computedMethod;
|
|
14
|
+
readonly stats: RequestContextStats;
|
|
15
|
+
private readonly host;
|
|
16
|
+
readonly eventHandlers: RequestEventHandlers<R>;
|
|
17
|
+
readonly backend: RequestBackend;
|
|
18
|
+
private canceler;
|
|
19
|
+
response: ApiResponse<R> | null | undefined;
|
|
20
|
+
error: RequestError | null;
|
|
21
|
+
readonly cacheInfo: RequestCacheInfo;
|
|
22
|
+
cancelled: boolean;
|
|
23
|
+
readonly computedConfig: ComputedRequestConfig<P, Q, B>;
|
|
24
|
+
private computedRequestUrl;
|
|
25
|
+
readonly mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined;
|
|
26
|
+
private parsedBody;
|
|
27
|
+
readonly validation: Validation<R, P, Q, B>;
|
|
28
|
+
constructor(backend: RequestBackend, host: RequestHost, config: ComputedRequestConfig<P, Q, B>, computedPath: string, mocking: EndpointMockingConfig<R, P, Q, B> | null | undefined);
|
|
29
|
+
get method(): RequestMethod;
|
|
30
|
+
get api(): Api;
|
|
31
|
+
get baseUrl(): string;
|
|
32
|
+
get responseType(): ResponseType | undefined;
|
|
33
|
+
private initMiddleware;
|
|
34
|
+
private generateKey;
|
|
35
|
+
updateHeaders(newHeaders: Headers): this;
|
|
36
|
+
private parseRequestBody;
|
|
37
|
+
getParsedBody(): any;
|
|
38
|
+
updateQuery(newQuery: Partial<Q>): this;
|
|
39
|
+
triggerEvent(eventType: RequestEvent): Promise<EventResult<R> | undefined>;
|
|
40
|
+
addCanceller(canceler: () => void): void;
|
|
41
|
+
cancel(): void;
|
|
42
|
+
get requestUrl(): URL;
|
|
43
|
+
get path(): string;
|
|
44
|
+
updatePath(path: string): void;
|
|
45
|
+
updateBaseUrl(baseUrl: string): void;
|
|
46
|
+
updateMethod(method: RequestMethod): void;
|
|
47
|
+
private generateRequestUrl;
|
|
48
|
+
}
|