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.
Files changed (88) hide show
  1. package/README.md +471 -0
  2. package/dist/core/schema/references/find-references.d.ts +35 -0
  3. package/dist/core/schema/references/find-references.js +42 -0
  4. package/dist/core/schema/references/get-camel-case-schema-name.d.ts +7 -0
  5. package/dist/core/schema/references/get-camel-case-schema-name.js +14 -0
  6. package/dist/core/schema/references/get-ref-name.d.ts +38 -0
  7. package/dist/core/schema/references/get-ref-name.js +47 -0
  8. package/dist/core/schema/references/resolve-schema-order.d.ts +30 -0
  9. package/dist/core/schema/references/resolve-schema-order.js +48 -0
  10. package/dist/core/schema/references/resolve-schema-references.d.ts +49 -0
  11. package/dist/core/schema/references/resolve-schema-references.js +61 -0
  12. package/dist/core/schema/references/resolve-schemas-dependencies.d.ts +49 -0
  13. package/dist/core/schema/references/resolve-schemas-dependencies.js +65 -0
  14. package/dist/core/schema/references/traverse_schema.d.ts +48 -0
  15. package/dist/core/schema/references/traverse_schema.js +70 -0
  16. package/dist/core/text/capitalize.d.ts +18 -0
  17. package/dist/core/text/capitalize.js +23 -0
  18. package/dist/core/text/decapitalize.d.ts +17 -0
  19. package/dist/core/text/decapitalize.js +22 -0
  20. package/dist/core/text/remove-zod-prefix.d.ts +8 -0
  21. package/dist/core/text/remove-zod-prefix.js +13 -0
  22. package/dist/core/validator/is-format-number.d.ts +2 -0
  23. package/dist/core/validator/is-format-number.js +6 -0
  24. package/dist/core/validator/is-format-string.d.ts +2 -0
  25. package/dist/core/validator/is-format-string.js +24 -0
  26. package/dist/core/validator/is-http-method.d.ts +9 -0
  27. package/dist/core/validator/is-http-method.js +19 -0
  28. package/dist/core/validator/is-operation.d.ts +15 -0
  29. package/dist/core/validator/is-operation.js +19 -0
  30. package/dist/core/zod/get-zod-string-format.d.ts +30 -0
  31. package/dist/core/zod/get-zod-string-format.js +66 -0
  32. package/dist/generators/hono/generate-hono.d.ts +9 -0
  33. package/dist/generators/hono/generate-hono.js +29 -0
  34. package/dist/generators/openapi/components/generate-components-code.d.ts +46 -0
  35. package/dist/generators/openapi/components/generate-components-code.js +79 -0
  36. package/dist/generators/openapi/paths/generate-create-route.d.ts +58 -0
  37. package/dist/generators/openapi/paths/generate-create-route.js +61 -0
  38. package/dist/generators/openapi/paths/generate-route-code.d.ts +53 -0
  39. package/dist/generators/openapi/paths/generate-route-code.js +85 -0
  40. package/dist/generators/openapi/paths/generate-route-name.d.ts +30 -0
  41. package/dist/generators/openapi/paths/generate-route-name.js +48 -0
  42. package/dist/generators/openapi/paths/generate-route.d.ts +51 -0
  43. package/dist/generators/openapi/paths/generate-route.js +73 -0
  44. package/dist/generators/openapi/paths/generate-schemas-export.d.ts +11 -0
  45. package/dist/generators/openapi/paths/generate-schemas-export.js +18 -0
  46. package/dist/generators/request/body/generate-insert-request-body.d.ts +32 -0
  47. package/dist/generators/request/body/generate-insert-request-body.js +37 -0
  48. package/dist/generators/request/body/generate-request-body.d.ts +18 -0
  49. package/dist/generators/request/body/generate-request-body.js +23 -0
  50. package/dist/generators/request/object/generate-format-request-object.d.ts +29 -0
  51. package/dist/generators/request/object/generate-format-request-object.js +34 -0
  52. package/dist/generators/request/params/generate-params-object.d.ts +62 -0
  53. package/dist/generators/request/params/generate-params-object.js +93 -0
  54. package/dist/generators/request/params/generate-request-parameter.d.ts +49 -0
  55. package/dist/generators/request/params/generate-request-parameter.js +79 -0
  56. package/dist/generators/request/params/generate-request-params-array.d.ts +37 -0
  57. package/dist/generators/request/params/generate-request-params-array.js +56 -0
  58. package/dist/generators/request/params/generate-request-params.d.ts +34 -0
  59. package/dist/generators/request/params/generate-request-params.js +39 -0
  60. package/dist/generators/response/schemas/generate-response-schema.d.ts +59 -0
  61. package/dist/generators/response/schemas/generate-response-schema.js +83 -0
  62. package/dist/generators/zod/generate-zod-array.d.ts +31 -0
  63. package/dist/generators/zod/generate-zod-array.js +36 -0
  64. package/dist/generators/zod/generate-zod-coerce.d.ts +10 -0
  65. package/dist/generators/zod/generate-zod-coerce.js +17 -0
  66. package/dist/generators/zod/generate-zod-integer-schema.d.ts +15 -0
  67. package/dist/generators/zod/generate-zod-integer-schema.js +24 -0
  68. package/dist/generators/zod/generate-zod-number-schema.d.ts +16 -0
  69. package/dist/generators/zod/generate-zod-number-schema.js +26 -0
  70. package/dist/generators/zod/generate-zod-object-schema.d.ts +36 -0
  71. package/dist/generators/zod/generate-zod-object-schema.js +43 -0
  72. package/dist/generators/zod/generate-zod-properties-schema.d.ts +50 -0
  73. package/dist/generators/zod/generate-zod-properties-schema.js +60 -0
  74. package/dist/generators/zod/generate-zod-property-schema.d.ts +25 -0
  75. package/dist/generators/zod/generate-zod-property-schema.js +47 -0
  76. package/dist/generators/zod/generate-zod-record-schema.d.ts +34 -0
  77. package/dist/generators/zod/generate-zod-record-schema.js +37 -0
  78. package/dist/generators/zod/generate-zod-schema-definition.d.ts +22 -0
  79. package/dist/generators/zod/generate-zod-schema-definition.js +27 -0
  80. package/dist/generators/zod/generate-zod-schema.d.ts +59 -0
  81. package/dist/generators/zod/generate-zod-schema.js +136 -0
  82. package/dist/generators/zod/generate-zod-string-schema.d.ts +46 -0
  83. package/dist/generators/zod/generate-zod-string-schema.js +54 -0
  84. package/dist/index.d.ts +20 -0
  85. package/dist/index.js +85 -0
  86. package/dist/types/index.d.ts +170 -0
  87. package/dist/types/index.js +2 -0
  88. package/package.json +47 -0
