hono-takibi 0.2.1 → 0.2.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 +36 -3
- package/dist/config/index.d.ts +2 -0
- package/dist/core/helper/get-variable-name-helper.d.ts +2 -0
- package/dist/core/helper/get-variable-name-helper.js +9 -0
- package/dist/core/helper/get-variable-schema-name-helper.d.ts +2 -0
- package/dist/core/helper/get-variable-schema-name-helper.js +11 -0
- package/dist/core/validator/is-all-optional.d.ts +6 -0
- package/dist/core/validator/is-all-optional.js +11 -0
- package/dist/generators/hono/generate-zod-openapi-hono.js +2 -2
- package/dist/generators/openapi/components/generate-components-code.d.ts +2 -1
- package/dist/generators/openapi/components/generate-components-code.js +8 -9
- package/dist/generators/openapi/paths/generate-route-code.d.ts +2 -1
- package/dist/generators/openapi/paths/generate-route-code.js +4 -2
- package/dist/generators/openapi/paths/generate-route.d.ts +2 -1
- package/dist/generators/openapi/paths/generate-route.js +5 -3
- package/dist/generators/openapi/paths/generate-schemas-export.d.ts +2 -1
- package/dist/generators/openapi/paths/generate-schemas-export.js +4 -11
- package/dist/generators/request/params/generate-params-object.d.ts +2 -1
- package/dist/generators/request/params/generate-params-object.js +5 -3
- package/dist/generators/request/params/generate-request-parameter.d.ts +2 -1
- package/dist/generators/request/params/generate-request-parameter.js +3 -3
- package/dist/generators/response/schemas/generate-response-schema.d.ts +2 -1
- package/dist/generators/response/schemas/generate-response-schema.js +4 -2
- package/dist/generators/types/generate-types-code.js +0 -2
- package/dist/generators/zod/generate-partial-schema.d.ts +1 -0
- package/dist/generators/zod/generate-partial-schema.js +7 -0
- package/dist/generators/zod/generate-zod-infer.js +4 -8
- package/dist/generators/zod/generate-zod-properties-schema.d.ts +2 -1
- package/dist/generators/zod/generate-zod-properties-schema.js +10 -6
- package/dist/generators/zod/generate-zod-property-schema.d.ts +2 -1
- package/dist/generators/zod/generate-zod-property-schema.js +7 -10
- package/dist/generators/zod/generate-zod-record-schema.d.ts +2 -1
- package/dist/generators/zod/generate-zod-record-schema.js +2 -2
- package/dist/generators/zod/generate-zod-schema.d.ts +2 -1
- package/dist/generators/zod/generate-zod-schema.js +4 -4
- package/dist/index.js +3 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,7 +12,6 @@ npm add -D hono-takibi
|
|
|
12
12
|
|
|
13
13
|
You can customize the code generation behavior by creating a `hono-takibi.json` file in your project root.
|
|
14
14
|
|
|
15
|
-
|
|
16
15
|
### Schema Options
|
|
17
16
|
|
|
18
17
|
| Option | Type | Default | Description |
|
|
@@ -27,9 +26,44 @@ You can customize the code generation behavior by creating a `hono-takibi.json`
|
|
|
27
26
|
| `namingCase` | `"camelCase"` \| `"PascalCase"` | `"PascalCase"` | Naming convention for generated type definitions |
|
|
28
27
|
| `exportEnabled` | `boolean` | `false` | When true, exports all type definitions |
|
|
29
28
|
|
|
29
|
+
## Input and Output
|
|
30
|
+
|
|
31
|
+
You can specify input and output paths in two ways:
|
|
32
|
+
|
|
33
|
+
1. Command line arguments:
|
|
34
|
+
|
|
35
|
+
2. Configuration file (`hono-takibi.json`):
|
|
36
|
+
|
|
37
|
+
| Option | Type | Default | Description |
|
|
38
|
+
|--------|------|---------|-------------|
|
|
39
|
+
| `input` | `string` | `""` | Input file path |
|
|
40
|
+
| `output` | `string` | `""` | Output file path |
|
|
41
|
+
|
|
42
|
+
> **⚠️** When using a configuration file, command line arguments are not required. The configuration file settings take precedence over command line arguments.
|
|
43
|
+
>
|
|
44
|
+
> **🔥** When you have configured `hono-takibi.json`, you can simply run:
|
|
45
|
+
> ```bash
|
|
46
|
+
> npx hono-takibi
|
|
47
|
+
> ```
|
|
48
|
+
|
|
30
49
|
### Examples
|
|
31
50
|
|
|
32
|
-
|
|
51
|
+
* Default Behavior (camelCase schemas, PascalCase types)
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"input": "src/openapi/openapi.yaml",
|
|
56
|
+
"output": "src/openapi/index.ts",
|
|
57
|
+
"schemaOptions": {
|
|
58
|
+
"namingCase": "camelCase",
|
|
59
|
+
"exportEnabled": false
|
|
60
|
+
},
|
|
61
|
+
"typeOptions": {
|
|
62
|
+
"namingCase": "PascalCase",
|
|
63
|
+
"exportEnabled": false
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
33
67
|
|
|
34
68
|
## Migrate Legacy APIs to Hono
|
|
35
69
|
|
|
@@ -467,7 +501,6 @@ export const deletePostsIdRoute = createRoute({
|
|
|
467
501
|
})
|
|
468
502
|
```
|
|
469
503
|
|
|
470
|
-
|
|
471
504
|
This project is in **early development** and being maintained by a developer with about 2 years of experience. While I'm doing my best to create a useful tool:
|
|
472
505
|
|
|
473
506
|
### ⚠️ WARNING: Potential Breaking Changes Without Notice
|
package/dist/config/index.d.ts
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVariableNameHelper = void 0;
|
|
4
|
+
const capitalize_1 = require("../text/capitalize");
|
|
5
|
+
const decapitalize_1 = require("../text/decapitalize");
|
|
6
|
+
const getVariableNameHelper = (name, config) => {
|
|
7
|
+
return config.typeOptions.namingCase === 'camelCase' ? (0, decapitalize_1.decapitalize)(name) : (0, capitalize_1.capitalize)(name);
|
|
8
|
+
};
|
|
9
|
+
exports.getVariableNameHelper = getVariableNameHelper;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVariableSchemaNameHelper = void 0;
|
|
4
|
+
const get_camel_case_schema_name_1 = require("../schema/references/get-camel-case-schema-name");
|
|
5
|
+
const get_pascal_case_schema_name_1 = require("../schema/references/get-pascal-case-schema-name");
|
|
6
|
+
const getVariableSchemaNameHelper = (name, config) => {
|
|
7
|
+
return config.schemaOptions.namingCase === 'camelCase'
|
|
8
|
+
? (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(name)
|
|
9
|
+
: (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(name);
|
|
10
|
+
};
|
|
11
|
+
exports.getVariableSchemaNameHelper = getVariableSchemaNameHelper;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if all properties in the object are optional
|
|
3
|
+
* @param objectProperties - An array of strings representing the object properties
|
|
4
|
+
* @returns A boolean indicating if all properties are optional
|
|
5
|
+
*/
|
|
6
|
+
export declare function isAllOptional(objectProperties: string[]): boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isAllOptional = isAllOptional;
|
|
4
|
+
/**
|
|
5
|
+
* Check if all properties in the object are optional
|
|
6
|
+
* @param objectProperties - An array of strings representing the object properties
|
|
7
|
+
* @returns A boolean indicating if all properties are optional
|
|
8
|
+
*/
|
|
9
|
+
function isAllOptional(objectProperties) {
|
|
10
|
+
return objectProperties.every((prop) => prop.includes('.optional()'));
|
|
11
|
+
}
|
|
@@ -22,9 +22,9 @@ function generateZodOpenAPIHono(openAPISpec, config) {
|
|
|
22
22
|
// 2. get paths
|
|
23
23
|
const { paths } = openAPISpec;
|
|
24
24
|
// 3. generate components code
|
|
25
|
-
const componentsCode = (0, generate_components_code_1.generateComponentsCode)(components, config
|
|
25
|
+
const componentsCode = (0, generate_components_code_1.generateComponentsCode)(components, config);
|
|
26
26
|
// 4. generate route code
|
|
27
|
-
const routeCode = (0, generate_route_code_1.generateRouteCode)(paths, config
|
|
27
|
+
const routeCode = (0, generate_route_code_1.generateRouteCode)(paths, config);
|
|
28
28
|
// 5. generate types code
|
|
29
29
|
const typesCode = (0, generate_types_code_1.generateTypesCode)(components, config);
|
|
30
30
|
// 6. generate hono code
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Components } from '../../../types';
|
|
2
|
+
import type { Config } from '../../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates TypeScript code for OpenAPI components, converting them to Zod schemas.
|
|
4
5
|
* If no schemas are present, returns an empty string.
|
|
@@ -15,4 +16,4 @@ import type { Components } from '../../../types';
|
|
|
15
16
|
* 5. Creates exports for all schemas
|
|
16
17
|
* 6. Returns the complete code with proper ordering to avoid reference errors
|
|
17
18
|
*/
|
|
18
|
-
export declare function generateComponentsCode(components: Components,
|
|
19
|
+
export declare function generateComponentsCode(components: Components, config: Config): string;
|
|
@@ -4,9 +4,8 @@ exports.generateComponentsCode = generateComponentsCode;
|
|
|
4
4
|
const generate_zod_schema_definition_1 = require("../../zod/generate-zod-schema-definition");
|
|
5
5
|
const generate_zod_schema_1 = require("../../../generators/zod/generate-zod-schema");
|
|
6
6
|
const resolve_schemas_dependencies_1 = require("../../../core/schema/references/resolve-schemas-dependencies");
|
|
7
|
-
const get_camel_case_schema_name_1 = require("../../../core/schema/references/get-camel-case-schema-name");
|
|
8
7
|
const generate_schemas_export_1 = require("../paths/generate-schemas-export");
|
|
9
|
-
const
|
|
8
|
+
const get_variable_schema_name_helper_1 = require("../../../core/helper/get-variable-schema-name-helper");
|
|
10
9
|
/**
|
|
11
10
|
* Generates TypeScript code for OpenAPI components, converting them to Zod schemas.
|
|
12
11
|
* If no schemas are present, returns an empty string.
|
|
@@ -23,7 +22,9 @@ const get_pascal_case_schema_name_1 = require("../../../core/schema/references/g
|
|
|
23
22
|
* 5. Creates exports for all schemas
|
|
24
23
|
* 6. Returns the complete code with proper ordering to avoid reference errors
|
|
25
24
|
*/
|
|
26
|
-
function generateComponentsCode(components,
|
|
25
|
+
function generateComponentsCode(components,
|
|
26
|
+
// namingCase: 'camelCase' | 'PascalCase' = 'camelCase',
|
|
27
|
+
config) {
|
|
27
28
|
// 1. schema extraction
|
|
28
29
|
const { schemas } = components;
|
|
29
30
|
// 2. resolve schema dependencies to obtain proper ordering
|
|
@@ -38,19 +39,17 @@ function generateComponentsCode(components, namingCase = 'camelCase', exportEnab
|
|
|
38
39
|
// 4.1 get schema definition corresponding to schema name
|
|
39
40
|
const schema = schemas[schemaName];
|
|
40
41
|
// 4.2 get variable name
|
|
41
|
-
const variableName =
|
|
42
|
-
? (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(schemaName)
|
|
43
|
-
: (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(schemaName);
|
|
42
|
+
const variableName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(schemaName, config);
|
|
44
43
|
// 4.3 generate zod schema
|
|
45
|
-
const zodSchema = (0, generate_zod_schema_1.generateZodSchema)(schema, undefined, undefined
|
|
44
|
+
const zodSchema = (0, generate_zod_schema_1.generateZodSchema)(config, schema, undefined, undefined);
|
|
46
45
|
// 4.4 generate zod schema definition
|
|
47
46
|
return (0, generate_zod_schema_definition_1.generateZodSchemaDefinition)(variableName, zodSchema, schemaName);
|
|
48
47
|
})
|
|
49
48
|
.join('\n\n');
|
|
50
49
|
// 5. generate export statement
|
|
51
|
-
const exports = (0, generate_schemas_export_1.generateSchemasExport)(orderedSchemas,
|
|
50
|
+
const exports = (0, generate_schemas_export_1.generateSchemasExport)(orderedSchemas, config);
|
|
52
51
|
// 6. final code assembly
|
|
53
|
-
if (exportEnabled) {
|
|
52
|
+
if (config.schemaOptions.exportEnabled) {
|
|
54
53
|
return `${schemaDefinitions}\n\n${exports}`;
|
|
55
54
|
}
|
|
56
55
|
return schemaDefinitions;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { OpenAPIPaths } from '../../../types';
|
|
2
|
+
import type { Config } from '../../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates TypeScript code for all valid routes based on OpenAPI paths
|
|
4
5
|
*
|
|
@@ -18,4 +19,4 @@ import type { OpenAPIPaths } from '../../../types';
|
|
|
18
19
|
* - Generates type-safe route handlers using zod validation
|
|
19
20
|
* - Combines all routes with proper spacing
|
|
20
21
|
*/
|
|
21
|
-
export declare function generateRouteCode(openAPIPaths: OpenAPIPaths,
|
|
22
|
+
export declare function generateRouteCode(openAPIPaths: OpenAPIPaths, config: Config): string;
|
|
@@ -23,7 +23,9 @@ const is_operation_1 = require("../../../core/validator/is-operation");
|
|
|
23
23
|
* - Generates type-safe route handlers using zod validation
|
|
24
24
|
* - Combines all routes with proper spacing
|
|
25
25
|
*/
|
|
26
|
-
function generateRouteCode(openAPIPaths,
|
|
26
|
+
function generateRouteCode(openAPIPaths,
|
|
27
|
+
// namingCase: 'camelCase' | 'PascalCase' = 'camelCase',
|
|
28
|
+
config) {
|
|
27
29
|
const routes = [];
|
|
28
30
|
// 1. flattening and processing OpenAPI paths
|
|
29
31
|
for (const [path, pathItem] of Object.entries(openAPIPaths)) {
|
|
@@ -45,7 +47,7 @@ function generateRouteCode(openAPIPaths, namingCase = 'camelCase') {
|
|
|
45
47
|
if (!(0, is_operation_1.isOperation)(pathItemValue))
|
|
46
48
|
continue;
|
|
47
49
|
// 3.5 generating the root code and adding it to the array
|
|
48
|
-
routes.push((0, generate_route_1.generateRoute)(path, method, pathItemValue,
|
|
50
|
+
routes.push((0, generate_route_1.generateRoute)(path, method, pathItemValue, config));
|
|
49
51
|
}
|
|
50
52
|
}
|
|
51
53
|
// 4. exclude invalid routes and join them with a newline
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Operation } from '../../../types';
|
|
2
|
+
import type { Config } from '../../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates TypeScript code for a Hono route based on OpenAPI operation details
|
|
4
5
|
*
|
|
@@ -21,4 +22,4 @@ import type { Operation } from '../../../types';
|
|
|
21
22
|
* - Handles optional parameters appropriately
|
|
22
23
|
* - Integrates with Hono's createRoute function
|
|
23
24
|
*/
|
|
24
|
-
export declare function generateRoute(path: string, method: string, operation: Operation,
|
|
25
|
+
export declare function generateRoute(path: string, method: string, operation: Operation, config: Config): string;
|
|
@@ -28,11 +28,13 @@ const escape_quote_1 = require("../../../core/text/escape-quote");
|
|
|
28
28
|
* - Handles optional parameters appropriately
|
|
29
29
|
* - Integrates with Hono's createRoute function
|
|
30
30
|
*/
|
|
31
|
-
function generateRoute(path, method, operation,
|
|
31
|
+
function generateRoute(path, method, operation,
|
|
32
|
+
// namingCase: 'camelCase' | 'PascalCase',
|
|
33
|
+
config) {
|
|
32
34
|
const { tags, summary, description, security, parameters, requestBody, responses } = operation;
|
|
33
35
|
const routeName = (0, generate_route_name_1.generateRouteName)(method, path);
|
|
34
36
|
const tagList = tags ? JSON.stringify(tags) : '[]';
|
|
35
|
-
const requestParams = (0, generate_request_parameter_1.generateRequestParameter)(parameters, requestBody,
|
|
37
|
+
const requestParams = (0, generate_request_parameter_1.generateRequestParameter)(parameters, requestBody, config);
|
|
36
38
|
const create_args = {
|
|
37
39
|
routeName,
|
|
38
40
|
tagsCode: `tags:${tagList},`,
|
|
@@ -42,7 +44,7 @@ function generateRoute(path, method, operation, namingCase) {
|
|
|
42
44
|
descriptionCode: description ? `description:'${(0, escape_quote_1.escapeQuote)(description)}',` : '',
|
|
43
45
|
securityCode: security ? `security:${JSON.stringify(security)},` : '',
|
|
44
46
|
requestParams: requestParams ? `${requestParams}` : '',
|
|
45
|
-
responsesCode: responses ? `responses:{${(0, generate_response_schema_1.generateResponseSchema)(responses,
|
|
47
|
+
responsesCode: responses ? `responses:{${(0, generate_response_schema_1.generateResponseSchema)(responses, config)}}` : '',
|
|
46
48
|
};
|
|
47
49
|
return (0, generate_create_route_1.generateCreateRoute)(create_args);
|
|
48
50
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Config } from '../../../config';
|
|
1
2
|
/**
|
|
2
3
|
* Generates a TypeScript export for a list of schema names
|
|
3
4
|
*
|
|
@@ -8,4 +9,4 @@
|
|
|
8
9
|
* @example
|
|
9
10
|
* // Returns: 'export const schemas = { userSchema, postSchema }'
|
|
10
11
|
*/
|
|
11
|
-
export declare function generateSchemasExport(orderedSchemas: string[],
|
|
12
|
+
export declare function generateSchemasExport(orderedSchemas: string[], config: Config): string;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateSchemasExport = generateSchemasExport;
|
|
4
|
-
const
|
|
5
|
-
const get_pascal_case_schema_name_1 = require("../../../core/schema/references/get-pascal-case-schema-name");
|
|
4
|
+
const get_variable_schema_name_helper_1 = require("../../../core/helper/get-variable-schema-name-helper");
|
|
6
5
|
/**
|
|
7
6
|
* Generates a TypeScript export for a list of schema names
|
|
8
7
|
*
|
|
@@ -13,13 +12,7 @@ const get_pascal_case_schema_name_1 = require("../../../core/schema/references/g
|
|
|
13
12
|
* @example
|
|
14
13
|
* // Returns: 'export const schemas = { userSchema, postSchema }'
|
|
15
14
|
*/
|
|
16
|
-
function generateSchemasExport(orderedSchemas,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return `export const schemas = {\n${camelCaseSchemas.join(',\n')}\n}`;
|
|
20
|
-
}
|
|
21
|
-
if (namingCase === 'PascalCase') {
|
|
22
|
-
const pascalCaseSchemas = orderedSchemas.map((schemaName) => (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(schemaName));
|
|
23
|
-
return `export const schemas = {\n${pascalCaseSchemas.join(',\n')}\n}`;
|
|
24
|
-
}
|
|
15
|
+
function generateSchemasExport(orderedSchemas, config) {
|
|
16
|
+
const variableNames = orderedSchemas.map((schemaName) => (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(schemaName, config));
|
|
17
|
+
return `export const schemas = {\n${variableNames.join(',\n')}\n}`;
|
|
25
18
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ParamsObject, Parameters } from '../../../types';
|
|
2
|
+
import type { Config } from '../../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates a params object containing Zod schemas for different parameter locations
|
|
4
5
|
*
|
|
@@ -12,4 +13,4 @@ import type { ParamsObject, Parameters } from '../../../types';
|
|
|
12
13
|
* - Organizes parameters into appropriate objects based on their location
|
|
13
14
|
* - Maintains empty objects for unused parameter locations
|
|
14
15
|
*/
|
|
15
|
-
export declare function generateParamsObject(parameters: Parameters[],
|
|
16
|
+
export declare function generateParamsObject(parameters: Parameters[], config: Config): ParamsObject;
|
|
@@ -16,13 +16,15 @@ const generate_zod_schema_1 = require("../../zod/generate-zod-schema");
|
|
|
16
16
|
* - Organizes parameters into appropriate objects based on their location
|
|
17
17
|
* - Maintains empty objects for unused parameter locations
|
|
18
18
|
*/
|
|
19
|
-
function generateParamsObject(parameters,
|
|
19
|
+
function generateParamsObject(parameters,
|
|
20
|
+
// namingCase: 'camelCase' | 'PascalCase' = 'camelCase',
|
|
21
|
+
config) {
|
|
20
22
|
return parameters.reduce((acc, param) => {
|
|
21
23
|
const optionalSuffix = param.required ? '' : '.optional()';
|
|
22
24
|
// path params are generated with the param name
|
|
23
25
|
const baseSchema = param.in === 'path'
|
|
24
|
-
? (0, generate_zod_schema_1.generateZodSchema)(param.schema, param.name, true
|
|
25
|
-
: (0, generate_zod_schema_1.generateZodSchema)(param.schema,
|
|
26
|
+
? (0, generate_zod_schema_1.generateZodSchema)(config, param.schema, param.name, true)
|
|
27
|
+
: (0, generate_zod_schema_1.generateZodSchema)(config, param.schema, param.name, false);
|
|
26
28
|
// Initialize section if it doesn't exist
|
|
27
29
|
if (!acc[param.in]) {
|
|
28
30
|
acc[param.in] = {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Parameters, RequestBody } from '../../../types';
|
|
2
|
+
import type { Config } from '../../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates TypeScript code for request validation based on OpenAPI specification
|
|
4
5
|
*
|
|
@@ -19,4 +20,4 @@ import type { Parameters, RequestBody } from '../../../types';
|
|
|
19
20
|
* - Properly combines parameters and body when both are present
|
|
20
21
|
* - Handles schema references and inline schemas
|
|
21
22
|
*/
|
|
22
|
-
export declare function generateRequestParameter(parameters: Parameters[] | undefined, requestBody: RequestBody | undefined,
|
|
23
|
+
export declare function generateRequestParameter(parameters: Parameters[] | undefined, requestBody: RequestBody | undefined, config: Config): string;
|
|
@@ -28,7 +28,7 @@ const generate_zod_property_schema_1 = require("../../zod/generate-zod-property-
|
|
|
28
28
|
* - Properly combines parameters and body when both are present
|
|
29
29
|
* - Handles schema references and inline schemas
|
|
30
30
|
*/
|
|
31
|
-
function generateRequestParameter(parameters, requestBody,
|
|
31
|
+
function generateRequestParameter(parameters, requestBody, config) {
|
|
32
32
|
// Early return if no parameters or content
|
|
33
33
|
if (!(parameters || requestBody?.content)) {
|
|
34
34
|
return '';
|
|
@@ -36,7 +36,7 @@ function generateRequestParameter(parameters, requestBody, namingCase = 'camelCa
|
|
|
36
36
|
const requestBodyContentTypes = Object.keys(requestBody?.content || {});
|
|
37
37
|
const params = parameters
|
|
38
38
|
? (() => {
|
|
39
|
-
const paramsObj = (0, generate_params_object_1.generateParamsObject)(parameters,
|
|
39
|
+
const paramsObj = (0, generate_params_object_1.generateParamsObject)(parameters, config);
|
|
40
40
|
const requestParamsArr = (0, generate_request_params_array_1.generateRequestParamsArray)(paramsObj);
|
|
41
41
|
return requestParamsArr.length ? (0, generate_format_request_object_1.generateFormatRequestObject)(requestParamsArr) : '';
|
|
42
42
|
})()
|
|
@@ -46,7 +46,7 @@ function generateRequestParameter(parameters, requestBody, namingCase = 'camelCa
|
|
|
46
46
|
const uniqueSchemas = new Map();
|
|
47
47
|
for (const contentType of requestBodyContentTypes) {
|
|
48
48
|
const { schema } = requestBody.content[contentType];
|
|
49
|
-
const zodSchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema,
|
|
49
|
+
const zodSchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema, config);
|
|
50
50
|
uniqueSchemas.set(zodSchema, zodSchema);
|
|
51
51
|
}
|
|
52
52
|
const request_body_required = requestBody.required ?? false;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Responses } from '../../../types';
|
|
2
|
+
import type { Config } from '../../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates a response schema for different status codes
|
|
4
5
|
*
|
|
@@ -19,4 +20,4 @@ import type { Responses } from '../../../types';
|
|
|
19
20
|
* - Handles nested schema structures
|
|
20
21
|
* - Automatically resolves schema references
|
|
21
22
|
*/
|
|
22
|
-
export declare function generateResponseSchema(responses: Responses,
|
|
23
|
+
export declare function generateResponseSchema(responses: Responses, config: Config): string;
|
|
@@ -24,7 +24,9 @@ const generate_zod_property_schema_1 = require("../../zod/generate-zod-property-
|
|
|
24
24
|
* - Handles nested schema structures
|
|
25
25
|
* - Automatically resolves schema references
|
|
26
26
|
*/
|
|
27
|
-
function generateResponseSchema(responses,
|
|
27
|
+
function generateResponseSchema(responses,
|
|
28
|
+
// namingCase: 'camelCase' | 'PascalCase' = 'camelCase',
|
|
29
|
+
config) {
|
|
28
30
|
// 1. get response codes (200, 404, etc.)
|
|
29
31
|
const responseCodes = Object.keys(responses);
|
|
30
32
|
// 2. processing for each response code
|
|
@@ -41,7 +43,7 @@ function generateResponseSchema(responses, namingCase = 'camelCase') {
|
|
|
41
43
|
const contentParts = [];
|
|
42
44
|
for (const contentType of contentTypes) {
|
|
43
45
|
const content = response.content[contentType];
|
|
44
|
-
const zodSchema = (0, generate_zod_property_schema_1.generatePropertySchema)(content.schema,
|
|
46
|
+
const zodSchema = (0, generate_zod_property_schema_1.generatePropertySchema)(content.schema, config);
|
|
45
47
|
contentParts.push(`'${contentType}':{schema:${zodSchema}}`);
|
|
46
48
|
}
|
|
47
49
|
return `${code}:{description:'${(0, escape_quote_1.escapeQuote)(response.description)}',content:{${contentParts.join(',')}},},`;
|
|
@@ -15,8 +15,6 @@ exports.generateTypesCode = generateTypesCode;
|
|
|
15
15
|
// // 4. generate code for each schema
|
|
16
16
|
// }
|
|
17
17
|
const resolve_schemas_dependencies_1 = require("../../core/schema/references/resolve-schemas-dependencies");
|
|
18
|
-
// import { getCamelCaseSchemaName } from '../../core/schema/references/get-camel-case-schema-name'
|
|
19
|
-
// import { getPascalCaseSchemaName } from '../../core/schema/references/get-pascal-case-schema-name'
|
|
20
18
|
const generate_zod_infer_1 = require("../zod/generate-zod-infer");
|
|
21
19
|
function generateTypesCode(components, config) {
|
|
22
20
|
// 1. schema extraction
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function generatePartialSchema(objectProperties: string[]): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generatePartialSchema = generatePartialSchema;
|
|
4
|
+
function generatePartialSchema(objectProperties) {
|
|
5
|
+
const cleanProperties = objectProperties.map((prop) => prop.replace('.optional()', ''));
|
|
6
|
+
return `z.object({${cleanProperties}}).partial()`;
|
|
7
|
+
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateZodInfer = generateZodInfer;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const capitalize_1 = require("../../core/text/capitalize");
|
|
7
|
-
const decapitalize_1 = require("../../core/text/decapitalize");
|
|
4
|
+
const get_variable_name_helper_1 = require("../../core/helper/get-variable-name-helper");
|
|
5
|
+
const get_variable_schema_name_helper_1 = require("../../core/helper/get-variable-schema-name-helper");
|
|
8
6
|
/**
|
|
9
7
|
* Generates a TypeScript type definition for an inferred Zod schema.
|
|
10
8
|
*
|
|
@@ -12,9 +10,7 @@ const decapitalize_1 = require("../../core/text/decapitalize");
|
|
|
12
10
|
* @returns A string containing the inferred type definition.
|
|
13
11
|
*/
|
|
14
12
|
function generateZodInfer(schema, config) {
|
|
15
|
-
const schemaName =
|
|
16
|
-
|
|
17
|
-
: (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(schema);
|
|
18
|
-
const variableName = config?.typeOptions?.namingCase === 'camelCase' ? (0, decapitalize_1.decapitalize)(schema) : (0, capitalize_1.capitalize)(schema);
|
|
13
|
+
const schemaName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(schema, config);
|
|
14
|
+
const variableName = (0, get_variable_name_helper_1.getVariableNameHelper)(schema, config);
|
|
19
15
|
return `export type ${variableName} = z.infer<typeof ${schemaName}>`;
|
|
20
16
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Schema } from '../../types';
|
|
2
|
+
import type { Config } from '../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates a Zod object schema with properties and their requirements
|
|
4
5
|
*
|
|
@@ -47,4 +48,4 @@ import type { Schema } from '../../types';
|
|
|
47
48
|
* - Uses .partial() when no properties are required
|
|
48
49
|
* - Maintains property order from input
|
|
49
50
|
*/
|
|
50
|
-
export declare function generateZodPropertiesSchema(properties: Record<string, Schema>, required: string[],
|
|
51
|
+
export declare function generateZodPropertiesSchema(properties: Record<string, Schema>, required: string[], config: Config): string;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateZodPropertiesSchema = generateZodPropertiesSchema;
|
|
4
|
+
const is_all_optional_1 = require("../../core/validator/is-all-optional");
|
|
5
|
+
const generate_partial_schema_1 = require("./generate-partial-schema");
|
|
4
6
|
const generate_zod_property_schema_1 = require("./generate-zod-property-schema");
|
|
5
7
|
/**
|
|
6
8
|
* Generates a Zod object schema with properties and their requirements
|
|
@@ -50,17 +52,19 @@ const generate_zod_property_schema_1 = require("./generate-zod-property-schema")
|
|
|
50
52
|
* - Uses .partial() when no properties are required
|
|
51
53
|
* - Maintains property order from input
|
|
52
54
|
*/
|
|
53
|
-
function generateZodPropertiesSchema(properties, required,
|
|
55
|
+
function generateZodPropertiesSchema(properties, required,
|
|
56
|
+
// namingCase: 'camelCase' | 'PascalCase' = 'camelCase',
|
|
57
|
+
config) {
|
|
54
58
|
const objectProperties = Object.entries(properties).map(([key, schema]) => {
|
|
55
59
|
const isRequired = required.includes(key);
|
|
56
|
-
const propertySchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema,
|
|
60
|
+
const propertySchema = (0, generate_zod_property_schema_1.generatePropertySchema)(schema, config);
|
|
57
61
|
return `${key}:${propertySchema}${isRequired ? '' : '.optional()'}`;
|
|
58
62
|
});
|
|
59
|
-
|
|
63
|
+
// Check if all properties are optional
|
|
64
|
+
const allOptional = (0, is_all_optional_1.isAllOptional)(objectProperties);
|
|
65
|
+
// If all properties are optional and no required properties, return partial schema
|
|
60
66
|
if (required.length === 0 && allOptional) {
|
|
61
|
-
|
|
62
|
-
return `z.object({${cleanProperties}}).partial()`;
|
|
67
|
+
return (0, generate_partial_schema_1.generatePartialSchema)(objectProperties);
|
|
63
68
|
}
|
|
64
69
|
return `z.object({${objectProperties}})`;
|
|
65
|
-
// return `z.object({${objectProperties}})${required.length === 0 ? '.partial()' : ''}`
|
|
66
70
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Schema } from '../../types';
|
|
2
|
+
import type { Config } from '../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates a Zod schema string for a given OpenAPI schema definition
|
|
4
5
|
*
|
|
@@ -22,4 +23,4 @@ import type { Schema } from '../../types';
|
|
|
22
23
|
* generatePropertySchema({ type: 'string' })
|
|
23
24
|
* // Returns: 'z.string()'
|
|
24
25
|
*/
|
|
25
|
-
export declare function generatePropertySchema(schema: Schema,
|
|
26
|
+
export declare function generatePropertySchema(schema: Schema, config: Config): string;
|
|
@@ -4,8 +4,7 @@ exports.generatePropertySchema = generatePropertySchema;
|
|
|
4
4
|
const get_ref_name_1 = require("../../core/schema/references/get-ref-name");
|
|
5
5
|
const generate_zod_array_1 = require("./generate-zod-array");
|
|
6
6
|
const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
7
|
-
const
|
|
8
|
-
const get_pascal_case_schema_name_1 = require("../../core/schema/references/get-pascal-case-schema-name");
|
|
7
|
+
const get_variable_schema_name_helper_1 = require("../../core/helper/get-variable-schema-name-helper");
|
|
9
8
|
// import { generateZodOpenAPIExample } from './generate-zod-openapi-example'
|
|
10
9
|
/**
|
|
11
10
|
* Generates a Zod schema string for a given OpenAPI schema definition
|
|
@@ -30,13 +29,13 @@ const get_pascal_case_schema_name_1 = require("../../core/schema/references/get-
|
|
|
30
29
|
* generatePropertySchema({ type: 'string' })
|
|
31
30
|
* // Returns: 'z.string()'
|
|
32
31
|
*/
|
|
33
|
-
function generatePropertySchema(schema,
|
|
32
|
+
function generatePropertySchema(schema,
|
|
33
|
+
// namingCase: 'camelCase' | 'PascalCase' = 'camelCase',
|
|
34
|
+
config) {
|
|
34
35
|
if (schema.$ref) {
|
|
35
36
|
const refName = (0, get_ref_name_1.getRefName)(schema.$ref);
|
|
36
37
|
if (refName) {
|
|
37
|
-
const variableName =
|
|
38
|
-
? (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(refName)
|
|
39
|
-
: (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(refName);
|
|
38
|
+
const variableName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(refName, config);
|
|
40
39
|
// return getRefName(variableName) || 'z.any()'
|
|
41
40
|
return variableName || 'z.any()';
|
|
42
41
|
}
|
|
@@ -44,11 +43,9 @@ function generatePropertySchema(schema, namingCase = 'camelCase') {
|
|
|
44
43
|
if (schema.type === 'array' && schema.items?.$ref) {
|
|
45
44
|
const refName = (0, get_ref_name_1.getRefName)(schema.items.$ref);
|
|
46
45
|
if (refName) {
|
|
47
|
-
const variableName =
|
|
48
|
-
? (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(refName)
|
|
49
|
-
: (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(refName);
|
|
46
|
+
const variableName = (0, get_variable_schema_name_helper_1.getVariableSchemaNameHelper)(refName, config);
|
|
50
47
|
return (0, generate_zod_array_1.generateZodArray)(variableName);
|
|
51
48
|
}
|
|
52
49
|
}
|
|
53
|
-
return (0, generate_zod_schema_1.generateZodSchema)(schema, undefined, undefined
|
|
50
|
+
return (0, generate_zod_schema_1.generateZodSchema)(config, schema, undefined, undefined);
|
|
54
51
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Config } from '../../config';
|
|
1
2
|
import type { Format, Type } from '../../types';
|
|
2
3
|
/**
|
|
3
4
|
* Generates a Zod record schema for objects with additional properties
|
|
@@ -31,4 +32,4 @@ import type { Format, Type } from '../../types';
|
|
|
31
32
|
export declare function generateZodRecordSchema(additionalProperties: {
|
|
32
33
|
type: Type;
|
|
33
34
|
format: Format;
|
|
34
|
-
}): string;
|
|
35
|
+
}, config: Config): string;
|
|
@@ -31,7 +31,7 @@ const generate_zod_schema_1 = require("./generate-zod-schema");
|
|
|
31
31
|
* - Value type is determined by the additionalProperties schema
|
|
32
32
|
* - Supports all Zod-compatible types and formats
|
|
33
33
|
*/
|
|
34
|
-
function generateZodRecordSchema(additionalProperties) {
|
|
35
|
-
const schema = (0, generate_zod_schema_1.generateZodSchema)(additionalProperties);
|
|
34
|
+
function generateZodRecordSchema(additionalProperties, config) {
|
|
35
|
+
const schema = (0, generate_zod_schema_1.generateZodSchema)(config, additionalProperties);
|
|
36
36
|
return `z.record(z.string(),${schema})`;
|
|
37
37
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Schema } from '../../types';
|
|
2
|
+
import type { Config } from '../../config';
|
|
2
3
|
/**
|
|
3
4
|
* Generates a Zod schema string from an OpenAPI/JSON Schema definition
|
|
4
5
|
*
|
|
@@ -56,4 +57,4 @@ import type { Schema } from '../../types';
|
|
|
56
57
|
* - Falls back to basic type mapping for simple types
|
|
57
58
|
* - Returns z.any() for unknown types with a warning
|
|
58
59
|
*/
|
|
59
|
-
export declare function generateZodSchema(schema: Schema, paramName?: string, isPath?: boolean
|
|
60
|
+
export declare function generateZodSchema(config: Config, schema: Schema, paramName?: string, isPath?: boolean): string;
|
|
@@ -86,7 +86,7 @@ const TYPE_TO_ZOD_SCHEMA = {
|
|
|
86
86
|
* - Falls back to basic type mapping for simple types
|
|
87
87
|
* - Returns z.any() for unknown types with a warning
|
|
88
88
|
*/
|
|
89
|
-
function generateZodSchema(schema, paramName, isPath
|
|
89
|
+
function generateZodSchema(config, schema, paramName, isPath) {
|
|
90
90
|
// enum
|
|
91
91
|
if (schema.enum) {
|
|
92
92
|
if (schema.example) {
|
|
@@ -97,10 +97,10 @@ function generateZodSchema(schema, paramName, isPath, namingCase = 'camelCase')
|
|
|
97
97
|
// object
|
|
98
98
|
if (schema.type === 'object') {
|
|
99
99
|
if (schema.additionalProperties)
|
|
100
|
-
return (0, generate_zod_record_schema_1.generateZodRecordSchema)(schema.additionalProperties);
|
|
100
|
+
return (0, generate_zod_record_schema_1.generateZodRecordSchema)(schema.additionalProperties, config);
|
|
101
101
|
if (!schema.properties)
|
|
102
102
|
return 'z.object({})';
|
|
103
|
-
return (0, generate_zod_properties_schema_1.generateZodPropertiesSchema)(schema.properties, schema.required || [],
|
|
103
|
+
return (0, generate_zod_properties_schema_1.generateZodPropertiesSchema)(schema.properties, schema.required || [], config);
|
|
104
104
|
}
|
|
105
105
|
// string
|
|
106
106
|
if (schema.type === 'string') {
|
|
@@ -144,7 +144,7 @@ function generateZodSchema(schema, paramName, isPath, namingCase = 'camelCase')
|
|
|
144
144
|
}
|
|
145
145
|
// array
|
|
146
146
|
if (schema.type === 'array' && schema.items)
|
|
147
|
-
return (0, generate_zod_array_1.generateZodArray)(generateZodSchema(schema.items, undefined, undefined
|
|
147
|
+
return (0, generate_zod_array_1.generateZodArray)(generateZodSchema(config, schema.items, undefined, undefined));
|
|
148
148
|
// fallback
|
|
149
149
|
if (schema.type)
|
|
150
150
|
return TYPE_TO_ZOD_SCHEMA[schema.type];
|
package/dist/index.js
CHANGED
|
@@ -34,9 +34,10 @@ async function main(dev = false, config = (0, config_1.getConfig)()) {
|
|
|
34
34
|
// 2. slice [ 'example/pet-store.yaml', '-o', 'routes/petstore-index.ts' ]
|
|
35
35
|
const args = process.argv.slice(2);
|
|
36
36
|
// 3. input = 'example/pet-store.yaml'
|
|
37
|
-
const input = args[0];
|
|
37
|
+
const input = config.input ?? args[0];
|
|
38
|
+
// const input = args[0]
|
|
38
39
|
// 4. output = 'routes/petstore-index.ts'
|
|
39
|
-
const output = args[args.indexOf('-o') + 1];
|
|
40
|
+
const output = config.output ?? args[args.indexOf('-o') + 1];
|
|
40
41
|
try {
|
|
41
42
|
// 5. parse OpenAPI YAML or JSON
|
|
42
43
|
const openAPI = (await swagger_parser_1.default.parse(input));
|