hono-takibi 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -3
- package/dist/generators/request/params/generate-params-object.js +10 -5
- package/dist/generators/zod/generate-zod-coerce.d.ts +5 -1
- package/dist/generators/zod/generate-zod-coerce.js +12 -4
- package/dist/generators/zod/generate-zod-integer-schema.d.ts +2 -0
- package/dist/generators/zod/generate-zod-integer-schema.js +3 -0
- package/dist/generators/zod/generate-zod-number-schema.d.ts +2 -0
- package/dist/generators/zod/generate-zod-number-schema.js +3 -0
- package/dist/generators/zod/generate-zod-schema.js +5 -1
- package/dist/generators/zod/generate-zod-string-schema.d.ts +2 -1
- package/dist/generators/zod/generate-zod-string-schema.js +4 -1
- package/dist/types/index.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -426,9 +426,19 @@ export const deletePostsIdRoute = createRoute({
|
|
|
426
426
|
})
|
|
427
427
|
```
|
|
428
428
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
This
|
|
429
|
+
### ⚠️ WARNING: Potential Breaking Changes Without Notice
|
|
430
|
+
|
|
431
|
+
**This package is in active development and may introduce breaking changes without prior notice.**
|
|
432
|
+
Specifically:
|
|
433
|
+
- Query parameter coercion behavior may change
|
|
434
|
+
- Schema generation logic might be updated
|
|
435
|
+
- Output code structure could be modified
|
|
436
|
+
- Example value handling might be altered
|
|
437
|
+
|
|
438
|
+
We strongly recommend:
|
|
439
|
+
- Pinning to exact versions in production
|
|
440
|
+
- Testing thoroughly when updating versions
|
|
441
|
+
- Reviewing generated code after updates
|
|
432
442
|
|
|
433
443
|
We welcome feedback and contributions to improve the tool!
|
|
434
444
|
|
|
@@ -2,6 +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_openapi_example_1 = require("../../zod/generate-zod-openapi-example");
|
|
5
6
|
const generate_zod_schema_1 = require("../../zod/generate-zod-schema");
|
|
6
7
|
/**
|
|
7
8
|
* Mapping of parameter locations to their corresponding object keys
|
|
@@ -72,6 +73,7 @@ const PARAM_LOCATION_TO_KEY = {
|
|
|
72
73
|
* - Organizes parameters into appropriate objects based on their location
|
|
73
74
|
* - Maintains empty objects for unused parameter locations
|
|
74
75
|
*/
|
|
76
|
+
// want to refactor this
|
|
75
77
|
function generateParamsObject(parameters) {
|
|
76
78
|
const initialParamsObj = {
|
|
77
79
|
query: {},
|
|
@@ -82,11 +84,14 @@ function generateParamsObject(parameters) {
|
|
|
82
84
|
return parameters.reduce((acc, param) => {
|
|
83
85
|
const optionalSuffix = param.required ? '' : '.optional()';
|
|
84
86
|
const paramLocation = PARAM_LOCATION_TO_KEY[param.in];
|
|
85
|
-
|
|
86
|
-
const
|
|
87
|
-
param.schema.type === 'integer'
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
const baseSchema = (0, generate_zod_schema_1.generateZodSchema)(param.schema);
|
|
88
|
+
const isCoerceNeeded = (paramLocation === 'query' && param.schema.type === 'number') ||
|
|
89
|
+
param.schema.type === 'integer';
|
|
90
|
+
const zodSchema = isCoerceNeeded
|
|
91
|
+
? (0, generate_zod_coerce_1.generateZodCoerce)('z.string()', baseSchema, param.schema.example)
|
|
92
|
+
: param.schema.example
|
|
93
|
+
? (0, generate_zod_openapi_example_1.generateZodOpenAPIExample)(baseSchema, param.schema.example)
|
|
94
|
+
: baseSchema;
|
|
90
95
|
acc[paramLocation][param.name] = `${zodSchema}${optionalSuffix}`;
|
|
91
96
|
return acc;
|
|
92
97
|
}, initialParamsObj);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ExampleValue } from '../../types';
|
|
1
2
|
/**
|
|
2
3
|
* @description
|
|
3
4
|
* Generates a zod pipe function to coerce a value to a zod schema.
|
|
@@ -7,4 +8,7 @@
|
|
|
7
8
|
* @param zodSchema - The zod schema to coerce.
|
|
8
9
|
* @returns A zod pipe function to coerce a value to a zod schema.
|
|
9
10
|
*/
|
|
10
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Generates a Zod coercion schema using pipe with example support
|
|
13
|
+
*/
|
|
14
|
+
export declare function generateZodCoerce(inputSchema: string, outputSchema: string, example?: ExampleValue): string;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateZodCoerce = generateZodCoerce;
|
|
4
|
-
const
|
|
4
|
+
const generate_zod_openapi_example_1 = require("./generate-zod-openapi-example");
|
|
5
5
|
/**
|
|
6
6
|
* @description
|
|
7
7
|
* Generates a zod pipe function to coerce a value to a zod schema.
|
|
@@ -11,7 +11,15 @@ const remove_zod_prefix_1 = require("../../core/text/remove-zod-prefix");
|
|
|
11
11
|
* @param zodSchema - The zod schema to coerce.
|
|
12
12
|
* @returns A zod pipe function to coerce a value to a zod schema.
|
|
13
13
|
*/
|
|
14
|
-
function generateZodCoerce(z, zodSchema) {
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
// export function generateZodCoerce(z: string, zodSchema: string) {
|
|
15
|
+
// const zod = removeZodPrefix(zodSchema)
|
|
16
|
+
// return `${z}.pipe(z.coerce.${zod})`
|
|
17
|
+
// }
|
|
18
|
+
/**
|
|
19
|
+
* Generates a Zod coercion schema using pipe with example support
|
|
20
|
+
*/
|
|
21
|
+
function generateZodCoerce(inputSchema, outputSchema, example) {
|
|
22
|
+
const coerceSchema = outputSchema.replace('z.', 'z.coerce.');
|
|
23
|
+
const finalSchema = example ? (0, generate_zod_openapi_example_1.generateZodOpenAPIExample)(coerceSchema, example) : coerceSchema;
|
|
24
|
+
return `${inputSchema}.pipe(${finalSchema})`;
|
|
17
25
|
}
|
|
@@ -20,5 +20,8 @@ function generateZodIntegerSchema(args) {
|
|
|
20
20
|
validations.push(`.min(${minimum})`);
|
|
21
21
|
if (typeof maximum === 'number')
|
|
22
22
|
validations.push(`.max(${maximum})`);
|
|
23
|
+
// default
|
|
24
|
+
if (args.default)
|
|
25
|
+
validations.push(`.default(${args.default})`);
|
|
23
26
|
return validations.join('');
|
|
24
27
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import type { DefaultValue } from '../../types';
|
|
1
2
|
type GenerateZodNumberSchemaParams = {
|
|
2
3
|
pattern?: string;
|
|
3
4
|
minLength?: number;
|
|
4
5
|
maxLength?: number;
|
|
5
6
|
minimum?: number;
|
|
6
7
|
maximum?: number;
|
|
8
|
+
default?: DefaultValue;
|
|
7
9
|
};
|
|
8
10
|
/**
|
|
9
11
|
* Generates a zod schema for a number.
|
|
@@ -22,5 +22,8 @@ function generateZodNumberSchema(args) {
|
|
|
22
22
|
validations.push(`.min(${minimum})`);
|
|
23
23
|
if (typeof maximum === 'number')
|
|
24
24
|
validations.push(`.max(${maximum})`);
|
|
25
|
+
// default
|
|
26
|
+
if (args.default)
|
|
27
|
+
validations.push(`.default(${args.default})`);
|
|
25
28
|
return validations.join('');
|
|
26
29
|
}
|
|
@@ -87,7 +87,8 @@ const TYPE_TO_ZOD_SCHEMA = {
|
|
|
87
87
|
* - Returns z.any() for unknown types with a warning
|
|
88
88
|
*/
|
|
89
89
|
function generateZodSchema(schema) {
|
|
90
|
-
const { type, format, pattern, minLength, maxLength, minimum, maximum,
|
|
90
|
+
const { type, format, pattern, minLength, maxLength, minimum, maximum, default: defaultValue, // reserved word
|
|
91
|
+
properties, required = [], items, enum: enumValues, additionalProperties, } = schema;
|
|
91
92
|
// enum
|
|
92
93
|
if (enumValues)
|
|
93
94
|
return `z.enum(${JSON.stringify(enumValues)})`;
|
|
@@ -106,6 +107,7 @@ function generateZodSchema(schema) {
|
|
|
106
107
|
minLength,
|
|
107
108
|
maxLength,
|
|
108
109
|
format: format && (0, is_format_string_1.isFormatString)(format) ? format : undefined,
|
|
110
|
+
default: defaultValue,
|
|
109
111
|
});
|
|
110
112
|
}
|
|
111
113
|
// number
|
|
@@ -116,6 +118,7 @@ function generateZodSchema(schema) {
|
|
|
116
118
|
maxLength,
|
|
117
119
|
minimum,
|
|
118
120
|
maximum,
|
|
121
|
+
default: defaultValue,
|
|
119
122
|
});
|
|
120
123
|
}
|
|
121
124
|
// integer
|
|
@@ -125,6 +128,7 @@ function generateZodSchema(schema) {
|
|
|
125
128
|
maxLength,
|
|
126
129
|
minimum,
|
|
127
130
|
maximum,
|
|
131
|
+
default: defaultValue,
|
|
128
132
|
});
|
|
129
133
|
}
|
|
130
134
|
// array
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { FormatString } from '../../types';
|
|
1
|
+
import type { FormatString, DefaultValue } from '../../types';
|
|
2
2
|
type GenerateZodStringSchemaParams = {
|
|
3
3
|
pattern?: string;
|
|
4
4
|
minLength?: number;
|
|
5
5
|
maxLength?: number;
|
|
6
6
|
format?: FormatString;
|
|
7
|
+
default?: DefaultValue;
|
|
7
8
|
};
|
|
8
9
|
/**
|
|
9
10
|
* Generates a Zod schema string for string validation
|
|
@@ -41,7 +41,7 @@ const get_zod_string_format_1 = require("../../core/zod/get-zod-string-format");
|
|
|
41
41
|
*/
|
|
42
42
|
function generateZodStringSchema(args) {
|
|
43
43
|
const validations = ['z.string()'];
|
|
44
|
-
const { pattern, minLength, maxLength, format } = args;
|
|
44
|
+
const { pattern, minLength, maxLength, format, default: defaultValue } = args;
|
|
45
45
|
if (pattern)
|
|
46
46
|
validations.push(`.regex(/${pattern}/)`);
|
|
47
47
|
if (minLength)
|
|
@@ -50,5 +50,8 @@ function generateZodStringSchema(args) {
|
|
|
50
50
|
validations.push(`.max(${maxLength})`);
|
|
51
51
|
if (format)
|
|
52
52
|
validations.push((0, get_zod_string_format_1.getZodFormatString)(format));
|
|
53
|
+
// default
|
|
54
|
+
if (defaultValue)
|
|
55
|
+
validations.push(`.default(${JSON.stringify(defaultValue)})`);
|
|
53
56
|
return validations.join('');
|
|
54
57
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -53,6 +53,10 @@ type ArrayExample = Array<PrimitiveExample | ObjectExample>;
|
|
|
53
53
|
* Combined example type
|
|
54
54
|
*/
|
|
55
55
|
export type ExampleValue = PrimitiveExample | ObjectExample | ArrayExample;
|
|
56
|
+
/**
|
|
57
|
+
* Default value type
|
|
58
|
+
*/
|
|
59
|
+
export type DefaultValue = PrimitiveExample | ObjectExample | ArrayExample;
|
|
56
60
|
/**
|
|
57
61
|
* Content type definitions with their schemas
|
|
58
62
|
*/
|
|
@@ -129,6 +133,7 @@ export type Schema = {
|
|
|
129
133
|
maxLength?: number;
|
|
130
134
|
minimum?: number;
|
|
131
135
|
maximum?: number;
|
|
136
|
+
default?: DefaultValue;
|
|
132
137
|
example?: ExampleValue;
|
|
133
138
|
properties?: Record<string, Schema>;
|
|
134
139
|
required?: string[];
|