api-def 0.12.0-alpha.4 → 0.12.0-alpha.7
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 +71 -16
- package/cjs/Api.js +2 -2
- package/cjs/ApiTypes.d.ts +6 -4
- package/cjs/Endpoint.d.ts +9 -9
- package/cjs/Endpoint.js +3 -2
- package/cjs/EndpointBuilder.d.ts +11 -9
- package/cjs/EndpointBuilder.js +8 -2
- package/cjs/RequestConfig.d.ts +2 -2
- package/esm/Api.js +2 -2
- package/esm/ApiTypes.d.ts +6 -4
- package/esm/Endpoint.d.ts +9 -9
- package/esm/Endpoint.js +3 -2
- package/esm/EndpointBuilder.d.ts +11 -9
- package/esm/EndpointBuilder.js +8 -2
- package/esm/RequestConfig.d.ts +2 -2
- package/package.json +1 -1
- package/bin/OpenApiToSourceCode.js +0 -174
package/bin/index.js
CHANGED
|
@@ -258551,16 +258551,14 @@ var METHOD_COLORS = {
|
|
|
258551
258551
|
var openApiToSourceCode = async (options) => {
|
|
258552
258552
|
const { openApiPath } = options;
|
|
258553
258553
|
const inContents = fs.readFileSync(openApiPath, "utf-8");
|
|
258554
|
-
const ast = await openapiTS(inContents, {
|
|
258555
|
-
rootTypes: true
|
|
258556
|
-
});
|
|
258554
|
+
const ast = await openapiTS(inContents, {});
|
|
258557
258555
|
const bundleResults = await (0, import_openapi_core3.bundle)({
|
|
258558
258556
|
ref: openApiPath,
|
|
258559
258557
|
config: await (0, import_openapi_core3.createConfig)({})
|
|
258560
258558
|
});
|
|
258561
258559
|
const routes = bundleResults.bundle.parsed.paths;
|
|
258562
258560
|
const server2 = bundleResults.bundle.parsed.servers[0];
|
|
258563
|
-
|
|
258561
|
+
const extraTypes = {};
|
|
258564
258562
|
const source = `import { Api } from "api-def";
|
|
258565
258563
|
|
|
258566
258564
|
const API = new Api({
|
|
@@ -258573,16 +258571,30 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258573
258571
|
const id = methodDef.operationId;
|
|
258574
258572
|
const responseStatuses = Object.keys(methodDef.responses);
|
|
258575
258573
|
const successfulResponse = responseStatuses.filter((status) => status.startsWith("2") || status.startsWith("3"));
|
|
258574
|
+
let responseType = void 0;
|
|
258576
258575
|
const responseTypes = [];
|
|
258577
258576
|
for (const status of successfulResponse) {
|
|
258578
258577
|
const response = methodDef.responses[status];
|
|
258579
258578
|
if (response.$ref) {
|
|
258580
|
-
|
|
258579
|
+
const responseDef = bundleResults.bundle.parsed.components.responses[response.$ref.split("/").pop()];
|
|
258580
|
+
for (const mediaType in responseDef.content) {
|
|
258581
|
+
const schema = responseDef.content[mediaType].schema;
|
|
258582
|
+
if (!responseType) {
|
|
258583
|
+
responseType = mediaType.split(";")[0];
|
|
258584
|
+
}
|
|
258585
|
+
if (schema?.$ref) {
|
|
258586
|
+
const name = schema.$ref.split("/").pop();
|
|
258587
|
+
extraTypes[`Response${name}`] = `components["schemas"]["${name}"]`;
|
|
258588
|
+
responseTypes.push(`Response${name}`);
|
|
258589
|
+
}
|
|
258590
|
+
}
|
|
258581
258591
|
}
|
|
258582
258592
|
}
|
|
258583
258593
|
const bodyTypes = [];
|
|
258584
258594
|
if (methodDef.requestBody?.$ref) {
|
|
258585
|
-
|
|
258595
|
+
const name = methodDef.requestBody.$ref.split("/").pop();
|
|
258596
|
+
extraTypes[`Body${name}`] = `components["schemas"]["${name}"]`;
|
|
258597
|
+
bodyTypes.push(`Body${name}`);
|
|
258586
258598
|
}
|
|
258587
258599
|
const queryTypes = [];
|
|
258588
258600
|
if (methodDef.parameters?.length) {
|
|
@@ -258598,15 +258610,51 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258598
258610
|
return false;
|
|
258599
258611
|
});
|
|
258600
258612
|
if (anyQueryParams) {
|
|
258601
|
-
extraTypes
|
|
258602
|
-
`;
|
|
258613
|
+
extraTypes[`Query${(0, import_lodash.upperFirst)(id)}`] = `operations["${id}"]["parameters"]["query"]`;
|
|
258603
258614
|
queryTypes.push(`Query${(0, import_lodash.upperFirst)(id)}`);
|
|
258604
258615
|
}
|
|
258605
258616
|
}
|
|
258617
|
+
const requestHeaderTypes = [];
|
|
258618
|
+
if (methodDef.parameters?.length) {
|
|
258619
|
+
const anyHeaderParams = methodDef.parameters.some((param) => {
|
|
258620
|
+
if (param.in === "header") {
|
|
258621
|
+
return true;
|
|
258622
|
+
}
|
|
258623
|
+
if (param.$ref) {
|
|
258624
|
+
const ref = param.$ref;
|
|
258625
|
+
const paramDef = bundleResults.bundle.parsed.components.parameters[ref.split("/").pop()];
|
|
258626
|
+
return paramDef.in === "header";
|
|
258627
|
+
}
|
|
258628
|
+
return false;
|
|
258629
|
+
});
|
|
258630
|
+
if (anyHeaderParams) {
|
|
258631
|
+
extraTypes[`Headers${(0, import_lodash.upperFirst)(id)}`] = `operations["${id}"]["parameters"]["header"]`;
|
|
258632
|
+
requestHeaderTypes.push(`Headers${(0, import_lodash.upperFirst)(id)}`);
|
|
258633
|
+
}
|
|
258634
|
+
}
|
|
258635
|
+
const responseHeaderTypes = [];
|
|
258636
|
+
let pathParams = [];
|
|
258637
|
+
if (methodDef.parameters?.length) {
|
|
258638
|
+
pathParams = methodDef.parameters.reduce((pathParams2, param) => {
|
|
258639
|
+
if (param.in === "path") {
|
|
258640
|
+
pathParams2.push(param.name);
|
|
258641
|
+
} else if (param.$ref) {
|
|
258642
|
+
const ref = param.$ref;
|
|
258643
|
+
const paramDef = bundleResults.bundle.parsed.components.parameters[ref.split("/").pop()];
|
|
258644
|
+
if (paramDef.in === "path") {
|
|
258645
|
+
pathParams2.push(paramDef.name);
|
|
258646
|
+
}
|
|
258647
|
+
}
|
|
258648
|
+
return pathParams2;
|
|
258649
|
+
}, []);
|
|
258650
|
+
}
|
|
258606
258651
|
const endpointParts = [
|
|
258607
|
-
|
|
258608
|
-
|
|
258609
|
-
|
|
258652
|
+
pathParams.length > 0 ? `.paramsOf<"${pathParams.join("|")}">()` : "",
|
|
258653
|
+
responseTypes.length > 0 ? `.responseOf<${responseTypes.join("|")}>()` : "",
|
|
258654
|
+
bodyTypes.length > 0 ? `.bodyOf<${bodyTypes.join("|")}>()` : "",
|
|
258655
|
+
queryTypes.length > 0 ? `.queryOf<${queryTypes.join("|")}>()` : "",
|
|
258656
|
+
requestHeaderTypes.length > 0 ? `.requestHeadersOf<${requestHeaderTypes.join("|")}>()` : "",
|
|
258657
|
+
responseHeaderTypes.length > 0 ? `.responseHeadersOf<${responseHeaderTypes.join("|")}>()` : ""
|
|
258610
258658
|
];
|
|
258611
258659
|
return `export const ${id} = API.endpoint()
|
|
258612
258660
|
${endpointParts.filter(Boolean).map((part) => ` ${part}`).join("\n")}
|
|
@@ -258618,13 +258666,13 @@ ${endpointParts.filter(Boolean).map((part) => ` ${part}`).join("\n")}
|
|
|
258618
258666
|
});
|
|
258619
258667
|
}).join("\n\n")}
|
|
258620
258668
|
|
|
258621
|
-
export default API
|
|
258622
|
-
`;
|
|
258669
|
+
export default API;`;
|
|
258623
258670
|
const types = astToString(ast);
|
|
258671
|
+
const extraTypesSource = Object.keys(extraTypes).sort().map((key) => `export type ${key} = ${extraTypes[key]};`).join("\n");
|
|
258624
258672
|
return `// Type Defs
|
|
258625
258673
|
|
|
258626
258674
|
${types}
|
|
258627
|
-
${
|
|
258675
|
+
${extraTypesSource}
|
|
258628
258676
|
|
|
258629
258677
|
//API Def
|
|
258630
258678
|
|
|
@@ -258636,10 +258684,17 @@ program.name("api-def");
|
|
|
258636
258684
|
var packageJson = JSON.parse(fs2.readFileSync("package.json", "utf-8"));
|
|
258637
258685
|
program.version(packageJson.version);
|
|
258638
258686
|
program.command("generate").argument("<inPath>", "Path to the OpenAPI spec").argument("<outPath>", "Path to the output file").description("Generate an api-def from an OpenAPI spec").action(async (inPath, outPath) => {
|
|
258687
|
+
const resolvedInPath = path.resolve(inPath);
|
|
258688
|
+
if (!fs2.existsSync(resolvedInPath)) {
|
|
258689
|
+
console.error(`File not found: ${resolvedInPath}`);
|
|
258690
|
+
process.exit(1);
|
|
258691
|
+
}
|
|
258639
258692
|
const output = await openApiToSourceCode({
|
|
258640
|
-
openApiPath:
|
|
258693
|
+
openApiPath: resolvedInPath
|
|
258641
258694
|
});
|
|
258642
|
-
fs2.writeFileSync(path.resolve(outPath),
|
|
258695
|
+
fs2.writeFileSync(path.resolve(outPath), `// Generated by 'api-def' version ${packageJson.version}
|
|
258696
|
+
${output}`);
|
|
258697
|
+
console.log(`Generated api-def at ${outPath}`);
|
|
258643
258698
|
});
|
|
258644
258699
|
program.parse(process.argv);
|
|
258645
258700
|
/*! Bundled license information:
|
package/cjs/Api.js
CHANGED
|
@@ -75,14 +75,14 @@ var HotRequestHost = /** @class */ (function () {
|
|
|
75
75
|
configurable: true
|
|
76
76
|
});
|
|
77
77
|
HotRequestHost.prototype.computeConfig = function (config) {
|
|
78
|
-
var apiDefaults = this.api.
|
|
78
|
+
var apiDefaults = this.api.computeRequestConfig();
|
|
79
79
|
return (0, RequestConfig_1.processRequestConfigs)([apiDefaults, config]);
|
|
80
80
|
};
|
|
81
81
|
HotRequestHost.prototype.computePath = function (path, config) {
|
|
82
82
|
return path.startsWith("/") ? path : "/".concat(path);
|
|
83
83
|
};
|
|
84
84
|
HotRequestHost.prototype.getRequestBackend = function () {
|
|
85
|
-
return this.api.
|
|
85
|
+
return this.api.requestBackend;
|
|
86
86
|
};
|
|
87
87
|
return HotRequestHost;
|
|
88
88
|
}());
|
package/cjs/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/cjs/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 EndpointOptions<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string> {
|
|
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,18 +32,18 @@ export interface EndpointOptions<TResult, TParams extends Params | undefined, TQ
|
|
|
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 type EndpointInfo<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string> = EndpointOptions<TResult, TParams, TQuery, TBody, TState, TPath> & Required<Pick<EndpointOptions<TResult, TParams, TQuery, TBody, TState, TPath>, "name" | "validation">>;
|
|
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
36
|
/**
|
|
37
37
|
* @deprecated Use `EndpointInfo` instead
|
|
38
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> = 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> implements EndpointInfo<TResponse, TParams, TQuery, TBody, TState>, RequestHost {
|
|
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 {
|
|
41
41
|
readonly api: Api;
|
|
42
42
|
private readonly info;
|
|
43
|
-
constructor(api: Api, options: EndpointOptions<TResponse, TParams, TQuery, TBody, TState>);
|
|
43
|
+
constructor(api: Api, options: EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>);
|
|
44
44
|
get id(): string;
|
|
45
45
|
get method(): RequestMethod;
|
|
46
|
-
get path():
|
|
46
|
+
get path(): TPath;
|
|
47
47
|
get name(): string;
|
|
48
48
|
get description(): string | undefined;
|
|
49
49
|
get config(): BaseRequestConfig;
|
|
@@ -56,7 +56,7 @@ export default class Endpoint<TResponse = any, TParams extends Params | undefine
|
|
|
56
56
|
/**
|
|
57
57
|
* @deprecated Use `computeRequestConfig` instead
|
|
58
58
|
*/
|
|
59
|
-
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>;
|
|
60
|
-
computeRequestConfig<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>;
|
|
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>;
|
|
61
61
|
getRequestBackend(): RequestBackend;
|
|
62
62
|
}
|
package/cjs/Endpoint.js
CHANGED
|
@@ -143,10 +143,11 @@ var Endpoint = /** @class */ (function () {
|
|
|
143
143
|
var keys = Object.keys(request.params);
|
|
144
144
|
for (var i = 0; i < keys.length; i++) {
|
|
145
145
|
var argName = keys[i];
|
|
146
|
-
|
|
146
|
+
var argValue = request.params[argName];
|
|
147
|
+
computedPath = computedPath.replace(":".concat(argName), argValue).replace("{".concat(argName, "}"), argValue);
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
|
-
if (computedPath.includes(":")) {
|
|
150
|
+
if (computedPath.includes(":") || computedPath.includes("{")) {
|
|
150
151
|
throw new Error("[api-def] Not all path params have been resolved: '".concat(computedPath, "'"));
|
|
151
152
|
}
|
|
152
153
|
return computedPath;
|
package/cjs/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>;
|
|
15
17
|
}
|
package/cjs/EndpointBuilder.js
CHANGED
|
@@ -40,8 +40,14 @@ var EndpointBuilder = /** @class */ (function () {
|
|
|
40
40
|
this.validation.state = schema;
|
|
41
41
|
return this;
|
|
42
42
|
};
|
|
43
|
-
EndpointBuilder.prototype.
|
|
44
|
-
|
|
43
|
+
EndpointBuilder.prototype.requestHeadersOf = function () {
|
|
44
|
+
return this;
|
|
45
|
+
};
|
|
46
|
+
EndpointBuilder.prototype.responseHeadersOf = function () {
|
|
47
|
+
return this;
|
|
48
|
+
};
|
|
49
|
+
EndpointBuilder.prototype.build = function (options) {
|
|
50
|
+
var endpoint = new Endpoint_1.default(this.api, __assign(__assign({}, options), { validation: this.validation }));
|
|
45
51
|
this.api.endpoints[endpoint.id] = endpoint;
|
|
46
52
|
return endpoint;
|
|
47
53
|
};
|
package/cjs/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 processRequestConfigs: <TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State>(configs: (RequestConfig<TParams, TQuery, TBody, TState> | BaseRequestConfig | undefined)[]) => ComputedRequestConfig<TParams, TQuery, TBody, TState>;
|
|
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/Api.js
CHANGED
|
@@ -38,14 +38,14 @@ class HotRequestHost {
|
|
|
38
38
|
return this.api.baseUrl;
|
|
39
39
|
}
|
|
40
40
|
computeConfig(config) {
|
|
41
|
-
const apiDefaults = this.api.
|
|
41
|
+
const apiDefaults = this.api.computeRequestConfig();
|
|
42
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 {
|
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 EndpointOptions<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string> {
|
|
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,18 +32,18 @@ export interface EndpointOptions<TResult, TParams extends Params | undefined, TQ
|
|
|
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 type EndpointInfo<TResult, TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State = State, TPath extends string = string> = EndpointOptions<TResult, TParams, TQuery, TBody, TState, TPath> & Required<Pick<EndpointOptions<TResult, TParams, TQuery, TBody, TState, TPath>, "name" | "validation">>;
|
|
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
36
|
/**
|
|
37
37
|
* @deprecated Use `EndpointInfo` instead
|
|
38
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> = 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> implements EndpointInfo<TResponse, TParams, TQuery, TBody, TState>, RequestHost {
|
|
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 {
|
|
41
41
|
readonly api: Api;
|
|
42
42
|
private readonly info;
|
|
43
|
-
constructor(api: Api, options: EndpointOptions<TResponse, TParams, TQuery, TBody, TState>);
|
|
43
|
+
constructor(api: Api, options: EndpointOptions<TResponse, TParams, TQuery, TBody, TState, TPath, TRequestHeaders, TResponseHeaders>);
|
|
44
44
|
get id(): string;
|
|
45
45
|
get method(): RequestMethod;
|
|
46
|
-
get path():
|
|
46
|
+
get path(): TPath;
|
|
47
47
|
get name(): string;
|
|
48
48
|
get description(): string | undefined;
|
|
49
49
|
get config(): BaseRequestConfig;
|
|
@@ -56,7 +56,7 @@ export default class Endpoint<TResponse = any, TParams extends Params | undefine
|
|
|
56
56
|
/**
|
|
57
57
|
* @deprecated Use `computeRequestConfig` instead
|
|
58
58
|
*/
|
|
59
|
-
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>;
|
|
60
|
-
computeRequestConfig<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>;
|
|
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>;
|
|
61
61
|
getRequestBackend(): RequestBackend;
|
|
62
62
|
}
|
package/esm/Endpoint.js
CHANGED
|
@@ -64,10 +64,11 @@ export default class Endpoint {
|
|
|
64
64
|
const keys = Object.keys(request.params);
|
|
65
65
|
for (let i = 0; i < keys.length; i++) {
|
|
66
66
|
const argName = keys[i];
|
|
67
|
-
|
|
67
|
+
const argValue = request.params[argName];
|
|
68
|
+
computedPath = computedPath.replace(`:${argName}`, argValue).replace(`{${argName}}`, argValue);
|
|
68
69
|
}
|
|
69
70
|
}
|
|
70
|
-
if (computedPath.includes(":")) {
|
|
71
|
+
if (computedPath.includes(":") || computedPath.includes("{")) {
|
|
71
72
|
throw new Error(`[api-def] Not all path params have been resolved: '${computedPath}'`);
|
|
72
73
|
}
|
|
73
74
|
return computedPath;
|
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>;
|
|
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 processRequestConfigs: <TParams extends Params | undefined, TQuery extends Query | undefined, TBody extends Body | undefined, TState extends State>(configs: (RequestConfig<TParams, TQuery, TBody, TState> | BaseRequestConfig | undefined)[]) => ComputedRequestConfig<TParams, TQuery, TBody, TState>;
|
|
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/package.json
CHANGED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
45
|
-
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);
|
|
46
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
47
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
48
|
-
function step(op) {
|
|
49
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
50
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
51
|
-
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;
|
|
52
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
53
|
-
switch (op[0]) {
|
|
54
|
-
case 0: case 1: t = op; break;
|
|
55
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
56
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
57
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
58
|
-
default:
|
|
59
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
60
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
61
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
62
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
63
|
-
if (t[2]) _.ops.pop();
|
|
64
|
-
_.trys.pop(); continue;
|
|
65
|
-
}
|
|
66
|
-
op = body.call(thisArg, _);
|
|
67
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
68
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
72
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
73
|
-
};
|
|
74
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
75
|
-
exports.openApiToSourceCode = void 0;
|
|
76
|
-
var fs = __importStar(require("node:fs"));
|
|
77
|
-
var openapi_core_1 = require("@redocly/openapi-core");
|
|
78
|
-
// @ts-ignore
|
|
79
|
-
var chalk_1 = __importDefault(require("chalk"));
|
|
80
|
-
var lodash_1 = require("lodash");
|
|
81
|
-
var openapi_typescript_1 = __importStar(require("openapi-typescript"));
|
|
82
|
-
var METHOD_COLORS = {
|
|
83
|
-
get: chalk_1.default.green,
|
|
84
|
-
post: chalk_1.default.blue,
|
|
85
|
-
put: chalk_1.default.yellow,
|
|
86
|
-
delete: chalk_1.default.red,
|
|
87
|
-
patch: chalk_1.default.magenta,
|
|
88
|
-
};
|
|
89
|
-
var openApiToSourceCode = function (options) { return __awaiter(void 0, void 0, void 0, function () {
|
|
90
|
-
var openApiPath, inContents, ast, bundleResults, _a, routes, server, extraTypes, source, types;
|
|
91
|
-
var _b;
|
|
92
|
-
return __generator(this, function (_c) {
|
|
93
|
-
switch (_c.label) {
|
|
94
|
-
case 0:
|
|
95
|
-
openApiPath = options.openApiPath;
|
|
96
|
-
inContents = fs.readFileSync(openApiPath, "utf-8");
|
|
97
|
-
return [4 /*yield*/, (0, openapi_typescript_1.default)(inContents, {
|
|
98
|
-
rootTypes: true,
|
|
99
|
-
})];
|
|
100
|
-
case 1:
|
|
101
|
-
ast = _c.sent();
|
|
102
|
-
_a = openapi_core_1.bundle;
|
|
103
|
-
_b = {
|
|
104
|
-
ref: openApiPath
|
|
105
|
-
};
|
|
106
|
-
return [4 /*yield*/, (0, openapi_core_1.createConfig)({})];
|
|
107
|
-
case 2: return [4 /*yield*/, _a.apply(void 0, [(_b.config = _c.sent(),
|
|
108
|
-
_b)])];
|
|
109
|
-
case 3:
|
|
110
|
-
bundleResults = _c.sent();
|
|
111
|
-
routes = bundleResults.bundle.parsed.paths;
|
|
112
|
-
server = bundleResults.bundle.parsed.servers[0];
|
|
113
|
-
extraTypes = "";
|
|
114
|
-
source = "import { Api } from \"api-def\";\n\nconst API = new Api({\n name: \"".concat(bundleResults.bundle.parsed.info.title || "Generate Api", "\",\n baseUrl: \"").concat(server.url, "\",\n});\n\n").concat(Object.entries(routes)
|
|
115
|
-
.flatMap(function (_a) {
|
|
116
|
-
var path = _a[0], route = _a[1];
|
|
117
|
-
return Object.entries(route).map(function (_a) {
|
|
118
|
-
var _b, _c;
|
|
119
|
-
var method = _a[0], methodDef = _a[1];
|
|
120
|
-
var id = methodDef.operationId;
|
|
121
|
-
var responseStatuses = Object.keys(methodDef.responses);
|
|
122
|
-
var successfulResponse = responseStatuses.filter(function (status) { return status.startsWith("2") || status.startsWith("3"); });
|
|
123
|
-
var responseTypes = [];
|
|
124
|
-
for (var _i = 0, successfulResponse_1 = successfulResponse; _i < successfulResponse_1.length; _i++) {
|
|
125
|
-
var status_1 = successfulResponse_1[_i];
|
|
126
|
-
var response = methodDef.responses[status_1];
|
|
127
|
-
if (response.$ref) {
|
|
128
|
-
responseTypes.push("Response".concat(response.$ref.split("/").pop()));
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
var bodyTypes = [];
|
|
132
|
-
if ((_b = methodDef.requestBody) === null || _b === void 0 ? void 0 : _b.$ref) {
|
|
133
|
-
bodyTypes.push("RequestBody".concat(methodDef.requestBody.$ref.split("/").pop()));
|
|
134
|
-
}
|
|
135
|
-
var queryTypes = [];
|
|
136
|
-
if ((_c = methodDef.parameters) === null || _c === void 0 ? void 0 : _c.length) {
|
|
137
|
-
var anyQueryParams = methodDef.parameters.some(function (param) {
|
|
138
|
-
if (param.in === "query") {
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
if (param.$ref) {
|
|
142
|
-
var ref = param.$ref;
|
|
143
|
-
var paramDef = bundleResults.bundle.parsed.components.parameters[ref.split("/").pop()];
|
|
144
|
-
return paramDef.in === "query";
|
|
145
|
-
}
|
|
146
|
-
return false;
|
|
147
|
-
});
|
|
148
|
-
if (anyQueryParams) {
|
|
149
|
-
extraTypes += "export type Query".concat((0, lodash_1.upperFirst)(id), " = operations[\"").concat(id, "\"][\"parameters\"][\"query\"];\n");
|
|
150
|
-
queryTypes.push("Query".concat((0, lodash_1.upperFirst)(id)));
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
/*
|
|
154
|
-
const methodColor = METHOD_COLORS[method] || chalk.gray;
|
|
155
|
-
console.log(`Generating ${methodColor(method.toUpperCase())} '${id}'`);
|
|
156
|
-
*/
|
|
157
|
-
var endpointParts = [
|
|
158
|
-
responseTypes.length > 0 ? ".response<".concat(responseTypes.join("|"), ">()") : "",
|
|
159
|
-
bodyTypes.length > 0 ? ".body<".concat(bodyTypes.join("|"), ">()") : "",
|
|
160
|
-
queryTypes.length > 0 ? ".query<".concat(queryTypes.join("|"), ">()") : "",
|
|
161
|
-
];
|
|
162
|
-
return "export const ".concat(id, " = API.endpoint()\n").concat(endpointParts
|
|
163
|
-
.filter(Boolean)
|
|
164
|
-
.map(function (part) { return " ".concat(part); })
|
|
165
|
-
.join("\n"), "\n .build({\n method: \"").concat(method, "\",\n path: \"").concat(path, "\",\n id: \"").concat(id, "\",\n });");
|
|
166
|
-
});
|
|
167
|
-
})
|
|
168
|
-
.join("\n\n"), "\n\nexport default API;\n ");
|
|
169
|
-
types = (0, openapi_typescript_1.astToString)(ast);
|
|
170
|
-
return [2 /*return*/, "// Type Defs\n\n".concat(types, "\n").concat(extraTypes, "\n\n//API Def\n\n").concat(source)];
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
|
-
}); };
|
|
174
|
-
exports.openApiToSourceCode = openApiToSourceCode;
|