@@ -0,0 +1,34 @@
1
+ import type { Format, Type } from '../../types';
2
+ /**
3
+ * Generates a Zod record schema for objects with additional properties
4
+ *
5
+ * @function generateZodRecordSchema
6
+ * @param additionalProperties - Schema definition for record values
7
+ * @param additionalProperties.type - Type of the record values (e.g., 'string', 'number')
8
+ * @param additionalProperties.format - Format specification for the value type (e.g., 'date-time', 'email')
9
+ * @returns Generated Zod record schema string
10
+ *
11
+ * @example
12
+ * // Basic string values
13
+ * generateZodRecordSchema({ type: 'string', format: undefined })
14
+ * // Returns: 'z.record(z.string(), z.string())'
15
+ *
16
+ * @example
17
+ * // Email format values
18
+ * generateZodRecordSchema({ type: 'string', format: 'email' })
19
+ * // Returns: 'z.record(z.string(), z.string().email())'
20
+ *
21
+ * @example
22
+ * // Integer values
23
+ * generateZodRecordSchema({ type: 'integer', format: 'int64' })
24
+ * // Returns: 'z.record(z.string(), z.number().int())'
25
+ *
26
+ * @remarks
27
+ * - Always uses string keys (z.string())
28
+ * - Value type is determined by the additionalProperties schema
29
+ * - Supports all Zod-compatible types and formats
30
+ */
31
+ export declare function generateZodRecordSchema(additionalProperties: {
32
+ type: Type;
33
+ format: Format;
34
+ }): string;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodRecordSchema = generateZodRecordSchema;
4
+ const generate_zod_schema_1 = require("./generate-zod-schema");
5
+ /**
6
+ * Generates a Zod record schema for objects with additional properties
7
+ *
8
+ * @function generateZodRecordSchema
9
+ * @param additionalProperties - Schema definition for record values
10
+ * @param additionalProperties.type - Type of the record values (e.g., 'string', 'number')
11
+ * @param additionalProperties.format - Format specification for the value type (e.g., 'date-time', 'email')
12
+ * @returns Generated Zod record schema string
13
+ *
14
+ * @example
15
+ * // Basic string values
16
+ * generateZodRecordSchema({ type: 'string', format: undefined })
17
+ * // Returns: 'z.record(z.string(), z.string())'
18
+ *
19
+ * @example
20
+ * // Email format values
21
+ * generateZodRecordSchema({ type: 'string', format: 'email' })
22
+ * // Returns: 'z.record(z.string(), z.string().email())'
23
+ *
24
+ * @example
25
+ * // Integer values
26
+ * generateZodRecordSchema({ type: 'integer', format: 'int64' })
27
+ * // Returns: 'z.record(z.string(), z.number().int())'
28
+ *
29
+ * @remarks
30
+ * - Always uses string keys (z.string())
31
+ * - Value type is determined by the additionalProperties schema
32
+ * - Supports all Zod-compatible types and formats
33
+ */
34
+ function generateZodRecordSchema(additionalProperties) {
35
+ const schema = (0, generate_zod_schema_1.generateZodSchema)(additionalProperties);
36
+ return `z.record(z.string(), ${schema})`;
37
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Creates a Zod schema constant declaration
3
+ *
4
+ * @function generateZodSchemaDefinition
5
+ * @param name - Name of the schema constant
6
+ * @param zodSchema - Zod schema definition string
7
+ * @returns Generated constant declaration string
8
+ *
9
+ * @example
10
+ * generateZodSchemaDefinition('userSchema', 'z.object({ id: z.number(), name: z.string() })')
11
+ * // Returns: 'const userSchema = z.object({ id: z.number(), name: z.string() })'
12
+ *
13
+ * @example
14
+ * generateZodSchemaDefinition('statusSchema', 'z.enum(["active", "inactive"])')
15
+ * // Returns: 'const statusSchema = z.enum(["active", "inactive"])'
16
+ *
17
+ * @remarks
18
+ * - Used for generating Zod schema constant declarations
19
+ * - Part of OpenAPI to Zod schema conversion process
20
+ * - Creates reusable schema definitions
21
+ */
22
+ export declare function generateZodSchemaDefinition(name: string, zodSchema: string): string;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodSchemaDefinition = generateZodSchemaDefinition;
4
+ /**
5
+ * Creates a Zod schema constant declaration
6
+ *
7
+ * @function generateZodSchemaDefinition
8
+ * @param name - Name of the schema constant
9
+ * @param zodSchema - Zod schema definition string
10
+ * @returns Generated constant declaration string
11
+ *
12
+ * @example
13
+ * generateZodSchemaDefinition('userSchema', 'z.object({ id: z.number(), name: z.string() })')
14
+ * // Returns: 'const userSchema = z.object({ id: z.number(), name: z.string() })'
15
+ *
16
+ * @example
17
+ * generateZodSchemaDefinition('statusSchema', 'z.enum(["active", "inactive"])')
18
+ * // Returns: 'const statusSchema = z.enum(["active", "inactive"])'
19
+ *
20
+ * @remarks
21
+ * - Used for generating Zod schema constant declarations
22
+ * - Part of OpenAPI to Zod schema conversion process
23
+ * - Creates reusable schema definitions
24
+ */
25
+ function generateZodSchemaDefinition(name, zodSchema) {
26
+ return `const ${name} = ${zodSchema}`;
27
+ }
@@ -0,0 +1,59 @@
1
+ import type { Schema } from '../../types';
2
+ /**
3
+ * Generates a Zod schema string from an OpenAPI/JSON Schema definition
4
+ *
5
+ * @function generateZodSchema
6
+ * @param schema - The schema definition object
7
+ * @param schema.type - The type of the schema (e.g., 'string', 'object', 'array')
8
+ * @param schema.format - Optional format specification (e.g., 'date-time', 'email')
9
+ * @param schema.pattern - Optional regex pattern for string validation
10
+ * @param schema.minLength - Optional minimum length for string validation
11
+ * @param schema.maxLength - Optional maximum length for string validation
12
+ * @param schema.properties - Object properties definition
13
+ * @param schema.required - Array of required property names
14
+ * @param schema.items - Schema for array items
15
+ * @param schema.enum - Array of enum values
16
+ * @param schema.additionalProperties - Schema for additional properties in objects
17
+ * @returns Generated Zod schema string
18
+ *
19
+ * @example
20
+ * // Enum type
21
+ * generateZodSchema({ enum: ['active', 'inactive'] })
22
+ * // Returns: 'z.enum(["active","inactive"])'
23
+ *
24
+ * @example
25
+ * // Object with properties
26
+ * generateZodSchema({
27
+ * type: 'object',
28
+ * properties: {
29
+ * name: { type: 'string' },
30
+ * age: { type: 'number' }
31
+ * },
32
+ * required: ['name']
33
+ * })
34
+ * // Returns: 'z.object({name: z.string(), age: z.number().optional()})'
35
+ *
36
+ * @example
37
+ * // String with validation
38
+ * generateZodSchema({
39
+ * type: 'string',
40
+ * minLength: 3,
41
+ * maxLength: 10,
42
+ * pattern: '^[a-z]+$'
43
+ * })
44
+ * // Returns: 'z.string().min(3).max(10).regex(/^[a-z]+$/)'
45
+ *
46
+ * @example
47
+ * // Array with items
48
+ * generateZodSchema({
49
+ * type: 'array',
50
+ * items: { type: 'string' }
51
+ * })
52
+ * // Returns: 'z.array(z.string())'
53
+ *
54
+ * @remarks
55
+ * - Handles special cases first (enum, object, string with validation, array with items)
56
+ * - Falls back to basic type mapping for simple types
57
+ * - Returns z.any() for unknown types with a warning
58
+ */
59
+ export declare function generateZodSchema(schema: Schema): string;
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodSchema = generateZodSchema;
4
+ const generate_zod_array_1 = require("./generate-zod-array");
5
+ const generate_zod_string_schema_1 = require("./generate-zod-string-schema");
6
+ const generate_zod_properties_schema_1 = require("./generate-zod-properties-schema");
7
+ const generate_zod_record_schema_1 = require("./generate-zod-record-schema");
8
+ const is_format_string_1 = require("../../core/validator/is-format-string");
9
+ const generate_zod_number_schema_1 = require("./generate-zod-number-schema");
10
+ const generate_zod_integer_schema_1 = require("./generate-zod-integer-schema");
11
+ /**
12
+ * Mapping of OpenAPI/JSON Schema types to Zod schema strings
13
+ *
14
+ * @remarks
15
+ * - Basic types are mapped directly to their Zod equivalents
16
+ * - Complex types (object, string with validation, array with items) are handled separately
17
+ * - Used as a fallback for simple type conversions
18
+ */
19
+ const TYPE_TO_ZOD_SCHEMA = {
20
+ number: 'z.number()',
21
+ integer: 'z.number().int()',
22
+ bigint: 'z.bigint()',
23
+ boolean: 'z.boolean()',
24
+ date: 'z.date()',
25
+ null: 'z.null()',
26
+ any: 'z.any()',
27
+ unknown: 'z.unknown()',
28
+ string: 'z.string()',
29
+ object: 'z.object({})',
30
+ array: 'z.array()',
31
+ };
32
+ /**
33
+ * Generates a Zod schema string from an OpenAPI/JSON Schema definition
34
+ *
35
+ * @function generateZodSchema
36
+ * @param schema - The schema definition object
37
+ * @param schema.type - The type of the schema (e.g., 'string', 'object', 'array')
38
+ * @param schema.format - Optional format specification (e.g., 'date-time', 'email')
39
+ * @param schema.pattern - Optional regex pattern for string validation
40
+ * @param schema.minLength - Optional minimum length for string validation
41
+ * @param schema.maxLength - Optional maximum length for string validation
42
+ * @param schema.properties - Object properties definition
43
+ * @param schema.required - Array of required property names
44
+ * @param schema.items - Schema for array items
45
+ * @param schema.enum - Array of enum values
46
+ * @param schema.additionalProperties - Schema for additional properties in objects
47
+ * @returns Generated Zod schema string
48
+ *
49
+ * @example
50
+ * // Enum type
51
+ * generateZodSchema({ enum: ['active', 'inactive'] })
52
+ * // Returns: 'z.enum(["active","inactive"])'
53
+ *
54
+ * @example
55
+ * // Object with properties
56
+ * generateZodSchema({
57
+ * type: 'object',
58
+ * properties: {
59
+ * name: { type: 'string' },
60
+ * age: { type: 'number' }
61
+ * },
62
+ * required: ['name']
63
+ * })
64
+ * // Returns: 'z.object({name: z.string(), age: z.number().optional()})'
65
+ *
66
+ * @example
67
+ * // String with validation
68
+ * generateZodSchema({
69
+ * type: 'string',
70
+ * minLength: 3,
71
+ * maxLength: 10,
72
+ * pattern: '^[a-z]+$'
73
+ * })
74
+ * // Returns: 'z.string().min(3).max(10).regex(/^[a-z]+$/)'
75
+ *
76
+ * @example
77
+ * // Array with items
78
+ * generateZodSchema({
79
+ * type: 'array',
80
+ * items: { type: 'string' }
81
+ * })
82
+ * // Returns: 'z.array(z.string())'
83
+ *
84
+ * @remarks
85
+ * - Handles special cases first (enum, object, string with validation, array with items)
86
+ * - Falls back to basic type mapping for simple types
87
+ * - Returns z.any() for unknown types with a warning
88
+ */
89
+ function generateZodSchema(schema) {
90
+ const { type, format, pattern, minLength, maxLength, minimum, maximum, properties, required = [], items, enum: enumValues, additionalProperties, } = schema;
91
+ if (enumValues) {
92
+ return `z.enum(${JSON.stringify(enumValues)})`;
93
+ }
94
+ if (type === 'object') {
95
+ if (additionalProperties) {
96
+ return (0, generate_zod_record_schema_1.generateZodRecordSchema)(additionalProperties);
97
+ }
98
+ if (!properties) {
99
+ return 'z.object({})';
100
+ }
101
+ return (0, generate_zod_properties_schema_1.generateZodPropertiesSchema)(properties, required);
102
+ }
103
+ if (type === 'string') {
104
+ return (0, generate_zod_string_schema_1.generateZodStringSchema)({
105
+ pattern,
106
+ minLength,
107
+ maxLength,
108
+ format: format && (0, is_format_string_1.isFormatString)(format) ? format : undefined,
109
+ });
110
+ }
111
+ if (type === 'number') {
112
+ return (0, generate_zod_number_schema_1.generateZodNumberSchema)({
113
+ pattern,
114
+ minLength,
115
+ maxLength,
116
+ minimum,
117
+ maximum,
118
+ });
119
+ }
120
+ if (type === 'integer') {
121
+ return (0, generate_zod_integer_schema_1.generateZodIntegerSchema)({
122
+ minLength,
123
+ maxLength,
124
+ minimum,
125
+ maximum,
126
+ });
127
+ }
128
+ if (type === 'array' && items) {
129
+ return (0, generate_zod_array_1.generateZodArray)(generateZodSchema(items));
130
+ }
131
+ if (type) {
132
+ return TYPE_TO_ZOD_SCHEMA[type];
133
+ }
134
+ console.warn(`Unknown type: ${type}, falling back to z.any()`);
135
+ return 'z.any()';
136
+ }
@@ -0,0 +1,46 @@
1
+ import type { FormatString } from '../../types';
2
+ type GenerateZodStringSchemaParams = {
3
+ pattern?: string;
4
+ minLength?: number;
5
+ maxLength?: number;
6
+ format?: FormatString;
7
+ };
8
+ /**
9
+ * Generates a Zod schema string for string validation
10
+ *
11
+ * @function generateZodStringSchema
12
+ * @param args - Parameters for string schema generation
13
+ * @returns Generated Zod schema string with chained validations
14
+ *
15
+ * @example
16
+ * // Basic string validation
17
+ * generateZodStringSchema({})
18
+ * // Returns: 'z.string()'
19
+ *
20
+ * @example
21
+ * // With regex pattern
22
+ * generateZodStringSchema({ pattern: '^[A-Z]+$' })
23
+ * // Returns: 'z.string().regex(/^[A-Z]+$/)'
24
+ *
25
+ * @example
26
+ * // With length constraints
27
+ * generateZodStringSchema({ minLength: 3, maxLength: 10 })
28
+ * // Returns: 'z.string().min(3).max(10)'
29
+ *
30
+ * @example
31
+ * // With format
32
+ * generateZodStringSchema({ format: 'email' })
33
+ * // Returns: 'z.string().email()'
34
+ *
35
+ * @example
36
+ * // Combined validations
37
+ * generateZodStringSchema({
38
+ * pattern: '^[a-z]+$',
39
+ * minLength: 3,
40
+ * maxLength: 10,
41
+ * format: 'email'
42
+ * })
43
+ * // Returns: 'z.string().regex(/^[a-z]+$/).min(3).max(10).email()'
44
+ */
45
+ export declare function generateZodStringSchema(args: GenerateZodStringSchemaParams): string;
46
+ export {};
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodStringSchema = generateZodStringSchema;
4
+ const get_zod_string_format_1 = require("../../core/zod/get-zod-string-format");
5
+ /**
6
+ * Generates a Zod schema string for string validation
7
+ *
8
+ * @function generateZodStringSchema
9
+ * @param args - Parameters for string schema generation
10
+ * @returns Generated Zod schema string with chained validations
11
+ *
12
+ * @example
13
+ * // Basic string validation
14
+ * generateZodStringSchema({})
15
+ * // Returns: 'z.string()'
16
+ *
17
+ * @example
18
+ * // With regex pattern
19
+ * generateZodStringSchema({ pattern: '^[A-Z]+$' })
20
+ * // Returns: 'z.string().regex(/^[A-Z]+$/)'
21
+ *
22
+ * @example
23
+ * // With length constraints
24
+ * generateZodStringSchema({ minLength: 3, maxLength: 10 })
25
+ * // Returns: 'z.string().min(3).max(10)'
26
+ *
27
+ * @example
28
+ * // With format
29
+ * generateZodStringSchema({ format: 'email' })
30
+ * // Returns: 'z.string().email()'
31
+ *
32
+ * @example
33
+ * // Combined validations
34
+ * generateZodStringSchema({
35
+ * pattern: '^[a-z]+$',
36
+ * minLength: 3,
37
+ * maxLength: 10,
38
+ * format: 'email'
39
+ * })
40
+ * // Returns: 'z.string().regex(/^[a-z]+$/).min(3).max(10).email()'
41
+ */
42
+ function generateZodStringSchema(args) {
43
+ const validations = ['z.string()'];
44
+ const { pattern, minLength, maxLength, format } = args;
45
+ if (pattern)
46
+ validations.push(`.regex(/${pattern}/)`);
47
+ if (minLength)
48
+ validations.push(`.min(${minLength})`);
49
+ if (maxLength)
50
+ validations.push(`.max(${maxLength})`);
51
+ if (format)
52
+ validations.push((0, get_zod_string_format_1.getZodFormatString)(format));
53
+ return validations.join('');
54
+ }
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * CLI entry point for hono-takibi
4
+ *
5
+ * Usage:
6
+ * ```
7
+ * hono-takibi <input-file> [-o output-file]
8
+ * ```
9
+ *
10
+ * @example
11
+ * ```bash
12
+ * # Global install
13
+ * npm install -g hono-takibi
14
+ * hono-takibi openapi.yaml -o routes.ts
15
+ *
16
+ * # NPX usage
17
+ * npx hono-takibi openapi.yaml -o routes.ts
18
+ * ```
19
+ */
20
+ export declare function main(dev?: boolean): Promise<boolean>;
package/dist/index.js ADDED
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.main = main;
8
+ const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"));
9
+ const node_fs_1 = __importDefault(require("node:fs"));
10
+ const node_path_1 = __importDefault(require("node:path"));
11
+ const prettier_1 = require("prettier");
12
+ const generate_hono_1 = require("./generators/hono/generate-hono");
13
+ /**
14
+ * CLI entry point for hono-takibi
15
+ *
16
+ * Usage:
17
+ * ```
18
+ * hono-takibi <input-file> [-o output-file]
19
+ * ```
20
+ *
21
+ * @example
22
+ * ```bash
23
+ * # Global install
24
+ * npm install -g hono-takibi
25
+ * hono-takibi openapi.yaml -o routes.ts
26
+ *
27
+ * # NPX usage
28
+ * npx hono-takibi openapi.yaml -o routes.ts
29
+ * ```
30
+ */
31
+ async function main(dev = false) {
32
+ // 1. argv ['**/bin/node', '**/dist/index.js', 'example/pet-store.yaml', '-o', 'routes/petstore-index.ts']
33
+ // 2. slice [ 'example/pet-store.yaml', '-o', 'routes/petstore-index.ts' ]
34
+ const args = process.argv.slice(2);
35
+ // 3. input = 'example/pet-store.yaml'
36
+ const input = args[0];
37
+ // 4. output = 'routes/petstore-index.ts'
38
+ const output = args[args.indexOf('-o') + 1];
39
+ try {
40
+ // 5. parse OpenAPI YAML or JSON
41
+ const openAPI = (await swagger_parser_1.default.parse(input));
42
+ // 6. generate Hono code
43
+ const hono = (0, generate_hono_1.generateHono)(openAPI);
44
+ // 7. format code
45
+ const formattedCode = await (0, prettier_1.format)(hono, {
46
+ parser: 'typescript',
47
+ printWidth: 100,
48
+ singleQuote: true,
49
+ semi: false,
50
+ });
51
+ // 8. write to file
52
+ if (output) {
53
+ // 8.1 output routes/petstore-index.ts
54
+ const outputDir = node_path_1.default.dirname(output);
55
+ // 8.2 outputDir routes
56
+ if (!node_fs_1.default.existsSync(outputDir)) {
57
+ node_fs_1.default.mkdirSync(outputDir, { recursive: true });
58
+ }
59
+ }
60
+ // 9. write to file
61
+ node_fs_1.default.writeFileSync(output, formattedCode, { encoding: 'utf-8' });
62
+ console.log(`Generated code written to ${output}`);
63
+ return true;
64
+ }
65
+ catch (e) {
66
+ console.error('Usage: hono-takibi <input-file> [-o output-file]');
67
+ if (e instanceof Error) {
68
+ console.error('Error processing OpenAPI document:', e.message);
69
+ if (dev) {
70
+ throw e;
71
+ }
72
+ }
73
+ if (dev) {
74
+ throw new Error('Unknown error occurred');
75
+ }
76
+ return false;
77
+ }
78
+ }
79
+ if (require.main === module) {
80
+ main().then((success) => {
81
+ if (!success) {
82
+ process.exit(1);
83
+ }
84
+ });
85
+ }