@povio/openapi-codegen-cli 0.16.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/dist/commands/check.d.ts +1 -1
- package/dist/commands/generate.d.ts +2 -2
- package/dist/generators/const/deps.const.d.ts +2 -3
- package/dist/generators/const/zod.const.d.ts +6 -0
- package/dist/generators/core/resolveConfig.d.ts +7 -0
- package/dist/generators/generate/generateZodExtended.d.ts +2 -0
- package/dist/generators/types/generate.d.ts +0 -1
- package/dist/generators/types/options.d.ts +7 -6
- package/dist/generators/utils/generate/generate.imports.utils.d.ts +1 -1
- package/dist/generators/utils/generate/generate.utils.d.ts +1 -1
- package/dist/generators/utils/generate-files.utils.d.ts +1 -1
- package/dist/index.js +46 -46
- package/dist/sh.js +159 -159
- package/package.json +3 -3
- package/src/assets/rest-client.ts +31 -29
- package/src/generators/templates/endpoints.hbs +4 -4
- package/src/generators/templates/models.hbs +1 -5
- package/src/generators/templates/partials/endpoint-param-parse.hbs +1 -1
- package/src/generators/templates/queries.hbs +4 -5
- package/src/generators/templates/zod-extended.hbs +49 -0
- package/dist/generators/const/brands.const.d.ts +0 -8
- package/dist/generators/generate/generateZodUtils.d.ts +0 -2
- package/dist/generators/utils/brand.utils.d.ts +0 -7
- package/src/generators/templates/zod-utils.hbs +0 -70
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@povio/openapi-codegen-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"bin": {
|
|
6
6
|
"openapi-codegen": "./dist/sh.js"
|
|
@@ -49,8 +49,8 @@
|
|
|
49
49
|
"@types/prettier": "^3.0.0",
|
|
50
50
|
"@types/prompt-sync": "^4.2.3",
|
|
51
51
|
"@types/yargs": "^17.0.32",
|
|
52
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
53
|
-
"@typescript-eslint/parser": "^
|
|
52
|
+
"@typescript-eslint/eslint-plugin": "^8.40.0",
|
|
53
|
+
"@typescript-eslint/parser": "^8.40.0",
|
|
54
54
|
"chalk": "^5.3.0",
|
|
55
55
|
"esbuild": "^0.21.3",
|
|
56
56
|
"eslint": "^8.57.0",
|
|
@@ -2,14 +2,16 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, CreateAxiosDef
|
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
import { RestInterceptor } from "./rest-interceptor";
|
|
4
4
|
|
|
5
|
-
interface RequestInfo<
|
|
6
|
-
resSchema: z.
|
|
5
|
+
interface RequestInfo<ZOutput> {
|
|
6
|
+
resSchema: z.ZodType<ZOutput>;
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
interface RequestConfig<
|
|
10
|
-
rawResponse?:
|
|
9
|
+
interface RequestConfig<IsRawRes extends boolean = false> {
|
|
10
|
+
rawResponse?: IsRawRes;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
type Response<ZOutput, IsRawRes extends boolean = false> = IsRawRes extends true ? AxiosResponse<ZOutput> : ZOutput;
|
|
14
|
+
|
|
13
15
|
export class RestClient {
|
|
14
16
|
private client: AxiosInstance;
|
|
15
17
|
|
|
@@ -38,60 +40,60 @@ export class RestClient {
|
|
|
38
40
|
interceptor.removeInterceptor(this.client);
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
public async get<
|
|
42
|
-
requestInfo: RequestInfo<
|
|
43
|
+
public async get<ZOutput, IsRawRes extends boolean = false>(
|
|
44
|
+
requestInfo: RequestInfo<ZOutput>,
|
|
43
45
|
url: string,
|
|
44
|
-
requestConfig?: AxiosRequestConfig & RequestConfig<
|
|
45
|
-
): Promise<
|
|
46
|
+
requestConfig?: AxiosRequestConfig & RequestConfig<IsRawRes>,
|
|
47
|
+
): Promise<Response<ZOutput, IsRawRes>> {
|
|
46
48
|
return this.makeRequest(requestInfo, { ...requestConfig, method: "get", url });
|
|
47
49
|
}
|
|
48
50
|
|
|
49
|
-
public async post<
|
|
50
|
-
requestInfo: RequestInfo<
|
|
51
|
+
public async post<ZOutput, IsRawRes extends boolean = false>(
|
|
52
|
+
requestInfo: RequestInfo<ZOutput>,
|
|
51
53
|
url: string,
|
|
52
54
|
data?: any,
|
|
53
|
-
requestConfig?: AxiosRequestConfig & RequestConfig<
|
|
54
|
-
): Promise<
|
|
55
|
+
requestConfig?: AxiosRequestConfig & RequestConfig<IsRawRes>,
|
|
56
|
+
): Promise<Response<ZOutput, IsRawRes>> {
|
|
55
57
|
return this.makeRequest(requestInfo, { ...requestConfig, method: "post", url, data });
|
|
56
58
|
}
|
|
57
59
|
|
|
58
|
-
public async patch<
|
|
59
|
-
requestInfo: RequestInfo<
|
|
60
|
+
public async patch<ZOutput, IsRawRes extends boolean = false>(
|
|
61
|
+
requestInfo: RequestInfo<ZOutput>,
|
|
60
62
|
url: string,
|
|
61
63
|
data?: any,
|
|
62
|
-
requestConfig?: AxiosRequestConfig & RequestConfig<
|
|
63
|
-
): Promise<
|
|
64
|
+
requestConfig?: AxiosRequestConfig & RequestConfig<IsRawRes>,
|
|
65
|
+
): Promise<Response<ZOutput, IsRawRes>> {
|
|
64
66
|
return this.makeRequest(requestInfo, { ...requestConfig, method: "patch", url, data });
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
public async put<
|
|
68
|
-
requestInfo: RequestInfo<
|
|
69
|
+
public async put<ZOutput, IsRawRes extends boolean = false>(
|
|
70
|
+
requestInfo: RequestInfo<ZOutput>,
|
|
69
71
|
url: string,
|
|
70
72
|
data?: any,
|
|
71
|
-
requestConfig?: AxiosRequestConfig & RequestConfig<
|
|
72
|
-
): Promise<
|
|
73
|
+
requestConfig?: AxiosRequestConfig & RequestConfig<IsRawRes>,
|
|
74
|
+
): Promise<Response<ZOutput, IsRawRes>> {
|
|
73
75
|
return this.makeRequest(requestInfo, { ...requestConfig, method: "put", url, data });
|
|
74
76
|
}
|
|
75
77
|
|
|
76
|
-
public async delete<
|
|
77
|
-
requestInfo: RequestInfo<
|
|
78
|
+
public async delete<ZOutput, IsRawRes extends boolean = false>(
|
|
79
|
+
requestInfo: RequestInfo<ZOutput>,
|
|
78
80
|
url: string,
|
|
79
81
|
data?: any,
|
|
80
|
-
requestConfig?: AxiosRequestConfig & RequestConfig<
|
|
81
|
-
): Promise<
|
|
82
|
+
requestConfig?: AxiosRequestConfig & RequestConfig<IsRawRes>,
|
|
83
|
+
): Promise<Response<ZOutput, IsRawRes>> {
|
|
82
84
|
return this.makeRequest(requestInfo, { ...requestConfig, method: "delete", url, data });
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
private async makeRequest<
|
|
86
|
-
requestInfo: RequestInfo<
|
|
87
|
-
requestConfig: AxiosRequestConfig & RequestConfig<
|
|
88
|
-
): Promise<
|
|
87
|
+
private async makeRequest<ZOutput, IsRawRes extends boolean = false>(
|
|
88
|
+
requestInfo: RequestInfo<ZOutput>,
|
|
89
|
+
requestConfig: AxiosRequestConfig & RequestConfig<IsRawRes>,
|
|
90
|
+
): Promise<Response<ZOutput, IsRawRes>> {
|
|
89
91
|
const { rawResponse, ...config } = requestConfig;
|
|
90
92
|
|
|
91
93
|
const res = await this.client(config);
|
|
92
94
|
|
|
93
95
|
const resData = requestInfo.resSchema.parse(res.data);
|
|
94
96
|
|
|
95
|
-
return (rawResponse ? { ...res, data: resData } : resData) as
|
|
97
|
+
return (rawResponse ? { ...res, data: resData } : resData) as Response<ZOutput, IsRawRes>;
|
|
96
98
|
}
|
|
97
99
|
}
|
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
{{#if hasZodImport}}
|
|
9
9
|
{{{genImport zodImport}}}
|
|
10
10
|
{{/if}}
|
|
11
|
-
{{! Zod
|
|
12
|
-
{{#if
|
|
13
|
-
{{{genImport
|
|
11
|
+
{{! Zod extended import }}
|
|
12
|
+
{{#if hasZodExtendedImport}}
|
|
13
|
+
{{{genImport zodExtendedImport}}}
|
|
14
14
|
{{/if}}
|
|
15
15
|
{{! Models import }}
|
|
16
16
|
{{#each modelsImports as | modelsImport |}}
|
|
@@ -41,4 +41,4 @@ export const {{endpointName endpoint}} = ({{{genEndpointParams endpoint}}}{{#if
|
|
|
41
41
|
{{/each}}
|
|
42
42
|
{{#if includeNamespace}}
|
|
43
43
|
}
|
|
44
|
-
{{/if}}
|
|
44
|
+
{{/if}}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
{{! Zod import}}
|
|
2
2
|
{{{genImport zodImport}}}
|
|
3
|
-
{{! Zod utils import }}
|
|
4
|
-
{{#if hasZodUtilsImport}}
|
|
5
|
-
{{{genImport zodUtilsImport}}}
|
|
6
|
-
{{/if}}
|
|
7
3
|
{{! Models import }}
|
|
8
4
|
{{#each modelsImports as | modelsImport |}}
|
|
9
5
|
{{{genImport modelsImport}}}
|
|
@@ -25,4 +21,4 @@ export type {{zodInferedType @key}} = z.infer<typeof {{@key}}>;
|
|
|
25
21
|
{{/each}}
|
|
26
22
|
{{#if includeNamespace}}
|
|
27
23
|
}
|
|
28
|
-
{{/if}}
|
|
24
|
+
{{/if}}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{{
|
|
1
|
+
{{zodExtendedNamespace}}.{{parse}}({{#if param.parameterSortingEnumSchemaName}}{{zodExtendedNamespace}}.{{sortExp}}({{{importedZodSchemaName param.parameterSortingEnumSchemaName}}}){{#if addOptional}}.optional(){{/if}}{{else}}{{{importedZodSchemaName param.zodSchema}}}{{#if addOptional}}.optional(){{/if}}{{/if}}, {{paramName}}{{#if isQuery}}, { type: "query", name: "{{paramName}}" }{{/if}})
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
{{! Acl check import }}
|
|
16
16
|
{{#if hasAclCheck}}
|
|
17
17
|
{{{genImport aclCheckImport}}}
|
|
18
|
+
{{#each aclImports as | aclImport |}}
|
|
19
|
+
{{{genImport aclImport}}}
|
|
20
|
+
{{/each}}
|
|
18
21
|
{{/if}}
|
|
19
22
|
{{! React query types import }}
|
|
20
23
|
{{{genImport queryTypesImport}}}
|
|
@@ -26,10 +29,6 @@
|
|
|
26
29
|
{{#each endpointsImports as | endpointsImport |}}
|
|
27
30
|
{{{genImport endpointsImport}}}
|
|
28
31
|
{{/each}}
|
|
29
|
-
{{! Acl import }}
|
|
30
|
-
{{#each aclImports as | aclImport |}}
|
|
31
|
-
{{{genImport aclImport}}}
|
|
32
|
-
{{/each}}
|
|
33
32
|
|
|
34
33
|
{{#if includeNamespace}}
|
|
35
34
|
export namespace {{namespace}} {
|
|
@@ -52,4 +51,4 @@ export const {{queriesModuleName}} = {{#if hasMutationEffects}}{{queryModuleEnum
|
|
|
52
51
|
|
|
53
52
|
{{#if includeNamespace}}
|
|
54
53
|
}
|
|
55
|
-
{{/if}}
|
|
54
|
+
{{/if}}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{{! Zod import }}
|
|
2
|
+
{{{genImport zodImport}}}
|
|
3
|
+
{{! Error handling import }}
|
|
4
|
+
{{{genImport errorHandlingImport}}}
|
|
5
|
+
|
|
6
|
+
export namespace ZodExtended {
|
|
7
|
+
interface ParseOptions {
|
|
8
|
+
type: "body" | "query";
|
|
9
|
+
name?: string;
|
|
10
|
+
errorHandler?: ErrorHandler<never>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function {{parse}}<ZOutput, ZInput>(
|
|
14
|
+
schema: z.ZodType<ZOutput, ZInput>,
|
|
15
|
+
data: unknown,
|
|
16
|
+
{ type, name, errorHandler }: ParseOptions = { type: "body" },
|
|
17
|
+
) {
|
|
18
|
+
try {
|
|
19
|
+
return schema.parse(data);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
if (e instanceof z.ZodError) {
|
|
22
|
+
e.name = `FE Request ${type === "body" ? "body" : "query param"}${name ? ` ("${name}")` : ""} schema mismatch - ZodError`;
|
|
23
|
+
}
|
|
24
|
+
(errorHandler ?? {{sharedErrorHandler}}).rethrowError(e);
|
|
25
|
+
throw e;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function is{{capitalize sortExp}}Valid(enumSchema: z.ZodEnum, data?: string) {
|
|
30
|
+
if (data === undefined || data === "" || enumSchema.options.length === 0) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const prefixedEnumOptions = `([+-]?(${enumSchema.options.join("|")}))`;
|
|
35
|
+
const commaSeparatedOptions = `(${prefixedEnumOptions})(\s*,\s*${prefixedEnumOptions})*`;
|
|
36
|
+
return new RegExp(`^${commaSeparatedOptions}$`).test(data);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const {{sortExp}} = (enumSchema: z.ZodEnum) =>
|
|
40
|
+
z.string().superRefine((arg, ctx) => {
|
|
41
|
+
if (!is{{capitalize sortExp}}Valid(enumSchema, arg)) {
|
|
42
|
+
ctx.addIssue({
|
|
43
|
+
code: "invalid_value",
|
|
44
|
+
message: "Invalid sorting string.",
|
|
45
|
+
values: [],
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { OpenAPIV3 } from "openapi-types";
|
|
2
|
-
import { BrandEnum } from "src/generators/const/brands.const";
|
|
3
|
-
import { GenerateOptions } from "src/generators/types/options";
|
|
4
|
-
export declare function getPrimitiveBrands(): BrandEnum[];
|
|
5
|
-
export declare function getOtherBrands(): BrandEnum[];
|
|
6
|
-
export declare function matchesBrand(schema: OpenAPIV3.SchemaObject, brand: BrandEnum): boolean;
|
|
7
|
-
export declare function wrapWithBrand(schema: string, brand: BrandEnum, options: GenerateOptions): string;
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
{{! Zod import }}
|
|
2
|
-
{{{genImport zodImport}}}
|
|
3
|
-
{{! Error handling import }}
|
|
4
|
-
{{{genImport errorHandlingImport}}}
|
|
5
|
-
|
|
6
|
-
interface ParseOptions {
|
|
7
|
-
type: "body" | "query";
|
|
8
|
-
name?: string;
|
|
9
|
-
errorHandler?: ErrorHandler<never>;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function is{{capitalize sortExp}}Valid(enumSchema: z.ZodEnum<[string, ...string[]]>, data?: string) {
|
|
13
|
-
if (data === undefined || data === "" || enumSchema.options.length === 0) {
|
|
14
|
-
return true;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
const prefixedEnumOptions = `([+-]?(${enumSchema.options.join("|")}))`;
|
|
18
|
-
const commaSeparatedOptions = `(${prefixedEnumOptions})(\s*,\s*${prefixedEnumOptions})*`;
|
|
19
|
-
return new RegExp(`^${commaSeparatedOptions}$`).test(data);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export namespace {{zodUtilsNamespace}} {
|
|
23
|
-
export function {{parse}}<T>(schema: z.ZodSchema<T>, data: unknown, { type, name, errorHandler }: ParseOptions = { type: "body" }) {
|
|
24
|
-
try {
|
|
25
|
-
return schema.parse(data);
|
|
26
|
-
} catch (e) {
|
|
27
|
-
if (e instanceof z.ZodError) {
|
|
28
|
-
e.name = `FE Request ${type === "body" ? "body" : "query param"}${name ? ` ("${name}")` : ""} schema mismatch - ZodError`;
|
|
29
|
-
}
|
|
30
|
-
(errorHandler ?? {{sharedErrorHandler}}).rethrowError(e);
|
|
31
|
-
throw e;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const {{sortExp}} = (enumSchema: z.ZodEnum<[string, ...string[]]>) =>
|
|
36
|
-
z.string().superRefine((arg, ctx) => {
|
|
37
|
-
if (!is{{capitalize sortExp}}Valid(enumSchema, arg)) {
|
|
38
|
-
ctx.addIssue({
|
|
39
|
-
code: z.ZodIssueCode.custom,
|
|
40
|
-
message: "Invalid sorting string.",
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
{{#if branded}}
|
|
46
|
-
export class ZodBranded<T extends z.ZodTypeAny, B extends string> extends z.ZodType<T["_output"], z.ZodTypeDef, T["_input"]> {
|
|
47
|
-
readonly _schema: T;
|
|
48
|
-
|
|
49
|
-
readonly _brand: B;
|
|
50
|
-
|
|
51
|
-
constructor(_schema: T, _brand: B) {
|
|
52
|
-
super(_schema._def);
|
|
53
|
-
this._schema = _schema;
|
|
54
|
-
this._brand = _brand;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
_parse(input: z.ParseInput) {
|
|
58
|
-
return this._schema._parse(input);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
unwrap() {
|
|
62
|
-
return this._schema;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function {{brand}}<T extends z.ZodTypeAny, B extends string>(schema: T, _brand: B): ZodBranded<T, B> {
|
|
67
|
-
return new ZodBranded(schema, _brand);
|
|
68
|
-
}
|
|
69
|
-
{{/if}}
|
|
70
|
-
}
|