chore: 更新版本号至0.0.79,并在 route.ts 中修改 toJSONSchema 函数以支持可选参数
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/package",
|
"$schema": "https://json.schemastore.org/package",
|
||||||
"name": "@kevisual/router",
|
"name": "@kevisual/router",
|
||||||
"version": "0.0.78",
|
"version": "0.0.79",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/router.js",
|
"main": "./dist/router.js",
|
||||||
|
|||||||
15
src/route.ts
15
src/route.ts
@@ -245,7 +245,7 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
|
|||||||
throw new CustomError(...args);
|
throw new CustomError(...args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export const extractArgs = (args: any) => {
|
const extractArgs = (args: any) => {
|
||||||
if (args && typeof args === 'object' && typeof args.shape === 'object') {
|
if (args && typeof args === 'object' && typeof args.shape === 'object') {
|
||||||
return args.shape as z.ZodRawShape;
|
return args.shape as z.ZodRawShape;
|
||||||
}
|
}
|
||||||
@@ -286,11 +286,20 @@ const fromJSONSchemaRoute = (route: RouteInfo): RouteInfo => {
|
|||||||
* @param args
|
* @param args
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const toJSONSchema = (args: any): { [key: string]: any } => {
|
export const toJSONSchema = (args: any, opts?: { mergeObject?: boolean }): { [key: string]: any } => {
|
||||||
|
const mergeObject = opts?.mergeObject ?? true;
|
||||||
|
if (!args) return {};
|
||||||
|
if (mergeObject) {
|
||||||
|
if (typeof args === 'object' && typeof args.toJSONSchema === 'function') {
|
||||||
|
return args.toJSONSchema();
|
||||||
|
}
|
||||||
|
const schema = z.object(args);
|
||||||
|
return schema.toJSONSchema();
|
||||||
|
}
|
||||||
// 如果 args 本身是一个 zod object schema,先提取 shape
|
// 如果 args 本身是一个 zod object schema,先提取 shape
|
||||||
args = extractArgs(args);
|
args = extractArgs(args);
|
||||||
const keys = Object.keys(args);
|
const keys = Object.keys(args);
|
||||||
const newArgs: { [key: string]: any } = {};
|
let newArgs: { [key: string]: any } = {};
|
||||||
for (let key of keys) {
|
for (let key of keys) {
|
||||||
const item = args[key] as z.ZodAny;
|
const item = args[key] as z.ZodAny;
|
||||||
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
|
||||||
|
|||||||
Reference in New Issue
Block a user