api-def 0.12.0-alpha.32 → 0.12.0-alpha.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -11
- package/cjs/Api.js +1 -1
- package/cjs/ApiTypes.d.ts +1 -1
- package/cjs/Endpoint.d.ts +1 -1
- package/cjs/Endpoint.js +4 -4
- package/cjs/Requester.js +1 -1
- package/esm/Api.js +1 -1
- package/esm/ApiTypes.d.ts +1 -1
- package/esm/Endpoint.d.ts +1 -1
- package/esm/Endpoint.js +4 -4
- package/esm/Requester.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,30 +4,33 @@ Typed APIs with middleware support
|
|
|
4
4
|
|
|
5
5
|
API def provides a unified way to type your endpoints allowing for compile time checking of query, body, response and even url parameters
|
|
6
6
|
|
|
7
|
-
```
|
|
7
|
+
```bash
|
|
8
|
+
npm i api-def
|
|
9
|
+
```
|
|
8
10
|
|
|
9
11
|
- [Documentation](https://censkh.github.io/api-def/)
|
|
10
12
|
|
|
11
13
|
```typescript
|
|
12
|
-
import {Api} from "api-def";
|
|
14
|
+
import { Api } from "api-def";
|
|
13
15
|
|
|
14
16
|
const api = new Api({
|
|
15
17
|
baseUrl: "https://my-api/",
|
|
16
18
|
name: "My API",
|
|
17
19
|
});
|
|
18
20
|
|
|
19
|
-
const fetchData = api
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
21
|
+
const fetchData = api
|
|
22
|
+
.endpoint()
|
|
23
|
+
.queryOf<{ includeAwesome: boolean }>()
|
|
24
|
+
.responseOf<{ data: { awesome: boolean } }>()
|
|
25
|
+
.build({
|
|
26
|
+
id: "fetch_data",
|
|
27
|
+
method: "get",
|
|
28
|
+
path: "/data",
|
|
29
|
+
});
|
|
27
30
|
|
|
28
31
|
// calls GET https://my-api/data?includeAwesome=true
|
|
29
32
|
const res = await fetchData.submit({
|
|
30
|
-
query: {includeAwesome: true}
|
|
33
|
+
query: { includeAwesome: true },
|
|
31
34
|
});
|
|
32
35
|
|
|
33
36
|
console.log(res.data); // { data: { awesome: true } }
|
package/cjs/Api.js
CHANGED
|
@@ -78,7 +78,7 @@ var HotRequestHost = /** @class */ (function () {
|
|
|
78
78
|
var apiDefaults = this.api.computeRequestConfig();
|
|
79
79
|
return (0, RequestConfig_1.processRequestConfigs)([apiDefaults, config]);
|
|
80
80
|
};
|
|
81
|
-
HotRequestHost.prototype.computePath = function (path
|
|
81
|
+
HotRequestHost.prototype.computePath = function (path) {
|
|
82
82
|
return path.startsWith("/") ? path : "/".concat(path);
|
|
83
83
|
};
|
|
84
84
|
HotRequestHost.prototype.getRequestBackend = function () {
|
package/cjs/ApiTypes.d.ts
CHANGED
|
@@ -101,7 +101,7 @@ export interface RequestHost {
|
|
|
101
101
|
readonly responseType: ResponseType | undefined;
|
|
102
102
|
readonly validation: Validation;
|
|
103
103
|
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>;
|
|
104
|
-
computePath(path: string,
|
|
104
|
+
computePath(path: string, params: Record<string, string> | undefined): string;
|
|
105
105
|
getRequestBackend(): RequestBackend;
|
|
106
106
|
}
|
|
107
107
|
export interface CancelledRequestError extends Error {
|
package/cjs/Endpoint.d.ts
CHANGED
|
@@ -51,7 +51,7 @@ export default class Endpoint<TResponse = any, TParams extends Params | undefine
|
|
|
51
51
|
get mocking(): Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | undefined;
|
|
52
52
|
get validation(): Validation<TResponse, TParams, TQuery, TBody, TState>;
|
|
53
53
|
submit(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): Promise<ApiResponse<TResponse>>;
|
|
54
|
-
computePath(path: string,
|
|
54
|
+
computePath(path: string, params: Record<string, string> | undefined): string;
|
|
55
55
|
get baseUrl(): string;
|
|
56
56
|
/**
|
|
57
57
|
* @deprecated Use `computeRequestConfig` instead
|
package/cjs/Endpoint.js
CHANGED
|
@@ -137,13 +137,13 @@ var Endpoint = /** @class */ (function () {
|
|
|
137
137
|
});
|
|
138
138
|
});
|
|
139
139
|
};
|
|
140
|
-
Endpoint.prototype.computePath = function (path,
|
|
140
|
+
Endpoint.prototype.computePath = function (path, params) {
|
|
141
141
|
var computedPath = path.startsWith("/") ? path : "/".concat(path);
|
|
142
|
-
if (
|
|
143
|
-
var keys = Object.keys(
|
|
142
|
+
if (params) {
|
|
143
|
+
var keys = Object.keys(params);
|
|
144
144
|
for (var i = 0; i < keys.length; i++) {
|
|
145
145
|
var argName = keys[i];
|
|
146
|
-
var argValue =
|
|
146
|
+
var argValue = params[argName];
|
|
147
147
|
computedPath = computedPath.replace(":".concat(argName), argValue).replace("{".concat(argName, "}"), argValue);
|
|
148
148
|
}
|
|
149
149
|
}
|
package/cjs/Requester.js
CHANGED
|
@@ -65,7 +65,7 @@ var submit = function (host, config, mocking) { return __awaiter(void 0, void 0,
|
|
|
65
65
|
case 0:
|
|
66
66
|
computedConfig = host.computeConfig(config);
|
|
67
67
|
backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
68
|
-
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
68
|
+
context = new RequestContext_1.default(backend, host, computedConfig, host.computePath(host.path, config.params), mocking);
|
|
69
69
|
key = context.key;
|
|
70
70
|
lock = (context.computedConfig || {}).lock;
|
|
71
71
|
if (typeof lock === "string") {
|
package/esm/Api.js
CHANGED
|
@@ -41,7 +41,7 @@ class HotRequestHost {
|
|
|
41
41
|
const apiDefaults = this.api.computeRequestConfig();
|
|
42
42
|
return processRequestConfigs([apiDefaults, config]);
|
|
43
43
|
}
|
|
44
|
-
computePath(path
|
|
44
|
+
computePath(path) {
|
|
45
45
|
return path.startsWith("/") ? path : `/${path}`;
|
|
46
46
|
}
|
|
47
47
|
getRequestBackend() {
|
package/esm/ApiTypes.d.ts
CHANGED
|
@@ -101,7 +101,7 @@ export interface RequestHost {
|
|
|
101
101
|
readonly responseType: ResponseType | undefined;
|
|
102
102
|
readonly validation: Validation;
|
|
103
103
|
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>;
|
|
104
|
-
computePath(path: string,
|
|
104
|
+
computePath(path: string, params: Record<string, string> | undefined): string;
|
|
105
105
|
getRequestBackend(): RequestBackend;
|
|
106
106
|
}
|
|
107
107
|
export interface CancelledRequestError extends Error {
|
package/esm/Endpoint.d.ts
CHANGED
|
@@ -51,7 +51,7 @@ export default class Endpoint<TResponse = any, TParams extends Params | undefine
|
|
|
51
51
|
get mocking(): Mocking.EndpointMockingConfig<TResponse, TParams, TQuery, TBody, TState> | undefined;
|
|
52
52
|
get validation(): Validation<TResponse, TParams, TQuery, TBody, TState>;
|
|
53
53
|
submit(config: RequestConfig<TParams, TQuery, TBody, TState, TRequestHeaders>): Promise<ApiResponse<TResponse>>;
|
|
54
|
-
computePath(path: string,
|
|
54
|
+
computePath(path: string, params: Record<string, string> | undefined): string;
|
|
55
55
|
get baseUrl(): string;
|
|
56
56
|
/**
|
|
57
57
|
* @deprecated Use `computeRequestConfig` instead
|
package/esm/Endpoint.js
CHANGED
|
@@ -58,13 +58,13 @@ export default class Endpoint {
|
|
|
58
58
|
return Requester.submit(this, config, mock ? this.mocking : null);
|
|
59
59
|
});
|
|
60
60
|
}
|
|
61
|
-
computePath(path,
|
|
61
|
+
computePath(path, params) {
|
|
62
62
|
let computedPath = path.startsWith("/") ? path : `/${path}`;
|
|
63
|
-
if (
|
|
64
|
-
const keys = Object.keys(
|
|
63
|
+
if (params) {
|
|
64
|
+
const keys = Object.keys(params);
|
|
65
65
|
for (let i = 0; i < keys.length; i++) {
|
|
66
66
|
const argName = keys[i];
|
|
67
|
-
const argValue =
|
|
67
|
+
const argValue = params[argName];
|
|
68
68
|
computedPath = computedPath.replace(`:${argName}`, argValue).replace(`{${argName}}`, argValue);
|
|
69
69
|
}
|
|
70
70
|
}
|
package/esm/Requester.js
CHANGED
|
@@ -20,7 +20,7 @@ const MOCK_REQUEST_BACKEND = new MockRequestBackend();
|
|
|
20
20
|
export const submit = (host, config, mocking) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
21
|
const computedConfig = host.computeConfig(config);
|
|
22
22
|
const backend = mocking ? MOCK_REQUEST_BACKEND : host.getRequestBackend();
|
|
23
|
-
const context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config), mocking);
|
|
23
|
+
const context = new RequestContext(backend, host, computedConfig, host.computePath(host.path, config.params), mocking);
|
|
24
24
|
const { key } = context;
|
|
25
25
|
// don't do this -- should only be for GET requests anyway and should be opt-in
|
|
26
26
|
/*
|