hono-takibi 0.7.2 → 0.7.3

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 (40) hide show
  1. package/dist/cli/app.d.ts +3 -0
  2. package/dist/cli/app.js +15 -0
  3. package/dist/cli/helper/set-config.d.ts +2 -2
  4. package/dist/cli/index.d.ts +4 -0
  5. package/dist/cli/index.js +38 -0
  6. package/dist/cli/takibi.d.ts +15 -0
  7. package/dist/cli/takibi.js +14 -0
  8. package/dist/cli/types/index.d.ts +2 -2
  9. package/dist/cli/validator/index.d.ts +2 -0
  10. package/dist/cli/validator/index.js +2 -0
  11. package/dist/cli/validator/is-ts.d.ts +1 -0
  12. package/dist/cli/validator/is-ts.js +3 -0
  13. package/dist/cli/validator/is-yaml-or-json.d.ts +1 -0
  14. package/dist/cli/validator/is-yaml-or-json.js +3 -0
  15. package/dist/cli/validator/parse-cli-args.d.ts +2 -2
  16. package/dist/cli/validator/parse-help.js +1 -1
  17. package/dist/cli/validator/parse-io.d.ts +4 -4
  18. package/dist/cli/validator/parse-io.js +7 -3
  19. package/dist/config/index.d.ts +2 -2
  20. package/dist/generator/zod-openapi-hono/app/helper/get-route-maps.js +6 -4
  21. package/dist/generator/zod-openapi-hono/app/helper/process-import-map.d.ts +1 -2
  22. package/dist/generator/zod-openapi-hono/app/helper/process-import-map.js +2 -2
  23. package/dist/generator/zod-openapi-hono/app/index.d.ts +2 -3
  24. package/dist/generator/zod-openapi-hono/app/index.js +5 -6
  25. package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler-name.d.ts → generator/handler-name.d.ts} +1 -1
  26. package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler-name.js → generator/handler-name.js} +1 -1
  27. package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler.d.ts → generator/handler.d.ts} +1 -1
  28. package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler.js → generator/handler.js} +1 -1
  29. package/dist/generator/zod-openapi-hono/handler/{generators/generate-import-handlers.d.ts → generator/import-handlers.d.ts} +3 -4
  30. package/dist/generator/zod-openapi-hono/handler/{generators/generate-import-handlers.js → generator/import-handlers.js} +3 -3
  31. package/dist/generator/zod-openapi-hono/handler/generator/index.d.ts +3 -0
  32. package/dist/generator/zod-openapi-hono/handler/generator/index.js +3 -0
  33. package/dist/generator/zod-openapi-hono/handler/zod-openapi-hono-handler.d.ts +3 -4
  34. package/dist/generator/zod-openapi-hono/handler/zod-openapi-hono-handler.js +10 -12
  35. package/dist/index.d.ts +1 -20
  36. package/dist/index.js +2 -66
  37. package/dist/vite-plugin/index.d.ts +2 -2
  38. package/dist/vite-plugin/index.js +1 -1
  39. package/dist/vite-plugin/vite.js +1 -1
  40. package/package.json +1 -1
@@ -0,0 +1,3 @@
1
+ import type { OpenAPISpec } from '../openapi/index.js';
2
+ import type { Result } from '../result/index.js';
3
+ export declare function app(openAPI: OpenAPISpec, output: `${string}.ts`, test: boolean, template: boolean, basePath?: string): Promise<Result<void, string>>;
@@ -0,0 +1,15 @@
1
+ import path from 'node:path';
2
+ import { ok, asyncAndThen } from '../result/index.js';
3
+ import { readdir, writeFile } from '../fsp/index.js';
4
+ import { fmt } from '../format/index.js';
5
+ import { generateApp } from '../generator/zod-openapi-hono/app/index.js';
6
+ import { zodOpenapiHonoHandler } from '../generator/zod-openapi-hono/handler/zod-openapi-hono-handler.js';
7
+ export async function app(openAPI, output, test, template, basePath) {
8
+ if (!(template || !output.includes('/')))
9
+ return ok(undefined);
10
+ const dir = path.dirname(output);
11
+ return await asyncAndThen(await fmt(generateApp(openAPI, output, basePath)), async (appCode) => asyncAndThen(await readdir(dir), async (files) => {
12
+ const target = path.join(dir, files.includes('index.ts') ? 'main.ts' : 'index.ts');
13
+ return await asyncAndThen(await writeFile(target, appCode), async () => asyncAndThen(await zodOpenapiHonoHandler(openAPI, output, test), async () => ok(undefined)));
14
+ }));
15
+ }
@@ -16,6 +16,6 @@ export declare function setConfig(base: Config, cli: CliFlags): Result<{
16
16
  name: 'PascalCase' | 'camelCase';
17
17
  export: boolean;
18
18
  };
