hono-takibi 0.3.0 → 0.3.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.
Files changed (73) hide show
  1. package/README.md +19 -6
  2. package/dist/config/index.d.ts +8 -2
  3. package/dist/config/index.js +5 -1
  4. package/dist/core/{schema/references/get-camel-case-schema-name.js → helper/get-camel-case-schema-name-helper.js} +1 -1
  5. package/dist/core/{schema/references/get-pascal-case-schema-name.js → helper/get-pascal-case-schema-name-helper.js} +1 -1
  6. package/dist/core/helper/get-variable-schema-name-helper.js +4 -4
  7. package/dist/core/text/sanitize.d.ts +1 -0
  8. package/dist/core/text/sanitize.js +6 -0
  9. package/dist/format/index.d.ts +1 -0
  10. package/dist/format/index.js +12 -0
  11. package/dist/generators/app/generate-app-route-handler.d.ts +1 -0
  12. package/dist/generators/app/generate-app-route-handler.js +6 -0
  13. package/dist/generators/handler/generate-handler-name.d.ts +1 -0
  14. package/dist/generators/handler/generate-handler-name.js +7 -0
  15. package/dist/generators/handler/generate-handler.d.ts +1 -0
  16. package/dist/generators/handler/generate-handler.js +6 -0
  17. package/dist/generators/hono/app/generators/generate-apply-openapi-routes.d.ts +5 -0
  18. package/dist/generators/hono/app/generators/generate-apply-openapi-routes.js +11 -0
  19. package/dist/generators/hono/app/generators/generate-import-routes.d.ts +3 -0
  20. package/dist/generators/hono/app/generators/generate-import-routes.js +17 -0
  21. package/dist/generators/hono/app/helper/get-route-maps.d.ts +6 -0
  22. package/dist/generators/hono/app/helper/get-route-maps.js +16 -0
  23. package/dist/generators/hono/app/helper/process-import-map.d.ts +8 -0
  24. package/dist/generators/hono/app/helper/process-import-map.js +17 -0
  25. package/dist/generators/hono/app/index.d.ts +3 -0
  26. package/dist/generators/hono/app/index.js +55 -0
  27. package/dist/generators/hono/generate-zod-openapi-hono.js +4 -1
  28. package/dist/generators/hono/handler/generate-zod-openapi-hono-handler.d.ts +9 -0
  29. package/dist/generators/hono/handler/generate-zod-openapi-hono-handler.js +61 -0
  30. package/dist/generators/hono/handler/helper/group-handlers-by-file-name-helper.d.ts +2 -0
  31. package/dist/generators/hono/handler/helper/group-handlers-by-file-name-helper.js +24 -0
  32. package/dist/generators/hono/handler/import/generate-import-handlers.d.ts +4 -0
  33. package/dist/generators/hono/handler/import/generate-import-handlers.js +21 -0
  34. package/dist/generators/hono/handler/import/get-handler-imports.d.ts +7 -0
  35. package/dist/generators/hono/handler/import/get-handler-imports.js +18 -0
  36. package/dist/generators/openapi/components/allof/generate-allof-code.js +3 -1
  37. package/dist/generators/openapi/components/anyof/generate-anyof-code.js +3 -2
  38. package/dist/generators/openapi/components/generate-components-code.js +6 -3
  39. package/dist/generators/openapi/components/generate-register-component.d.ts +2 -0
  40. package/dist/generators/openapi/components/generate-register-component.js +10 -0
  41. package/dist/generators/openapi/components/oneof/generate-oneof-code.js +3 -2
  42. package/dist/generators/openapi/docs/generate-docs.d.ts +8 -0
  43. package/dist/generators/openapi/docs/generate-docs.js +12 -0
  44. package/dist/generators/openapi/paths/generate-route.js +3 -3
  45. package/dist/generators/request/params/generate-params-object.js +1 -1
  46. package/dist/generators/types/generate-types-code.js +3 -0
  47. package/dist/generators/zod/generate-zod-enum.d.ts +2 -0
  48. package/dist/generators/zod/generate-zod-enum.js +11 -0
  49. package/dist/generators/zod/generate-zod-integer-schema.js +1 -1
  50. package/dist/generators/zod/generate-zod-intersection.d.ts +7 -0
  51. package/dist/generators/zod/generate-zod-intersection.js +12 -0
  52. package/dist/generators/zod/generate-zod-number-schema.js +1 -1
  53. package/dist/generators/zod/generate-zod-object.d.ts +3 -0
  54. package/dist/generators/zod/generate-zod-object.js +25 -0
  55. package/dist/generators/zod/generate-zod-property-schema.js +1 -1
  56. package/dist/generators/zod/generate-zod-record-schema.js +1 -1
  57. package/dist/generators/zod/generate-zod-string-schema.js +1 -1
  58. package/dist/generators/zod/generate-zod-union.d.ts +12 -0
  59. package/dist/generators/zod/generate-zod-union.js +17 -0
  60. package/dist/generators/zod/{generate-zod-schema-definition.d.ts → openapi/generate-zod-to-openapi-schema-definition.d.ts} +1 -1
  61. package/dist/generators/zod/{generate-zod-schema-definition.js → openapi/generate-zod-to-openapi-schema-definition.js} +2 -2
  62. package/dist/generators/zod/{generate-zod-to-openapi.d.ts → openapi/generate-zod-to-openapi.d.ts} +1 -1
  63. package/dist/generators/zod/{generate-zod-schema.d.ts → schema/generate-zod-schema.d.ts} +2 -2
  64. package/dist/generators/zod/{generate-zod-schema.js → schema/generate-zod-schema.js} +13 -31
  65. package/dist/generators/zod/sub/generate-zod-schema-from-sub-schema.js +1 -1
  66. package/dist/index.js +28 -9
  67. package/dist/types/index.d.ts +13 -1
  68. package/dist/vite-plugin/index.d.ts +10 -0
  69. package/dist/vite-plugin/index.js +31 -0
  70. package/package.json +10 -1
  71. /package/dist/core/{schema/references/get-camel-case-schema-name.d.ts → helper/get-camel-case-schema-name-helper.d.ts} +0 -0
  72. /package/dist/core/{schema/references/get-pascal-case-schema-name.d.ts → helper/get-pascal-case-schema-name-helper.d.ts} +0 -0
  73. /package/dist/generators/zod/{generate-zod-to-openapi.js → openapi/generate-zod-to-openapi.js} +0 -0
package/README.md CHANGED
@@ -452,16 +452,25 @@ You can customize the code generation behavior by creating a `hono-takibi.json`
452
452
 
453
453
  | Option | Type | Default | Description |
454
454
  |--------|------|---------|-------------|
455
- | `name` | `"camelCase"` \| `"PascalCase"` | `"camelCase"` | Naming convention for generated schema variables |
455
+ | `name` | `"PascalCase"` \| `"camelCase"` | `"PascalCase"` | Naming convention for generated schema variables |
456
456
  | `export` | `boolean` | `false` | When true, exports all schema definitions |
457
457
 
458
458
  ### Type Options
459
459
 
460
460
  | Option | Type | Default | Description |
