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.
- package/dist/cli/app.d.ts +3 -0
- package/dist/cli/app.js +15 -0
- package/dist/cli/helper/set-config.d.ts +2 -2
- package/dist/cli/index.d.ts +4 -0
- package/dist/cli/index.js +38 -0
- package/dist/cli/takibi.d.ts +15 -0
- package/dist/cli/takibi.js +14 -0
- package/dist/cli/types/index.d.ts +2 -2
- package/dist/cli/validator/index.d.ts +2 -0
- package/dist/cli/validator/index.js +2 -0
- package/dist/cli/validator/is-ts.d.ts +1 -0
- package/dist/cli/validator/is-ts.js +3 -0
- package/dist/cli/validator/is-yaml-or-json.d.ts +1 -0
- package/dist/cli/validator/is-yaml-or-json.js +3 -0
- package/dist/cli/validator/parse-cli-args.d.ts +2 -2
- package/dist/cli/validator/parse-help.js +1 -1
- package/dist/cli/validator/parse-io.d.ts +4 -4
- package/dist/cli/validator/parse-io.js +7 -3
- package/dist/config/index.d.ts +2 -2
- package/dist/generator/zod-openapi-hono/app/helper/get-route-maps.js +6 -4
- package/dist/generator/zod-openapi-hono/app/helper/process-import-map.d.ts +1 -2
- package/dist/generator/zod-openapi-hono/app/helper/process-import-map.js +2 -2
- package/dist/generator/zod-openapi-hono/app/index.d.ts +2 -3
- package/dist/generator/zod-openapi-hono/app/index.js +5 -6
- package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler-name.d.ts → generator/handler-name.d.ts} +1 -1
- package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler-name.js → generator/handler-name.js} +1 -1
- package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler.d.ts → generator/handler.d.ts} +1 -1
- package/dist/generator/zod-openapi-hono/handler/{generators/generate-handler.js → generator/handler.js} +1 -1
- package/dist/generator/zod-openapi-hono/handler/{generators/generate-import-handlers.d.ts → generator/import-handlers.d.ts} +3 -4
- package/dist/generator/zod-openapi-hono/handler/{generators/generate-import-handlers.js → generator/import-handlers.js} +3 -3
- package/dist/generator/zod-openapi-hono/handler/generator/index.d.ts +3 -0
- package/dist/generator/zod-openapi-hono/handler/generator/index.js +3 -0
- package/dist/generator/zod-openapi-hono/handler/zod-openapi-hono-handler.d.ts +3 -4
- package/dist/generator/zod-openapi-hono/handler/zod-openapi-hono-handler.js +10 -12
- package/dist/index.d.ts +1 -20
- package/dist/index.js +2 -66
- package/dist/vite-plugin/index.d.ts +2 -2
- package/dist/vite-plugin/index.js +1 -1
- package/dist/vite-plugin/vite.js +1 -1
- package/package.json +1 -1
package/dist/cli/app.js
ADDED
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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 @@
|
|
|
1
|
+
export declare function isYamlOrJson(i: string): i is `${string}.yaml` | `${string}.json`;
|
|
@@ -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>;
|
|
@@ -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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
}
|
package/dist/config/index.d.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
}[],
|
|
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,
|
|
7
|
+
export function processImportMap(routeMappings, output) {
|
|
8
8
|
const importsMap = {};
|
|
9
9
|
for (const { routeName } of routeMappings) {
|
|
10
|
-
const path =
|
|
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 {
|
|
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,
|
|
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 {
|
|
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 {
|
|
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,
|
|
23
|
+
export function generateApp(openAPISpec, output, basePath) {
|
|
24
24
|
const routeMappings = getRouteMaps(openAPISpec);
|
|
25
|
-
const importsMap = processImportMap(routeMappings,
|
|
25
|
+
const importsMap = processImportMap(routeMappings, output);
|
|
26
26
|
const importRoutes = generateImportRoutes(importsMap);
|
|
27
27
|
const handlerImportsMap = getHandlerImports(routeMappings);
|
|
28
|
-
const
|
|
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}`;
|
|
@@ -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
|
|
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
|
|
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
|
|
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 {
|
|
4
|
+
* @param { string } output - Output file path
|
|
6
5
|
* @returns { string[] } Import handlers
|
|
7
6
|
*/
|
|
8
|
-
export declare function
|
|
7
|
+
export declare function importHandlers(handlerImportsMap: {
|
|
9
8
|
[fileName: string]: string[];
|
|
10
|
-
},
|
|
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 {
|
|
4
|
+
* @param { string } output - Output file path
|
|
5
5
|
* @returns { string[] } Import handlers
|
|
6
6
|
*/
|
|
7
|
-
export function
|
|
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 =
|
|
11
|
+
const replacePath = output?.replace(/\/[^/]+\.ts$/, '');
|
|
12
12
|
const dirPath = replacePath === undefined ? '.' : replacePath;
|
|
13
13
|
const handlerPath = 'handler';
|
|
14
14
|
if (dirPath === '.') {
|
|
@@ -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 {
|
|
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,
|
|
16
|
+
export declare function zodOpenapiHonoHandler(openapi: OpenAPISpec, output: string, test: boolean): Promise<Result<void, string>>;
|
|
@@ -1,31 +1,29 @@
|
|
|
1
|
-
import {
|
|
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 {
|
|
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,
|
|
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
|
|
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 ? '
|
|
28
|
-
const testFileName = path_name.length === 0 ? '
|
|
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 =
|
|
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 =
|
|
44
|
+
const match = output?.match(/[^/]+\.ts$/);
|
|
47
45
|
const matchPath = match ? match[0] : '';
|
|
48
|
-
const path =
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
1
|
+
import { type Config } from '../config/index.js';
|
|
2
2
|
import type { ViteDevServer } from 'vite';
|
|
3
|
-
export default function HonoTakibiVite(settings
|
|
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 {
|
package/dist/vite-plugin/vite.js
CHANGED