Skip to content

Commit e1c6817

Browse files
committed
Sorting parse fix
1 parent 8aabe65 commit e1c6817

11 files changed

Lines changed: 93 additions & 34 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@povio/openapi-codegen-cli",
3-
"version": "0.10.6",
3+
"version": "0.10.7",
44
"main": "./dist/index.js",
55
"bin": {
66
"openapi-codegen": "./dist/sh.js"

src/assets/zodExtended.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/commands/generate.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export async function generate({ input, excludeTags, monorepo, prettier, verbose
4949
input,
5050
excludeTags: excludeTags.split(","),
5151
restClientImportPath: TEMPLATE_IMPORTS.appRestClient[template],
52+
errorHandlingImportPath: TEMPLATE_IMPORTS.errorHandling[template],
5253
queryTypesImportPath: TEMPLATE_IMPORTS.queryTypes[template],
5354
...params,
5455
});

src/generators/const/deps.const.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GenerateFile } from "../types/generate";
1+
import { GenerateFile, Import } from "../types/generate";
22

33
export const APP_REST_CLIENT_NAME = "AppRestClient";
44

@@ -21,6 +21,19 @@ export const TEMPLATE_IMPORTS: Record<string, { template: string; monorepoTempla
2121
template: "@/types/react-query",
2222
monorepoTemplate: "@povio/utils/types/react-query",
2323
},
24+
errorHandling: {
25+
template: "@/util/vendor/error-handling",
26+
monorepoTemplate: "@povio/utils/vendor/error-handling",
27+
},
28+
};
29+
30+
export const ERROR_HANDLERS = {
31+
ErrorHandler: "ErrorHandler",
32+
SharedErrorHandler: "SharedErrorHandler",
33+
};
34+
export const ERROR_HANDLING_IMPORT: Import = {
35+
bindings: [ERROR_HANDLERS.ErrorHandler, ERROR_HANDLERS.SharedErrorHandler],
36+
from: "@/util/vendor/error-handling",
2437
};
2538