461
461
  |--------|------|---------|-------------|
462
- | `name` | `"camelCase"` \| `"PascalCase"` | `"PascalCase"` | Naming convention for generated type definitions |
462
+ | `name` | `"PascalCase"` \| `"camelCase"` | `"PascalCase"` | Naming convention for generated type definitions |
463
463
  | `export` | `boolean` | `false` | When true, exports all type definitions |
464
464
 
465
+ ### App Options
466
+
467
+ | Option | Type | Default | Description |
468
+ |--------|------|---------|-------------|
469
+ | output | boolean | false | Controls the generation of application and handler files. When enabled, creates both the main application file and corresponding route handlers |
470
+ | test | boolean | false | Enables automatic generation of test files for your API endpoints. |
471
+ | basePath | string | "" | Specifies the base URL path for your API endpoints. |
472
+ | isDev | string | "process.env.NODE_ENV" | Defines the environment variable used to determine development mode. Controls features like Swagger UI availability |
473
+
465
474
  ## Input and Output
466
475
 
467
476
  You can specify input and output paths in two ways:
@@ -484,19 +493,23 @@ You can specify input and output paths in two ways:
484
493
 
485
494
  ### Examples
486
495
 
487
- * Default Behavior (camelCase schemas, PascalCase types)
496
+ * Default Behavior (PascalCase schemas, PascalCase types)
488
497
 
489
498
  ```json
490
499
  {
491
- "input": "src/openapi/openapi.yaml",
492
- "output": "src/openapi/index.ts",
493
500
  "schema": {
494
- "name": "camelCase",
501
+ "name": "PascalCase",
495
502
  "export": false
496
503
  },
497
504
  "type": {
498
505
  "name": "PascalCase",
499
506
  "export": false
507
+ },
508
+ "app": {
509
+ "output": true,
510
+ "test": true,
511
+ "basePath": "api",
512
+ "isDev": "process.env.NODE_ENV"
500
513
  }
501
514
  }
502
515
  ```
@@ -1,12 +1,18 @@
1
1
  export type Config = {
2
2
  schema: {
3
- name: 'camelCase' | 'PascalCase';
3
+ name: 'PascalCase' | 'camelCase';
4
4
  export: boolean;
5
5
  };
6
6
  type: {
7
- name: 'camelCase' | 'PascalCase';
7
+ name: 'PascalCase' | 'camelCase';
8
8
  export: boolean;
9
9
  };
10
+ app?: {
11
+ output?: boolean;
12
+ test?: boolean;
13
+ basePath?: string;
14
+ isDev?: string;
15
+ };
10
16
  input?: string;
11
17
  output?: string;
12
18
  };
@@ -8,13 +8,17 @@ exports.getConfig = getConfig;
8
8
  const node_fs_1 = __importDefault(require("node:fs"));
9
9
  exports.DEFAULT_CONFIG = {
10
10
  schema: {
11
- name: 'camelCase',
11
+ name: 'PascalCase',
12
12
  export: false,
13
13
  },
14
14
  type: {
15
15
  name: 'PascalCase',
16
16
  export: false,
17
17
  },
18
+ app: {
19
+ output: false,
20
+ test: false,
21
+ },
18
22
  };
