From 058e6843b19409831859bfde34158f52466dc535 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Wed, 18 Feb 2026 11:19:58 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E8=87=B30.0.79=EF=BC=8C=E5=B9=B6=E5=9C=A8=20route.ts?= =?UTF-8?q?=20=E4=B8=AD=E4=BF=AE=E6=94=B9=20toJSONSchema=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=BB=A5=E6=94=AF=E6=8C=81=E5=8F=AF=E9=80=89=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/route.ts | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) 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') {