2639
// Standalone
@@ -48,9 +61,10 @@ export const INVALIDATE_QUERY_OPTIONS_FILE: GenerateFile = { fileName: "invalida
4861

4962
// ZodExtended
5063
export const ZOD_EXTENDED = {
51-
name: "zodExtended",
64+
name: "zod",
5265
properties: {
5366
sortingString: "sortingString",
67+
parse: "parse",
5468
},
5569
};
56-
export const ZOD_EXTENDED_FILE: GenerateFile = { fileName: "zodExtended", extension: "ts" };
70+
export const ZOD_EXTENDED_FILE: GenerateFile = { fileName: "zod", extension: "ts" };

src/generators/const/options.const.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const DEFAULT_GENERATE_OPTIONS: GenerateOptions = {
4242
replaceOptionalWithNullish: false,
4343
// Endpoints options
4444
restClientImportPath: TEMPLATE_IMPORTS.appRestClient.template,
45+
errorHandlingImportPath: TEMPLATE_IMPORTS.errorHandling.template,
4546
removeOperationPrefixEndingWith: "Controller_",
4647
// Queries options
4748
queryTypesImportPath: TEMPLATE_IMPORTS.queryTypes.template,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { ERROR_HANDLERS, ERROR_HANDLING_IMPORT, ZOD_EXTENDED } from "../const/deps.const";
2+
import { ZOD_IMPORT } from "../const/zod.const";
3+
import { SchemaResolver } from "../core/SchemaResolver.class";
4+
import { getHbsTemplateDelegate } from "../utils/hbs/hbs-template.utils";
5+
6+
export function generateZod(resolver: SchemaResolver) {
7+
const hbsTemplate = getHbsTemplateDelegate(resolver, "zod");
8+
9+
return hbsTemplate({
10+
zodImport: ZOD_IMPORT,
11+
zodExtension: ZOD_EXTENDED.name,
12+
sortingString: ZOD_EXTENDED.properties.sortingString,
13+
parse: ZOD_EXTENDED.properties.parse,
14+
errorHandler: ERROR_HANDLERS.ErrorHandler,
15+
sharedErrorHandler: ERROR_HANDLERS.SharedErrorHandler,
16+
errorHandlingImport: {
17+
...ERROR_HANDLING_IMPORT,
18+
from: resolver.options.errorHandlingImportPath,
19+
},
20+
});
21+
}

src/generators/generateCodeFromOpenAPIDoc.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { generateEndpoints } from "./generate/generateEndpoints";
1515
import { generateInvalidateQueries } from "./generate/generateInvalidateQueries";
1616
import { generateModels } from "./generate/generateModels";
1717
import { generateQueries } from "./generate/generateQueries";
18+
import { generateZod } from "./generate/generateZod";
1819
import { GenerateData, GenerateFileData, GenerateType, GenerateTypeParams } from "./types/generate";
1920
import { GenerateOptions } from "./types/options";
2021
import { getOutputFileName, readAssetSync } from "./utils/file.utils";
@@ -76,11 +77,14 @@ export function generateCodeFromOpenAPIDoc(openApiDoc: OpenAPIV3.Document, cliOp
7677
}
7778

7879
if (hasZodExtendedFile(data)) {
79-
const fileName = getFileNameWithExtension(ZOD_EXTENDED_FILE);
80-
generateFilesData.push({
81-
content: readAssetSync(fileName),
82-
fileName: getOutputFileName({ output: resolver.options.output, fileName }),
83-
});
80+
const zodContent = generateZod(resolver);
81+
if (zodContent) {
82+
const fileName = getOutputFileName({
83+
output: options.output,
84+
fileName: getFileNameWithExtension(ZOD_EXTENDED_FILE),
85+
});
86+
generateFilesData.push({ fileName, content: zodContent });
87+
}
8488
}
8589

8690
return generateFilesData;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{zodExtended}}.{{sortingString}}({{importedZodSchemaName enumSchemaName}}).parse({{paramName}})
1+
{{zodExtended}}.{{parse}}({{zodExtended}}.{{sortingString}}({{importedZodSchemaName enumSchemaName}}), {{paramName}})

src/generators/templates/zod.hbs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* eslint-disable no-useless-escape */
2+
{{! Zod import }}
3+
{{{genImport zodImport}}}
4+
{{! Error handling import }}
5+
{{{genImport errorHandlingImport}}}
6+
7+
export const {{zodExtension}} = {
8+
{{sortingString}}: (enumSchema: z.ZodEnum<[string, ...string[]]>) =>
9+
z
10+
.string()
11+
.optional()
12+
.superRefine((arg, ctx) => {
13+
if (!isSortingStringValid(enumSchema, arg)) {
14+
ctx.addIssue({
15+
code: z.ZodIssueCode.custom,
16+
message: "Invalid sorting string.",
17+
});
18+
}
19+
}),
20+
{{parse}},
21+
};
22+
23+
function isSortingStringValid(enumSchema: z.ZodEnum<[string, ...string[]]>, data?: string) {
24+
if (data === undefined || data === "" || enumSchema.options.length === 0) {
25+
return true;
26+
}
27+
28+
const prefixedEnumOptions = `([+-]?(${enumSchema.options.join("|")}))`;
29+
const commaSeparatedOptions = `(${prefixedEnumOptions})(\s*,\s*${prefixedEnumOptions})*`;
30+
return new RegExp(`^${commaSeparatedOptions}$`).test(data);
31+
}
32+
33+
function {{parse}}(schema: z.ZodSchema, data: unknown, errorHandler?: {{errorHandler}}<never>) {
34+
try {
35+
return schema.parse(data);
36+
} catch (e: unknown) {
37+
(errorHandler ?? {{sharedErrorHandler}}).rethrowError(e);
38+
throw e;
39+
}
40+
}

src/generators/types/options.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface ZodGenerateOptions {
1414

1515
interface EndpointsGenerateOptions {
1616
restClientImportPath: string;
17+
errorHandlingImportPath: string;
1718
withDeprecatedEndpoints?: boolean;
1819
removeOperationPrefixEndingWith?: string;
1920
}

0 commit comments

Comments
 (0)