hono-takibi 0.0.1
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 +471 -0
- package/dist/core/schema/references/find-references.d.ts +35 -0
- package/dist/core/schema/references/find-references.js +42 -0
- package/dist/core/schema/references/get-camel-case-schema-name.d.ts +7 -0
- package/dist/core/schema/references/get-camel-case-schema-name.js +14 -0
- package/dist/core/schema/references/get-ref-name.d.ts +38 -0
- package/dist/core/schema/references/get-ref-name.js +47 -0
- package/dist/core/schema/references/resolve-schema-order.d.ts +30 -0
- package/dist/core/schema/references/resolve-schema-order.js +48 -0
- package/dist/core/schema/references/resolve-schema-references.d.ts +49 -0
- package/dist/core/schema/references/resolve-schema-references.js +61 -0
- package/dist/core/schema/references/resolve-schemas-dependencies.d.ts +49 -0
- package/dist/core/schema/references/resolve-schemas-dependencies.js +65 -0
- package/dist/core/schema/references/traverse_schema.d.ts +48 -0
- package/dist/core/schema/references/traverse_schema.js +70 -0
- package/dist/core/text/capitalize.d.ts +18 -0
- package/dist/core/text/capitalize.js +23 -0
- package/dist/core/text/decapitalize.d.ts +17 -0
- package/dist/core/text/decapitalize.js +22 -0
- package/dist/core/text/remove-zod-prefix.d.ts +8 -0
- package/dist/core/text/remove-zod-prefix.js +13 -0
- package/dist/core/validator/is-format-number.d.ts +2 -0
- package/dist/core/validator/is-format-number.js +6 -0
- package/dist/core/validator/is-format-string.d.ts +2 -0
- package/dist/core/validator/is-format-string.js +24 -0
- package/dist/core/validator/is-http-method.d.ts +9 -0
- package/dist/core/validator/is-http-method.js +19 -0
- package/dist/core/validator/is-operation.d.ts +15 -0
- package/dist/core/validator/is-operation.js +19 -0
- package/dist/core/zod/get-zod-string-format.d.ts +30 -0
- package/dist/core/zod/get-zod-string-format.js +66 -0
- package/dist/generators/hono/generate-hono.d.ts +9 -0
- package/dist/generators/hono/generate-hono.js +29 -0
- package/dist/generators/openapi/components/generate-components-code.d.ts +46 -0
- package/dist/generators/openapi/components/generate-components-code.js +79 -0
- package/dist/generators/openapi/paths/generate-create-route.d.ts +58 -0
- package/dist/generators/openapi/paths/generate-create-route.js +61 -0
- package/dist/generators/openapi/paths/generate-route-code.d.ts +53 -0
- package/dist/generators/openapi/paths/generate-route-code.js +85 -0
- package/dist/generators/openapi/paths/generate-route-name.d.ts +30 -0
- package/dist/generators/openapi/paths/generate-route-name.js +48 -0
- package/dist/generators/openapi/paths/generate-route.d.ts +51 -0
- package/dist/generators/openapi/paths/generate-route.js +73 -0
- package/dist/generators/openapi/paths/generate-schemas-export.d.ts +11 -0
- package/dist/generators/openapi/paths/generate-schemas-export.js +18 -0
- package/dist/generators/request/body/generate-insert-request-body.d.ts +32 -0
- package/dist/generators/request/body/generate-insert-request-body.js +37 -0
- package/dist/generators/request/body/generate-request-body.d.ts +18 -0
- package/dist/generators/request/body/generate-request-body.js +23 -0
- package/dist/generators/request/object/generate-format-request-object.d.ts +29 -0
- package/dist/generators/request/object/generate-format-request-object.js +34 -0
- package/dist/generators/request/params/generate-params-object.d.ts +62 -0
- package/dist/generators/request/params/generate-params-object.js +93 -0
- package/dist/generators/request/params/generate-request-parameter.d.ts +49 -0
- package/dist/generators/request/params/generate-request-parameter.js +79 -0
- package/dist/generators/request/params/generate-request-params-array.d.ts +37 -0
- package/dist/generators/request/params/generate-request-params-array.js +56 -0
- package/dist/generators/request/params/generate-request-params.d.ts +34 -0
- package/dist/generators/request/params/generate-request-params.js +39 -0
- package/dist/generators/response/schemas/generate-response-schema.d.ts +59 -0
- package/dist/generators/response/schemas/generate-response-schema.js +83 -0
- package/dist/generators/zod/generate-zod-array.d.ts +31 -0
- package/dist/generators/zod/generate-zod-array.js +36 -0
- package/dist/generators/zod/generate-zod-coerce.d.ts +10 -0
- package/dist/generators/zod/generate-zod-coerce.js +17 -0
- package/dist/generators/zod/generate-zod-integer-schema.d.ts +15 -0
- package/dist/generators/zod/generate-zod-integer-schema.js +24 -0
- package/dist/generators/zod/generate-zod-number-schema.d.ts +16 -0
- package/dist/generators/zod/generate-zod-number-schema.js +26 -0
- package/dist/generators/zod/generate-zod-object-schema.d.ts +36 -0
- package/dist/generators/zod/generate-zod-object-schema.js +43 -0
- package/dist/generators/zod/generate-zod-properties-schema.d.ts +50 -0
- package/dist/generators/zod/generate-zod-properties-schema.js +60 -0
- package/dist/generators/zod/generate-zod-property-schema.d.ts +25 -0
- package/dist/generators/zod/generate-zod-property-schema.js +47 -0
- package/dist/generators/zod/generate-zod-record-schema.d.ts +34 -0
- package/dist/generators/zod/generate-zod-record-schema.js +37 -0
- package/dist/generators/zod/generate-zod-schema-definition.d.ts +22 -0
- package/dist/generators/zod/generate-zod-schema-definition.js +27 -0
- package/dist/generators/zod/generate-zod-schema.d.ts +59 -0
- package/dist/generators/zod/generate-zod-schema.js +136 -0
- package/dist/generators/zod/generate-zod-string-schema.d.ts +46 -0
- package/dist/generators/zod/generate-zod-string-schema.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +85 -0
- package/dist/types/index.d.ts +170 -0
- package/dist/types/index.js +2 -0
- package/package.json +47 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getZodFormatString = getZodFormatString;
|
|
4
|
+
/**
|
|
5
|
+
* Mapping of OpenAPI formats to Zod validation methods
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* - String formats map to string validation methods
|
|
9
|
+
* - Number formats map to number type
|
|
10
|
+
* - Empty string for unrecognized formats
|
|
11
|
+
*/
|
|
12
|
+
const FORMAT_STRING_TO_ZOD_VALIDATION = {
|
|
13
|
+
// max: '.max()',
|
|
14
|
+
// min: '.min()',
|
|
15
|
+
// length: '.length()',
|
|
16
|
+
email: '.email()',
|
|
17
|
+
uri: '.url()',
|
|
18
|
+
emoji: '.emoji()',
|
|
19
|
+
uuid: '.uuid()',
|
|
20
|
+
nanoid: '.nanoid()',
|
|
21
|
+
cuid: '.cuid()',
|
|
22
|
+
cuid2: '.cuid2()',
|
|
23
|
+
ulid: '.ulid()',
|
|
24
|
+
'date-time': '.datetime()',
|
|
25
|
+
ip: '.ip()',
|
|
26
|
+
cidr: '.cidr()',
|
|
27
|
+
trim: '.trim()',
|
|
28
|
+
toLowerCase: '.toLowerCase()',
|
|
29
|
+
toUpperCase: '.toUpperCase()',
|
|
30
|
+
date: '.date()',
|
|
31
|
+
time: '.time()',
|
|
32
|
+
duration: '.duration()',
|
|
33
|
+
base64: '.base64()',
|
|
34
|
+
binary: '.instanceof(Uint8Array)',
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Converts OpenAPI format to Zod validation method
|
|
38
|
+
*
|
|
39
|
+
* @function getZodFormatString
|
|
40
|
+
* @param format - OpenAPI format type
|
|
41
|
+
* @returns Zod validation method string
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* // String validations
|
|
45
|
+
* getZodFormatString('email') // Returns: '.email()'
|
|
46
|
+
* getZodFormatString('uri') // Returns: '.url()'
|
|
47
|
+
* getZodFormatString('uuid') // Returns: '.uuid()'
|
|
48
|
+
* getZodFormatString('date-time') // Returns: '.datetime()'
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* // Number types
|
|
52
|
+
* getZodFormatString('int32') // Returns: 'z.number()'
|
|
53
|
+
* getZodFormatString('float') // Returns: 'z.number()'
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* // Unknown format
|
|
57
|
+
* getZodFormatString('unknown') // Returns: ''
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* - Returns the corresponding Zod validation method for known formats
|
|
61
|
+
* - Returns empty string for unrecognized formats
|
|
62
|
+
* - Used in schema generation for request/response validation
|
|
63
|
+
*/
|
|
64
|
+
function getZodFormatString(format) {
|
|
65
|
+
return FORMAT_STRING_TO_ZOD_VALIDATION[format];
|
|
66
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { OpenAPISpec } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates TypeScript code from OpenAPI specification for Hono/zod-openapi
|
|
4
|
+
*
|
|
5
|
+
* @function generateHonoCode
|
|
6
|
+
* @param openAPISpec - OpenAPI specification object containing components and paths
|
|
7
|
+
* @returns Generated TypeScript code
|
|
8
|
+
*/
|
|
9
|
+
export declare function generateHono(openAPISpec: OpenAPISpec): string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateHono = generateHono;
|
|
4
|
+
const generate_components_code_1 = require("../openapi/components/generate-components-code");
|
|
5
|
+
const generate_route_code_1 = require("../openapi/paths/generate-route-code");
|
|
6
|
+
/**
|
|
7
|
+
* Import statement for Hono's zod-openapi package
|
|
8
|
+
* @constant
|
|
9
|
+
*/
|
|
10
|
+
const IMPORT_CODE = "import { createRoute, z } from '@hono/zod-openapi';";
|
|
11
|
+
/**
|
|
12
|
+
* Generates TypeScript code from OpenAPI specification for Hono/zod-openapi
|
|
13
|
+
*
|
|
14
|
+
* @function generateHonoCode
|
|
15
|
+
* @param openAPISpec - OpenAPI specification object containing components and paths
|
|
16
|
+
* @returns Generated TypeScript code
|
|
17
|
+
*/
|
|
18
|
+
function generateHono(openAPISpec) {
|
|
19
|
+
// 1. get components
|
|
20
|
+
const components = openAPISpec.components;
|
|
21
|
+
// 2. get paths
|
|
22
|
+
const { paths } = openAPISpec;
|
|
23
|
+
// 3. generate components code
|
|
24
|
+
const componentsCode = (0, generate_components_code_1.generateComponentsCode)(components);
|
|
25
|
+
// 4. generate route code
|
|
26
|
+
const routeCode = (0, generate_route_code_1.generateRouteCode)(paths);
|
|
27
|
+
// 5. generate hono code
|
|
28
|
+
return `${IMPORT_CODE}\n\n${componentsCode}\n\n${routeCode}`.trimEnd();
|
|
29
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Components } from '../../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates TypeScript code for OpenAPI components, converting them to Zod schemas.
|
|
4
|
+
* If no schemas are present, returns an empty string.
|
|
5
|
+
*
|
|
6
|
+
* @function generateComponentsCode
|
|
7
|
+
* @param components - OpenAPI components object containing schema definitions
|
|
8
|
+
* @returns Generated TypeScript code string containing Zod schema definitions and exports, or empty string if no schemas
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* // With schemas
|
|
12
|
+
* const components = {
|
|
13
|
+
* schemas: {
|
|
14
|
+
* User: {
|
|
15
|
+
* type: 'object',
|
|
16
|
+
* properties: {
|
|
17
|
+
* profile: { $ref: '#/components/schemas/Profile' }
|
|
18
|
+
* }
|
|
19
|
+
* },
|
|
20
|
+
* Profile: {
|
|
21
|
+
* type: 'object',
|
|
22
|
+
* properties: {
|
|
23
|
+
* name: { type: 'string' }
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* generateComponentsCode(components)
|
|
29
|
+
* // Returns:
|
|
30
|
+
* // export const profileSchema = z.object({ name: z.string() })
|
|
31
|
+
* // export const userSchema = z.object({ profile: profileSchema })
|
|
32
|
+
* // export const schemas = { profileSchema, userSchema }
|
|
33
|
+
*
|
|
34
|
+
* // Without schemas
|
|
35
|
+
* generateComponentsCode({})
|
|
36
|
+
* // Returns: ''
|
|
37
|
+
*
|
|
38
|
+
* @note The function:
|
|
39
|
+
* 1. Extracts schemas from components
|
|
40
|
+
* 2. Resolves dependencies between schemas to determine correct generation order
|
|
41
|
+
* 3. Returns empty string if no schemas are present
|
|
42
|
+
* 4. Generates Zod schema definitions for each schema
|
|
43
|
+
* 5. Creates exports for all schemas
|
|
44
|
+
* 6. Returns the complete code with proper ordering to avoid reference errors
|
|
45
|
+
*/
|
|
46
|
+
export declare function generateComponentsCode(components: Components): string;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateComponentsCode = generateComponentsCode;
|
|
4
|
+
const generate_zod_schema_definition_1 = require("../../zod/generate-zod-schema-definition");
|
|
5
|
+
const generate_zod_schema_1 = require("../../../generators/zod/generate-zod-schema");
|
|
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
|
+
const generate_schemas_export_1 = require("../paths/generate-schemas-export");
|
|
9
|
+
/**
|
|
10
|
+
* Generates TypeScript code for OpenAPI components, converting them to Zod schemas.
|
|
11
|
+
* If no schemas are present, returns an empty string.
|
|
12
|
+
*
|
|
13
|
+
* @function generateComponentsCode
|
|
14
|
+
* @param components - OpenAPI components object containing schema definitions
|
|
15
|
+
* @returns Generated TypeScript code string containing Zod schema definitions and exports, or empty string if no schemas
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* // With schemas
|
|
19
|
+
* const components = {
|
|
20
|
+
* schemas: {
|
|
21
|
+
* User: {
|
|
22
|
+
* type: 'object',
|
|
23
|
+
* properties: {
|
|
24
|
+
* profile: { $ref: '#/components/schemas/Profile' }
|
|
25
|
+
* }
|
|
26
|
+
* },
|
|
27
|
+
* Profile: {
|
|
28
|
+
* type: 'object',
|
|
29
|
+
* properties: {
|
|
30
|
+
* name: { type: 'string' }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* generateComponentsCode(components)
|
|
36
|
+
* // Returns:
|
|
37
|
+
* // export const profileSchema = z.object({ name: z.string() })
|
|
38
|
+
* // export const userSchema = z.object({ profile: profileSchema })
|
|
39
|
+
* // export const schemas = { profileSchema, userSchema }
|
|
40
|
+
*
|
|
41
|
+
* // Without schemas
|
|
42
|
+
* generateComponentsCode({})
|
|
43
|
+
* // Returns: ''
|
|
44
|
+
*
|
|
45
|
+
* @note The function:
|
|
46
|
+
* 1. Extracts schemas from components
|
|
47
|
+
* 2. Resolves dependencies between schemas to determine correct generation order
|
|
48
|
+
* 3. Returns empty string if no schemas are present
|
|
49
|
+
* 4. Generates Zod schema definitions for each schema
|
|
50
|
+
* 5. Creates exports for all schemas
|
|
51
|
+
* 6. Returns the complete code with proper ordering to avoid reference errors
|
|
52
|
+
*/
|
|
53
|
+
function generateComponentsCode(components) {
|
|
54
|
+
// 1. schema extraction
|
|
55
|
+
const { schemas } = components;
|
|
56
|
+
// 2. resolve schema dependencies to obtain proper ordering
|
|
57
|
+
const orderedSchemas = (0, resolve_schemas_dependencies_1.resolveSchemasDependencies)(schemas);
|
|
58
|
+
// 3. if there are no schemas, return an empty string
|
|
59
|
+
if (orderedSchemas.length === 0) {
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
// 4. generate code for each schema
|
|
63
|
+
const schemaDefinitions = orderedSchemas
|
|
64
|
+
.map((schemaName) => {
|
|
65
|
+
// 4.1 get schema definition corresponding to schema name
|
|
66
|
+
const schema = schemas[schemaName];
|
|
67
|
+
// 4.2 get camelCase schema name
|
|
68
|
+
const camelCaseSchemaName = (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(schemaName);
|
|
69
|
+
// 4.3 generate zod schema
|
|
70
|
+
const zodSchema = (0, generate_zod_schema_1.generateZodSchema)(schema);
|
|
71
|
+
// 4.4 generate zod schema definition
|
|
72
|
+
return (0, generate_zod_schema_definition_1.generateZodSchemaDefinition)(camelCaseSchemaName, zodSchema);
|
|
73
|
+
})
|
|
74
|
+
.join('\n\n');
|
|
75
|
+
// 5. generate export statement
|
|
76
|
+
const exports = (0, generate_schemas_export_1.generateSchemasExport)(orderedSchemas);
|
|
77
|
+
// 6. final code assembly
|
|
78
|
+
return `${schemaDefinitions}\n\n${exports}`;
|
|
79
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
type GenerateCreateRouteParams = {
|
|
2
|
+
routeName: string;
|
|
3
|
+
tagsCode: string;
|
|
4
|
+
methodCode: string;
|
|
5
|
+
pathCode: string;
|
|
6
|
+
descriptionCode?: string;
|
|
7
|
+
securityCode?: string;
|
|
8
|
+
requestParams: string;
|
|
9
|
+
responsesCode: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Generates TypeScript code for a Hono route definition
|
|
13
|
+
*
|
|
14
|
+
* @function generateCreateRoute
|
|
15
|
+
* @param args - Object containing route definition properties
|
|
16
|
+
* @returns Generated TypeScript code string for the route definition
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* const args = {
|
|
20
|
+
* routeName: 'getUsersRoute',
|
|
21
|
+
* tagsCode: 'tags:["users"],',
|
|
22
|
+
* methodCode: 'method:"get",',
|
|
23
|
+
* pathCode: 'path:"/users",',
|
|
24
|
+
* descriptionCode: 'description:"Get all users",',
|
|
25
|
+
* securityCode: 'security:[{"bearerAuth":[]}],',
|
|
26
|
+
* requestParams: 'request:{query:z.object({limit:z.number()})},',
|
|
27
|
+
* responsesCode: 'responses:{200:{description:"Success",content:{"application/json":{schema:UserList}}}}'
|
|
28
|
+
* }
|
|
29
|
+
*
|
|
30
|
+
* const code = generateCreateRoute(args)
|
|
31
|
+
* // Returns:
|
|
32
|
+
* // export const getUsersRoute = createRoute({
|
|
33
|
+
* // tags:["users"],
|
|
34
|
+
* // method:"get",
|
|
35
|
+
* // path:"/users",
|
|
36
|
+
* // description:"Get all users",
|
|
37
|
+
* // security:[{"bearerAuth":[]}],
|
|
38
|
+
* // request:{query:z.object({limit:z.number()})},
|
|
39
|
+
* // responses:{200:{description:"Success",content:{"application/json":{schema:UserList}}}}
|
|
40
|
+
* // })
|
|
41
|
+
*
|
|
42
|
+
* @note
|
|
43
|
+
* - Combines all route properties into a single createRoute call
|
|
44
|
+
* - Handles:
|
|
45
|
+
* - Route name export
|
|
46
|
+
* - OpenAPI tags
|
|
47
|
+
* - HTTP method
|
|
48
|
+
* - Path definition
|
|
49
|
+
* - Route description
|
|
50
|
+
* - Security requirements
|
|
51
|
+
* - Request parameters and validation
|
|
52
|
+
* - Response definitions
|
|
53
|
+
* - Properties are joined without additional spacing
|
|
54
|
+
* - Undefined/empty properties are safely handled
|
|
55
|
+
* - Creates a valid TypeScript export statement
|
|
56
|
+
*/
|
|
57
|
+
export declare function generateCreateRoute(args: GenerateCreateRouteParams): string;
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateCreateRoute = generateCreateRoute;
|
|
4
|
+
/**
|
|
5
|
+
* Generates TypeScript code for a Hono route definition
|
|
6
|
+
*
|
|
7
|
+
* @function generateCreateRoute
|
|
8
|
+
* @param args - Object containing route definition properties
|
|
9
|
+
* @returns Generated TypeScript code string for the route definition
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const args = {
|
|
13
|
+
* routeName: 'getUsersRoute',
|
|
14
|
+
* tagsCode: 'tags:["users"],',
|
|
15
|
+
* methodCode: 'method:"get",',
|
|
16
|
+
* pathCode: 'path:"/users",',
|
|
17
|
+
* descriptionCode: 'description:"Get all users",',
|
|
18
|
+
* securityCode: 'security:[{"bearerAuth":[]}],',
|
|
19
|
+
* requestParams: 'request:{query:z.object({limit:z.number()})},',
|
|
20
|
+
* responsesCode: 'responses:{200:{description:"Success",content:{"application/json":{schema:UserList}}}}'
|
|
21
|
+
* }
|
|
22
|
+
*
|
|
23
|
+
* const code = generateCreateRoute(args)
|
|
24
|
+
* // Returns:
|
|
25
|
+
* // export const getUsersRoute = createRoute({
|
|
26
|
+
* // tags:["users"],
|
|
27
|
+
* // method:"get",
|
|
28
|
+
* // path:"/users",
|
|
29
|
+
* // description:"Get all users",
|
|
30
|
+
* // security:[{"bearerAuth":[]}],
|
|
31
|
+
* // request:{query:z.object({limit:z.number()})},
|
|
32
|
+
* // responses:{200:{description:"Success",content:{"application/json":{schema:UserList}}}}
|
|
33
|
+
* // })
|
|
34
|
+
*
|
|
35
|
+
* @note
|
|
36
|
+
* - Combines all route properties into a single createRoute call
|
|
37
|
+
* - Handles:
|
|
38
|
+
* - Route name export
|
|
39
|
+
* - OpenAPI tags
|
|
40
|
+
* - HTTP method
|
|
41
|
+
* - Path definition
|
|
42
|
+
* - Route description
|
|
43
|
+
* - Security requirements
|
|
44
|
+
* - Request parameters and validation
|
|
45
|
+
* - Response definitions
|
|
46
|
+
* - Properties are joined without additional spacing
|
|
47
|
+
* - Undefined/empty properties are safely handled
|
|
48
|
+
* - Creates a valid TypeScript export statement
|
|
49
|
+
*/
|
|
50
|
+
function generateCreateRoute(args) {
|
|
51
|
+
const properties = [
|
|
52
|
+
args.tagsCode,
|
|
53
|
+
args.methodCode,
|
|
54
|
+
args.pathCode,
|
|
55
|
+
args.descriptionCode,
|
|
56
|
+
args.securityCode,
|
|
57
|
+
args.requestParams,
|
|
58
|
+
args.responsesCode,
|
|
59
|
+
].join('');
|
|
60
|
+
return `export const ${args.routeName}=createRoute({${properties}})`;
|
|
61
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { OpenAPIPaths } from '../../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates TypeScript code for all valid routes based on OpenAPI paths
|
|
4
|
+
*
|
|
5
|
+
* @function generateRouteCode
|
|
6
|
+
* @param openAPIPaths - OpenAPI paths object containing route definitions
|
|
7
|
+
* @returns Generated TypeScript code string for all valid routes
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const paths = {
|
|
11
|
+
* '/users': {
|
|
12
|
+
* get: {
|
|
13
|
+
* operationId: 'getUsers',
|
|
14
|
+
* responses: {
|
|
15
|
+
* '200': {
|
|
16
|
+
* description: 'List of users',
|
|
17
|
+
* content: {
|
|
18
|
+
* 'application/json': {
|
|
19
|
+
* schema: { $ref: '#/components/schemas/UserList' }
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* },
|
|
25
|
+
* post: {
|
|
26
|
+
* operationId: 'createUser',
|
|
27
|
+
* requestBody: {
|
|
28
|
+
* content: {
|
|
29
|
+
* 'application/json': {
|
|
30
|
+
* schema: { $ref: '#/components/schemas/CreateUserInput' }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* const code = generateRouteCode(paths)
|
|
39
|
+
* // Returns TypeScript code for both GET and POST /users routes
|
|
40
|
+
*
|
|
41
|
+
* @note
|
|
42
|
+
* - Processes each path and HTTP method combination
|
|
43
|
+
* - Validates HTTP methods and operation objects
|
|
44
|
+
* - Skips invalid or unsupported route definitions
|
|
45
|
+
* - Handles:
|
|
46
|
+
* - Path parameters
|
|
47
|
+
* - Query parameters
|
|
48
|
+
* - Request bodies
|
|
49
|
+
* - Response schemas
|
|
50
|
+
* - Generates type-safe route handlers using zod validation
|
|
51
|
+
* - Combines all routes with proper spacing
|
|
52
|
+
*/
|
|
53
|
+
export declare function generateRouteCode(openAPIPaths: OpenAPIPaths): string;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRouteCode = generateRouteCode;
|
|
4
|
+
const is_http_method_1 = require("../../../core/validator/is-http-method");
|
|
5
|
+
const generate_route_1 = require("./generate-route");
|
|
6
|
+
const is_operation_1 = require("../../../core/validator/is-operation");
|
|
7
|
+
/**
|
|
8
|
+
* Generates TypeScript code for all valid routes based on OpenAPI paths
|
|
9
|
+
*
|
|
10
|
+
* @function generateRouteCode
|
|
11
|
+
* @param openAPIPaths - OpenAPI paths object containing route definitions
|
|
12
|
+
* @returns Generated TypeScript code string for all valid routes
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const paths = {
|
|
16
|
+
* '/users': {
|
|
17
|
+
* get: {
|
|
18
|
+
* operationId: 'getUsers',
|
|
19
|
+
* responses: {
|
|
20
|
+
* '200': {
|
|
21
|
+
* description: 'List of users',
|
|
22
|
+
* content: {
|
|
23
|
+
* 'application/json': {
|
|
24
|
+
* schema: { $ref: '#/components/schemas/UserList' }
|
|
25
|
+
* }
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* }
|
|
29
|
+
* },
|
|
30
|
+
* post: {
|
|
31
|
+
* operationId: 'createUser',
|
|
32
|
+
* requestBody: {
|
|
33
|
+
* content: {
|
|
34
|
+
* 'application/json': {
|
|
35
|
+
* schema: { $ref: '#/components/schemas/CreateUserInput' }
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* const code = generateRouteCode(paths)
|
|
44
|
+
* // Returns TypeScript code for both GET and POST /users routes
|
|
45
|
+
*
|
|
46
|
+
* @note
|
|
47
|
+
* - Processes each path and HTTP method combination
|
|
48
|
+
* - Validates HTTP methods and operation objects
|
|
49
|
+
* - Skips invalid or unsupported route definitions
|
|
50
|
+
* - Handles:
|
|
51
|
+
* - Path parameters
|
|
52
|
+
* - Query parameters
|
|
53
|
+
* - Request bodies
|
|
54
|
+
* - Response schemas
|
|
55
|
+
* - Generates type-safe route handlers using zod validation
|
|
56
|
+
* - Combines all routes with proper spacing
|
|
57
|
+
*/
|
|
58
|
+
function generateRouteCode(openAPIPaths) {
|
|
59
|
+
const routes = [];
|
|
60
|
+
// 1. flattening and processing OpenAPI paths
|
|
61
|
+
for (const [path, pathItem] of Object.entries(openAPIPaths)) {
|
|
62
|
+
// 2.skip if path definition does not exist
|
|
63
|
+
if (!pathItem)
|
|
64
|
+
continue;
|
|
65
|
+
// 3. loop through HTTP methods for each path
|
|
66
|
+
for (const [method, pathItemValue] of Object.entries(pathItem)) {
|
|
67
|
+
// 3.1. skip parameters key and undefined operations
|
|
68
|
+
if (method === 'parameters' || !pathItemValue)
|
|
69
|
+
continue;
|
|
70
|
+
// 3.2. check if the method is an actual HTTP method
|
|
71
|
+
if (!(0, is_http_method_1.isHttpMethod)(method))
|
|
72
|
+
continue;
|
|
73
|
+
// 3.3 exclude the possibility of string or Parameter[]
|
|
74
|
+
if (typeof pathItemValue === 'string' || Array.isArray(pathItemValue))
|
|
75
|
+
continue;
|
|
76
|
+
// 3.4 at this point, pathItemValue is only a possibility for Operation
|
|
77
|
+
if (!(0, is_operation_1.isOperation)(pathItemValue))
|
|
78
|
+
continue;
|
|
79
|
+
// 3.5 generating the root code and adding it to the array
|
|
80
|
+
routes.push((0, generate_route_1.generateRoute)(path, method, pathItemValue));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// 4. exclude invalid routes and join them with a newline
|
|
84
|
+
return routes.filter(Boolean).join('\n\n');
|
|
85
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a route name from HTTP method and path
|
|
3
|
+
*
|
|
4
|
+
* @param method - HTTP method (e.g., 'get', 'post', 'put')
|
|
5
|
+
* @param path - URL path pattern (e.g., '/users/{id}/posts')
|
|
6
|
+
* @returns Formatted route name string
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // Basic path
|
|
10
|
+
* generateRouteName('get', '/users')
|
|
11
|
+
* // Returns: 'getUsersRoute'
|
|
12
|
+
*
|
|
13
|
+
* // Path with parameters
|
|
14
|
+
* generateRouteName('post', '/users/{id}/posts')
|
|
15
|
+
* // Returns: 'postUsersIdPostsRoute'
|
|
16
|
+
*
|
|
17
|
+
* // Path with hyphens
|
|
18
|
+
* generateRouteName('put', '/user-profiles/{id}')
|
|
19
|
+
* // Returns: 'putUserProfilesIdRoute'
|
|
20
|
+
*
|
|
21
|
+
* @remarks
|
|
22
|
+
* Transformation process:
|
|
23
|
+
* 1. Replace special characters (/{}-) with spaces
|
|
24
|
+
* 2. Remove leading and trailing spaces
|
|
25
|
+
* 3. Split string by consecutive spaces
|
|
26
|
+
* 4. Capitalize each word
|
|
27
|
+
* 5. Join all words together
|
|
28
|
+
* 6. Add HTTP method and Route suffix
|
|
29
|
+
*/
|
|
30
|
+
export declare function generateRouteName(method: string, path: string): string;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.generateRouteName = generateRouteName;
|
|
4
|
+
const capitalize_1 = require("../../../core/text/capitalize");
|
|
5
|
+
/**
|
|
6
|
+
* Generates a route name from HTTP method and path
|
|
7
|
+
*
|
|
8
|
+
* @param method - HTTP method (e.g., 'get', 'post', 'put')
|
|
9
|
+
* @param path - URL path pattern (e.g., '/users/{id}/posts')
|
|
10
|
+
* @returns Formatted route name string
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Basic path
|
|
14
|
+
* generateRouteName('get', '/users')
|
|
15
|
+
* // Returns: 'getUsersRoute'
|
|
16
|
+
*
|
|
17
|
+
* // Path with parameters
|
|
18
|
+
* generateRouteName('post', '/users/{id}/posts')
|
|
19
|
+
* // Returns: 'postUsersIdPostsRoute'
|
|
20
|
+
*
|
|
21
|
+
* // Path with hyphens
|
|
22
|
+
* generateRouteName('put', '/user-profiles/{id}')
|
|
23
|
+
* // Returns: 'putUserProfilesIdRoute'
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Transformation process:
|
|
27
|
+
* 1. Replace special characters (/{}-) with spaces
|
|
28
|
+
* 2. Remove leading and trailing spaces
|
|
29
|
+
* 3. Split string by consecutive spaces
|
|
30
|
+
* 4. Capitalize each word
|
|
31
|
+
* 5. Join all words together
|
|
32
|
+
* 6. Add HTTP method and Route suffix
|
|
33
|
+
*/
|
|
34
|
+
function generateRouteName(method, path) {
|
|
35
|
+
// 1. api_path: `/user/createWithList`
|
|
36
|
+
// 2. replace(/[\/{}-]/g, ' ') -> ` user createWithList`
|
|
37
|
+
// 3. trim() -> `user createWithList`
|
|
38
|
+
// 4. split(/\s+/) -> `['user', 'createWithList']`
|
|
39
|
+
// 5. map(capitalize) -> `['User', 'CreateWithList']`
|
|
40
|
+
// 6. join('') -> `UserCreateWithList`
|
|
41
|
+
const api_path = path
|
|
42
|
+
.replace(/[\/{}-]/g, ' ')
|
|
43
|
+
.trim()
|
|
44
|
+
.split(/\s+/)
|
|
45
|
+
.map(capitalize_1.capitalize)
|
|
46
|
+
.join('');
|
|
47
|
+
return `${method}${api_path}Route`;
|
|
48
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Operation } from '../../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Generates TypeScript code for a Hono route based on OpenAPI operation details
|
|
4
|
+
*
|
|
5
|
+
* @function generateRoute
|
|
6
|
+
* @param path - The URL path pattern for the route
|
|
7
|
+
* @param method - The HTTP method (GET, POST, etc.)
|
|
8
|
+
* @param operation - The OpenAPI Operation object containing route details
|
|
9
|
+
* @returns Generated TypeScript code string for the route
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const operation = {
|
|
13
|
+
* tags: ['users'],
|
|
14
|
+
* description: 'Create a new user',
|
|
15
|
+
* security: [{ bearerAuth: [] }],
|
|
16
|
+
* requestBody: {
|
|
17
|
+
* content: {
|
|
18
|
+
* 'application/json': {
|
|
19
|
+
* schema: { $ref: '#/components/schemas/CreateUserInput' }
|
|
20
|
+
* }
|
|
21
|
+
* }
|
|
22
|
+
* },
|
|
23
|
+
* responses: {
|
|
24
|
+
* '201': {
|
|
25
|
+
* description: 'User created successfully',
|
|
26
|
+
* content: {
|
|
27
|
+
* 'application/json': {
|
|
28
|
+
* schema: { $ref: '#/components/schemas/User' }
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* const route = generateRoute('/users', 'post', operation)
|
|
36
|
+
* // Returns: TypeScript code for a type-safe POST /users route
|
|
37
|
+
*
|
|
38
|
+
* @note
|
|
39
|
+
* - Generates a complete route definition including:
|
|
40
|
+
* - Route name based on method and path
|
|
41
|
+
* - OpenAPI tags for documentation
|
|
42
|
+
* - HTTP method and path
|
|
43
|
+
* - Route description
|
|
44
|
+
* - Security requirements
|
|
45
|
+
* - Request parameters and body validation
|
|
46
|
+
* - Response schemas for different status codes
|
|
47
|
+
* - All components are properly escaped and formatted
|
|
48
|
+
* - Handles optional parameters appropriately
|
|
49
|
+
* - Integrates with Hono's createRoute function
|
|
50
|
+
*/
|
|
51
|
+
export declare function generateRoute(path: string, method: string, operation: Operation): string;
|