@kevisual/router 0.0.76 → 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 +38 -14
- package/dist/opencode.d.ts +11 -2
- package/dist/router-browser.d.ts +25 -4
- package/dist/router-browser.js +37 -13
- package/dist/router-define.d.ts +11 -2
- package/dist/router.d.ts +25 -4
- package/dist/router.js +37 -13
- package/dist/ws.d.ts +11 -2
- package/package.json +2 -2
- package/src/route.ts +52 -13
- package/src/test/schema.ts +14 -0
package/dist/app.js
CHANGED
|
@@ -16925,7 +16925,7 @@ var extractArgs = (args) => {
|
|
|
16925
16925
|
}
|
|
16926
16926
|
return args || {};
|
|
16927
16927
|
};
|
|
16928
|
-
var
|
|
16928
|
+
var toJSONSchemaRoute = (route) => {
|
|
16929
16929
|
const pickValues = pick(route, pickValue);
|
|
16930
16930
|
if (pickValues?.metadata?.args) {
|
|
16931
16931
|
let args = pickValues.metadata.args;
|
|
@@ -16944,22 +16944,46 @@ var toJSONSchema2 = (route) => {
|
|
|
16944
16944
|
}
|
|
16945
16945
|
return pickValues;
|
|
16946
16946
|
};
|
|
16947
|
-
var
|
|
16948
|
-
|
|
16949
|
-
if (!args)
|
|
16950
|
-
return route;
|
|
16951
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16952
|
-
route.metadata.args = exports_external.fromJSONSchema(args);
|
|
16953
|
-
return route;
|
|
16954
|
-
}
|
|
16947
|
+
var toJSONSchema2 = (args) => {
|
|
16948
|
+
args = extractArgs(args);
|
|
16955
16949
|
const keys = Object.keys(args);
|
|
16956
16950
|
const newArgs = {};
|
|
16957
16951
|
for (let key of keys) {
|
|
16958
16952
|
const item = args[key];
|
|
16959
|
-
|
|
16953
|
+
if (item && typeof item === "object" && typeof item.toJSONSchema === "function") {
|
|
16954
|
+
newArgs[key] = item.toJSONSchema();
|
|
16955
|
+
} else {
|
|
16956
|
+
newArgs[key] = args[key];
|
|
16957
|
+
}
|
|
16958
|
+
}
|
|
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);
|
|
16960
16985
|
}
|
|
16961
|
-
|
|
16962
|
-
return route;
|
|
16986
|
+
return resultArgs;
|
|
16963
16987
|
};
|
|
16964
16988
|
|
|
16965
16989
|
class QueryRouter {
|
|
@@ -17299,7 +17323,7 @@ class QueryRouter {
|
|
|
17299
17323
|
ctx.body = {
|
|
17300
17324
|
list: list.map((item) => {
|
|
17301
17325
|
const route = pick(item, ["id", "path", "key", "description", "middleware", "metadata"]);
|
|
17302
|
-
return
|
|
17326
|
+
return toJSONSchemaRoute(route);
|
|
17303
17327
|
}),
|
|
17304
17328
|
isUser
|
|
17305
17329
|
};
|
|
@@ -19483,7 +19507,7 @@ app
|
|
|
19483
19507
|
10. **中间件找不到会返回 404**,错误信息中会包含找不到的中间件列表。
|
|
19484
19508
|
`;
|
|
19485
19509
|
// package.json
|
|
19486
|
-
var version2 = "0.0.
|
|
19510
|
+
var version2 = "0.0.77";
|
|
19487
19511
|
|
|
19488
19512
|
// agent/routes/route-create.ts
|
|
19489
19513
|
app.route({
|
package/dist/opencode.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';
|
|
@@ -347,8 +348,16 @@ declare class QueryRouter {
|
|
|
347
348
|
filter?: (route: Route) => boolean;
|
|
348
349
|
routeListMiddleware?: string[];
|
|
349
350
|
}): Promise<void>;
|
|
350
|
-
toJSONSchema: (
|
|
351
|
-
|
|
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
|
+
};
|
|
352
361
|
}
|
|
353
362
|
type QueryRouterServerOpts = {
|
|
354
363
|
handleFn?: HandleFn;
|
package/dist/router-browser.d.ts
CHANGED
|
@@ -204,8 +204,21 @@ declare class Route<U = {
|
|
|
204
204
|
}, opts?: AddOpts): void;
|
|
205
205
|
throw(code?: number | string, message?: string, tips?: string): void;
|
|
206
206
|
}
|
|
207
|
-
|
|
208
|
-
|
|
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
|
+
};
|
|
209
222
|
/**
|
|
210
223
|
* @parmas overwrite 是否覆盖已存在的route,默认true
|
|
211
224
|
*/
|
|
@@ -377,8 +390,16 @@ declare class QueryRouter {
|
|
|
377
390
|
filter?: (route: Route) => boolean;
|
|
378
391
|
routeListMiddleware?: string[];
|
|
379
392
|
}): Promise<void>;
|
|
380
|
-
toJSONSchema: (
|
|
381
|
-
|
|
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
|
+
};
|
|
382
403
|
}
|
|
383
404
|
type QueryRouterServerOpts = {
|
|
384
405
|
handleFn?: HandleFn;
|
package/dist/router-browser.js
CHANGED
|
@@ -14089,7 +14089,7 @@ var extractArgs = (args) => {
|
|
|
14089
14089
|
}
|
|
14090
14090
|
return args || {};
|
|
14091
14091
|
};
|
|
14092
|
-
var
|
|
14092
|
+
var toJSONSchemaRoute = (route) => {
|
|
14093
14093
|
const pickValues = pick(route, pickValue);
|
|
14094
14094
|
if (pickValues?.metadata?.args) {
|
|
14095
14095
|
let args = pickValues.metadata.args;
|
|
@@ -14108,22 +14108,46 @@ var toJSONSchema2 = (route) => {
|
|
|
14108
14108
|
}
|
|
14109
14109
|
return pickValues;
|
|
14110
14110
|
};
|
|
14111
|
-
var
|
|
14112
|
-
|
|
14113
|
-
if (!args)
|
|
14114
|
-
return route;
|
|
14115
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
14116
|
-
route.metadata.args = exports_external.fromJSONSchema(args);
|
|
14117
|
-
return route;
|
|
14118
|
-
}
|
|
14111
|
+
var toJSONSchema2 = (args) => {
|
|
14112
|
+
args = extractArgs(args);
|
|
14119
14113
|
const keys = Object.keys(args);
|
|
14120
14114
|
const newArgs = {};
|
|
14121
14115
|
for (let key of keys) {
|
|
14122
14116
|
const item = args[key];
|
|
14123
|
-
|
|
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);
|
|
14124
14149
|
}
|
|
14125
|
-
|
|
14126
|
-
return route;
|
|
14150
|
+
return resultArgs;
|
|
14127
14151
|
};
|
|
14128
14152
|
|
|
14129
14153
|
class QueryRouter {
|
|
@@ -14463,7 +14487,7 @@ class QueryRouter {
|
|
|
14463
14487
|
ctx.body = {
|
|
14464
14488
|
list: list.map((item) => {
|
|
14465
14489
|
const route = pick(item, ["id", "path", "key", "description", "middleware", "metadata"]);
|
|
14466
|
-
return
|
|
14490
|
+
return toJSONSchemaRoute(route);
|
|
14467
14491
|
}),
|
|
14468
14492
|
isUser
|
|
14469
14493
|
};
|
package/dist/router-define.d.ts
CHANGED
|
@@ -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 {
|
|
@@ -344,8 +345,16 @@ declare class QueryRouter {
|
|
|
344
345
|
filter?: (route: Route) => boolean;
|
|
345
346
|
routeListMiddleware?: string[];
|
|
346
347
|
}): Promise<void>;
|
|
347
|
-
toJSONSchema: (
|
|
348
|
-
|
|
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
|
+
};
|
|
349
358
|
}
|
|
350
359
|
type QueryRouterServerOpts = {
|
|
351
360
|
handleFn?: HandleFn;
|
package/dist/router.d.ts
CHANGED
|
@@ -210,8 +210,21 @@ declare class Route<U = {
|
|
|
210
210
|
}, opts?: AddOpts): void;
|
|
211
211
|
throw(code?: number | string, message?: string, tips?: string): void;
|
|
212
212
|
}
|
|
213
|
-
|
|
214
|
-
|
|
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
|
+
};
|
|
215
228
|
/**
|
|
216
229
|
* @parmas overwrite 是否覆盖已存在的route,默认true
|
|
217
230
|
*/
|
|
@@ -383,8 +396,16 @@ declare class QueryRouter {
|
|
|
383
396
|
filter?: (route: Route) => boolean;
|
|
384
397
|
routeListMiddleware?: string[];
|
|
385
398
|
}): Promise<void>;
|
|
386
|
-
toJSONSchema: (
|
|
387
|
-
|
|
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
|
+
};
|
|
388
409
|
}
|
|
389
410
|
type QueryRouterServerOpts = {
|
|
390
411
|
handleFn?: HandleFn;
|
package/dist/router.js
CHANGED
|
@@ -16922,7 +16922,7 @@ var extractArgs = (args) => {
|
|
|
16922
16922
|
}
|
|
16923
16923
|
return args || {};
|
|
16924
16924
|
};
|
|
16925
|
-
var
|
|
16925
|
+
var toJSONSchemaRoute = (route) => {
|
|
16926
16926
|
const pickValues = pick(route, pickValue);
|
|
16927
16927
|
if (pickValues?.metadata?.args) {
|
|
16928
16928
|
let args = pickValues.metadata.args;
|
|
@@ -16941,22 +16941,46 @@ var toJSONSchema2 = (route) => {
|
|
|
16941
16941
|
}
|
|
16942
16942
|
return pickValues;
|
|
16943
16943
|
};
|
|
16944
|
-
var
|
|
16945
|
-
|
|
16946
|
-
if (!args)
|
|
16947
|
-
return route;
|
|
16948
|
-
if (args["$schema"] || args.type === "object" && args.properties && typeof args.properties === "object") {
|
|
16949
|
-
route.metadata.args = exports_external.fromJSONSchema(args);
|
|
16950
|
-
return route;
|
|
16951
|
-
}
|
|
16944
|
+
var toJSONSchema2 = (args) => {
|
|
16945
|
+
args = extractArgs(args);
|
|
16952
16946
|
const keys = Object.keys(args);
|
|
16953
16947
|
const newArgs = {};
|
|
16954
16948
|
for (let key of keys) {
|
|
16955
16949
|
const item = args[key];
|
|
16956
|
-
|
|
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);
|
|
16957
16982
|
}
|
|
16958
|
-
|
|
16959
|
-
return route;
|
|
16983
|
+
return resultArgs;
|
|
16960
16984
|
};
|
|
16961
16985
|
|
|
16962
16986
|
class QueryRouter {
|
|
@@ -17296,7 +17320,7 @@ class QueryRouter {
|
|
|
17296
17320
|
ctx.body = {
|
|
17297
17321
|
list: list.map((item) => {
|
|
17298
17322
|
const route = pick(item, ["id", "path", "key", "description", "middleware", "metadata"]);
|
|
17299
|
-
return
|
|
17323
|
+
return toJSONSchemaRoute(route);
|
|
17300
17324
|
}),
|
|
17301
17325
|
isUser
|
|
17302
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';
|
|
@@ -394,8 +395,16 @@ declare class QueryRouter {
|
|
|
394
395
|
filter?: (route: Route) => boolean;
|
|
395
396
|
routeListMiddleware?: string[];
|
|
396
397
|
}): Promise<void>;
|
|
397
|
-
toJSONSchema: (
|
|
398
|
-
|
|
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
|
+
};
|
|
399
408
|
}
|
|
400
409
|
interface HandleFn<T = any> {
|
|
401
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.
|
|
4
|
+
"version": "0.0.77",
|
|
5
5
|
"description": "",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/router.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@kevisual/dts": "^0.0.4",
|
|
28
28
|
"@kevisual/js-filter": "^0.0.5",
|
|
29
29
|
"@kevisual/local-proxy": "^0.0.8",
|
|
30
|
-
"@kevisual/query": "^0.0.
|
|
30
|
+
"@kevisual/query": "^0.0.47",
|
|
31
31
|
"@kevisual/use-config": "^1.0.30",
|
|
32
32
|
"@opencode-ai/plugin": "^1.2.6",
|
|
33
33
|
"@types/bun": "^1.3.9",
|
package/src/route.ts
CHANGED
|
@@ -252,7 +252,7 @@ export const extractArgs = (args: any) => {
|
|
|
252
252
|
return args || {};
|
|
253
253
|
};
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
const toJSONSchemaRoute = (route: RouteInfo) => {
|
|
256
256
|
const pickValues = pick(route, pickValue as any);
|
|
257
257
|
if (pickValues?.metadata?.args) {
|
|
258
258
|
let args = pickValues.metadata.args;
|
|
@@ -273,23 +273,62 @@ export const toJSONSchema = (route: RouteInfo) => {
|
|
|
273
273
|
}
|
|
274
274
|
return pickValues;
|
|
275
275
|
}
|
|
276
|
-
|
|
277
|
-
export const fromJSONSchema = (route: RouteInfo): RouteInfo => {
|
|
276
|
+
const fromJSONSchemaRoute = (route: RouteInfo): RouteInfo => {
|
|
278
277
|
const args = route?.metadata?.args;
|
|
279
278
|
if (!args) return route;
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
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);
|
|
285
292
|
const keys = Object.keys(args);
|
|
286
293
|
const newArgs: { [key: string]: any } = {};
|
|
287
294
|
for (let key of keys) {
|
|
288
|
-
const item = args[key];
|
|
289
|
-
|
|
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
|
+
}
|
|
290
301
|
}
|
|
291
|
-
|
|
292
|
-
|
|
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;
|
|
293
332
|
}
|
|
294
333
|
|
|
295
334
|
/**
|
|
@@ -698,7 +737,7 @@ export class QueryRouter {
|
|
|
698
737
|
ctx.body = {
|
|
699
738
|
list: list.map((item) => {
|
|
700
739
|
const route = pick(item, ['id', 'path', 'key', 'description', 'middleware', 'metadata'] as const);
|
|
701
|
-
return
|
|
740
|
+
return toJSONSchemaRoute(route);
|
|
702
741
|
}),
|
|
703
742
|
isUser
|
|
704
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);
|