19
23
  /**
20
24
  * Loads the configuration from the `hono-takibi.json` file or returns the default configuration.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getCamelCaseSchemaName = getCamelCaseSchemaName;
4
- const decapitalize_1 = require("../../text/decapitalize");
4
+ const decapitalize_1 = require("../text/decapitalize");
5
5
  /**
6
6
  * Generates a camelCase schema name from a given schema name.
7
7
  *
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getPascalCaseSchemaName = getPascalCaseSchemaName;
4
- const capitalize_1 = require("../../text/capitalize");
4
+ const capitalize_1 = require("../text/capitalize");
5
5
  /**
6
6
  * Generates a PascalCase schema name from a given schema name.
7
7
  *
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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");
4
+ const get_camel_case_schema_name_helper_1 = require("./get-camel-case-schema-name-helper");
5
+ const get_pascal_case_schema_name_helper_1 = require("./get-pascal-case-schema-name-helper");
6
6
  const getVariableSchemaNameHelper = (name, config) => {
7
7
  return config.schema.name === 'camelCase'
8
- ? (0, get_camel_case_schema_name_1.getCamelCaseSchemaName)(name)
9
- : (0, get_pascal_case_schema_name_1.getPascalCaseSchemaName)(name);
8
+ ? (0, get_camel_case_schema_name_helper_1.getCamelCaseSchemaName)(name)
9
+ : (0, get_pascal_case_schema_name_helper_1.getPascalCaseSchemaName)(name);
10
10
  };
11
11
  exports.getVariableSchemaNameHelper = getVariableSchemaNameHelper;
@@ -0,0 +1 @@
1
+ export declare function sanitize(text: string): string;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sanitize = sanitize;
4
+ function sanitize(text) {
5
+ return text.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim();
6
+ }
@@ -0,0 +1 @@
1
+ export declare function formatCode(code: string): Promise<string>;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatCode = formatCode;
4
+ const prettier_1 = require("prettier");
5
+ async function formatCode(code) {
6
+ return await (0, prettier_1.format)(code, {
7
+ parser: 'typescript',
8
+ printWidth: 100,
9
+ singleQuote: true,
10
+ semi: false,
11
+ });
12
+ }
@@ -0,0 +1 @@
1
+ export declare function generateAppRouteHandler(routeName: string, handlerName: string): string;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateAppRouteHandler = generateAppRouteHandler;
4
+ function generateAppRouteHandler(routeName, handlerName) {
5
+ return `.openapi(${routeName},${handlerName})`;
6
+ }
@@ -0,0 +1 @@
1
+ export declare function generateHandlerName(method: string, path: string): string;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateHandlerName = generateHandlerName;
4
+ const generate_route_name_1 = require("../openapi/paths/generate-route-name");
5
+ function generateHandlerName(method, path) {
6
+ return `${(0, generate_route_name_1.generateRouteName)(method, path)}Handler`;
7
+ }
@@ -0,0 +1 @@
1
+ export declare function generateHandler(handlerName: string, routeName: string): string;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateHandler = generateHandler;
4
+ function generateHandler(handlerName, routeName) {
5
+ return `export const ${handlerName}:RouteHandler<typeof ${routeName}>=async(c)=>{}`;
6
+ }
@@ -0,0 +1,5 @@
1
+ export declare function generateApplyOpenapiRoutes(routeMappings: {
2
+ routeName: string;
3
+ handlerName: string;
4
+ path: string;
5
+ }[]): string;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateApplyOpenapiRoutes = generateApplyOpenapiRoutes;
4
+ const generate_app_route_handler_1 = require("../../../app/generate-app-route-handler");
5
+ function generateApplyOpenapiRoutes(routeMappings) {
6
+ return routeMappings
7
+ .map(({ routeName, handlerName }) => {
8
+ return (0, generate_app_route_handler_1.generateAppRouteHandler)(routeName, handlerName);
9
+ })
10
+ .join('\n');
11
+ }
@@ -0,0 +1,3 @@
1
+ export declare function generateImportRoutes(importsMap: {
2
+ [importPath: string]: string[];
3
+ }): string[];
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateImportRoutes = generateImportRoutes;
4
+ function generateImportRoutes(importsMap) {
5
+ const importRoutes = [];
6
+ for (const [importPath, names] of Object.entries(importsMap)) {
7
+ const uniqueNames = Array.from(new Set(names));
8
+ if (importPath.includes('/index.ts')) {
9
+ const normalizedPath = importPath.replace(/\/index\.ts$/, '');
10
+ importRoutes.push(`import { ${uniqueNames.join(',')} } from './${normalizedPath}';`);
11
+ }
12
+ else {
13
+ importRoutes.push(`import { ${uniqueNames.join(',')} } from './${importPath}';`);
14
+ }
15
+ }
16
+ return importRoutes;
17
+ }
@@ -0,0 +1,6 @@
1
+ import type { OpenAPISpec } from '../../../../types';
2
+ export declare function getRouteMaps(openAPISpec: OpenAPISpec): {
3
+ routeName: string;
4
+ handlerName: string;
5
+ path: string;
6
+ }[];
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getRouteMaps = getRouteMaps;
4
+ const generate_handler_name_1 = require("../../../handler/generate-handler-name");
5
+ const generate_route_name_1 = require("../../../openapi/paths/generate-route-name");
6
+ function getRouteMaps(openAPISpec) {
7
+ const paths = openAPISpec.paths;
8
+ const routeMappings = Object.entries(paths).flatMap(([path, pathItem]) => {
9
+ return Object.entries(pathItem).flatMap(([method]) => {
10
+ const routeName = (0, generate_route_name_1.generateRouteName)(method, path);
11
+ const handlerName = (0, generate_handler_name_1.generateHandlerName)(method, path);
12
+ return { routeName, handlerName, path };
13
+ });
14
+ });
15
+ return routeMappings;
16
+ }
@@ -0,0 +1,8 @@
1
+ import type { Config } from '../../../../config';
2
+ export declare function processImportMap(routeMappings: {
3
+ routeName: string;
4
+ handlerName: string;
5
+ path: string;
6
+ }[], config: Config): {
7
+ [importPath: string]: string[];
8
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processImportMap = processImportMap;
4
+ function processImportMap(routeMappings, config) {
5
+ const importsMap = {};
6
+ for (const { routeName } of routeMappings) {
7
+ const importPath = config.output;
8
+ if (!importPath) {
9
+ throw new Error('Output path is required');
10
+ }
11
+ if (!importsMap[importPath]) {
12
+ importsMap[importPath] = [];
13
+ }
14
+ importsMap[importPath].push(routeName);
15
+ }
16
+ return importsMap;
17
+ }
@@ -0,0 +1,3 @@
1
+ import type { OpenAPISpec } from '../../../types';
2
+ import type { Config } from '../../../config';
3
+ export declare function generateApp(openAPISpec: OpenAPISpec, config: Config): string;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateApp = generateApp;
4
+ const generate_docs_1 = require("../../openapi/docs/generate-docs");
5
+ const get_handler_imports_1 = require("../handler/import/get-handler-imports");
6
+ const get_route_maps_1 = require("./helper/get-route-maps");
7
+ const generate_import_handlers_1 = require("../handler/import/generate-import-handlers");
8
+ const generate_register_component_1 = require("../../openapi/components/generate-register-component");
9
+ const generate_import_routes_1 = require("./generators/generate-import-routes");
10
+ const generate_apply_openapi_routes_1 = require("./generators/generate-apply-openapi-routes");
11
+ const process_import_map_1 = require("./helper/process-import-map");
12
+ const OPENAPI_HONO_IMPORT = `import { OpenAPIHono } from '@hono/zod-openapi'`;
13
+ const SWAGGER_UI_IMPORT = `import { swaggerUI } from '@hono/swagger-ui'`;
14
+ const APP = 'const app = new OpenAPIHono()';
15
+ const ADD_TYPE = 'export type AddType = typeof api';
16
+ const EXPORT_APP = 'export default app';
17
+ function generateApp(openAPISpec, config) {
18
+ const routeMappings = (0, get_route_maps_1.getRouteMaps)(openAPISpec);
19
+ const importsMap = (0, process_import_map_1.processImportMap)(routeMappings, config);
20
+ const importRoutes = (0, generate_import_routes_1.generateImportRoutes)(importsMap);
21
+ const handlerImportsMap = (0, get_handler_imports_1.getHandlerImports)(routeMappings);
22
+ const importHandlers = (0, generate_import_handlers_1.generateImportHandlers)(handlerImportsMap, config);
23
+ const importHandlersCode = importHandlers.join('\n');
24
+ const applyOpenapiRoutes = (0, generate_apply_openapi_routes_1.generateApplyOpenapiRoutes)(routeMappings);
25
+ const openAPIHono = config.app?.basePath ? `${APP}.basePath('${config.app.basePath}')` : APP;
26
+ const app = `app${applyOpenapiRoutes}`;
27
+ const api = `const api = ${app}`;
28
+ const docs = (0, generate_docs_1.generateDocs)(openAPISpec);
29
+ const isDev = config?.app?.isDev
30
+ ? `const isDev = ${config.app.isDev} === 'development'`
31
+ : `const isDev = process.env.NODE_ENV === 'development'`;
32
+ const basePath = config?.app?.basePath ? `/${config.app.basePath}/doc` : '/doc';
33
+ const { components } = openAPISpec;
34
+ const registerComponent = components?.securitySchemes
35
+ ? (0, generate_register_component_1.generateRegisterComponent)(components.securitySchemes)
36
+ : '';
37
+ const swagger = `if(isDev){${registerComponent}\napp.doc('${'/doc'}',${JSON.stringify(docs)}).get('/ui',swaggerUI({url:'${basePath}'}))}`;
38
+ const sections = [
39
+ // 1. imports
40
+ [OPENAPI_HONO_IMPORT, SWAGGER_UI_IMPORT, importRoutes.join(''), importHandlersCode].join('\n'),
41
+ // 2. app initialization
42
+ openAPIHono,
43
+ // 3. api setup
44
+ api,
45
+ // 4. development settings
46
+ isDev,
47
+ // 5. swagger setup
48
+ swagger,
49
+ // 6. types and exports
50
+ ADD_TYPE,
51
+ // 7. export app
52
+ EXPORT_APP,
53
+ ];
54
+ return sections.join('\n\n');
55
+ }
@@ -18,7 +18,10 @@ const IMPORT_CODE = "import { createRoute, z } from '@hono/zod-openapi';";
18
18
  */
