api-def 0.12.0-alpha.4 → 0.12.0-alpha.5
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 +45 -6
- package/cjs/Api.js +2 -2
- package/cjs/ApiTypes.d.ts +6 -4
- package/cjs/Endpoint.d.ts +9 -9
- 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/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
|
@@ -258573,11 +258573,21 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258573
258573
|
const id = methodDef.operationId;
|
|
258574
258574
|
const responseStatuses = Object.keys(methodDef.responses);
|
|
258575
258575
|
const successfulResponse = responseStatuses.filter((status) => status.startsWith("2") || status.startsWith("3"));
|
|
258576
|
+
let responseType = void 0;
|
|
258576
258577
|
const responseTypes = [];
|
|
258577
258578
|
for (const status of successfulResponse) {
|
|
258578
258579
|
const response = methodDef.responses[status];
|
|
258579
258580
|
if (response.$ref) {
|
|
258580
|
-
|
|
258581
|
+
const responseDef = bundleResults.bundle.parsed.components.responses[response.$ref.split("/").pop()];
|
|
258582
|
+
for (const mediaType in responseDef.content) {
|
|
258583
|
+
const schema = responseDef.content[mediaType].schema;
|
|
258584
|
+
if (!responseType) {
|
|
258585
|
+
responseType = mediaType.split(";")[0];
|
|
258586
|
+
}
|
|
258587
|
+
if (schema?.$ref) {
|
|
258588
|
+
responseTypes.push(`Schema${schema.$ref.split("/").pop()}`);
|
|
258589
|
+
}
|
|
258590
|
+
}
|
|
258581
258591
|
}
|
|
258582
258592
|
}
|
|
258583
258593
|
const bodyTypes = [];
|
|
@@ -258603,10 +258613,32 @@ ${Object.entries(routes).flatMap(([path2, route]) => {
|
|
|
258603
258613
|
queryTypes.push(`Query${(0, import_lodash.upperFirst)(id)}`);
|
|
258604
258614
|
}
|
|
258605
258615
|
}
|
|
258616
|
+
const requestHeaderTypes = [];
|
|
258617
|
+
if (methodDef.parameters?.length) {
|
|
258618
|
+
const anyHeaderParams = methodDef.parameters.some((param) => {
|
|
258619
|
+
if (param.in === "header") {
|
|
258620
|
+
return true;
|
|
258621
|
+
}
|
|
258622
|
+
if (param.$ref) {
|
|
258623
|
+
const ref = param.$ref;
|
|
258624
|
+
const paramDef = bundleResults.bundle.parsed.components.parameters[ref.split("/").pop()];
|
|
258625
|
+
return paramDef.in === "header";
|
|
258626
|
+
}
|
|
258627
|
+
return false;
|
|
258628
|
+
});
|
|
258629
|
+
if (anyHeaderParams) {
|
|
258630
|
+
extraTypes += `export type Header${(0, import_lodash.upperFirst)(id)} = operations["${id}"]["parameters"]["header"];
|
|
258631
|
+
`;
|
|
258632
|
+
requestHeaderTypes.push(`Header${(0, import_lodash.upperFirst)(id)}`);
|
|
258633
|
+
}
|
|
258634
|
+
}
|
|
258635
|
+
const responseHeaderTypes = [];
|
|
258606
258636
|
const endpointParts = [
|
|
258607
|
-
responseTypes.length > 0 ? `.
|
|
258608
|
-
bodyTypes.length > 0 ? `.
|
|
258609
|
-
queryTypes.length > 0 ? `.
|
|
258637
|
+
responseTypes.length > 0 ? `.responseOf<${responseTypes.join("|")}>()` : "",
|
|
258638
|
+
bodyTypes.length > 0 ? `.bodyOf<${bodyTypes.join("|")}>()` : "",
|
|
258639
|
+
queryTypes.length > 0 ? `.queryOf<${queryTypes.join("|")}>()` : "",
|
|
258640
|
+
requestHeaderTypes.length > 0 ? `.requestHeadersOf<${requestHeaderTypes.join("|")}>()` : "",
|
|
258641
|
+
responseHeaderTypes.length > 0 ? `.responseHeadersOf<${responseHeaderTypes.join("|")}>()` : ""
|
|
258610
258642
|
];
|
|
258611
258643
|
return `export const ${id} = API.endpoint()
|
|
258612
258644
|
${endpointParts.filter(Boolean).map((part) => ` ${part}`).join("\n")}
|
|
@@ -258636,10 +258668,17 @@ program.name("api-def");
|
|
|
258636
258668
|
var packageJson = JSON.parse(fs2.readFileSync("package.json", "utf-8"));
|
|
258637
258669
|
program.version(packageJson.version);
|
|
258638
258670
|
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) => {
|
|
258671
|
+
const resolvedInPath = path.resolve(inPath);
|
|
258672
|
+
if (!fs2.existsSync(resolvedInPath)) {
|
|
258673
|
+
console.error(`File not found: ${resolvedInPath}`);
|
|
258674
|
+
process.exit(1);
|
|
258675
|
+
}
|
|
258639
258676
|
const output = await openApiToSourceCode({
|
|
258640
|
-
openApiPath:
|
|
258677
|
+
openApiPath: resolvedInPath
|
|
258641
258678
|
});
|
|
258642
|
-
fs2.writeFileSync(path.resolve(outPath),
|
|
258679
|
+
fs2.writeFileSync(path.resolve(outPath), `// Generated by 'api-def' version ${packageJson.version}
|
|
258680
|
+
${output}`);
|
|
258681
|
+
console.log(`Generated api-def at ${outPath}`);
|
|
258643
258682
|
});
|
|
258644
258683
|
program.parse(process.argv);
|
|
258645
258684
|
/*! 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/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/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;
|