api-def 0.11.1 → 0.12.0-alpha.10
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/bin/index.js +258727 -0
- package/cjs/Api.d.ts +23 -8
- package/cjs/Api.js +96 -15
- package/cjs/ApiTypes.d.ts +6 -4
- package/cjs/Endpoint.d.ts +25 -15
- package/cjs/Endpoint.js +90 -17
- package/cjs/EndpointBuilder.d.ts +11 -9
- package/cjs/EndpointBuilder.js +8 -2
- package/cjs/RequestConfig.d.ts +2 -2
- package/cjs/RequestConfig.js +3 -3
- package/cjs/RequestContext.js +2 -2
- package/cjs/Requester.js +2 -2
- package/cjs/backend/AxiosRequestBackend.js +5 -4
- package/cjs/backend/FetchRequestBackend.js +2 -2
- package/cjs/backend/MockRequestBackend.js +2 -2
- package/cjs/cache/Caching.js +2 -2
- package/cjs/cache/LocalStorageCacheBackend.js +2 -2
- package/cjs/index.d.ts +1 -1
- package/cjs/middleware/CacheMiddleware.js +2 -2
- package/esm/Api.d.ts +23 -8
- package/esm/Api.js +67 -14
- package/esm/ApiTypes.d.ts +6 -4
- package/esm/Endpoint.d.ts +25 -15
- package/esm/Endpoint.js +42 -16
- package/esm/EndpointBuilder.d.ts +11 -9
- package/esm/EndpointBuilder.js +8 -2
- package/esm/RequestConfig.d.ts +2 -2
- package/esm/RequestConfig.js +1 -1
- package/esm/backend/AxiosRequestBackend.js +2 -1
- package/esm/index.d.ts +1 -1
- package/package.json +30 -14
|
@@ -9,8 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
-
return g =
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
14
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
15
|
function step(op) {
|
|
16
16
|
if (f) throw new TypeError("Generator is already executing.");
|
package/esm/Api.d.ts
CHANGED
|
@@ -6,26 +6,41 @@ import type RequestBackend from "./backend/RequestBackend";
|
|
|
6
6
|
export declare const getRequestBackend: () => RequestBackend | null;
|
|
7
7
|
export declare const isRequestBackendDefault: () => boolean;
|
|
8
8
|
export declare const setRequestBackend: (backend: RequestBackend) => void;
|
|
9
|
-
export interface
|
|
9
|
+
export interface ApiOptions {
|
|
10
10
|
readonly name: string;
|
|
11
11
|
readonly baseUrl: string;
|
|
12
12
|
readonly middleware?: RequestMiddleware[];
|
|
13
|
+
/** @deprecated use `requestConfig` instead */
|
|
13
14
|
readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
15
|
+
readonly requestConfig?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
14
16
|
readonly mocking?: ApiMockingConfig;
|
|
15
17
|
readonly requestBackend?: RequestBackend;
|
|
16
18
|
}
|
|
19
|
+
export type ApiInfo = ApiOptions & Required<Pick<ApiOptions, "middleware" | "requestBackend">>;
|
|
17
20
|
export declare class Api implements ApiInfo {
|
|
18
|
-
readonly
|
|
19
|
-
readonly name: string;
|
|
20
|
-
readonly middleware: RequestMiddleware[];
|
|
21
|
-
readonly config?: BaseRequestConfig | (() => BaseRequestConfig);
|
|
22
|
-
readonly mocking?: ApiMockingConfig;
|
|
23
|
-
readonly requestBackend: RequestBackend;
|
|
21
|
+
private readonly info;
|
|
24
22
|
protected readonly endpoints: Record<string, Endpoint>;
|
|
25
|
-
constructor(
|
|
23
|
+
constructor(options: ApiOptions);
|
|
24
|
+
get requestBackend(): RequestBackend;
|
|
25
|
+
get baseUrl(): string;
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated use `requestConfig` instead
|
|
28
|
+
*/
|
|
29
|
+
get config(): BaseRequestConfig | (() => BaseRequestConfig) | undefined;
|
|
30
|
+
get requestConfig(): BaseRequestConfig | (() => BaseRequestConfig) | undefined;
|
|
31
|
+
get middleware(): RequestMiddleware[];
|
|
32
|
+
get mocking(): ApiMockingConfig | undefined;
|
|
33
|
+
get name(): string;
|
|
26
34
|
endpoint(): EndpointBuilder;
|
|
35
|
+
/**
|
|
36
|
+
* @deprecated use `requestBackend` instead
|
|
37
|
+
*/
|
|
27
38
|
getRequestBackend(): RequestBackend;
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated use `computeRequestConfig()` instead
|
|
41
|
+
*/
|
|
28
42
|
getConfig(): BaseRequestConfig;
|
|
43
|
+
computeRequestConfig(): BaseRequestConfig;
|
|
29
44
|
private hotRequest;
|
|
30
45
|
get: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
|
31
46
|
post: <R = unknown>(path: string, config: RequestConfig) => Promise<ApiResponse<R>>;
|
package/esm/Api.js
CHANGED
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { RequestMethod } from "./ApiConstants";
|
|
11
11
|
import EndpointBuilder from "./EndpointBuilder";
|
|
12
|
-
import {
|
|
12
|
+
import { processRequestConfigs } from "./RequestConfig";
|
|
13
13
|
import * as Requester from "./Requester";
|
|
14
14
|
import * as Utils from "./Utils";
|
|
15
15
|
import FetchRequestBackend from "./backend/FetchRequestBackend";
|
|
@@ -38,45 +38,98 @@ class HotRequestHost {
|
|
|
38
38
|
return this.api.baseUrl;
|
|
39
39
|
}
|
|
40
40
|
computeConfig(config) {
|
|
41
|
-
const apiDefaults = this.api.
|
|
42
|
-
return
|
|
41
|
+
const apiDefaults = this.api.computeRequestConfig();
|
|
42
|
+
return processRequestConfigs([apiDefaults, config]);
|
|
43
43
|
}
|
|
44
44
|
computePath(path, config) {
|
|
45
45
|
return path.startsWith("/") ? path : `/${path}`;
|
|
46
46
|
}
|
|
47
47
|
getRequestBackend() {
|
|
48
|
-
return this.api.
|
|
48
|
+
return this.api.requestBackend;
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
export class Api {
|
|
52
|
-
constructor(
|
|
52
|
+
constructor(options) {
|
|
53
53
|
var _a, _b;
|
|
54
54
|
this.endpoints = {};
|
|
55
|
+
/*reconfigure(info: Partial<ApiInfo>): void {
|
|
56
|
+
if (info.requestBackend) {
|
|
57
|
+
(this as any).requestBackend = info.requestBackend;
|
|
58
|
+
}
|
|
59
|
+
if (info.config) {
|
|
60
|
+
(this as any).config = info.config;
|
|
61
|
+
}
|
|
62
|
+
if (info.middleware) {
|
|
63
|
+
(this as any).middleware = info.middleware;
|
|
64
|
+
}
|
|
65
|
+
if (info.mocking) {
|
|
66
|
+
(this as any).mocking = info.mocking;
|
|
67
|
+
}
|
|
68
|
+
if (info.baseUrl) {
|
|
69
|
+
(this as any).baseUrl = info.baseUrl;
|
|
70
|
+
}
|
|
71
|
+
}*/
|
|
55
72
|
this.hotRequest = (requestMethod) => (path, config) => __awaiter(this, void 0, void 0, function* () { return yield Requester.submit(new HotRequestHost(this, path, requestMethod), config, null); });
|
|
56
73
|
this.get = this.hotRequest(RequestMethod.GET);
|
|
57
74
|
this.post = this.hotRequest(RequestMethod.POST);
|
|
58
75
|
this.put = this.hotRequest(RequestMethod.PUT);
|
|
59
76
|
this.delete = this.hotRequest(RequestMethod.DELETE);
|
|
60
77
|
this.patch = this.hotRequest(RequestMethod.PATCH);
|
|
61
|
-
|
|
62
|
-
this.baseUrl = info.baseUrl;
|
|
63
|
-
this.middleware = info.middleware || [];
|
|
64
|
-
this.endpoints = {};
|
|
65
|
-
this.config = info.config;
|
|
66
|
-
this.mocking = (_a = info.mocking) !== null && _a !== void 0 ? _a : undefined;
|
|
67
|
-
const requestBackend = (_b = info.requestBackend) !== null && _b !== void 0 ? _b : getRequestBackend();
|
|
78
|
+
const requestBackend = (_a = options.requestBackend) !== null && _a !== void 0 ? _a : getRequestBackend();
|
|
68
79
|
if (!requestBackend) {
|
|
69
80
|
throw new Error("[api-def] No request backend provided in either Api options or globally, use `setRequestBackend()` to set one or pass one via `requestBackend`");
|
|
70
81
|
}
|
|
71
|
-
this.
|
|
82
|
+
this.info = {
|
|
83
|
+
name: options.name,
|
|
84
|
+
baseUrl: options.baseUrl,
|
|
85
|
+
middleware: options.middleware || [],
|
|
86
|
+
config: options.config,
|
|
87
|
+
requestConfig: options.requestConfig,
|
|
88
|
+
mocking: (_b = options.mocking) !== null && _b !== void 0 ? _b : undefined,
|
|
89
|
+
requestBackend: requestBackend,
|
|
90
|
+
};
|
|
91
|
+
this.endpoints = {};
|
|
92
|
+
}
|
|
93
|
+
get requestBackend() {
|
|
94
|
+
return this.info.requestBackend;
|
|
95
|
+
}
|
|
96
|
+
get baseUrl() {
|
|
97
|
+
return this.info.baseUrl;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* @deprecated use `requestConfig` instead
|
|
101
|
+
*/
|
|
102
|
+
get config() {
|
|
103
|
+
return this.info.config;
|
|
104
|
+
}
|
|
105
|
+
get requestConfig() {
|
|
106
|
+
return this.info.requestConfig;
|
|
107
|
+
}
|
|
108
|
+
get middleware() {
|
|
109
|
+
return this.info.middleware;
|
|
110
|
+
}
|
|
111
|
+
get mocking() {
|
|
112
|
+
return this.info.mocking;
|
|
113
|
+
}
|
|
114
|
+
get name() {
|
|
115
|
+
return this.info.name;
|
|
72
116
|
}
|
|
73
117
|
endpoint() {
|
|
74
118
|
return new EndpointBuilder(this);
|
|
75
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* @deprecated use `requestBackend` instead
|
|
122
|
+
*/
|
|
76
123
|
getRequestBackend() {
|
|
77
124
|
return this.requestBackend;
|
|
78
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* @deprecated use `computeRequestConfig()` instead
|
|
128
|
+
*/
|
|
79
129
|
getConfig() {
|
|
80
|
-
return
|
|
130
|
+
return this.computeRequestConfig();
|
|
131
|
+
}
|
|
132
|
+
computeRequestConfig() {
|
|
133
|
+
return (typeof this.requestConfig === "function" ? this.requestConfig() : this.requestConfig) || {};
|
|
81
134
|
}
|
|
82
135
|
}
|
package/esm/ApiTypes.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export interface BaseRequestConfig {
|
|
|
43
43
|
queryParser?: QueryStringify;
|
|
44
44
|
queryHandling?: Partial<QueryHandling>;
|
|
45
45
|
}
|
|
46
|
-
export type RequestConfig<TParams extends Params | undefined = Params | undefined, TQuery extends Query | undefined = Query | undefined, TBody extends Body | undefined = Body | undefined, TState extends State = State> = (TParams extends undefined ? {
|
|
46
|
+
export type RequestConfig<TParams extends Params | undefined = Params | undefined, TQuery extends Query | undefined = Query | undefined, TBody extends Body | undefined = Body | undefined, TState extends State = State, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined> = (TParams extends undefined ? {
|
|
47
47
|
params?: never;
|
|
48
48
|
} : {
|
|
49
49
|
params: Record<TParams extends Params ? TParams : never, string>;
|
|
@@ -59,9 +59,11 @@ export type RequestConfig<TParams extends Params | undefined = Params | undefine
|
|
|
59
59
|
state?: TState;
|
|
60
60
|
} : {
|
|
61
61
|
state: TState;
|
|
62
|
-
}) & BaseRequestConfig
|
|
62
|
+
}) & (Omit<BaseRequestConfig, "headers"> & {
|
|
63
|
+
headers?: TRequestHeaders & RawHeaders;
|
|
64
|
+
});
|
|
63
65
|
export declare const COMPUTED_CONFIG_SYMBOL: unique symbol;
|
|
64
|
-
export type ComputedRequestConfig<TParams extends Params | undefined = Params | undefined, TQuery extends Query | undefined = Query | undefined, TBody extends Body | undefined = Body | undefined, TState extends State = State> = Omit<RequestConfig<TParams, TQuery, TBody, TState>, "queryParser" | "query" | "queryHandling" | "state"> & {
|
|
66
|
+
export type ComputedRequestConfig<TParams extends Params | undefined = Params | undefined, TQuery extends Query | undefined = Query | undefined, TBody extends Body | undefined = Body | undefined, TState extends State = State, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined> = Omit<RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>, "queryParser" | "query" | "queryHandling" | "state"> & {
|
|
65
67
|
[COMPUTED_CONFIG_SYMBOL]: true;
|
|
66
68
|
state: TState;
|
|
67
69
|
queryObject: Record<string, any> | undefined;
|
|
@@ -97,7 +99,7 @@ export interface RequestHost {
|
|
|
97
99
|
readonly path: string;
|
|
98
100
|
readonly responseType: ResponseType | undefined;
|
|
99
101
|
readonly validation: Validation;
|
|
100
|
-
computeConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State>(config: RequestConfig<TParams, TQuery, TBody, TState>): ComputedRequestConfig<TParams, TQuery, TBody, TState>;
|
|
102
|
+
computeConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
|
|
101
103
|
computePath(path: string, config: RequestConfig): string;
|
|
102
104
|
getRequestBackend(): RequestBackend;
|
|
103
105
|
}
|
package/esm/Endpoint.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Api } from "./Api";
|
|
2
2
|
import type { RequestMethod, ResponseType } from "./ApiConstants";
|
|
3
|
-
import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RequestConfig, RequestHost, State } from "./ApiTypes";
|
|
3
|
+
import type { ApiResponse, BaseRequestConfig, Body, ComputedRequestConfig, Params, Query, RawHeaders, RequestConfig, RequestHost, State } from "./ApiTypes";
|
|
4
4
|
import type * as Mocking from "./MockingTypes";
|
|
5
5
|
import type { Validation } from "./Validation";
|
|
6
6
|
import type RequestBackend from "./backend/RequestBackend";
|
|
7
|
-
export interface
|
|
7
|
+
export interface EndpointOptions<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> {
|
|
8
8
|
readonly id: string;
|
|
9
9
|
readonly method: RequestMethod;
|
|
10
10
|
readonly path: TPath;
|
|
@@ -32,21 +32,31 @@ export interface EndpointConfig<TResult, TParams extends Params | undefined, TQu
|
|
|
32
32
|
readonly mocking?: Mocking.EndpointMockingConfig<TResult, TParams, TQuery, TBody, TState>;
|
|
33
33
|
readonly validation?: Validation<TResult, TParams, TQuery, TBody, TState>;
|
|
34
34
|
}
|
|
35
|
-
export
|
|
35
|
+
export type EndpointInfo<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> = EndpointOptions<TResult, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders> & Required<Pick<EndpointOptions<TResult, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "name" | "validation">>;
|
|
36
|
+
/**
|
|
37
|
+
* @deprecated Use `EndpointInfo` instead
|
|
38
|
+
*/
|
|
39
|
+
export type EndpointConfig<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> = EndpointInfo<TResult, TParams, TQuery, TBody, TState, TPath>;
|
|
40
|
+
export default class Endpoint<TResponse = any, TParams extends Params | undefined = Params | undefined, TQuery extends Query | undefined = Query | undefined, TBody extends Body | undefined = Body | undefined, TState extends State = State, TPath extends string = string, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> implements EndpointInfo<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, RequestHost {
|
|
36
41
|
readonly api: Api;
|
|
37
|
-
readonly
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
private readonly info;
|
|
43
|
+
constructor(api: Api, options: EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>);
|
|
44
|
+
get id(): string;
|
|
45
|
+
get method(): RequestMethod;
|
|
46
|
+
get path(): TPath;
|
|
47
|
+
get name(): string;
|
|
48
|
+
get description(): string | undefined;
|
|
49
|
+
get config(): BaseRequestConfig;
|
|
50
|
+
get responseType(): ResponseType | undefined;
|
|
51
|
+
get mocking(): Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | undefined;
|
|
52
|
+
get validation(): Validation<TResponse, TParams, TQuery, TBody, TState>;
|
|
53
|
+
submit(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): Promise<ApiResponse<TResponse>>;
|
|
48
54
|
computePath(path: string, request: RequestConfig): string;
|
|
49
55
|
get baseUrl(): string;
|
|
50
|
-
|
|
56
|
+
/**
|
|
57
|
+
* @deprecated Use `computeRequestConfig` instead
|
|
58
|
+
*/
|
|
59
|
+
computeConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
|
|
60
|
+
computeRequestConfig<TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
|
|
51
61
|
getRequestBackend(): RequestBackend;
|
|
52
62
|
}
|
package/esm/Endpoint.js
CHANGED
|
@@ -8,19 +8,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import * as Requester from "./Requester";
|
|
11
|
-
import {
|
|
11
|
+
import { processRequestConfigs } from "./RequestConfig";
|
|
12
12
|
export default class Endpoint {
|
|
13
|
-
constructor(api,
|
|
13
|
+
constructor(api, options) {
|
|
14
14
|
this.api = api;
|
|
15
|
-
this.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
|
|
15
|
+
this.info = Object.assign(Object.assign({}, options), { name: options.name || options.id, validation: options.validation || {} });
|
|
16
|
+
}
|
|
17
|
+
get id() {
|
|
18
|
+
return this.info.id;
|
|
19
|
+
}
|
|
20
|
+
get method() {
|
|
21
|
+
return this.info.method;
|
|
22
|
+
}
|
|
23
|
+
get path() {
|
|
24
|
+
return this.info.path;
|
|
25
|
+
}
|
|
26
|
+
get name() {
|
|
27
|
+
return this.info.name;
|
|
28
|
+
}
|
|
29
|
+
get description() {
|
|
30
|
+
return this.info.description;
|
|
31
|
+
}
|
|
32
|
+
get config() {
|
|
33
|
+
return this.info.config || {};
|
|
34
|
+
}
|
|
35
|
+
get responseType() {
|
|
36
|
+
return this.info.responseType;
|
|
37
|
+
}
|
|
38
|
+
get mocking() {
|
|
39
|
+
return this.info.mocking;
|
|
40
|
+
}
|
|
41
|
+
get validation() {
|
|
42
|
+
return this.info.validation;
|
|
24
43
|
}
|
|
25
44
|
submit(config) {
|
|
26
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -45,10 +64,11 @@ export default class Endpoint {
|
|
|
45
64
|
const keys = Object.keys(request.params);
|
|
46
65
|
for (let i = 0; i < keys.length; i++) {
|
|
47
66
|
const argName = keys[i];
|
|
48
|
-
|
|
67
|
+
const argValue = request.params[argName];
|
|
68
|
+
computedPath = computedPath.replace(`:${argName}`, argValue).replace(`{${argName}}`, argValue);
|
|
49
69
|
}
|
|
50
70
|
}
|
|
51
|
-
if (computedPath.includes(":")) {
|
|
71
|
+
if (computedPath.includes(":") || computedPath.includes("{")) {
|
|
52
72
|
throw new Error(`[api-def] Not all path params have been resolved: '${computedPath}'`);
|
|
53
73
|
}
|
|
54
74
|
return computedPath;
|
|
@@ -56,11 +76,17 @@ export default class Endpoint {
|
|
|
56
76
|
get baseUrl() {
|
|
57
77
|
return this.api.baseUrl;
|
|
58
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* @deprecated Use `computeRequestConfig` instead
|
|
81
|
+
*/
|
|
59
82
|
computeConfig(config) {
|
|
60
|
-
|
|
61
|
-
|
|
83
|
+
return this.computeRequestConfig(config);
|
|
84
|
+
}
|
|
85
|
+
computeRequestConfig(config) {
|
|
86
|
+
const apiDefaults = this.api.computeRequestConfig();
|
|
87
|
+
return processRequestConfigs([apiDefaults, this.config, config]);
|
|
62
88
|
}
|
|
63
89
|
getRequestBackend() {
|
|
64
|
-
return this.api.
|
|
90
|
+
return this.api.requestBackend;
|
|
65
91
|
}
|
|
66
92
|
}
|
package/esm/EndpointBuilder.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import type * as zod from "zod";
|
|
2
2
|
import type { Api } from "./Api";
|
|
3
|
-
import type { Body, Params, Query, State } from "./ApiTypes";
|
|
4
|
-
import Endpoint, { type
|
|
5
|
-
export default class EndpointBuilder<TResponse = any, TParams extends Params | undefined = undefined, TQuery extends Query | undefined = undefined, TBody extends Body | undefined = undefined, TState extends State = State> {
|
|
3
|
+
import type { Body, Params, Query, RawHeaders, State } from "./ApiTypes";
|
|
4
|
+
import Endpoint, { type EndpointOptions } from "./Endpoint";
|
|
5
|
+
export default class EndpointBuilder<TResponse = any, TParams extends Params | undefined = undefined, TQuery extends Query | undefined = undefined, TBody extends Body | undefined = undefined, TState extends State = State, TRequestHeaders extends RawHeaders | undefined = RawHeaders | undefined, TResponseHeaders extends RawHeaders | undefined = RawHeaders | undefined> {
|
|
6
6
|
private api;
|
|
7
7
|
private readonly validation;
|
|
8
8
|
constructor(api: Api);
|
|
9
|
-
queryOf<TNewQuery extends Query>(schema?: zod.Schema<TNewQuery>): EndpointBuilder<TResponse, TParams, TNewQuery, TBody>;
|
|
10
|
-
paramsOf<TNewParams extends Params>(): EndpointBuilder<TResponse, TNewParams, TQuery, TBody, TState>;
|
|
11
|
-
bodyOf<TNewBody extends Body>(schema?: zod.Schema<TNewBody>): EndpointBuilder<TResponse, TParams, TQuery, TNewBody, TState>;
|
|
12
|
-
responseOf<TNewResponse>(schema?: zod.Schema<TNewResponse>): EndpointBuilder<TNewResponse, TParams, TQuery, TBody, TState>;
|
|
13
|
-
stateOf<TNewState extends State>(schema?: zod.Schema<TNewState>): EndpointBuilder<TResponse, TParams, TQuery, TBody, TNewState>;
|
|
14
|
-
|
|
9
|
+
queryOf<TNewQuery extends Query>(schema?: zod.Schema<TNewQuery>): EndpointBuilder<TResponse, TParams, TNewQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
|
|
10
|
+
paramsOf<TNewParams extends Params>(): EndpointBuilder<TResponse, TNewParams, TQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
|
|
11
|
+
bodyOf<TNewBody extends Body>(schema?: zod.Schema<TNewBody>): EndpointBuilder<TResponse, TParams, TQuery, TNewBody, TState, TRequestHeaders, TResponseHeaders>;
|
|
12
|
+
responseOf<TNewResponse>(schema?: zod.Schema<TNewResponse>): EndpointBuilder<TNewResponse, TParams, TQuery, TBody, TState, TRequestHeaders, TResponseHeaders>;
|
|
13
|
+
stateOf<TNewState extends State>(schema?: zod.Schema<TNewState>): EndpointBuilder<TResponse, TParams, TQuery, TBody, TNewState, TRequestHeaders, TResponseHeaders>;
|
|
14
|
+
requestHeadersOf<TNewRequestHeaders extends RawHeaders>(): EndpointBuilder<TResponse, TParams, TQuery, TBody, TState, TNewRequestHeaders, TResponseHeaders>;
|
|
15
|
+
responseHeadersOf<TNewResponseHeaders extends RawHeaders>(): EndpointBuilder<TResponse, TParams, TQuery, TBody, TState, TRequestHeaders, TNewResponseHeaders>;
|
|
16
|
+
build<TPath extends string>(options: Omit<EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>, "validation">): Endpoint<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>;
|
|
15
17
|
}
|
package/esm/EndpointBuilder.js
CHANGED
|
@@ -27,8 +27,14 @@ export default class EndpointBuilder {
|
|
|
27
27
|
this.validation.state = schema;
|
|
28
28
|
return this;
|
|
29
29
|
}
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
requestHeadersOf() {
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
responseHeadersOf() {
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
build(options) {
|
|
37
|
+
const endpoint = new Endpoint(this.api, Object.assign(Object.assign({}, options), { validation: this.validation }));
|
|
32
38
|
this.api.endpoints[endpoint.id] = endpoint;
|
|
33
39
|
return endpoint;
|
|
34
40
|
}
|
package/esm/RequestConfig.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { type BaseRequestConfig, type Body, type ComputedRequestConfig, type Params, type Query, type RequestConfig, type State } from "./ApiTypes";
|
|
2
|
-
export declare const
|
|
1
|
+
import { type BaseRequestConfig, type Body, type ComputedRequestConfig, type Params, type Query, type RawHeaders, type RequestConfig, type State } from "./ApiTypes";
|
|
2
|
+
export declare const processRequestConfigs: <TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State, TRequestHeaders extends RawHeaders | undefined>(configs: (RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders> | BaseRequestConfig | undefined)[]) => ComputedRequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>;
|
package/esm/RequestConfig.js
CHANGED
|
@@ -2,7 +2,7 @@ import { COMPUTED_CONFIG_SYMBOL, } from "./ApiTypes";
|
|
|
2
2
|
import { DEFAULT_QUERY_PARSE, DEFAULT_QUERY_STRINGIFY } from "./QueryHandling";
|
|
3
3
|
import * as Utils from "./Utils";
|
|
4
4
|
const MERGED_CONFIG_KEYS = ["headers"];
|
|
5
|
-
export const
|
|
5
|
+
export const processRequestConfigs = (configs) => {
|
|
6
6
|
var _a, _b;
|
|
7
7
|
const computedConfig = Utils.assign({
|
|
8
8
|
[COMPUTED_CONFIG_SYMBOL]: true,
|
|
@@ -26,9 +26,10 @@ export default class AxiosRequestBackend {
|
|
|
26
26
|
}
|
|
27
27
|
convertResponse(context, response) {
|
|
28
28
|
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
var _a, _b, _c, _d;
|
|
29
30
|
return {
|
|
30
31
|
method: context.method,
|
|
31
|
-
url: response.request.res.responseUrl,
|
|
32
|
+
url: (_b = (_a = response.request.res) === null || _a === void 0 ? void 0 : _a.responseUrl) !== null && _b !== void 0 ? _b : (_d = (_c = response.request) === null || _c === void 0 ? void 0 : _c._redirectable) === null || _d === void 0 ? void 0 : _d._currentUrl,
|
|
32
33
|
data: response.data,
|
|
33
34
|
headers: response.headers,
|
|
34
35
|
status: response.status,
|
package/esm/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export * from "./ApiTypes";
|
|
|
3
3
|
export * from "./UtilTypes";
|
|
4
4
|
export * from "./ApiConstants";
|
|
5
5
|
export { default as retry } from "./util/retry";
|
|
6
|
-
export {
|
|
6
|
+
export type { RetryOptions } from "./util/retry/interfaces";
|
|
7
7
|
export { isRequestError, type RequestError } from "./RequestError";
|
|
8
8
|
export { default as Endpoint, type EndpointConfig } from "./Endpoint";
|
|
9
9
|
export { clearCache, setCacheBackend } from "./cache/Caching";
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-def",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0-alpha.10",
|
|
4
4
|
"description": "Typed API definitions with middleware support",
|
|
5
5
|
"main": "cjs/index.js",
|
|
6
6
|
"types": "esm/index.d.ts",
|
|
7
7
|
"module": "esm/index.js",
|
|
8
|
+
"bin": {
|
|
9
|
+
"api-def": "bin/index.js"
|
|
10
|
+
},
|
|
8
11
|
"sideEffects": false,
|
|
9
12
|
"scripts": {
|
|
10
13
|
"prepublishOnly": "npm run check:fix && npm run test && npm run build",
|
|
@@ -13,37 +16,50 @@
|
|
|
13
16
|
"check:fix": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check --write",
|
|
14
17
|
"check": "tsc --noEmit --project tsconfig.json && npx @biomejs/biome check",
|
|
15
18
|
"cleanup": "rimraf esm && rimraf cjs",
|
|
16
|
-
"build": "npm run cleanup && npm run build:esm && npm run build:cjs",
|
|
19
|
+
"build": "npm run cleanup && npm run build:esm && npm run build:cjs && npm run build:cli",
|
|
17
20
|
"build:esm": "tsc --module es2015 --target es2016 --outDir esm --preserveWatchOutput",
|
|
18
21
|
"build:cjs": "tsc --module commonjs --target es5 --outDir cjs --preserveWatchOutput",
|
|
22
|
+
"build:cli": "esbuild cli/index.ts --bundle --platform=node --outfile=bin/index.js",
|
|
19
23
|
"website:dev": "cd website && npm run start",
|
|
20
24
|
"website:deploy": "cd website && npm run deploy"
|
|
21
25
|
},
|
|
22
26
|
"keywords": ["typescript", "javascript", "node", "web", "api", "typed", "cache", "fetch", "retry", "middleware"],
|
|
23
27
|
"author": "James Waterhouse <09jwater@gmail.com>",
|
|
24
28
|
"license": "MIT",
|
|
25
|
-
"files": ["LICENSE", "README.md", "esm/", "cjs/"],
|
|
29
|
+
"files": ["LICENSE", "README.md", "esm/", "cjs/", "bin/"],
|
|
26
30
|
"jest": {
|
|
27
31
|
"transform": {
|
|
28
|
-
"^.+\\.(t|j)sx?$":
|
|
32
|
+
"^.+\\.(t|j)sx?$": [
|
|
33
|
+
"@swc/jest",
|
|
34
|
+
{
|
|
35
|
+
"env": {}
|
|
36
|
+
}
|
|
37
|
+
]
|
|
29
38
|
}
|
|
30
39
|
},
|
|
31
40
|
"repository": "https://github.com/Censkh/api-def",
|
|
32
41
|
"devDependencies": {
|
|
33
|
-
"@biomejs/biome": "1.
|
|
34
|
-
"@
|
|
35
|
-
"@swc/
|
|
42
|
+
"@biomejs/biome": "1.9.4",
|
|
43
|
+
"@redocly/openapi-core": "^1.27.1",
|
|
44
|
+
"@swc/core": "^1.10.7",
|
|
45
|
+
"@swc/jest": "^0.2.37",
|
|
36
46
|
"@types/axios": "0.14.0",
|
|
37
|
-
"@types/jest": "^29.5.
|
|
38
|
-
"@types/
|
|
39
|
-
"@types/
|
|
40
|
-
"
|
|
47
|
+
"@types/jest": "^29.5.14",
|
|
48
|
+
"@types/lodash": "^4.17.14",
|
|
49
|
+
"@types/node": "22.10.5",
|
|
50
|
+
"@types/qs": "6.9.17",
|
|
51
|
+
"axios": "1.7.9",
|
|
52
|
+
"chalk": "^4.1.2",
|
|
53
|
+
"commander": "^13.0.0",
|
|
41
54
|
"cross-env": "7.0.3",
|
|
55
|
+
"esbuild": "^0.24.2",
|
|
42
56
|
"jest": "latest",
|
|
57
|
+
"lodash": "^4.17.21",
|
|
43
58
|
"npm-run-all": "4.1.5",
|
|
44
|
-
"
|
|
59
|
+
"openapi-typescript": "^7.5.2",
|
|
60
|
+
"qs": "6.13.1",
|
|
45
61
|
"rimraf": "6.0.1",
|
|
46
|
-
"typescript": "5.
|
|
47
|
-
"zod": "3.
|
|
62
|
+
"typescript": "^5.7.3",
|
|
63
|
+
"zod": "3.24.1"
|
|
48
64
|
}
|
|
49
65
|
}
|