diff --git a/package.json b/package.json index 59e93a9..0cd7cd3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.0.78", + "version": "0.0.79", "description": "", "type": "module", "main": "./dist/router.js", diff --git a/src/route.ts b/src/route.ts index b25049d..bdd37ee 100644 --- a/src/route.ts +++ b/src/route.ts @@ -245,7 +245,7 @@ export class Route { +const extractArgs = (args: any) => { if (args && typeof args === 'object' && typeof args.shape === 'object') { return args.shape as z.ZodRawShape; } @@ -286,11 +286,20 @@ const fromJSONSchemaRoute = (route: RouteInfo): RouteInfo => { * @param args * @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 = extractArgs(args); const keys = Object.keys(args); - const newArgs: { [key: string]: any } = {}; + let newArgs: { [key: string]: any } = {}; for (let key of keys) { const item = args[key] as z.ZodAny; if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {