hono-takibi 0.1.3 → 0.1.4
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/core/validator/is-http-method.js +2 -1
- package/dist/core/validator/is-unique-content-schema.d.ts +8 -0
- package/dist/core/validator/is-unique-content-schema.js +13 -0
- package/dist/generators/openapi/paths/generate-route-code.js +2 -2
- package/dist/generators/request/body/generate-request-body.js +3 -2
- package/dist/generators/request/params/generate-params-object.js +11 -21
- package/dist/generators/request/params/generate-request-params-array.js +7 -1
- package/dist/generators/response/schemas/generate-response-schema.js +3 -2
- package/dist/types/index.d.ts +10 -13
- package/package.json +2 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Content } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Get unique content schema
|
|
4
|
+
* @param contentTypes - Content types
|
|
5
|
+
* @param content - Content
|
|
6
|
+
* @returns Unique content schema
|
|
7
|
+
*/
|
|
8
|
+
export declare function isUniqueContentSchema(contentTypes: string[], content: Content): boolean;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isUniqueContentSchema = isUniqueContentSchema;
|
|
4
|
+
/**
|
|
5
|
+
* Get unique content schema
|
|
6
|
+
* @param contentTypes - Content types
|
|
7
|
+
* @param content - Content
|
|
8
|
+
* @returns Unique content schema
|
|
9
|
+
*/
|
|
10
|
+
function isUniqueContentSchema(contentTypes, content) {
|
|
11
|
+
const schema = new Set(contentTypes.map((type) => JSON.stringify(content?.[type].schema)));
|
|
12
|
+
return schema.size === 1;
|
|
13
|
+
}
|
|
@@ -67,9 +67,9 @@ function generateRouteCode(openAPIPaths) {
|
|
|
67
67
|
// 3.1. skip parameters key and undefined operations
|
|
68
68
|
if (method === 'parameters' || !pathItemValue)
|
|
69
69
|
continue;
|
|
70
|
-
// 3.2. check if the method is an
|
|
70
|
+
// 3.2. check if the method is an HTTP method
|
|
71
71
|
if (!(0, is_http_method_1.isHttpMethod)(method))
|
|
72
|
-
|
|
72
|
+
throw new Error('Invalid HTTP method');
|
|
73
73
|
// 3.3 exclude the possibility of string or Parameter[]
|
|
74
74
|
if (typeof pathItemValue === 'string' || Array.isArray(pathItemValue))
|
|
75
75
|
continue;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateRequestBody = generateRequestBody;
|
|
4
|
+
const is_unique_content_schema_1 = require("../../../core/validator/is-unique-content-schema");
|
|
4
5
|
/**
|
|
5
6
|
* Generates a request body configuration for OpenAPI schema
|
|
6
7
|
*
|
|
@@ -23,9 +24,9 @@ function generateRequestBody(required, content, zodSchema) {
|
|
|
23
24
|
if (contentTypes.length === 0)
|
|
24
25
|
return '';
|
|
25
26
|
// check duplication
|
|
26
|
-
const
|
|
27
|
+
const isUniqueSchema = (0, is_unique_content_schema_1.isUniqueContentSchema)(contentTypes, content);
|
|
27
28
|
// all duplication same schema
|
|
28
|
-
if (
|
|
29
|
+
if (isUniqueSchema) {
|
|
29
30
|
const contentParts = [];
|
|
30
31
|
for (const contentType of contentTypes) {
|
|
31
32
|
contentParts.push(`'${contentType}':{schema:${zodSchema}}`);
|
|
@@ -3,15 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateParamsObject = generateParamsObject;
|
|
4
4
|
const generate_zod_coerce_1 = require("../../zod/generate-zod-coerce");
|
|
5
5
|
const generate_zod_schema_1 = require("../../zod/generate-zod-schema");
|
|
6
|
-
/**
|
|
7
|
-
* Mapping of parameter locations to their corresponding object keys
|
|
8
|
-
*/
|
|
9
|
-
const PARAM_LOCATION_TO_KEY = {
|
|
10
|
-
query: 'query',
|
|
11
|
-
path: 'params',
|
|
12
|
-
header: 'headers',
|
|
13
|
-
body: 'body',
|
|
14
|
-
};
|
|
15
6
|
/**
|
|
16
7
|
* Generates a params object containing Zod schemas for different parameter locations
|
|
17
8
|
*
|
|
@@ -72,22 +63,21 @@ const PARAM_LOCATION_TO_KEY = {
|
|
|
72
63
|
* - Organizes parameters into appropriate objects based on their location
|
|
73
64
|
* - Maintains empty objects for unused parameter locations
|
|
74
65
|
*/
|
|
75
|
-
// want to refactor this
|
|
76
66
|
function generateParamsObject(parameters) {
|
|
77
|
-
const initialParamsObj = {
|
|
78
|
-
query: {},
|
|
79
|
-
params: {},
|
|
80
|
-
headers: {},
|
|
81
|
-
body: {},
|
|
82
|
-
};
|
|
83
67
|
return parameters.reduce((acc, param) => {
|
|
68
|
+
const paramLocation = param.in;
|
|
84
69
|
const optionalSuffix = param.required ? '' : '.optional()';
|
|
85
|
-
const paramLocation = PARAM_LOCATION_TO_KEY[param.in];
|
|
86
70
|
const baseSchema = (0, generate_zod_schema_1.generateZodSchema)(param.schema);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
71
|
+
// Initialize section if it doesn't exist
|
|
72
|
+
if (!acc[paramLocation]) {
|
|
73
|
+
acc[paramLocation] = {};
|
|
74
|
+
}
|
|
75
|
+
// Handle coercion for query number/integer types
|
|
76
|
+
const zodSchema = param.in === 'query' && (param.schema.type === 'number' || param.schema.type === 'integer')
|
|
77
|
+
? (0, generate_zod_coerce_1.generateZodCoerce)('z.string()', baseSchema)
|
|
78
|
+
: baseSchema;
|
|
79
|
+
// Add parameter to its section
|
|
90
80
|
acc[paramLocation][param.name] = `${zodSchema}${optionalSuffix}`;
|
|
91
81
|
return acc;
|
|
92
|
-
},
|
|
82
|
+
}, {});
|
|
93
83
|
}
|
|
@@ -39,7 +39,9 @@ const generate_zod_object_schema_1 = require("../../zod/generate-zod-object-sche
|
|
|
39
39
|
*/
|
|
40
40
|
function generateRequestParamsArray(paramsObj) {
|
|
41
41
|
// 1. define sections to be processed
|
|
42
|
-
const sections =
|
|
42
|
+
const sections = Object.entries(paramsObj)
|
|
43
|
+
.filter(([_, obj]) => obj && Object.keys(obj).length > 0)
|
|
44
|
+
.map(([section]) => section);
|
|
43
45
|
// 2. processing of each section
|
|
44
46
|
return (sections
|
|
45
47
|
.map((section) => {
|
|
@@ -47,6 +49,10 @@ function generateRequestParamsArray(paramsObj) {
|
|
|
47
49
|
// 2.1 process only if object is not empty
|
|
48
50
|
if (Object.keys(obj).length) {
|
|
49
51
|
const schema = (0, generate_zod_object_schema_1.generateZodObjectSchema)(obj);
|
|
52
|
+
// path is params convention
|
|
53
|
+
if (section === 'path') {
|
|
54
|
+
return `params:${schema}`;
|
|
55
|
+
}
|
|
50
56
|
return `${section}:${schema}`;
|
|
51
57
|
}
|
|
52
58
|
return null;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateResponseSchema = generateResponseSchema;
|
|
4
4
|
const escape_quote_1 = require("../../../core/text/escape-quote");
|
|
5
|
+
const is_unique_content_schema_1 = require("../../../core/validator/is-unique-content-schema");
|
|
5
6
|
const generate_zod_property_schema_1 = require("../../zod/generate-zod-property-schema");
|
|
6
7
|
/**
|
|
7
8
|
* Generates a response schema for different status codes
|
|
@@ -79,9 +80,9 @@ function generateResponseSchema(responses) {
|
|
|
79
80
|
// 2.4 generating a response definition
|
|
80
81
|
// check duplication
|
|
81
82
|
const contentTypes = Object.keys(response.content);
|
|
82
|
-
const
|
|
83
|
+
const isUniqueSchema = (0, is_unique_content_schema_1.isUniqueContentSchema)(contentTypes, response.content);
|
|
83
84
|
// all duplication same schema
|
|
84
|
-
if (
|
|
85
|
+
if (isUniqueSchema) {
|
|
85
86
|
const contentParts = [];
|
|
86
87
|
for (const contentType of contentTypes) {
|
|
87
88
|
contentParts.push(`'${contentType}':{schema:${zodSchema}}`);
|
package/dist/types/index.d.ts
CHANGED
|
@@ -20,11 +20,7 @@ export type OpenAPIPaths = {
|
|
|
20
20
|
/**
|
|
21
21
|
* HTTP methods supported in OpenAPI
|
|
22
22
|
*/
|
|
23
|
-
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head';
|
|
24
|
-
/**
|
|
25
|
-
* Parameter types in OpenAPI
|
|
26
|
-
*/
|
|
27
|
-
export type Parameter = 'query' | 'path' | 'header' | 'body';
|
|
23
|
+
export type HttpMethod = 'get' | 'post' | 'put' | 'delete' | 'patch' | 'options' | 'head' | 'trace';
|
|
28
24
|
/**
|
|
29
25
|
* Data types supported in OpenAPI schemas
|
|
30
26
|
*/
|
|
@@ -73,7 +69,7 @@ export type Content = {
|
|
|
73
69
|
export type PathItem = {
|
|
74
70
|
summary?: string;
|
|
75
71
|
description?: string;
|
|
76
|
-
parameters?:
|
|
72
|
+
parameters?: string[];
|
|
77
73
|
} & {
|
|
78
74
|
[Method in HttpMethod]?: Operation;
|
|
79
75
|
};
|
|
@@ -99,7 +95,7 @@ export type Operation = {
|
|
|
99
95
|
/**
|
|
100
96
|
* Response definition with description and content
|
|
101
97
|
*/
|
|
102
|
-
type ResponseDefinition = {
|
|
98
|
+
export type ResponseDefinition = {
|
|
103
99
|
description: string;
|
|
104
100
|
content?: Content;
|
|
105
101
|
};
|
|
@@ -148,13 +144,14 @@ export type Components = {
|
|
|
148
144
|
requestBodies?: Record<string, RequestBody>;
|
|
149
145
|
};
|
|
150
146
|
/**
|
|
151
|
-
*
|
|
147
|
+
* Dynamic parameter section type
|
|
148
|
+
*/
|
|
149
|
+
export type ParamSection = Record<string, string>;
|
|
150
|
+
/**
|
|
151
|
+
* Flexible parameters object type that can handle any section name
|
|
152
152
|
*/
|
|
153
153
|
export type ParamsObject = {
|
|
154
|
-
|
|
155
|
-
params: Record<string, string>;
|
|
156
|
-
headers: Record<string, string>;
|
|
157
|
-
body: Record<string, string>;
|
|
154
|
+
[section: string]: ParamSection;
|
|
158
155
|
};
|
|
159
156
|
/**
|
|
160
157
|
* Parameter definition
|
|
@@ -164,7 +161,7 @@ export type Parameters = {
|
|
|
164
161
|
description?: string;
|
|
165
162
|
required?: boolean;
|
|
166
163
|
name: string;
|
|
167
|
-
in:
|
|
164
|
+
in: string;
|
|
168
165
|
explode?: boolean;
|
|
169
166
|
};
|
|
170
167
|
/**
|
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.1.
|
|
4
|
+
"version": "0.1.4",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"hono",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"prettier": "^3.4.2"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
+
"@hono/zod-openapi": "^0.18.3",
|
|
42
43
|
"@types/node": "^22.10.2",
|
|
43
44
|
"@vitest/coverage-v8": "^2.1.8",
|
|
44
45
|
"typescript": "^5.7.2",
|