19
- input: string;
20
- output: string;
19
+ input: `${string}.yaml` | `${string}.json`;
20
+ output: `${string}.ts`;
21
21
  }, string>;
@@ -0,0 +1,4 @@
1
+ import type { Result } from '../result/index.js';
2
+ export declare function honoTakibi(): Promise<Result<{
3
+ message: string;
4
+ }, string>>;
@@ -0,0 +1,38 @@
1
+ import { ok, asyncAndThen } from '../result/index.js';
2
+ import { sliceArgs } from './helper/slice-args.js';
3
+ import { parseHelp } from './validator/parse-help.js';
4
+ import { parseCliArgs } from './validator/parse-cli-args.js';
5
+ import { getConfig } from '../config/index.js';
6
+ import { setConfig } from './helper/set-config.js';
7
+ import { takibi } from './takibi.js';
8
+ const HELP_TEXT = `
9
+ Usage:
10
+ hono-takibi <input.yaml|json> -o <routes.ts> [options]
11
+
12
+ Options:
13
+ --export-type Export generated type aliases
14
+
15
+ --export-schema Export generated schema objects
16
+
17
+ --naming-case-type <PascalCase|camelCase>
18
+ Casing for generated *type aliases*
19
+ (default: PascalCase)
20
+
21
+ --naming-case-schema <PascalCase|camelCase>
22
+ Casing for generated *schema objects*
23
+ (default: PascalCase)
24
+
25
+ --template Generate an app file and handler stubs
26
+
27
+ --test Generate empty *.test.ts files for handlers
28
+
29
+ --base-path <path> API prefix (e.g. /api)
30
+
31
+ --help Show this help and exit
32
+ `.trimStart();
33
+ export async function honoTakibi() {
34
+ const args = sliceArgs(process.argv);
35
+ return parseHelp(args)
36
+ ? ok({ message: HELP_TEXT })
37
+ : await asyncAndThen(getConfig(), async (config) => asyncAndThen(parseCliArgs(args, config), async (cli) => asyncAndThen(setConfig(config, cli), async (finalConfig) => takibi(finalConfig, cli.template, cli.test, cli.basePath))));
38
+ }
@@ -0,0 +1,15 @@
1
+ import type { Result } from '../result/types.js';
2
+ export declare function takibi(config: {
3
+ schema: {
4
+ name: 'PascalCase' | 'camelCase';
5
+ export: boolean;
6
+ };
7
+ type: {
8
+ name: 'PascalCase' | 'camelCase';
9
+ export: boolean;
10
+ };
11
+ input: `${string}.yaml` | `${string}.json`;
12
+ output: `${string}.ts`;
13
+ }, template: boolean, test: boolean, basePath?: string): Promise<Result<{
14
+ message: string;
15
+ }, string>>;
@@ -0,0 +1,14 @@
1
+ import path from 'node:path';
2
+ import { ok, asyncAndThen } from '../result/index.js';
3
+ import { fmt } from '../format/index.js';
4
+ import { parseOpenAPI } from '../openapi/parse-openapi.js';
5
+ import { zodOpenAPIHono } from '../generator/zod-openapi-hono/openapi/zod-openapi-hono.js';
6
+ import { app } from './app.js';
7
+ import { mkdir, writeFile } from '../fsp/index.js';
8
+ export async function takibi(config, template, test, basePath) {
9
+ return await asyncAndThen(await parseOpenAPI(config.input), async (openAPI) => asyncAndThen(await fmt(zodOpenAPIHono(openAPI, config)), async (code) => asyncAndThen(await mkdir(path.dirname(config.output)), async () => asyncAndThen(await writeFile(config.output, code), async () => asyncAndThen(await app(openAPI, config.output, template, test, basePath), async () => ok({
10
+ message: template
11
+ ? 'Generated code and template files written'
12
+ : `Generated code written to ${config.output}`,
13
+ }))))));
14
+ }
@@ -1,6 +1,6 @@
1
1
  export type CliFlags = {
2
- input: string;
3
- output: string;
2
+ input: `${string}.yaml` | `${string}.json`;
3
+ output: `${string}.ts`;
4
4
  exportType?: boolean;
5
5
  exportSchema?: boolean;
6
6
  typeCase?: 'PascalCase' | 'camelCase';
@@ -2,3 +2,5 @@ export { parseCliArgs } from './parse-cli-args.js';
2
2
  export { parseHelp } from './parse-help.js';
3
3
  export { parseIO } from './parse-io.js';
4
4
  export { parseNaming } from './parse-naming.js';
5
+ export { isYamlOrJson } from './is-yaml-or-json.js';
6
+ export { isTs } from './is-ts.js';
@@ -2,3 +2,5 @@ export { parseCliArgs } from './parse-cli-args.js';
2
2
  export { parseHelp } from './parse-help.js';
3
3
  export { parseIO } from './parse-io.js';
4
4
  export { parseNaming } from './parse-naming.js';
5
+ export { isYamlOrJson } from './is-yaml-or-json.js';
6
+ export { isTs } from './is-ts.js';
@@ -0,0 +1 @@
1
+ export declare function isTs(o: string): o is `${string}.ts`;
@@ -0,0 +1,3 @@
1
+ export function isTs(o) {
2
+ return o.endsWith('.ts') && !o.endsWith('.d.ts');
3
+ }
@@ -0,0 +1 @@
1
+ export declare function isYamlOrJson(i: string): i is `${string}.yaml` | `${string}.json`;
@@ -0,0 +1,3 @@
1
+ export function isYamlOrJson(i) {
2
+ return i.endsWith('.yaml') || i.endsWith('.json');
3
+ }
@@ -6,6 +6,6 @@ import type { CliFlags } from '../types/index.js';
6
6
  * @returns A Result containing the parsed flags or an error message.
7
7
  */
8
8
  export declare function parseCliArgs(args: readonly string[], config: {
9
- input?: string;
10
- output?: string;
9
+ input?: `${string}.yaml` | `${string}.json`;
10
+ output?: `${string}.ts`;
11
11
  }): Result<CliFlags, string>;
@@ -4,5 +4,5 @@
4
4
  * @returns boolean
5
5
  */
6
6
  export function parseHelp(args) {
7
- return args.length === 0 || (args.length === 1 && (args[0] === '--help' || args[0] === '-h'));
7
+ return args.length === 1 && (args[0] === '--help' || args[0] === '-h');
8
8
  }
@@ -5,9 +5,9 @@ import type { Result } from '../../result/index.js';
5
5
  * @returns A Result containing the input and output file paths or an error message
6
6
  */
7
7
  export declare function parseIO(args: readonly string[], config: {
8
- input?: string;
9
- output?: string;
8
+ input?: `${string}.yaml` | `${string}.json`;
9
+ output?: `${string}.ts`;
10
10
  }): Result<{
11
- input: string;
12
- output: string;
11
+ input: `${string}.yaml` | `${string}.json`;
12
+ output: `${string}.ts`;
13
13
  }, string>;
@@ -1,4 +1,5 @@
1
1
  import { ok, err } from '../../result/index.js';
2
+ import { isTs, isYamlOrJson } from './index.js';
2
3
  /**
3
4
  * @param args - The CLI arguments
4
5
  * @param config - The configuration object containing input and output file paths
@@ -10,7 +11,10 @@ export function parseIO(args, config) {
10
11
  const cliOutput = oIdx !== -1 ? args[oIdx + 1] : undefined;
11
12
  const input = cliInput ?? config.input;
12
13
  const output = cliOutput ?? config.output;
13
- return input && output
14
- ? ok({ input, output })
15
- : err('Usage: hono-takibi <input-file> -o <output-file>');
14
+ if (output) {
15
+ if (isYamlOrJson(input) && isTs(output)) {
16
+ return ok({ input, output });
17
+ }
18
+ }
19
+ return err('Usage: hono-takibi <input.yaml|.json> -o <output.ts>');
16
20
  }
@@ -8,8 +8,8 @@ export type Config = {
8
8
  name: 'PascalCase' | 'camelCase';
9
9
  export: boolean;
10
10
  };
11
- input?: string;
12
- output?: string;
11
+ input?: `${string}.yaml` | `${string}.json`;
12
+ output?: `${string}.ts`;
13
13
  };
14
14
  export declare const DEFAULT_CONFIG: {
15
15
  readonly schema: {
@@ -1,4 +1,4 @@
1
- import { generateHandlerName } from '../../handler/generators/generate-handler-name.js';
1
+ import { handlerName } from '../../handler/generator/index.js';
2
2
  import { generateRouteName } from '../../openapi/route/generate-route-name.js';
3
3
  /**
4
4
  * Get route maps
@@ -9,9 +9,11 @@ export function getRouteMaps(openAPISpec) {
9
9
  const paths = openAPISpec.paths;
10
10
  const routeMappings = Object.entries(paths).flatMap(([path, pathItem]) => {
11
11
  return Object.entries(pathItem).flatMap(([method]) => {
12
- const routeName = generateRouteName(method, path);
13
- const handlerName = generateHandlerName(method, path);
14
- return { routeName, handlerName, path };
12
+ return {
13
+ routeName: generateRouteName(method, path),
14
+ handlerName: handlerName(method, path),
15
+ path,
16
+ };
15
17
  });
16
18
  });
17
19
  return routeMappings;
@@ -1,4 +1,3 @@
1
- import type { Config } from '../../../../config/index.js';
2
1
  /**
3
2
  * Process import map
4
3
  * @param { { routeName: string, handlerName: string, path: string }[] } routeMappings - Route mappings
@@ -9,6 +8,6 @@ export declare function processImportMap(routeMappings: {
9
8
  routeName: string;
10
9
  handlerName: string;
11
10
  path: string;
12
- }[], config: Config): {
11
+ }[], output: `${string}.ts`): {
13
12
  [importPath: string]: string[];
14
13
  };
@@ -4,10 +4,10 @@
4
4
  * @param { Config } config - Config
5
5
  * @returns { { [importPath: string]: string[] } } Import map
6
6
  */
7
- export function processImportMap(routeMappings, config) {
7
+ export function processImportMap(routeMappings, output) {
8
8
  const importsMap = {};
9
9
  for (const { routeName } of routeMappings) {
10
- const path = config.output;
10
+ const path = output;
11
11
  if (!path) {
12
12
  throw new Error('Output path is required');
13
13
  }
@@ -1,12 +1,11 @@
1
1
  import type { OpenAPISpec } from '../../../openapi/index.js';
2
- import type { Config } from '../../../config/index.js';
3
2
  /**
4
3
  * Generate app
5
4
  *
6
5
  * @function generateApp
7
6
  * @param { OpenAPISpec } openAPISpec - OpenAPI spec
8
- * @param { Config } config - Config
7
+ * @param { `${string}.ts` } output - Output file name
9
8
  * @param { string | undefined } basePath - Base path
10
9
  * @returns { string } Generated app code
11
10
  */
12
- export declare function generateApp(openAPISpec: OpenAPISpec, config: Config, basePath: string | undefined): string;
11
+ export declare function generateApp(openAPISpec: OpenAPISpec, output: `${string}.ts`, basePath: string | undefined): string;
@@ -1,7 +1,7 @@
1
1
  import { generateDocs } from './docs/generate-docs.js';
2
2
  import { getHandlerImports } from '../handler/import/get-handler-imports.js';
3
3
  import { getRouteMaps } from './helper/get-route-maps.js';
4
- import { generateImportHandlers } from '../handler/generators/generate-import-handlers.js';
4
+ import { importHandlers } from '../handler/generator/index.js';
5
5
  import { generateRegisterComponent } from './register-component/generate-register-component.js';
6
6
  import { generateImportRoutes } from './generators/generate-import-routes.js';
7
7
  import { generateApplyOpenapiRoutes } from './generators/generate-apply-openapi-routes.js';
@@ -16,17 +16,16 @@ const EXPORT_APP = 'export default app';
16
16
  *
17
17
  * @function generateApp
18
18
  * @param { OpenAPISpec } openAPISpec - OpenAPI spec
19
- * @param { Config } config - Config
19
+ * @param { `${string}.ts` } output - Output file name
20
20
  * @param { string | undefined } basePath - Base path
21
21
  * @returns { string } Generated app code
22
22
  */
23
- export function generateApp(openAPISpec, config, basePath) {
23
+ export function generateApp(openAPISpec, output, basePath) {
24
24
  const routeMappings = getRouteMaps(openAPISpec);
25
- const importsMap = processImportMap(routeMappings, config);
25
+ const importsMap = processImportMap(routeMappings, output);
26
26
  const importRoutes = generateImportRoutes(importsMap);
27
27
  const handlerImportsMap = getHandlerImports(routeMappings);
28
- const importHandlers = generateImportHandlers(handlerImportsMap, config);
29
- const importHandlersCode = importHandlers.join('\n');
28
+ const importHandlersCode = importHandlers(handlerImportsMap, output).join('\n');
30
29
  const applyOpenapiRoutes = generateApplyOpenapiRoutes(routeMappings);
31
30
  const openAPIHono = basePath ? `${APP}.basePath('${basePath}')` : APP;
32
31
  const app = `app${applyOpenapiRoutes}`;
@@ -4,4 +4,4 @@
4
4
  * @param { string } path - Path
5
5
  * @returns { string } Handler name
6
6
  */
7
- export declare function generateHandlerName(method: string, path: string): string;
7
+ export declare function handlerName(method: string, path: string): string;
@@ -5,6 +5,6 @@ import { generateRouteName } from '../../openapi/route/generate-route-name.js';
5
5
  * @param { string } path - Path
6
6
  * @returns { string } Handler name
7
7
  */
8
- export function generateHandlerName(method, path) {
8
+ export function handlerName(method, path) {
9
9
  return `${generateRouteName(method, path)}Handler`;
10
10
  }
@@ -4,4 +4,4 @@
4
4
  * @param { string } routeName - The name of the route.
5
5
  * @returns { string } A string of the handler function.
6
6
  */
7
- export declare function generateHandler(handlerName: string, routeName: string): string;
7
+ export declare function handler(handlerName: string, routeName: string): string;
@@ -4,6 +4,6 @@
4
4
  * @param { string } routeName - The name of the route.
5
5
  * @returns { string } A string of the handler function.
6
6
  */
7
- export function generateHandler(handlerName, routeName) {
7
+ export function handler(handlerName, routeName) {
8
8
  return `export const ${handlerName}:RouteHandler<typeof ${routeName}>=async(c)=>{}`;
9
9
  }
@@ -1,10 +1,9 @@
1
- import type { Config } from '../../../../config/index.js';
2
1
  /**
3
2
  * Generate import handlers
4
3
  * @param { { [fileName: string]: string[] } } handlerImportsMap - Handler imports map
5
- * @param { Config } config - Config
4
+ * @param { string } output - Output file path
6
5
  * @returns { string[] } Import handlers
7
6
  */
8
- export declare function generateImportHandlers(handlerImportsMap: {
7
+ export declare function importHandlers(handlerImportsMap: {
9
8
  [fileName: string]: string[];
10
- }, config: Config): string[];
9
+ }, output?: string): string[];
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * Generate import handlers
3
3
  * @param { { [fileName: string]: string[] } } handlerImportsMap - Handler imports map
4
- * @param { Config } config - Config
4
+ * @param { string } output - Output file path
5
5
  * @returns { string[] } Import handlers
6
6
  */
7
- export function generateImportHandlers(handlerImportsMap, config) {
7
+ export function importHandlers(handlerImportsMap, output) {
8
8
  const importHandlers = [];
9
9
  for (const [fileName, handlers] of Object.entries(handlerImportsMap)) {
10
10
  const uniqueHandlers = Array.from(new Set(handlers));
11
- const replacePath = config?.output?.replace(/\/[^/]+\.ts$/, '');
11
+ const replacePath = output?.replace(/\/[^/]+\.ts$/, '');
12
12
  const dirPath = replacePath === undefined ? '.' : replacePath;
13
13
  const handlerPath = 'handler';
14
14
  if (dirPath === '.') {
@@ -0,0 +1,3 @@
1
+ export { importHandlers } from './import-handlers.js';
2
+ export { handlerName } from './handler-name.js';
3
+ export { handler } from './handler.js';
@@ -0,0 +1,3 @@
1
+ export { importHandlers } from './import-handlers.js';
2
+ export { handlerName } from './handler-name.js';
3
+ export { handler } from './handler.js';
@@ -1,5 +1,4 @@
1
1
  import type { OpenAPISpec } from '../../../openapi/index.js';
2
- import type { Config } from '../../../config/index.js';
3
2
  import type { Result } from '../../../result/types.js';
4
3
  export type HandlerOutput = {
5
4
  fileName: string;
@@ -10,8 +9,8 @@ export type HandlerOutput = {
10
9
  /**
11
10
  * Generates the Zod OpenAPI Hono handler.
12
11
  * @param { OpenAPISpec } openapi - The OpenAPI specification.
13
- * @param { Config } config - The configuration.
12
+ * @param { output } output - The output directory or file path.
14
13
  * @param { boolean } test - Whether to generate the test file.
15
- * @returns { Promise<void> }
14
+ * @returns { Promise<Result<void, string>> } - A promise that resolves to a Result indicating success or failure.
16
15
  */
17
- export declare function zodOpenapiHonoHandler(openapi: OpenAPISpec, config: Config, test: boolean): Promise<Result<void, string>>;
16
+ export declare function zodOpenapiHonoHandler(openapi: OpenAPISpec, output: string, test: boolean): Promise<Result<void, string>>;
@@ -1,31 +1,29 @@
1
- import { generateHandler } from './generators/generate-handler.js';
1
+ import { handler, handlerName } from './generator/index.js';
2
2
  import { generateRouteName } from '../openapi/route/generate-route-name.js';
3
3
  import { groupHandlersByFileNameHelper } from './helper/group-handlers-by-file-name-helper.js';
4
4
  import { fmt } from '../../../format/index.js';
5
5
  import { mkdir, writeFile } from '../../../fsp/index.js';
6
- import { generateHandlerName } from './generators/generate-handler-name.js';
7
6
  const ROUTE_HANDLER = `import type { RouteHandler } from '@hono/zod-openapi'`;
8
7
  /**
9
8
  * Generates the Zod OpenAPI Hono handler.
10
9
  * @param { OpenAPISpec } openapi - The OpenAPI specification.
11
- * @param { Config } config - The configuration.
10
+ * @param { output } output - The output directory or file path.
12
11
  * @param { boolean } test - Whether to generate the test file.
13
- * @returns { Promise<void> }
12
+ * @returns { Promise<Result<void, string>> } - A promise that resolves to a Result indicating success or failure.
14
13
  */
15
- export async function zodOpenapiHonoHandler(openapi, config, test) {
14
+ export async function zodOpenapiHonoHandler(openapi, output, test) {
16
15
  const paths = openapi.paths;
17
16
  const handlers = [];
18
17
  for (const [path, pathItem] of Object.entries(paths)) {
19
18
  for (const [method] of Object.entries(pathItem)) {
20
19
  const routeName = generateRouteName(method, path);
21
- const handlerName = generateHandlerName(method, path);
22
- const routeHandlerContent = generateHandler(handlerName, routeName);
20
+ const routeHandlerContent = handler(handlerName(method, path), routeName);
23
21
  const path_name = path
24
22
  .replace(/[\/{}-]/g, ' ')
25
23
  .trim()
26
24
  .split(/\s+/)[0];
27
- const fileName = path_name.length === 0 ? 'index_handler.ts' : `${path_name}_handler.ts`;
28
- const testFileName = path_name.length === 0 ? 'index_handler.test.ts' : `${path_name}_handler.test.ts`;
25
+ const fileName = path_name.length === 0 ? 'index-handler.ts' : `${path_name}-handler.ts`;
26
+ const testFileName = path_name.length === 0 ? 'index-handler.test.ts' : `${path_name}-handler.test.ts`;
29
27
  handlers.push({
30
28
  fileName,
31
29
  testFileName,
@@ -36,16 +34,16 @@ export async function zodOpenapiHonoHandler(openapi, config, test) {
36
34
  }
37
35
  const mergedHandlers = groupHandlersByFileNameHelper(handlers);
38
36
  for (const handler of mergedHandlers) {
39
- const dirPath = config?.output?.replace(/\/[^/]+\.ts$/, '');
37
+ const dirPath = output?.replace(/\/[^/]+\.ts$/, '');
40
38
  const handlerPath = dirPath === 'index.ts' ? 'handler' : `${dirPath}/handler`;
41
39
  const mkdirResult = await mkdir(handlerPath);
42
40
  if (!mkdirResult.ok) {
43
41
  return { ok: false, error: mkdirResult.error };
44
42
  }
45
43
  const routeTypes = handler.routeNames.map((routeName) => `${routeName}`).join(', ');
46
- const match = config.output?.match(/[^/]+\.ts$/);
44
+ const match = output?.match(/[^/]+\.ts$/);
47
45
  const matchPath = match ? match[0] : '';
48
- const path = config.output === '.' || config.output === './' ? config.output : `../${matchPath}`;
46
+ const path = output === '.' || output === './' ? output : `../${matchPath}`;
49
47
  const importRouteTypes = routeTypes ? `import type { ${routeTypes} } from '${path}';` : '';
50
48
  const importStatements = `${ROUTE_HANDLER}\n${importRouteTypes}`;
51
49
  const fileContent = `${importStatements}\n\n${handler.routeHandlerContents.join('\n\n')}`;
package/dist/index.d.ts CHANGED
@@ -1,21 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import type { Result } from './result/types.js';
3
- /**
4
- * CLI entry point for hono-takibi
5
- * Usage:
6
- * ```
7
- * hono-takibi <input-file> [-o output-file]
8
- * ```
9
- * @example
10
- * ```bash
11
- * # Global install
12
- * npm install -g hono-takibi
13
- * hono-takibi openapi.yaml -o routes.ts
14
- *
15
- * # NPX usage
16
- * npx hono-takibi openapi.yaml -o routes.ts
17
- * ```
18
- */
19
- export declare function main(): Promise<Result<{
20
- message: string;
21
- }, string>>;
2
+ export {};
package/dist/index.js CHANGED
@@ -1,40 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import path from 'node:path';
3
- import { zodOpenAPIHono } from './generator/zod-openapi-hono/openapi/zod-openapi-hono.js';
4
- import { generateApp } from './generator/zod-openapi-hono/app/index.js';
5
- import { zodOpenapiHonoHandler } from './generator/zod-openapi-hono/handler/zod-openapi-hono-handler.js';
6
- import { getConfig } from './config/index.js';
7
- import { fmt } from './format/index.js';
8
- import { parseCliArgs, parseHelp } from './cli/validator/index.js';
9
- import { setConfig, sliceArgs } from './cli/helper/index.js';
10
- import { parseOpenAPI } from './openapi/index.js';
11
- import { mkdir, writeFile, readdir } from './fsp/index.js';
12
- import { ok, err, andThen, asyncAndThen } from './result/index.js';
13
- const HELP_TEXT = `
14
- Usage:
15
- hono-takibi <input.yaml|json> -o <routes.ts> [options]
16
-
17
- Options:
18
- --export-type Export generated type aliases
19
-
20
- --export-schema Export generated schema objects
21
-
22
- --naming-case-type <PascalCase|camelCase>
23
- Casing for generated *type aliases*
24
- (default: PascalCase)
25
-
26
- --naming-case-schema <PascalCase|camelCase>
27
- Casing for generated *schema objects*
28
- (default: PascalCase)
29
-
30
- --template Generate an app file and handler stubs
31
-
32
- --test Generate empty *.test.ts files for handlers
33
-
34
- --base-path <path> API prefix (e.g. /api)
35
-
36
- --help Show this help and exit
37
- `.trimStart();
2
+ import { honoTakibi } from './cli/index.js';
38
3
  /**
39
4
  * CLI entry point for hono-takibi
40
5
  * Usage:
@@ -51,36 +16,7 @@ Options:
51
16
  * npx hono-takibi openapi.yaml -o routes.ts
52
17
  * ```
53
18
  */
54
- export async function main() {
55
- const args = sliceArgs(process.argv);
56
- if (parseHelp(args))
57
- return ok({ message: HELP_TEXT });
58
- const setting = andThen(getConfig(), (config) => andThen(parseCliArgs(args, config), (cli) => ok({ config, cli })));
59
- if (!setting.ok)
60
- return err(setting.error);
61
- const cli = parseCliArgs(args, setting.value.config);
62
- if (!cli.ok)
63
- return err(cli.error);
64
- const setConfigResult = setConfig(setting.value.config, cli.value);
65
- if (!setConfigResult.ok)
66
- return err(setConfigResult.error);
67
- const config = setConfigResult.value;
68
- return await asyncAndThen(await parseOpenAPI(config.input), async (openAPI) => asyncAndThen(await fmt(zodOpenAPIHono(openAPI, config)), async (code) => asyncAndThen(await mkdir(path.dirname(config.output)), async () => asyncAndThen(await writeFile(config.output, code), async () => {
69
- if (cli.value.template && config.output.includes('/')) {
70
- return asyncAndThen(await fmt(generateApp(openAPI, config, cli.value.basePath)), async (appCode) => asyncAndThen(await readdir(path.dirname(config.output)), async (files) => {
71
- const tgt = files.includes('index.ts')
72
- ? path.join(path.dirname(config.output), 'main.ts')
73
- : path.join(path.dirname(config.output), 'index.ts');
74
- return asyncAndThen(await writeFile(tgt, appCode), async () => {
75
- await zodOpenapiHonoHandler(openAPI, config, cli.value.test);
76
- return ok({ message: 'Generated code written template code' });
77
- });
78
- }));
79
- }
80
- return ok({ message: `Generated code written to ${config.output}` });
81
- }))));
82
- }
83
- main().then((result) => {
19
+ honoTakibi().then((result) => {
84
20
  if (result.ok) {
85
21
  console.log(result.value.message);
86
22
  process.exit(0);
@@ -1,6 +1,6 @@
1
- import type { Config } from '../config/index.js';
1
+ import { type Config } from '../config/index.js';
2
2
  import type { ViteDevServer } from 'vite';
3
- export default function HonoTakibiVite(settings: Config): {
3
+ export default function HonoTakibiVite(settings?: Config): {
4
4
  name: string;
5
5
  configureServer(server: ViteDevServer): void;
6
6
  };
@@ -3,7 +3,7 @@ import { vite } from './vite.js';
3
3
  export default function HonoTakibiVite(settings) {
4
4
  const configResult = getConfig();
5
5
  if (!configResult.ok) {
6
- throw new Error(configResult.error);
6
+ throw new Error(`Failed to load configuration: ${configResult.error}`);
7
7
  }
8
8
  const config = settings ?? configResult.value;
9
9
  return {
@@ -27,7 +27,7 @@ export async function vite(config) {
27
27
  }
28
28
  }
29
29
  catch (e) {
30
- console.error('Usage: hono-takibi <input-file> [-o output-file]');
30
+ console.error('Usage: hono-takibi <input.yaml|.json> -o <output.ts>');
31
31
  return false;
32
32
  }
33
33
  }
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.7.2",
4
+ "version": "0.7.3",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "keywords": [