@kevisual/router 0.0.75 → 0.0.77

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/app.js CHANGED
@@ -16824,7 +16824,6 @@ class Route {
16824
16824
  metadata;
16825
16825
  middleware;
16826
16826
  type = "route";
16827
- data;
16828
16827
  isDebug;
16829
16828
  constructor(path = "", key = "", opts) {
16830
16829
  if (!path) {
@@ -16916,22 +16915,21 @@ class Route {
16916
16915
  addTo(router, opts) {
16917
16916
  router.add(this, opts);
16918
16917
  }
16919
- setData(data) {
16920
- this.data = data;
16921
- return this;
16922
- }
16923
16918
  throw(...args) {
16924
16919
  throw new CustomError(...args);
16925
16920
  }
16926
16921
  }
16927
- var toJSONSchema2 = (route) => {
16922
+ var extractArgs = (args) => {
16923
+ if (args && typeof args === "object" && typeof args.shape === "object") {
16924
+ return args.shape;
16925
+ }
16926
+ return args || {};
16927
+ };
16928
+ var toJSONSchemaRoute = (route) => {
16928
16929
  const pickValues = pick(route, pickValue);
16929
16930
  if (pickValues?.metadata?.args) {
16930
- const args = pickValues.metadata.args;
16931
- if (args && typeof args === "object" && args.toJSONSchema && typeof args.toJSONSchema === "function") {
16932
- pickValues.metadata.args = args.toJSONSchema();
16933
- return pickValues;
16934
- }
16931
+ let args = pickValues.metadata.args;
16932
+ args = extractArgs(args);
16935
16933
  const keys = Object.keys(args);
16936
16934
  const newArgs = {};
16937
16935
  for (let key of keys) {
@@ -16946,22 +16944,46 @@ var toJSONSchema2 = (route) => {
16946
16944
  }
16947
16945
  return pickValues;
16948
16946
  };
16949
- var fromJSONSchema2 = (route) => {
16950
- const args = route?.metadata?.args;
16951
- if (!args)
16952
- return route;
16953
- if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
16954
- route.metadata.args = exports_external.fromJSONSchema(args);
16955
- return route;
16956
- }
16947
+ var toJSONSchema2 = (args) => {
16948
+ args = extractArgs(args);
16957
16949
  const keys = Object.keys(args);
16958
16950
  const newArgs = {};
16959
16951
  for (let key of keys) {
16960
16952
  const item = args[key];
16961
- newArgs[key] = exports_external.fromJSONSchema(item);
16953
+ if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
16954
+ newArgs[key] = item.toJSONSchema();
16955
+ } else {
16956
+ newArgs[key] = args[key];
16957
+ }
16962
16958
  }
16963
- route.metadata.args = newArgs;
16964
- return route;
16959
+ return newArgs;
16960
+ };
16961
+ var fromJSONSchema2 = (args = {}, opts) => {
16962
+ let resultArgs = null;
16963
+ const mergeObject = opts?.mergeObject ?? true;
16964
+ if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
16965
+ const objectSchema = exports_external.fromJSONSchema(args);
16966
+ const extract = extractArgs(objectSchema);
16967
+ const keys = Object.keys(extract);
16968
+ const newArgs = {};
16969
+ for (let key of keys) {
16970
+ newArgs[key] = extract[key];
16971
+ }
16972
+ resultArgs = newArgs;
16973
+ }
16974
+ if (!resultArgs) {
16975
+ const keys = Object.keys(args);
16976
+ const newArgs = {};
16977
+ for (let key of keys) {
16978
+ const item = args[key];
16979
+ newArgs[key] = exports_external.fromJSONSchema(item);
16980
+ }
16981
+ resultArgs = newArgs;
16982
+ }
16983
+ if (mergeObject) {
16984
+ resultArgs = exports_external.object(resultArgs);
16985
+ }
16986
+ return resultArgs;
16965
16987
  };
16966
16988
 
16967
16989
  class QueryRouter {
@@ -17301,7 +17323,7 @@ class QueryRouter {
17301
17323
  ctx.body = {
17302
17324
  list: list.map((item) => {
17303
17325
  const route = pick(item, ["id", "path", "key", "description", "middleware", "metadata"]);
17304
- return toJSONSchema2(route);
17326
+ return toJSONSchemaRoute(route);
17305
17327
  }),
17306
17328
  isUser
17307
17329
  };
@@ -19251,7 +19273,7 @@ var createRouterAgentPluginFn = (opts) => {
19251
19273
  tool: {
19252
19274
  ...routes.reduce((acc, route) => {
19253
19275
  const metadata = route.metadata;
19254
- let args = extractArgs(metadata?.args);
19276
+ let args = extractArgs2(metadata?.args);
19255
19277
  acc[metadata.skill] = {
19256
19278
  name: metadata.title || metadata.skill,
19257
19279
  description: metadata.summary || "",
@@ -19287,7 +19309,7 @@ var createRouterAgentPluginFn = (opts) => {
19287
19309
  };
19288
19310
  return AgentPlugin;
19289
19311
  };
19290
- var extractArgs = (args) => {
19312
+ var extractArgs2 = (args) => {
19291
19313
  if (args && typeof args === "object" && typeof args.shape === "object") {
19292
19314
  return args.shape;
19293
19315
  }
@@ -19485,7 +19507,7 @@ app
19485
19507
  10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
19486
19508
  `;
19487
19509
  // package.json
19488
- var version2 = "0.0.75";
19510
+ var version2 = "0.0.77";
19489
19511
 
19490
19512
  // agent/routes/route-create.ts
19491
19513
  app.route({
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
+ import { z } from 'zod';
2
3
  import * as http from 'node:http';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
@@ -150,7 +151,6 @@ declare class Route<U = {
150
151
  metadata?: T;
151
152
  middleware?: RouteMiddleware[];
152
153
  type?: string;
153
- data?: any;
154
154
  /**
155
155
  * 是否开启debug,开启后会打印错误信息
156
156
  */
@@ -175,7 +175,6 @@ declare class Route<U = {
175
175
  add: (route: Route) => void;
176
176
  [key: string]: any;
177
177
  }, opts?: AddOpts): void;
178
- setData(data: any): this;
179
178
  throw(code?: number | string, message?: string, tips?: string): void;
180
179
  }
181
180
  /**
@@ -349,8 +348,16 @@ declare class QueryRouter {
349
348
  filter?: (route: Route) => boolean;
350
349
  routeListMiddleware?: string[];
351
350
  }): Promise<void>;
352
- toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
353
- fromJSONSchema: (route: RouteInfo) => RouteInfo;
351
+ toJSONSchema: (args: any) => {
352
+ [key: string]: any;
353
+ };
354
+ fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
355
+ mergeObject?: boolean;
356
+ }) => Merge extends true ? z.ZodObject<{
357
+ [key: string]: any;
358
+ }, z.core.$strip> : {
359
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
360
+ };
354
361
  }
355
362
  type QueryRouterServerOpts = {
356
363
  handleFn?: HandleFn;
@@ -178,7 +178,6 @@ declare class Route<U = {
178
178
  metadata?: T;
179
179
  middleware?: RouteMiddleware[];
180
180
  type?: string;
181
- data?: any;
182
181
  /**
183
182
  * 是否开启debug,开启后会打印错误信息
184
183
  */
@@ -203,11 +202,23 @@ declare class Route<U = {
203
202
  add: (route: Route) => void;
204
203
  [key: string]: any;
205
204
  }, opts?: AddOpts): void;
206
- setData(data: any): this;
207
205
  throw(code?: number | string, message?: string, tips?: string): void;
208
206
  }
209
- declare const toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
210
- declare const fromJSONSchema: (route: RouteInfo) => RouteInfo;
207
+ /**
208
+ * 剥离第一层schema,转换为JSON Schema,无论是skill还是其他的infer比纯粹的zod object schema更合适,因为它可能包含其他的字段,而不仅仅是schema
209
+ * @param args
210
+ * @returns
211
+ */
212
+ declare const toJSONSchema: (args: any) => {
213
+ [key: string]: any;
214
+ };
215
+ declare const fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
216
+ mergeObject?: boolean;
217
+ }) => Merge extends true ? z.ZodObject<{
218
+ [key: string]: any;
219
+ }, z.core.$strip> : {
220
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
221
+ };
211
222
  /**
212
223
  * @parmas overwrite 是否覆盖已存在的route,默认true
213
224
  */
@@ -379,8 +390,16 @@ declare class QueryRouter {
379
390
  filter?: (route: Route) => boolean;
380
391
  routeListMiddleware?: string[];
381
392
  }): Promise<void>;
382
- toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
383
- fromJSONSchema: (route: RouteInfo) => RouteInfo;
393
+ toJSONSchema: (args: any) => {
394
+ [key: string]: any;
395
+ };
396
+ fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
397
+ mergeObject?: boolean;
398
+ }) => Merge extends true ? z.ZodObject<{
399
+ [key: string]: any;
400
+ }, z.core.$strip> : {
401
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
402
+ };
384
403
  }
385
404
  type QueryRouterServerOpts = {
386
405
  handleFn?: HandleFn;
@@ -13988,7 +13988,6 @@ class Route {
13988
13988
  metadata;
13989
13989
  middleware;
13990
13990
  type = "route";
13991
- data;
13992
13991
  isDebug;
13993
13992
  constructor(path = "", key = "", opts) {
13994
13993
  if (!path) {
@@ -14080,22 +14079,21 @@ class Route {
14080
14079
  addTo(router, opts) {
14081
14080
  router.add(this, opts);
14082
14081
  }
14083
- setData(data) {
14084
- this.data = data;
14085
- return this;
14086
- }
14087
14082
  throw(...args) {
14088
14083
  throw new CustomError(...args);
14089
14084
  }
14090
14085
  }
14091
- var toJSONSchema2 = (route) => {
14086
+ var extractArgs = (args) => {
14087
+ if (args && typeof args === "object" && typeof args.shape === "object") {
14088
+ return args.shape;
14089
+ }
14090
+ return args || {};
14091
+ };
14092
+ var toJSONSchemaRoute = (route) => {
14092
14093
  const pickValues = pick(route, pickValue);
14093
14094
  if (pickValues?.metadata?.args) {
14094
- const args = pickValues.metadata.args;
14095
- if (args && typeof args === "object" && args.toJSONSchema && typeof args.toJSONSchema === "function") {
14096
- pickValues.metadata.args = args.toJSONSchema();
14097
- return pickValues;
14098
- }
14095
+ let args = pickValues.metadata.args;
14096
+ args = extractArgs(args);
14099
14097
  const keys = Object.keys(args);
14100
14098
  const newArgs = {};
14101
14099
  for (let key of keys) {
@@ -14110,22 +14108,46 @@ var toJSONSchema2 = (route) => {
14110
14108
  }
14111
14109
  return pickValues;
14112
14110
  };
14113
- var fromJSONSchema2 = (route) => {
14114
- const args = route?.metadata?.args;
14115
- if (!args)
14116
- return route;
14117
- if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
14118
- route.metadata.args = exports_external.fromJSONSchema(args);
14119
- return route;
14120
- }
14111
+ var toJSONSchema2 = (args) => {
14112
+ args = extractArgs(args);
14121
14113
  const keys = Object.keys(args);
14122
14114
  const newArgs = {};
14123
14115
  for (let key of keys) {
14124
14116
  const item = args[key];
14125
- newArgs[key] = exports_external.fromJSONSchema(item);
14117
+ if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
14118
+ newArgs[key] = item.toJSONSchema();
14119
+ } else {
14120
+ newArgs[key] = args[key];
14121
+ }
14122
+ }
14123
+ return newArgs;
14124
+ };
14125
+ var fromJSONSchema2 = (args = {}, opts) => {
14126
+ let resultArgs = null;
14127
+ const mergeObject = opts?.mergeObject ?? true;
14128
+ if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
14129
+ const objectSchema = exports_external.fromJSONSchema(args);
14130
+ const extract = extractArgs(objectSchema);
14131
+ const keys = Object.keys(extract);
14132
+ const newArgs = {};
14133
+ for (let key of keys) {
14134
+ newArgs[key] = extract[key];
14135
+ }
14136
+ resultArgs = newArgs;
14137
+ }
14138
+ if (!resultArgs) {
14139
+ const keys = Object.keys(args);
14140
+ const newArgs = {};
14141
+ for (let key of keys) {
14142
+ const item = args[key];
14143
+ newArgs[key] = exports_external.fromJSONSchema(item);
14144
+ }
14145
+ resultArgs = newArgs;
14146
+ }
14147
+ if (mergeObject) {
14148
+ resultArgs = exports_external.object(resultArgs);
14126
14149
  }
14127
- route.metadata.args = newArgs;
14128
- return route;
14150
+ return resultArgs;
14129
14151
  };
14130
14152
 
14131
14153
  class QueryRouter {
@@ -14465,7 +14487,7 @@ class QueryRouter {
14465
14487
  ctx.body = {
14466
14488
  list: list.map((item) => {
14467
14489
  const route = pick(item, ["id", "path", "key", "description", "middleware", "metadata"]);
14468
- return toJSONSchema2(route);
14490
+ return toJSONSchemaRoute(route);
14469
14491
  }),
14470
14492
  isUser
14471
14493
  };
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
+ import { z } from 'zod';
2
3
  import { Query, DataOpts, Result } from '@kevisual/query/query';
3
4
 
4
5
  declare class MockProcess {
@@ -147,7 +148,6 @@ declare class Route<U = {
147
148
  metadata?: T;
148
149
  middleware?: RouteMiddleware[];
149
150
  type?: string;
150
- data?: any;
151
151
  /**
152
152
  * 是否开启debug,开启后会打印错误信息
153
153
  */
@@ -172,7 +172,6 @@ declare class Route<U = {
172
172
  add: (route: Route) => void;
173
173
  [key: string]: any;
174
174
  }, opts?: AddOpts): void;
175
- setData(data: any): this;
176
175
  throw(code?: number | string, message?: string, tips?: string): void;
177
176
  }
178
177
  /**
@@ -346,8 +345,16 @@ declare class QueryRouter {
346
345
  filter?: (route: Route) => boolean;
347
346
  routeListMiddleware?: string[];
348
347
  }): Promise<void>;
349
- toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
350
- fromJSONSchema: (route: RouteInfo) => RouteInfo;
348
+ toJSONSchema: (args: any) => {
349
+ [key: string]: any;
350
+ };
351
+ fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
352
+ mergeObject?: boolean;
353
+ }) => Merge extends true ? z.ZodObject<{
354
+ [key: string]: any;
355
+ }, z.core.$strip> : {
356
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
357
+ };
351
358
  }
352
359
  type QueryRouterServerOpts = {
353
360
  handleFn?: HandleFn;
package/dist/router.d.ts CHANGED
@@ -184,7 +184,6 @@ declare class Route<U = {
184
184
  metadata?: T;
185
185
  middleware?: RouteMiddleware[];
186
186
  type?: string;
187
- data?: any;
188
187
  /**
189
188
  * 是否开启debug,开启后会打印错误信息
190
189
  */
@@ -209,11 +208,23 @@ declare class Route<U = {
209
208
  add: (route: Route) => void;
210
209
  [key: string]: any;
211
210
  }, opts?: AddOpts): void;
212
- setData(data: any): this;
213
211
  throw(code?: number | string, message?: string, tips?: string): void;
214
212
  }
215
- declare const toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
216
- declare const fromJSONSchema: (route: RouteInfo) => RouteInfo;
213
+ /**
214
+ * 剥离第一层schema,转换为JSON Schema,无论是skill还是其他的infer比纯粹的zod object schema更合适,因为它可能包含其他的字段,而不仅仅是schema
215
+ * @param args
216
+ * @returns
217
+ */
218
+ declare const toJSONSchema: (args: any) => {
219
+ [key: string]: any;
220
+ };
221
+ declare const fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
222
+ mergeObject?: boolean;
223
+ }) => Merge extends true ? z.ZodObject<{
224
+ [key: string]: any;
225
+ }, z.core.$strip> : {
226
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
227
+ };
217
228
  /**
218
229
  * @parmas overwrite 是否覆盖已存在的route,默认true
219
230
  */
@@ -385,8 +396,16 @@ declare class QueryRouter {
385
396
  filter?: (route: Route) => boolean;
386
397
  routeListMiddleware?: string[];
387
398
  }): Promise<void>;
388
- toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
389
- fromJSONSchema: (route: RouteInfo) => RouteInfo;
399
+ toJSONSchema: (args: any) => {
400
+ [key: string]: any;
401
+ };
402
+ fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
403
+ mergeObject?: boolean;
404
+ }) => Merge extends true ? z.ZodObject<{
405
+ [key: string]: any;
406
+ }, z.core.$strip> : {
407
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
408
+ };
390
409
  }
391
410
  type QueryRouterServerOpts = {
392
411
  handleFn?: HandleFn;
package/dist/router.js CHANGED
@@ -16821,7 +16821,6 @@ class Route {
16821
16821
  metadata;
16822
16822
  middleware;
16823
16823
  type = "route";
16824
- data;
16825
16824
  isDebug;
16826
16825
  constructor(path = "", key = "", opts) {
16827
16826
  if (!path) {
@@ -16913,22 +16912,21 @@ class Route {
16913
16912
  addTo(router, opts) {
16914
16913
  router.add(this, opts);
16915
16914
  }
16916
- setData(data) {
16917
- this.data = data;
16918
- return this;
16919
- }
16920
16915
  throw(...args) {
16921
16916
  throw new CustomError(...args);
16922
16917
  }
16923
16918
  }
16924
- var toJSONSchema2 = (route) => {
16919
+ var extractArgs = (args) => {
16920
+ if (args && typeof args === "object" && typeof args.shape === "object") {
16921
+ return args.shape;
16922
+ }
16923
+ return args || {};
16924
+ };
16925
+ var toJSONSchemaRoute = (route) => {
16925
16926
  const pickValues = pick(route, pickValue);
16926
16927
  if (pickValues?.metadata?.args) {
16927
- const args = pickValues.metadata.args;
16928
- if (args && typeof args === "object" && args.toJSONSchema && typeof args.toJSONSchema === "function") {
16929
- pickValues.metadata.args = args.toJSONSchema();
16930
- return pickValues;
16931
- }
16928
+ let args = pickValues.metadata.args;
16929
+ args = extractArgs(args);
16932
16930
  const keys = Object.keys(args);
16933
16931
  const newArgs = {};
16934
16932
  for (let key of keys) {
@@ -16943,22 +16941,46 @@ var toJSONSchema2 = (route) => {
16943
16941
  }
16944
16942
  return pickValues;
16945
16943
  };
16946
- var fromJSONSchema2 = (route) => {
16947
- const args = route?.metadata?.args;
16948
- if (!args)
16949
- return route;
16950
- if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
16951
- route.metadata.args = exports_external.fromJSONSchema(args);
16952
- return route;
16953
- }
16944
+ var toJSONSchema2 = (args) => {
16945
+ args = extractArgs(args);
16954
16946
  const keys = Object.keys(args);
16955
16947
  const newArgs = {};
16956
16948
  for (let key of keys) {
16957
16949
  const item = args[key];
16958
- newArgs[key] = exports_external.fromJSONSchema(item);
16950
+ if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
16951
+ newArgs[key] = item.toJSONSchema();
16952
+ } else {
16953
+ newArgs[key] = args[key];
16954
+ }
16955
+ }
16956
+ return newArgs;
16957
+ };
16958
+ var fromJSONSchema2 = (args = {}, opts) => {
16959
+ let resultArgs = null;
16960
+ const mergeObject = opts?.mergeObject ?? true;
16961
+ if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
16962
+ const objectSchema = exports_external.fromJSONSchema(args);
16963
+ const extract = extractArgs(objectSchema);
16964
+ const keys = Object.keys(extract);
16965
+ const newArgs = {};
16966
+ for (let key of keys) {
16967
+ newArgs[key] = extract[key];
16968
+ }
16969
+ resultArgs = newArgs;
16970
+ }
16971
+ if (!resultArgs) {
16972
+ const keys = Object.keys(args);
16973
+ const newArgs = {};
16974
+ for (let key of keys) {
16975
+ const item = args[key];
16976
+ newArgs[key] = exports_external.fromJSONSchema(item);
16977
+ }
16978
+ resultArgs = newArgs;
16979
+ }
16980
+ if (mergeObject) {
16981
+ resultArgs = exports_external.object(resultArgs);
16959
16982
  }
16960
- route.metadata.args = newArgs;
16961
- return route;
16983
+ return resultArgs;
16962
16984
  };
16963
16985
 
16964
16986
  class QueryRouter {
@@ -17298,7 +17320,7 @@ class QueryRouter {
17298
17320
  ctx.body = {
17299
17321
  list: list.map((item) => {
17300
17322
  const route = pick(item, ["id", "path", "key", "description", "middleware", "metadata"]);
17301
- return toJSONSchema2(route);
17323
+ return toJSONSchemaRoute(route);
17302
17324
  }),
17303
17325
  isUser
17304
17326
  };
package/dist/ws.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { EventEmitter } from 'eventemitter3';
2
+ import { z } from 'zod';
2
3
  import * as http from 'node:http';
3
4
  import { IncomingMessage, ServerResponse } from 'node:http';
4
5
  import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
@@ -197,7 +198,6 @@ declare class Route<U = {
197
198
  metadata?: T;
198
199
  middleware?: RouteMiddleware[];
199
200
  type?: string;
200
- data?: any;
201
201
  /**
202
202
  * 是否开启debug,开启后会打印错误信息
203
203
  */
@@ -222,7 +222,6 @@ declare class Route<U = {
222
222
  add: (route: Route) => void;
223
223
  [key: string]: any;
224
224
  }, opts?: AddOpts): void;
225
- setData(data: any): this;
226
225
  throw(code?: number | string, message?: string, tips?: string): void;
227
226
  }
228
227
  /**
@@ -396,8 +395,16 @@ declare class QueryRouter {
396
395
  filter?: (route: Route) => boolean;
397
396
  routeListMiddleware?: string[];
398
397
  }): Promise<void>;
399
- toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
400
- fromJSONSchema: (route: RouteInfo) => RouteInfo;
398
+ toJSONSchema: (args: any) => {
399
+ [key: string]: any;
400
+ };
401
+ fromJSONSchema: <Merge extends boolean = true>(args?: any, opts?: {
402
+ mergeObject?: boolean;
403
+ }) => Merge extends true ? z.ZodObject<{
404
+ [key: string]: any;
405
+ }, z.core.$strip> : {
406
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
407
+ };
401
408
  }
402
409
  interface HandleFn<T = any> {
403
410
  (msg: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.75",
4
+ "version": "0.0.77",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
@@ -21,14 +21,13 @@
21
21
  "keywords": [],
22
22
  "author": "abearxiong",
23
23
  "license": "MIT",
24
- "packageManager": "pnpm@10.30.0",
25
24
  "devDependencies": {
26
25
  "@kevisual/code-builder": "^0.0.6",
27
26
  "@kevisual/context": "^0.0.6",
28
27
  "@kevisual/dts": "^0.0.4",
29
28
  "@kevisual/js-filter": "^0.0.5",
30
29
  "@kevisual/local-proxy": "^0.0.8",
31
- "@kevisual/query": "^0.0.46",
30
+ "@kevisual/query": "^0.0.47",
32
31
  "@kevisual/use-config": "^1.0.30",
33
32
  "@opencode-ai/plugin": "^1.2.6",
34
33
  "@types/bun": "^1.3.9",
package/src/route.ts CHANGED
@@ -137,7 +137,6 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
137
137
  metadata?: T;
138
138
  middleware?: RouteMiddleware[]; // middleware
139
139
  type? = 'route';
140
- data?: any;
141
140
  /**
142
141
  * 是否开启debug,开启后会打印错误信息
143
142
  */
@@ -241,23 +240,25 @@ export class Route<U = { [key: string]: any }, T extends SimpleObject = SimpleOb
241
240
  addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }, opts?: AddOpts) {
242
241
  router.add(this, opts);
243
242
  }
244
- setData(data: any) {
245
- this.data = data;
246
- return this;
247
- }
248
243
  throw(code?: number | string, message?: string, tips?: string): void;
249
244
  throw(...args: any[]) {
250
245
  throw new CustomError(...args);
251
246
  }
252
247
  }
253
- export const toJSONSchema = (route: RouteInfo) => {
248
+ export const extractArgs = (args: any) => {
249
+ if (args && typeof args === 'object' && typeof args.shape === 'object') {
250
+ return args.shape as z.ZodRawShape;
251
+ }
252
+ return args || {};
253
+ };
254
+
255
+ const toJSONSchemaRoute = (route: RouteInfo) => {
254
256
  const pickValues = pick(route, pickValue as any);
255
257
  if (pickValues?.metadata?.args) {
256
- const args = pickValues.metadata.args;
257
- if (args && typeof args === 'object' && args.toJSONSchema && typeof args.toJSONSchema === 'function') {
258
- pickValues.metadata.args = args.toJSONSchema();
259
- return pickValues;
260
- }
258
+ let args = pickValues.metadata.args;
259
+ // 如果 args 本身是一个 zod object schema,先提取 shape
260
+ args = extractArgs(args);
261
+
261
262
  const keys = Object.keys(args);
262
263
  const newArgs: { [key: string]: any } = {};
263
264
  for (let key of keys) {
@@ -272,23 +273,62 @@ export const toJSONSchema = (route: RouteInfo) => {
272
273
  }
273
274
  return pickValues;
274
275
  }
275
-
276
- export const fromJSONSchema = (route: RouteInfo): RouteInfo => {
276
+ const fromJSONSchemaRoute = (route: RouteInfo): RouteInfo => {
277
277
  const args = route?.metadata?.args;
278
278
  if (!args) return route;
279
- if (args["$schema"] || (args.type === 'object' && args.properties && typeof args.properties === 'object')) {
280
- // 可能是整个schema
281
- route.metadata.args = z.fromJSONSchema(args);
282
- return route;
283
- }
279
+ const newArgs = fromJSONSchema(args);
280
+ route.metadata.args = newArgs;
281
+ return route;
282
+ }
283
+
284
+ /**
285
+ * 剥离第一层schema,转换为JSON Schema,无论是skill还是其他的infer比纯粹的zod object schema更合适,因为它可能包含其他的字段,而不仅仅是schema
286
+ * @param args
287
+ * @returns
288
+ */
289
+ export const toJSONSchema = (args: any): { [key: string]: any } => {
290
+ // 如果 args 本身是一个 zod object schema,先提取 shape
291
+ args = extractArgs(args);
284
292
  const keys = Object.keys(args);
285
293
  const newArgs: { [key: string]: any } = {};
286
294
  for (let key of keys) {
287
- const item = args[key];
288
- newArgs[key] = z.fromJSONSchema(item);
295
+ const item = args[key] as z.ZodAny;
296
+ if (item && typeof item === 'object' && typeof item.toJSONSchema === 'function') {
297
+ newArgs[key] = item.toJSONSchema();
298
+ } else {
299
+ newArgs[key] = args[key]; // 可能不是schema
300
+ }
289
301
  }
290
- route.metadata.args = newArgs;
291
- return route;
302
+ return newArgs;
303
+ }
304
+ export const fromJSONSchema = <Merge extends boolean = true>(args: any = {}, opts?: { mergeObject?: boolean }) => {
305
+ let resultArgs: any = null;
306
+ const mergeObject = opts?.mergeObject ?? true;
307
+ if (args["$schema"] || (args.type === 'object' && args.properties && typeof args.properties === 'object')) {
308
+ // 可能是整个schema
309
+ const objectSchema = z.fromJSONSchema(args);
310
+ const extract = extractArgs(objectSchema);
311
+ const keys = Object.keys(extract);
312
+ const newArgs: { [key: string]: any } = {};
313
+ for (let key of keys) {
314
+ newArgs[key] = extract[key];
315
+ }
316
+ resultArgs = newArgs;
317
+ }
318
+ if (!resultArgs) {
319
+ const keys = Object.keys(args);
320
+ const newArgs: { [key: string]: any } = {};
321
+ for (let key of keys) {
322
+ const item = args[key];
323
+ newArgs[key] = z.fromJSONSchema(item);
324
+ }
325
+ resultArgs = newArgs;
326
+ }
327
+ if (mergeObject) {
328
+ resultArgs = z.object(resultArgs);
329
+ }
330
+ type ResultArgs = Merge extends true ? z.ZodObject<{ [key: string]: any }> : { [key: string]: z.ZodTypeAny };
331
+ return resultArgs as unknown as ResultArgs;
292
332
  }
293
333
 
294
334
  /**
@@ -697,7 +737,7 @@ export class QueryRouter {
697
737
  ctx.body = {
698
738
  list: list.map((item) => {
699
739
  const route = pick(item, ['id', 'path', 'key', 'description', 'middleware', 'metadata'] as const);
700
- return toJSONSchema(route);
740
+ return toJSONSchemaRoute(route);
701
741
  }),
702
742
  isUser
703
743
  };
@@ -0,0 +1,14 @@
1
+ import { toJSONSchema, fromJSONSchema } from "@/route.ts";
2
+ import { z } from "zod";
3
+ const schema = z.object({
4
+ name: z.string(),
5
+ age: z.number(),
6
+
7
+ });
8
+ // console.log("schema", schema);
9
+ const jsonSchema = toJSONSchema(schema);
10
+ console.log("jsonSchema", jsonSchema);
11
+
12
+ const newSchema = fromJSONSchema<true>(jsonSchema, { mergeObject: true });
13
+ console.log("newSchema shape", Object.keys(newSchema.shape));
14
+ console.log('check', newSchema.safeParse({ name: "Alice", age: "30" })?.success);