19
19
  function generateZodOpenAPIHono(openAPISpec, config) {
20
20
  // 1. get components
21
- const components = openAPISpec.components;
21
+ const components = openAPISpec.components ? openAPISpec.components : undefined;
22
+ if (!components) {
23
+ throw new Error(`Cannot destructure property 'schemas' of 'components' as it is undefined.`);
24
+ }
22
25
  // 2. get paths
23
26
  const { paths } = openAPISpec;
24
27
  // 3. generate components code
@@ -0,0 +1,9 @@
1
+ import type { OpenAPISpec } from '../../../types';
2
+ import type { Config } from '../../../config';
3
+ export type HandlerOutput = {
4
+ fileName: string;
5
+ testFileName: string;
6
+ routeHandlerContents: string[];
7
+ routeNames: string[];
8
+ };
9
+ export declare function generateZodOpenapiHonoHandler(openapi: OpenAPISpec, config: Config): Promise<void>;
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateZodOpenapiHonoHandler = generateZodOpenapiHonoHandler;
7
+ const node_fs_1 = __importDefault(require("node:fs"));
8
+ const generate_handler_1 = require("../../handler/generate-handler");
9
+ const generate_route_name_1 = require("../../openapi/paths/generate-route-name");
10
+ const group_handlers_by_file_name_helper_1 = require("./helper/group-handlers-by-file-name-helper");
11
+ const format_1 = require("../../../format");
12
+ const generate_handler_name_1 = require("../../handler/generate-handler-name");
13
+ const ROUTE_HANDLER = `import type { RouteHandler } from '@hono/zod-openapi'`;
14
+ async function generateZodOpenapiHonoHandler(openapi, config) {
15
+ const paths = openapi.paths;
16
+ const handlers = [];
17
+ for (const [path, pathItem] of Object.entries(paths)) {
18
+ for (const [method] of Object.entries(pathItem)) {
19
+ const routeName = (0, generate_route_name_1.generateRouteName)(method, path);
20
+ const handlerName = (0, generate_handler_name_1.generateHandlerName)(method, path);
21
+ const routeHandlerContent = (0, generate_handler_1.generateHandler)(handlerName, routeName);
22
+ const path_name = path
23
+ .replace(/[\/{}-]/g, ' ')
24
+ .trim()
25
+ .split(/\s+/)[0];
26
+ const fileName = path_name.length === 0 ? 'index_handler.ts' : `${path_name}_handler.ts`;
27
+ const testFileName = path_name.length === 0 ? 'index_handler.test.ts' : `${path_name}_handler.test.ts`;
28
+ handlers.push({
29
+ fileName,
30
+ testFileName,
31
+ routeHandlerContents: [routeHandlerContent],
32
+ routeNames: [routeName],
33
+ });
34
+ }
35
+ }
36
+ const mergedHandlers = (0, group_handlers_by_file_name_helper_1.groupHandlersByFileNameHelper)(handlers);
37
+ for (const handler of mergedHandlers) {
38
+ if (config.app?.output === true) {
39
+ const dirPath = config?.output?.replace(/\/[^/]+\.ts$/, '');
40
+ const handlerPath = dirPath === 'index.ts' ? 'handler' : `${dirPath}/handler`;
41
+ if (!node_fs_1.default.existsSync(handlerPath)) {
42
+ node_fs_1.default.mkdirSync(handlerPath, { recursive: true });
43
+ }
44
+ const routeTypes = handler.routeNames.map((routeName) => `${routeName}`).join(', ');
45
+ const match = config.output?.match(/[^/]+\.ts$/);
46
+ const matchPath = match ? match[0] : '';
47
+ const path = config.output === '.' || config.output === './' ? config.output : `../${matchPath}`;
48
+ const importRouteTypes = routeTypes ? `import type { ${routeTypes} } from '${path}';` : '';
49
+ const importStatements = `${ROUTE_HANDLER}\n${importRouteTypes}`;
50
+ const fileContent = `${importStatements}\n\n${handler.routeHandlerContents.join('\n\n')}`;
51
+ node_fs_1.default.writeFileSync(`${handlerPath}/${handler.fileName}`, await (0, format_1.formatCode)(fileContent), {
52
+ encoding: 'utf-8',
53
+ });
54
+ if (config.app.test) {
55
+ node_fs_1.default.writeFileSync(`${handlerPath}/${handler.testFileName}`, '', {
56
+ encoding: 'utf-8',
57
+ });
58
+ }
59
+ }
60
+ }
61
+ }
@@ -0,0 +1,2 @@
1
+ import type { HandlerOutput } from '../generate-zod-openapi-hono-handler';
2
+ export declare function groupHandlersByFileNameHelper(handlers: HandlerOutput[]): HandlerOutput[];
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.groupHandlersByFileNameHelper = groupHandlersByFileNameHelper;
4
+ function groupHandlersByFileNameHelper(handlers) {
5
+ const mergedMap = new Map();
6
+ for (const handler of handlers) {
7
+ if (mergedMap.has(handler.fileName)) {
8
+ const existing = mergedMap.get(handler.fileName);
9
+ if (existing) {
10
+ existing.routeHandlerContents.push(...handler.routeHandlerContents);
11
+ existing.routeNames.push(...handler.routeNames);
12
+ }
13
+ }
14
+ else {
15
+ mergedMap.set(handler.fileName, {
16
+ fileName: handler.fileName,
17
+ testFileName: handler.testFileName,
18
+ routeHandlerContents: [...handler.routeHandlerContents],
19
+ routeNames: [...handler.routeNames],
20
+ });
21
+ }
22
+ }
23
+ return Array.from(mergedMap.values());
24
+ }
@@ -0,0 +1,4 @@
1
+ import type { Config } from '../../../../config';
2
+ export declare function generateImportHandlers(handlerImportsMap: {
3
+ [fileName: string]: string[];
4
+ }, config: Config): string[];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateImportHandlers = generateImportHandlers;
4
+ function generateImportHandlers(handlerImportsMap, config) {
5
+ const importHandlers = [];
6
+ for (const [fileName, handlers] of Object.entries(handlerImportsMap)) {
7
+ const uniqueHandlers = Array.from(new Set(handlers));
8
+ if (config?.app?.output === true) {
9
+ const replacePath = config?.output?.replace(/\/[^/]+\.ts$/, '');
10
+ const dirPath = replacePath === undefined ? '.' : replacePath;
11
+ const handlerPath = dirPath === 'index.ts' ? 'handler' : `${dirPath}/handler`;
12
+ if (dirPath === '.') {
13
+ importHandlers.push(`import { ${uniqueHandlers.join(',')} } from '${handlerPath}/${fileName}';`);
14
+ }
15
+ else {
16
+ importHandlers.push(`import { ${uniqueHandlers.join(',')} } from './${handlerPath}/${fileName}';`);
17
+ }
18
+ }
19
+ }
20
+ return importHandlers;
21
+ }
@@ -0,0 +1,7 @@
1
+ export declare function getHandlerImports(handlerMaps: {
2
+ routeName: string;
3
+ handlerName: string;
4
+ path: string;
5
+ }[]): {
6
+ [fileName: string]: string[];
7
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getHandlerImports = getHandlerImports;
4
+ function getHandlerImports(handlerMaps) {
5
+ const getHandlerImports = {};
6
+ for (const { handlerName, path } of handlerMaps) {
7
+ const path_name = path
8
+ .replace(/[\/{}-]/g, ' ')
9
+ .trim()
10
+ .split(/\s+/)[0];
11
+ const fileName = path_name.length === 0 ? 'index_handler.ts' : `${path_name}_handler.ts`;
12
+ if (!getHandlerImports[fileName]) {
13
+ getHandlerImports[fileName] = [];
14
+ }
15
+ getHandlerImports[fileName].push(handlerName);
16
+ }
17
+ return getHandlerImports;
18
+ }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateAllOfCode = generateAllOfCode;
4
4
  const process_alllof_1 = require("./process/process-alllof");
5
+ const generate_zod_intersection_1 = require("../../../zod/generate-zod-intersection");
5
6
  /**
6
7
  * Converts an `allOf` schema into a Zod schema.
7
8
  *
@@ -21,6 +22,7 @@ function generateAllOfCode(schema, config) {
21
22
  if (schemas.length === 1) {
22
23
  return nullable ? `${schemas[0]}.nullable()` : schemas[0];
23
24
  }
24
- const intersection = `z.intersection(${schemas.join(',')})${nullable ? '.nullable()' : ''}`;
25
+ const intersectionSchema = (0, generate_zod_intersection_1.generateZodIntersection)(schemas);
26
+ const intersection = `${intersectionSchema}${nullable ? '.nullable()' : ''}`;
25
27
  return intersection;
26
28
  }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateAnyOfCode = generateAnyOfCode;
4
- const generate_zod_schema_1 = require("../../../zod/generate-zod-schema");
4
+ const generate_zod_schema_1 = require("../../../zod/schema/generate-zod-schema");
5
5
  const get_ref_schema_name_1 = require("../../../../core/schema/references/get-ref-schema-name");
6
+ const generate_zod_union_1 = require("../../../zod/generate-zod-union");
6
7
  function generateAnyOfCode(schema, config) {
7
8
  if (!schema.anyOf || schema.anyOf.length === 0) {
8
9
  console.warn('not exists anyOf');
@@ -12,5 +13,5 @@ function generateAnyOfCode(schema, config) {
12
13
  subSchema.$ref ? (0, get_ref_schema_name_1.getRefSchemaName)(subSchema, config) : (0, generate_zod_schema_1.generateZodSchema)(config, subSchema);
13
14
  return (0, generate_zod_schema_1.generateZodSchema)(config, subSchema);
14
15
  });
15
- return `z.union([${zodSchemas.join(', ')}])`;
16
+ return (0, generate_zod_union_1.generateZodUnion)(zodSchemas);
16
17
  }
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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");
4
+ const generate_zod_to_openapi_schema_definition_1 = require("../../zod/openapi/generate-zod-to-openapi-schema-definition");
5
+ const generate_zod_schema_1 = require("../../zod/schema/generate-zod-schema");
6
6
  const resolve_schemas_dependencies_1 = require("../../../core/schema/references/resolve-schemas-dependencies");
7
7
  const generate_schemas_export_1 = require("../paths/generate-schemas-export");
8
8
  const get_variable_schema_name_helper_1 = require("../../../core/helper/get-variable-schema-name-helper");
@@ -27,6 +27,9 @@ function generateComponentsCode(components,
27
27
  config) {
28
28
  // 1. schema extraction
29
29
  const { schemas } = components;
30
+ if (!schemas) {
31
+ return '';
32
+ }
30
33
  // 2. resolve schema dependencies to obtain proper ordering
31
34
  const orderedSchemas = (0, resolve_schemas_dependencies_1.resolveSchemasDependencies)(schemas);
32
35
  // 3. if there are no schemas, return an empty string
@@ -43,7 +46,7 @@ config) {
43
46
  // 4.3 generate zod schema
44
47
  const zodSchema = (0, generate_zod_schema_1.generateZodSchema)(config, schema, undefined, undefined);
45
48
  // 4.4 generate zod schema definition
46
- return (0, generate_zod_schema_definition_1.generateZodSchemaDefinition)(variableName, zodSchema, schemaName);
49
+ return (0, generate_zod_to_openapi_schema_definition_1.generateZodToOpenAPISchemaDefinition)(variableName, zodSchema, schemaName);
47
50
  })
48
51
  .join('\n\n');
49
52
  // 5. generate export statement
@@ -0,0 +1,2 @@
1
+ import type { SecuritySchemes } from '../../../types';
2
+ export declare function generateRegisterComponent(securitySchemes: SecuritySchemes): string;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateRegisterComponent = generateRegisterComponent;
4
+ function generateRegisterComponent(securitySchemes) {
5
+ return Object.entries(securitySchemes)
6
+ .map(([name, scheme]) => {
7
+ return `app.openAPIRegistry.registerComponent('securitySchemes', '${name}', ${JSON.stringify(scheme)})`;
8
+ })
9
+ .join('\n');
10
+ }
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateOneOfCode = generateOneOfCode;
4
- const generate_zod_schema_1 = require("../../../zod/generate-zod-schema");
4
+ const generate_zod_schema_1 = require("../../../zod/schema/generate-zod-schema");
5
5
  const get_ref_schema_name_1 = require("../../../../core/schema/references/get-ref-schema-name");
6
+ const generate_zod_union_1 = require("../../../zod/generate-zod-union");
6
7
  function generateOneOfCode(schema, config) {
7
8
  if (!schema.oneOf || schema.oneOf.length === 0) {
8
9
  console.warn('not exists oneOf');
@@ -12,5 +13,5 @@ function generateOneOfCode(schema, config) {
12
13
  subSchema.$ref ? (0, get_ref_schema_name_1.getRefSchemaName)(subSchema, config) : (0, generate_zod_schema_1.generateZodSchema)(config, subSchema);
13
14
  return (0, generate_zod_schema_1.generateZodSchema)(config, subSchema);
14
15
  });
15
- return `z.union([${zodSchemas.join(',')}])`;
16
+ return (0, generate_zod_union_1.generateZodUnion)(zodSchemas);
16
17
  }
@@ -0,0 +1,8 @@
1
+ import type { OpenAPISpec } from '../../../types';
2
+ export declare function generateDocs(openAPISpec: OpenAPISpec): {
3
+ openapi: string | undefined;
4
+ info: OpenAPISpec['info'];
5
+ servers: OpenAPISpec['servers'];
6
+ externalDocs: OpenAPISpec['externalDocs'];
7
+ tags: OpenAPISpec['tags'];
8
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateDocs = generateDocs;
4
+ function generateDocs(openAPISpec) {
5
+ return {
6
+ openapi: openAPISpec.openapi,
7
+ info: openAPISpec.info,
8
+ servers: openAPISpec.servers,
9
+ externalDocs: openAPISpec.externalDocs,
10
+ tags: openAPISpec.tags,
11
+ };
12
+ }
@@ -5,7 +5,7 @@ const generate_create_route_1 = require("./generate-create-route");
5
5
  const generate_request_parameter_1 = require("../../request/params/generate-request-parameter");
6
6
  const generate_response_schema_1 = require("../../response/schemas/generate-response-schema");
7
7
  const generate_route_name_1 = require("./generate-route-name");
8
- const escape_quote_1 = require("../../../core/text/escape-quote");
8
+ const sanitize_1 = require("../../../core/text/sanitize");
9
9
  /**
10
10
  * Generates TypeScript code for a Hono route based on OpenAPI operation details
11
11
  *
@@ -40,8 +40,8 @@ config) {
40
40
  tagsCode: `tags:${tagList},`,
41
41
  methodCode: `method:'${method}',`,
42
42
  pathCode: `path:'${path}',`,
43
- summaryCode: summary ? `summary:'${(0, escape_quote_1.escapeQuote)(summary)}',` : '',
44
- descriptionCode: description ? `description:'${(0, escape_quote_1.escapeQuote)(description)}',` : '',
43
+ summaryCode: summary ? `summary:'${(0, sanitize_1.sanitize)(summary)}',` : '',
44
+ descriptionCode: description ? `description:'${(0, sanitize_1.sanitize)(description)}',` : '',
45
45
  securityCode: security ? `security:${JSON.stringify(security)},` : '',
46
46
  requestParams: requestParams ? `${requestParams}` : '',
47
47
  responsesCode: responses ? `responses:{${(0, generate_response_schema_1.generateResponseSchema)(responses, config)}}` : '',
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateParamsObject = generateParamsObject;
4
4
  const generate_zod_coerce_1 = require("../../zod/generate-zod-coerce");
5
- const generate_zod_schema_1 = require("../../zod/generate-zod-schema");
5
+ const generate_zod_schema_1 = require("../../zod/schema/generate-zod-schema");
6
6
  /**
7
7
  * Generates a params object containing Zod schemas for different parameter locations
8
8
  *
@@ -19,6 +19,9 @@ const generate_zod_infer_1 = require("../zod/generate-zod-infer");
19
19
  function generateTypesCode(components, config) {
20
20
  // 1. schema extraction
21
21
  const { schemas } = components;
22
+ if (!schemas) {
23
+ return '';
24
+ }
22
25
  // 2. resolve schema dependencies to obtain proper ordering
23
26
  const orderedSchemas = (0, resolve_schemas_dependencies_1.resolveSchemasDependencies)(schemas);
24
27
  // 3. if there are no schemas, return an empty string
@@ -0,0 +1,2 @@
1
+ import type { Schema } from '../../types';
2
+ export declare function generateZodEnum(schema: Schema): string;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodEnum = generateZodEnum;
4
+ const generate_zod_to_openapi_1 = require("./openapi/generate-zod-to-openapi");
5
+ function generateZodEnum(schema) {
6
+ if (schema.example) {
7
+ const openapi_example = (0, generate_zod_to_openapi_1.generateZodToOpenAPI)(schema.example);
8
+ return `z.enum(${JSON.stringify(schema.enum)})${openapi_example}`;
9
+ }
10
+ return `z.enum(${JSON.stringify(schema.enum)})`;
11
+ }
@@ -5,7 +5,7 @@ const generate_zod_default_1 = require("./generate-zod-default");
5
5
  const generate_zod_max_1 = require("./generate-zod-max");
6
6
  const generate_zod_min_1 = require("./generate-zod-min");
7
7
  const generate_zod_regex_1 = require("./generate-zod-regex");
8
- const generate_zod_to_openapi_1 = require("./generate-zod-to-openapi");
8
+ const generate_zod_to_openapi_1 = require("./openapi/generate-zod-to-openapi");
9
9
  /**
10
10
  * Generates a zod schema for an integer.
11
11
  *
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Generates a Zod intersection schema.
3
+ *
4
+ * @param schemas - The schemas to intersect.
5
+ * @returns The generated Zod intersection schema as a string.
6
+ */
7
+ export declare function generateZodIntersection(schemas: string[]): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodIntersection = generateZodIntersection;
4
+ /**
5
+ * Generates a Zod intersection schema.
6
+ *
7
+ * @param schemas - The schemas to intersect.
8
+ * @returns The generated Zod intersection schema as a string.
9
+ */
10
+ function generateZodIntersection(schemas) {
11
+ return `z.intersection(${schemas.join(',')})`;
12
+ }
@@ -5,7 +5,7 @@ const generate_zod_default_1 = require("./generate-zod-default");
5
5
  const generate_zod_max_1 = require("./generate-zod-max");
6
6
  const generate_zod_min_1 = require("./generate-zod-min");
7
7
  const generate_zod_regex_1 = require("./generate-zod-regex");
8
- const generate_zod_to_openapi_1 = require("./generate-zod-to-openapi");
8
+ const generate_zod_to_openapi_1 = require("./openapi/generate-zod-to-openapi");
9
9
  /**
10
10
  * Generates a zod schema for a number.
11
11
  *
@@ -0,0 +1,3 @@
1
+ import type { Schema } from '../../types';
2
+ import type { Config } from '../../config';
3
+ export declare function generateZodObject(schema: Schema, config: Config): string;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodObject = generateZodObject;
4
+ const generate_zod_record_schema_1 = require("./generate-zod-record-schema");
5
+ const generate_allof_code_1 = require("../openapi/components/allof/generate-allof-code");
6
+ const generate_oneof_code_1 = require("../openapi/components/oneof/generate-oneof-code");
7
+ const generate_anyof_code_1 = require("../openapi/components/anyof/generate-anyof-code");
8
+ const generate_zod_properties_schema_1 = require("./generate-zod-properties-schema");
9
+ function generateZodObject(schema, config) {
10
+ if (schema.additionalProperties)
11
+ return (0, generate_zod_record_schema_1.generateZodRecordSchema)(schema.additionalProperties, config);
12
+ if (schema.allOf) {
13
+ return (0, generate_allof_code_1.generateAllOfCode)(schema, config);
14
+ }
15
+ if (schema.oneOf) {
16
+ return (0, generate_oneof_code_1.generateOneOfCode)(schema, config);
17
+ }
18
+ if (schema.anyOf) {
19
+ return (0, generate_anyof_code_1.generateAnyOfCode)(schema, config);
20
+ }
21
+ if (!schema.properties) {
22
+ return 'z.object({})';
23
+ }
24
+ return (0, generate_zod_properties_schema_1.generateZodPropertiesSchema)(schema.properties, schema.required || [], config);
25
+ }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  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
- const generate_zod_schema_1 = require("./generate-zod-schema");
6
+ const generate_zod_schema_1 = require("./schema/generate-zod-schema");
7
7
  const get_variable_schema_name_helper_1 = require("../../core/helper/get-variable-schema-name-helper");
8
8
  // import { generateZodOpenAPIExample } from './generate-zod-openapi-example'
9
9
  /**
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateZodRecordSchema = generateZodRecordSchema;
4
- const generate_zod_schema_1 = require("./generate-zod-schema");
4
+ const generate_zod_schema_1 = require("./schema/generate-zod-schema");
5
5
  /**
6
6
  * Generates a Zod record schema for objects with additional properties
7
7
  *
@@ -6,7 +6,7 @@ const generate_zod_default_1 = require("./generate-zod-default");
6
6
  const generate_zod_max_1 = require("./generate-zod-max");
7
7
  const generate_zod_min_1 = require("./generate-zod-min");
8
8
  const generate_zod_regex_1 = require("./generate-zod-regex");
9
- const generate_zod_to_openapi_1 = require("./generate-zod-to-openapi");
9
+ const generate_zod_to_openapi_1 = require("./openapi/generate-zod-to-openapi");
10
10
  /**
11
11
  * Generates a Zod schema string for string validation
12
12
  *
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Generates a Zod union schema
3
+ *
4
+ * @function generateZodUnion
5
+ * @param schemas - An array of Zod schema strings
6
+ * @returns A Zod union schema string
7
+ *
8
+ * @example
9
+ * generateZodUnion(['z.string()', 'z.number()'])
10
+ * // Returns: 'z.union([z.string(), z.number()])'
11
+ */
12
+ export declare function generateZodUnion(schemas: string[]): string;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateZodUnion = generateZodUnion;
4
+ /**
5
+ * Generates a Zod union schema
6
+ *
7
+ * @function generateZodUnion
8
+ * @param schemas - An array of Zod schema strings
9
+ * @returns A Zod union schema string
10
+ *
11
+ * @example
12
+ * generateZodUnion(['z.string()', 'z.number()'])
13
+ * // Returns: 'z.union([z.string(), z.number()])'
14
+ */
15
+ function generateZodUnion(schemas) {
16
+ return `z.union([${schemas.join(',')}])`;
17
+ }
@@ -19,4 +19,4 @@
19
19
  * - Part of OpenAPI to Zod schema conversion process
20
20
  * - Creates reusable schema definitions
21
21
  */
22
- export declare function generateZodSchemaDefinition(name: string, zodSchema: string, schemaName: string): string;
22
+ export declare function generateZodToOpenAPISchemaDefinition(name: string, zodSchema: string, schemaName: string): string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateZodSchemaDefinition = generateZodSchemaDefinition;
3
+ exports.generateZodToOpenAPISchemaDefinition = generateZodToOpenAPISchemaDefinition;
4
4
  /**
5
5
  * Creates a Zod schema constant declaration
6
6
  *
@@ -22,6 +22,6 @@ exports.generateZodSchemaDefinition = generateZodSchemaDefinition;
22
22
  * - Part of OpenAPI to Zod schema conversion process
23
23
  * - Creates reusable schema definitions
24
24
  */
25
- function generateZodSchemaDefinition(name, zodSchema, schemaName) {
25
+ function generateZodToOpenAPISchemaDefinition(name, zodSchema, schemaName) {
26
26
  return `const ${name} = ${zodSchema}.openapi('${schemaName}')`;
27
27
  }
@@ -1,4 +1,4 @@
1
- import type { ExampleValue } from '../../types';
1
+ import type { ExampleValue } from '../../../types';
2
2
  /**
3
3
  * Generate OpenAPI example value from Zod example value
4
4
  *
@@ -1,5 +1,5 @@
1
- import type { Schema } from '../../types';
2
- import type { Config } from '../../config';
1
+ import type { Schema } from '../../../types';
2
+ import type { Config } from '../../../config';
3
3
  /**
4
4
  * Generates a Zod schema string from an OpenAPI/JSON Schema definition
5
5
  *
@@ -1,17 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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
- const generate_allof_code_1 = require("../openapi/components/allof/generate-allof-code");
12
- const generate_anyof_code_1 = require("../openapi/components/anyof/generate-anyof-code");
13
- const generate_oneof_code_1 = require("../openapi/components/oneof/generate-oneof-code");
14
- const get_variable_schema_name_helper_1 = require("../../core/helper/get-variable-schema-name-helper");
4
+ const generate_zod_array_1 = require("../generate-zod-array");
5
+ const generate_zod_string_schema_1 = require("../generate-zod-string-schema");
6
+ const is_format_string_1 = require("../../../core/validator/is-format-string");
7
+ const generate_zod_number_schema_1 = require("../generate-zod-number-schema");
8
+ const generate_zod_integer_schema_1 = require("../generate-zod-integer-schema");
9
+ const generate_allof_code_1 = require("../../openapi/components/allof/generate-allof-code");
10
+ const generate_anyof_code_1 = require("../../openapi/components/anyof/generate-anyof-code");
11
+ const generate_oneof_code_1 = require("../../openapi/components/oneof/generate-oneof-code");
12
+ const get_variable_schema_name_helper_1 = require("../../../core/helper/get-variable-schema-name-helper");
13
+ const generate_zod_object_1 = require("../generate-zod-object");
14
+ const generate_zod_enum_1 = require("../generate-zod-enum");
15
15
  /**
16
16
  * Mapping of OpenAPI/JSON Schema types to Zod schema strings
17
17
  *
@@ -93,29 +93,11 @@ const TYPE_TO_ZOD_SCHEMA = {
93
93
  function generateZodSchema(config, schema, paramName, isPath) {
94
94
  // enum
95
95
  if (schema.enum) {
96
- if (schema.example) {
97
- return `z.enum(${JSON.stringify(schema.enum)}).openapi({example:${JSON.stringify(schema.example)}})`;
98
- }
99
- return `z.enum(${JSON.stringify(schema.enum)})`;
96
+ return (0, generate_zod_enum_1.generateZodEnum)(schema);
100
97
  }
101
98
  // object
102
99
  if (schema.type === 'object') {
103
- if (schema.additionalProperties)
104
- return (0, generate_zod_record_schema_1.generateZodRecordSchema)(schema.additionalProperties, config);
105
- if (schema.allOf) {
106
- return (0, generate_allof_code_1.generateAllOfCode)(schema, config);
107
- }
108
- if (schema.oneOf) {
109
- return (0, generate_oneof_code_1.generateOneOfCode)(schema, config);
110
- }
111
- if (schema.anyOf) {
112
- return (0, generate_anyof_code_1.generateAnyOfCode)(schema, config);
113
- }
114
- if (!schema.properties) {
115
- // console.log(schema)
116
- return 'z.object({})';
117
- }
118
- return (0, generate_zod_properties_schema_1.generateZodPropertiesSchema)(schema.properties, schema.required || [], config);
100
+ return (0, generate_zod_object_1.generateZodObject)(schema, config);
119
101
  }
120
102
  // string
121
103
  if (schema.type === 'string') {
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateZodSchemaFromSubSchema = generateZodSchemaFromSubSchema;
4
4
  const get_ref_schema_name_1 = require("../../../core/schema/references/get-ref-schema-name");
5
- const generate_zod_schema_1 = require("../generate-zod-schema");
5
+ const generate_zod_schema_1 = require("../schema/generate-zod-schema");
6
6
  /**
7
7
  * Generates a Zod schema from a sub-schema.
8
8
  *
package/dist/index.js CHANGED
@@ -8,9 +8,11 @@ exports.main = main;
8
8
  const swagger_parser_1 = __importDefault(require("@apidevtools/swagger-parser"));
9
9
  const node_fs_1 = __importDefault(require("node:fs"));
10
10
  const node_path_1 = __importDefault(require("node:path"));
11
- const prettier_1 = require("prettier");
12
11
  const generate_zod_openapi_hono_1 = require("./generators/hono/generate-zod-openapi-hono");
12
+ const generate_zod_openapi_hono_handler_1 = require("./generators/hono/handler/generate-zod-openapi-hono-handler");
13
13
  const config_1 = require("./config");
14
+ const format_1 = require("./format");
15
+ const app_1 = require("./generators/hono/app");
14
16
  /**
15
17
  * CLI entry point for hono-takibi
16
18
  *
@@ -35,23 +37,19 @@ async function main(dev = false, config = (0, config_1.getConfig)()) {
35
37
  const args = process.argv.slice(2);
36
38
  // 3. input = 'example/pet-store.yaml'
37
39
  const input = config.input ?? args[0];
38
- // const input = args[0]
40
+ config.input = input;
39
41
  // 4. output = 'routes/petstore-index.ts'
40
42
  const output = config.output ?? args[args.indexOf('-o') + 1];
43
+ config.output = output;
41
44
  try {
42
45
  // 5. parse OpenAPI YAML or JSON
43
46
  const openAPI = (await swagger_parser_1.default.parse(input));
44
47
  // 6. generate Hono code
45
48
  const hono = (0, generate_zod_openapi_hono_1.generateZodOpenAPIHono)(openAPI, config);
46
49
  // 7. format code
47
- const formattedCode = await (0, prettier_1.format)(hono, {
48
- parser: 'typescript',
49
- printWidth: 100,
50
- singleQuote: true,
51
- semi: false,
52
- });
50
+ const formattedCode = await (0, format_1.formatCode)(hono);
53
51
  // 8. write to file
54
- if (output) {
52
+ if (config.output) {
55
53
  // 8.1 output routes/petstore-index.ts
56
54
  const outputDir = node_path_1.default.dirname(output);
57
55
  // 8.2 outputDir routes
@@ -61,6 +59,27 @@ async function main(dev = false, config = (0, config_1.getConfig)()) {
61
59
  }
62
60
  // 9. write to file
63
61
  node_fs_1.default.writeFileSync(output, formattedCode, { encoding: 'utf-8' });
62
+ // Vite Plugin Mode
63
+ const mode = process.argv[5];
64
+ const vite = process.argv[6];
65
+ if (mode === '--mode' && vite === 'vite') {
66
+ node_fs_1.default.writeFileSync(output, formattedCode, { encoding: 'utf-8' });
67
+ console.log(`Generated code written to ${output}`);
68
+ return true;
69
+ }
70
+ // 10. generate app code
71
+ const appCode = (0, app_1.generateApp)(openAPI, config);
72
+ if (config.app?.output === true) {
73
+ // 10.1 generate handler code
74
+ await (0, generate_zod_openapi_hono_handler_1.generateZodOpenapiHonoHandler)(openAPI, config);
75
+ // 10.2 format app code
76
+ const formattedAppCode = await (0, format_1.formatCode)(appCode);
77
+ // 10.3 write to file
78
+ const defaultFileName = 'index.ts';
79
+ const alternativeFileName = 'main.ts';
80
+ const outputFile = node_fs_1.default.existsSync(defaultFileName) ? alternativeFileName : defaultFileName;
81
+ node_fs_1.default.writeFileSync(outputFile, formattedAppCode, { encoding: 'utf-8' });
82
+ }
64
83
  console.log(`Generated code written to ${output}`);
65
84
  return true;
66
85
  }
@@ -7,6 +7,10 @@ export type OpenAPI = Awaited<ReturnType<typeof SwaggerParser.parse>>;
7
7
  * Extended OpenAPI specification with required components and paths
8
8
  */
9
9
  export type OpenAPISpec = OpenAPI & {
10
+ openapi?: string;
11
+ servers?: string | {
12
+ url: string;
13
+ }[];
10
14
  components: Components;
11
15
  } & {
12
16
  paths: OpenAPIPaths;
@@ -156,9 +160,17 @@ export type Schema = {
156
160
  * Components section of OpenAPI spec
157
161
  */
158
162
  export type Components = {
159
- schemas: Record<string, Schema>;
163
+ schemas?: Record<string, Schema>;
160
164
  parameters?: Record<string, Parameters>;
161
165
  requestBodies?: Record<string, RequestBody>;
166
+ securitySchemes?: SecuritySchemes;
167
+ };
168
+ export type SecuritySchemes = {
169
+ [key: string]: {
170
+ type?: string;
171
+ scheme?: string;
172
+ bearerFormat?: string;
173
+ };
162
174
  };
163
175
  /**
164
176
  * Dynamic parameter section type
@@ -0,0 +1,10 @@
1
+ type HonoTakibiPluginOptions = {
2
+ input: string;
3
+ output: string;
4
+ packageManager: 'pnpm' | 'npm' | 'yarn' | 'bun';
5
+ };
6
+ export default function honoTakibiPlugin(options: HonoTakibiPluginOptions): {
7
+ name: string;
8
+ configureServer(server: any): void;
9
+ };
10
+ export {};
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = honoTakibiPlugin;
4
+ const node_child_process_1 = require("node:child_process");
5
+ function honoTakibiPlugin(options) {
6
+ return {
7
+ name: 'hono-takibi-plugin',
8
+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
9
+ configureServer(server) {
10
+ server.watcher.add(options.input);
11
+ server.watcher.on('change', () => {
12
+ const commandPrefixMap = {
13
+ npm: 'npx',
14
+ pnpm: 'pnpm',
15
+ yarn: 'yarn',
16
+ bun: 'bun',
17
+ };
18
+ const commandPrefix = commandPrefixMap[options.packageManager];
19
+ const command = `${commandPrefix} hono-takibi ${options.input} -o ${options.output} --mode vite`;
20
+ (0, node_child_process_1.exec)(command, (error) => {
21
+ if (error) {
22
+ console.error(`[hono-takibi-plugin] error: ${error.message}`);
23
+ return;
24
+ }
25
+ // reload
26
+ server.ws.send({ type: 'full-reload' });
27
+ });
28
+ });
29
+ },
30
+ };
31
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "hono-takibi",
3
3
  "description": "Hono Takibi is a CLI tool that generates Hono routes from OpenAPI specifications.",
4
- "version": "0.3.0",
4
+ "version": "0.3.2",
5
5
  "license": "MIT",
6
6
  "keywords": [
7
7
  "hono",
@@ -26,7 +26,15 @@
26
26
  "bin": {
27
27
  "hono-takibi": "dist/index.js"
28
28
  },
29
+ "exports": {
30
+ "./vite-plugin": {
31
+ "types": "./dist/vite-plugin/index.d.ts",
32
+ "import": "./dist/vite-plugin/index.js",
33
+ "require": "./dist/vite-plugin/index.js"
34
+ }
35
+ },
29
36
  "scripts": {
37
+ "dev": "vite --host",
30
38
  "deps": "rm -rf node_modules && pnpm install",
31
39
  "build": "tsc",
32
40
  "typecheck": "tsc --noEmit",
@@ -43,6 +51,7 @@
43
51
  "@types/node": "^22.10.2",
44
52
  "@vitest/coverage-v8": "^2.1.8",
45
53
  "typescript": "^5.7.2",
54
+ "vite": "^6.1.0",
46
55
  "vitest": "^2.1.8"
47
56
  }
48
57
  }