@kevisual/router 0.1.4 → 0.1.5
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.d.ts +5 -0
- package/dist/app.js +20175 -0
- package/dist/commander.d.ts +817 -0
- package/dist/commander.js +16401 -0
- package/dist/opencode.d.ts +811 -0
- package/dist/opencode.js +15308 -0
- package/dist/router-browser.d.ts +676 -0
- package/dist/router-browser.js +33383 -0
- package/dist/router-define.d.ts +560 -0
- package/dist/router-define.js +135 -0
- package/dist/router-simple.d.ts +110 -0
- package/dist/router-simple.js +708 -0
- package/dist/router.d.ts +1082 -0
- package/dist/router.js +19059 -0
- package/dist/ws.d.ts +860 -0
- package/dist/ws.js +3041 -0
- package/package.json +1 -1
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
import { EventEmitter } from 'eventemitter3';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Query, DataOpts, Result } from '@kevisual/query/query';
|
|
4
|
+
|
|
5
|
+
type CustomErrorOptions = {
|
|
6
|
+
cause?: Error | string;
|
|
7
|
+
code?: number;
|
|
8
|
+
message?: string;
|
|
9
|
+
};
|
|
10
|
+
interface throwError {
|
|
11
|
+
throw(code?: number | string, message?: string): void;
|
|
12
|
+
throw(code?: number | string, opts?: CustomErrorOptions): void;
|
|
13
|
+
throw(opts?: CustomErrorOptions): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare class MockProcess {
|
|
17
|
+
emitter?: EventEmitter;
|
|
18
|
+
process?: NodeJS.Process;
|
|
19
|
+
constructor(opts?: {
|
|
20
|
+
emitter?: EventEmitter;
|
|
21
|
+
isNode?: boolean;
|
|
22
|
+
});
|
|
23
|
+
send(data?: any, callback?: (err?: Error) => void): void;
|
|
24
|
+
exit(flag?: number): void;
|
|
25
|
+
on(fn: (msg?: any) => any): void;
|
|
26
|
+
desctroy(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type RouterContextT = {
|
|
30
|
+
code?: number;
|
|
31
|
+
[key: string]: any;
|
|
32
|
+
};
|
|
33
|
+
type BuildRouteContext<M, U> = M extends {
|
|
34
|
+
args?: infer A;
|
|
35
|
+
} ? A extends z.ZodObject<any> ? RouteContext<{
|
|
36
|
+
args?: z.infer<A>;
|
|
37
|
+
}, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
|
|
38
|
+
args?: {
|
|
39
|
+
[K in keyof A]: z.infer<A[K]>;
|
|
40
|
+
};
|
|
41
|
+
}, U> : RouteContext<U> : RouteContext<U>;
|
|
42
|
+
type RouteContext<T = {
|
|
43
|
+
code?: number;
|
|
44
|
+
}, U extends SimpleObject$1 = {}, S = {
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
}> = {
|
|
47
|
+
/**
|
|
48
|
+
* 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
|
|
49
|
+
* 或者不需要登录的,直接调用
|
|
50
|
+
*/
|
|
51
|
+
appId?: string;
|
|
52
|
+
query?: {
|
|
53
|
+
[key: string]: any;
|
|
54
|
+
};
|
|
55
|
+
args?: {
|
|
56
|
+
[key: string]: any;
|
|
57
|
+
};
|
|
58
|
+
/** return body */
|
|
59
|
+
body?: number | string | Object;
|
|
60
|
+
forward?: (response: {
|
|
61
|
+
code: number;
|
|
62
|
+
data?: any;
|
|
63
|
+
message?: any;
|
|
64
|
+
}) => void;
|
|
65
|
+
/** return code */
|
|
66
|
+
code?: number;
|
|
67
|
+
/** return msg */
|
|
68
|
+
message?: string;
|
|
69
|
+
/**
|
|
70
|
+
* 传递状态
|
|
71
|
+
*/
|
|
72
|
+
state?: S;
|
|
73
|
+
/**
|
|
74
|
+
* 当前routerId
|
|
75
|
+
*/
|
|
76
|
+
currentId?: string;
|
|
77
|
+
/**
|
|
78
|
+
* 当前路径
|
|
79
|
+
*/
|
|
80
|
+
currentPath?: string;
|
|
81
|
+
/**
|
|
82
|
+
* 当前key
|
|
83
|
+
*/
|
|
84
|
+
currentKey?: string;
|
|
85
|
+
/**
|
|
86
|
+
* 当前route
|
|
87
|
+
*/
|
|
88
|
+
currentRoute?: Route;
|
|
89
|
+
/**
|
|
90
|
+
* 进度
|
|
91
|
+
*/
|
|
92
|
+
progress?: [string, string][];
|
|
93
|
+
nextQuery?: {
|
|
94
|
+
[key: string]: any;
|
|
95
|
+
};
|
|
96
|
+
end?: boolean;
|
|
97
|
+
app?: QueryRouter;
|
|
98
|
+
error?: any;
|
|
99
|
+
/** 请求 route的返回结果,不解析body为data */
|
|
100
|
+
call?: (message: {
|
|
101
|
+
path: string;
|
|
102
|
+
key?: string;
|
|
103
|
+
payload?: any;
|
|
104
|
+
[key: string]: any;
|
|
105
|
+
} | {
|
|
106
|
+
id: string;
|
|
107
|
+
apyload?: any;
|
|
108
|
+
[key: string]: any;
|
|
109
|
+
}, ctx?: RouteContext & {
|
|
110
|
+
[key: string]: any;
|
|
111
|
+
}) => Promise<any>;
|
|
112
|
+
/** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
|
|
113
|
+
run?: (message: {
|
|
114
|
+
path: string;
|
|
115
|
+
key?: string;
|
|
116
|
+
payload?: any;
|
|
117
|
+
}, ctx?: RouteContext) => Promise<any>;
|
|
118
|
+
index?: number;
|
|
119
|
+
throw?: throwError['throw'];
|
|
120
|
+
/** 是否需要序列化, 使用JSON.stringify和JSON.parse */
|
|
121
|
+
needSerialize?: boolean;
|
|
122
|
+
} & T & U;
|
|
123
|
+
type SimpleObject$1 = Record<string, any>;
|
|
124
|
+
type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
|
|
125
|
+
type RunMessage = {
|
|
126
|
+
path?: string;
|
|
127
|
+
key?: string;
|
|
128
|
+
id?: string;
|
|
129
|
+
payload?: any;
|
|
130
|
+
};
|
|
131
|
+
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
132
|
+
type RouteMiddleware = {
|
|
133
|
+
path?: string;
|
|
134
|
+
key?: string;
|
|
135
|
+
id?: string;
|
|
136
|
+
} | string;
|
|
137
|
+
type RouteOpts<U = {}, T = SimpleObject$1> = {
|
|
138
|
+
path?: string;
|
|
139
|
+
key?: string;
|
|
140
|
+
id?: string;
|
|
141
|
+
run?: Run<U>;
|
|
142
|
+
nextRoute?: NextRoute;
|
|
143
|
+
description?: string;
|
|
144
|
+
metadata?: T;
|
|
145
|
+
middleware?: RouteMiddleware[];
|
|
146
|
+
type?: 'route' | 'middleware' | 'compound';
|
|
147
|
+
isDebug?: boolean;
|
|
148
|
+
};
|
|
149
|
+
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
|
|
150
|
+
declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
|
|
151
|
+
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
152
|
+
/**
|
|
153
|
+
* @M 是 route的 metadate的类型,默认是 SimpleObject
|
|
154
|
+
* @U 是 RouteContext 里 state的类型
|
|
155
|
+
*/
|
|
156
|
+
declare class Route<M extends SimpleObject$1 = SimpleObject$1, U extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
157
|
+
/**
|
|
158
|
+
* 一级路径
|
|
159
|
+
*/
|
|
160
|
+
path?: string;
|
|
161
|
+
/**
|
|
162
|
+
* 二级路径
|
|
163
|
+
*/
|
|
164
|
+
key?: string;
|
|
165
|
+
id?: string;
|
|
166
|
+
run?: Run<BuildRouteContext<M, U>>;
|
|
167
|
+
nextRoute?: NextRoute;
|
|
168
|
+
description?: string;
|
|
169
|
+
metadata?: M;
|
|
170
|
+
middleware?: RouteMiddleware[];
|
|
171
|
+
type?: string;
|
|
172
|
+
/**
|
|
173
|
+
* 是否开启debug,开启后会打印错误信息
|
|
174
|
+
*/
|
|
175
|
+
isDebug?: boolean;
|
|
176
|
+
constructor(path?: string, key?: string, opts?: RouteOpts);
|
|
177
|
+
prompt(description: string): this;
|
|
178
|
+
prompt(description: Function): this;
|
|
179
|
+
define<T extends {
|
|
180
|
+
[key: string]: any;
|
|
181
|
+
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
182
|
+
define<T extends {
|
|
183
|
+
[key: string]: any;
|
|
184
|
+
} = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
185
|
+
define<T extends {
|
|
186
|
+
[key: string]: any;
|
|
187
|
+
} = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
188
|
+
define<T extends {
|
|
189
|
+
[key: string]: any;
|
|
190
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
|
|
191
|
+
update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
|
|
192
|
+
addTo(router: QueryRouter | {
|
|
193
|
+
add: (route: Route) => void;
|
|
194
|
+
[key: string]: any;
|
|
195
|
+
}, opts?: AddOpts): void;
|
|
196
|
+
throw(...args: any[]): void;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* @parmas overwrite 是否覆盖已存在的route,默认true
|
|
200
|
+
*/
|
|
201
|
+
type AddOpts = {
|
|
202
|
+
overwrite?: boolean;
|
|
203
|
+
};
|
|
204
|
+
declare class QueryRouter<T extends SimpleObject$1 = SimpleObject$1> implements throwError {
|
|
205
|
+
appId: string;
|
|
206
|
+
routes: Route[];
|
|
207
|
+
maxNextRoute: number;
|
|
208
|
+
context?: RouteContext<T>;
|
|
209
|
+
constructor();
|
|
210
|
+
/**
|
|
211
|
+
* add route
|
|
212
|
+
* @param route
|
|
213
|
+
* @param opts
|
|
214
|
+
*/
|
|
215
|
+
add(route: Route, opts?: AddOpts): void;
|
|
216
|
+
/**
|
|
217
|
+
* remove route by path and key
|
|
218
|
+
* @param route
|
|
219
|
+
*/
|
|
220
|
+
remove(route: Route | {
|
|
221
|
+
path: string;
|
|
222
|
+
key?: string;
|
|
223
|
+
}): void;
|
|
224
|
+
/**
|
|
225
|
+
* remove route by id
|
|
226
|
+
* @param uniqueId
|
|
227
|
+
*/
|
|
228
|
+
removeById(uniqueId: string): void;
|
|
229
|
+
/**
|
|
230
|
+
* 执行route
|
|
231
|
+
* @param path
|
|
232
|
+
* @param key
|
|
233
|
+
* @param ctx
|
|
234
|
+
* @returns
|
|
235
|
+
*/
|
|
236
|
+
runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
|
|
237
|
+
/**
|
|
238
|
+
* 第一次执行
|
|
239
|
+
* @param message
|
|
240
|
+
* @param ctx
|
|
241
|
+
* @returns
|
|
242
|
+
*/
|
|
243
|
+
parse(message: {
|
|
244
|
+
path: string;
|
|
245
|
+
key?: string;
|
|
246
|
+
payload?: any;
|
|
247
|
+
}, ctx?: RouteContext<T> & {
|
|
248
|
+
[key: string]: any;
|
|
249
|
+
}): Promise<RouteContext<T, {}, {
|
|
250
|
+
[key: string]: any;
|
|
251
|
+
}>>;
|
|
252
|
+
/**
|
|
253
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
254
|
+
* @param message
|
|
255
|
+
* @param ctx
|
|
256
|
+
* @returns
|
|
257
|
+
*/
|
|
258
|
+
call(message: {
|
|
259
|
+
id?: string;
|
|
260
|
+
path?: string;
|
|
261
|
+
key?: string;
|
|
262
|
+
payload?: any;
|
|
263
|
+
}, ctx?: RouteContext<T> & {
|
|
264
|
+
[key: string]: any;
|
|
265
|
+
}): Promise<RouteContext<T, {}, {
|
|
266
|
+
[key: string]: any;
|
|
267
|
+
}> | {
|
|
268
|
+
code: number;
|
|
269
|
+
body: any;
|
|
270
|
+
message: string;
|
|
271
|
+
}>;
|
|
272
|
+
/**
|
|
273
|
+
* 请求 result 的数据
|
|
274
|
+
* @param message
|
|
275
|
+
* @param ctx
|
|
276
|
+
* @deprecated use run or call instead
|
|
277
|
+
* @returns
|
|
278
|
+
*/
|
|
279
|
+
queryRoute(message: {
|
|
280
|
+
id?: string;
|
|
281
|
+
path: string;
|
|
282
|
+
key?: string;
|
|
283
|
+
payload?: any;
|
|
284
|
+
}, ctx?: RouteContext & {
|
|
285
|
+
[key: string]: any;
|
|
286
|
+
}): Promise<{
|
|
287
|
+
code: number;
|
|
288
|
+
data: any;
|
|
289
|
+
message: string;
|
|
290
|
+
}>;
|
|
291
|
+
/**
|
|
292
|
+
* Router Run获取数据
|
|
293
|
+
* @param message
|
|
294
|
+
* @param ctx
|
|
295
|
+
* @returns
|
|
296
|
+
*/
|
|
297
|
+
run(message: {
|
|
298
|
+
id?: string;
|
|
299
|
+
path?: string;
|
|
300
|
+
key?: string;
|
|
301
|
+
payload?: any;
|
|
302
|
+
}, ctx?: RouteContext<T> & {
|
|
303
|
+
[key: string]: any;
|
|
304
|
+
}): Promise<{
|
|
305
|
+
code: number;
|
|
306
|
+
data: any;
|
|
307
|
+
message: string;
|
|
308
|
+
}>;
|
|
309
|
+
/**
|
|
310
|
+
* 设置上下文
|
|
311
|
+
* @description 这里的上下文是为了在handle函数中使用
|
|
312
|
+
* @param ctx
|
|
313
|
+
*/
|
|
314
|
+
setContext(ctx: RouteContext): void;
|
|
315
|
+
getList(filter?: (route: Route) => boolean): RouteInfo[];
|
|
316
|
+
/**
|
|
317
|
+
* 获取handle函数, 这里会去执行parse函数
|
|
318
|
+
*/
|
|
319
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
|
|
320
|
+
id?: string;
|
|
321
|
+
path?: string;
|
|
322
|
+
key?: string;
|
|
323
|
+
[key: string]: any;
|
|
324
|
+
}, handleContext?: RouteContext<T>) => Promise<{
|
|
325
|
+
[key: string]: any;
|
|
326
|
+
code: string;
|
|
327
|
+
data?: any;
|
|
328
|
+
message?: string;
|
|
329
|
+
} | {
|
|
330
|
+
code: any;
|
|
331
|
+
data: any;
|
|
332
|
+
message: any;
|
|
333
|
+
} | {
|
|
334
|
+
code: number;
|
|
335
|
+
message: any;
|
|
336
|
+
data?: undefined;
|
|
337
|
+
}>;
|
|
338
|
+
exportRoutes(): Route<SimpleObject$1, SimpleObject$1>[];
|
|
339
|
+
importRoutes(routes: Route[]): void;
|
|
340
|
+
importRouter(router: QueryRouter): void;
|
|
341
|
+
throw(...args: any[]): void;
|
|
342
|
+
hasRoute(path: string, key?: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
343
|
+
findRoute(opts?: {
|
|
344
|
+
path?: string;
|
|
345
|
+
key?: string;
|
|
346
|
+
id?: string;
|
|
347
|
+
}): Route<SimpleObject$1, SimpleObject$1>;
|
|
348
|
+
createRouteList(opts?: {
|
|
349
|
+
force?: boolean;
|
|
350
|
+
filter?: (route: Route) => boolean;
|
|
351
|
+
middleware?: string[];
|
|
352
|
+
}): void;
|
|
353
|
+
/**
|
|
354
|
+
* 等待程序运行, 获取到message的数据,就执行
|
|
355
|
+
* params 是预设参数
|
|
356
|
+
* emitter = process
|
|
357
|
+
* -- .exit
|
|
358
|
+
* -- .on
|
|
359
|
+
* -- .send
|
|
360
|
+
*/
|
|
361
|
+
wait(params?: {
|
|
362
|
+
message: RunMessage;
|
|
363
|
+
}, opts?: {
|
|
364
|
+
mockProcess?: MockProcess;
|
|
365
|
+
timeout?: number;
|
|
366
|
+
getList?: boolean;
|
|
367
|
+
force?: boolean;
|
|
368
|
+
filter?: (route: Route) => boolean;
|
|
369
|
+
routeListMiddleware?: string[];
|
|
370
|
+
}): Promise<void>;
|
|
371
|
+
toJSONSchema: (args: any, opts?: {
|
|
372
|
+
mergeObject?: boolean;
|
|
373
|
+
override?: (opts: {
|
|
374
|
+
jsonSchema: any;
|
|
375
|
+
path: string[];
|
|
376
|
+
zodSchema: z.ZodTypeAny;
|
|
377
|
+
}) => void;
|
|
378
|
+
}) => {
|
|
379
|
+
[key: string]: any;
|
|
380
|
+
};
|
|
381
|
+
fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
|
|
382
|
+
mergeObject?: boolean;
|
|
383
|
+
}) => Merge extends true ? z.ZodObject<{
|
|
384
|
+
[key: string]: any;
|
|
385
|
+
}, z.core.$strip> : {
|
|
386
|
+
[key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
type QueryRouterServerOpts<C extends SimpleObject$1 = SimpleObject$1> = {
|
|
390
|
+
handleFn?: HandleFn;
|
|
391
|
+
context?: RouteContext<C>;
|
|
392
|
+
appId?: string;
|
|
393
|
+
initHandle?: boolean;
|
|
394
|
+
};
|
|
395
|
+
interface HandleFn<T = any> {
|
|
396
|
+
(msg: {
|
|
397
|
+
path: string;
|
|
398
|
+
[key: string]: any;
|
|
399
|
+
}, ctx?: any): {
|
|
400
|
+
code: string;
|
|
401
|
+
data?: any;
|
|
402
|
+
message?: string;
|
|
403
|
+
[key: string]: any;
|
|
404
|
+
};
|
|
405
|
+
(res: RouteContext<T>): any;
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* QueryRouterServer
|
|
409
|
+
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
410
|
+
* @template C 自定义 RouteContext 类型
|
|
411
|
+
*/
|
|
412
|
+
declare class QueryRouterServer<C extends SimpleObject$1 = SimpleObject$1> extends QueryRouter<C> {
|
|
413
|
+
appId: string;
|
|
414
|
+
handle: any;
|
|
415
|
+
context: RouteContext<C>;
|
|
416
|
+
constructor(opts?: QueryRouterServerOpts<C>);
|
|
417
|
+
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
|
|
418
|
+
addRoute(route: Route, opts?: AddOpts): void;
|
|
419
|
+
Route: typeof Route;
|
|
420
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(opts: RouteOpts & {
|
|
421
|
+
metadata?: M;
|
|
422
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
423
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, opts?: RouteOpts & {
|
|
424
|
+
metadata?: M;
|
|
425
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
426
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
|
|
427
|
+
route<M extends SimpleObject$1 = SimpleObject$1>(path: string, key?: string, opts?: RouteOpts & {
|
|
428
|
+
metadata?: M;
|
|
429
|
+
}): Route<M, Required<RouteContext<C>>>;
|
|
430
|
+
prompt(description: string): Route<SimpleObject$1, SimpleObject$1>;
|
|
431
|
+
/**
|
|
432
|
+
* 调用了handle
|
|
433
|
+
* @param param0
|
|
434
|
+
* @returns
|
|
435
|
+
*/
|
|
436
|
+
run(msg: {
|
|
437
|
+
id?: string;
|
|
438
|
+
path?: string;
|
|
439
|
+
key?: string;
|
|
440
|
+
payload?: any;
|
|
441
|
+
token?: string;
|
|
442
|
+
data?: any;
|
|
443
|
+
}, ctx?: Partial<RouteContext<C>>): Promise<any>;
|
|
444
|
+
runAction<T extends {
|
|
445
|
+
id?: string;
|
|
446
|
+
path?: string;
|
|
447
|
+
key?: string;
|
|
448
|
+
metadata?: {
|
|
449
|
+
args?: any;
|
|
450
|
+
};
|
|
451
|
+
} = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<any>;
|
|
452
|
+
}
|
|
453
|
+
/** JSON Schema 基本类型映射到 TypeScript 类型 */
|
|
454
|
+
type JsonSchemaTypeToTS<T> = T extends {
|
|
455
|
+
type: "string";
|
|
456
|
+
} ? string : T extends {
|
|
457
|
+
type: "boolean";
|
|
458
|
+
} ? boolean : T extends {
|
|
459
|
+
type: "number";
|
|
460
|
+
} ? number : T extends {
|
|
461
|
+
type: "integer";
|
|
462
|
+
} ? number : T extends {
|
|
463
|
+
type: "object";
|
|
464
|
+
} ? object : T extends {
|
|
465
|
+
type: "array";
|
|
466
|
+
} ? any[] : any;
|
|
467
|
+
/** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
|
|
468
|
+
type ArgsShapeToPayload<T> = {
|
|
469
|
+
[K in keyof T as T[K] extends {
|
|
470
|
+
optional: true;
|
|
471
|
+
} ? never : K]: JsonSchemaTypeToTS<T[K]>;
|
|
472
|
+
} & {
|
|
473
|
+
[K in keyof T as T[K] extends {
|
|
474
|
+
optional: true;
|
|
475
|
+
} ? K : never]?: JsonSchemaTypeToTS<T[K]>;
|
|
476
|
+
};
|
|
477
|
+
/** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
|
|
478
|
+
type ArgsToPayload<T> = T extends {
|
|
479
|
+
type: "object";
|
|
480
|
+
properties: infer P;
|
|
481
|
+
} ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
|
|
482
|
+
/** 从 API 定义中提取 metadata.args */
|
|
483
|
+
type ExtractArgs<T> = T extends {
|
|
484
|
+
metadata: {
|
|
485
|
+
args: infer A;
|
|
486
|
+
};
|
|
487
|
+
} ? A : {};
|
|
488
|
+
/** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
|
|
489
|
+
type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
|
|
490
|
+
|
|
491
|
+
type RouteObject = {
|
|
492
|
+
[key: string]: RouteOpts;
|
|
493
|
+
};
|
|
494
|
+
type SimpleObject = Record<string, any>;
|
|
495
|
+
declare function define<T extends Record<string, RouteOpts>>(value: T): {
|
|
496
|
+
[K in keyof T]: T[K] & RouteOpts;
|
|
497
|
+
};
|
|
498
|
+
type RouteArray = RouteOpts[];
|
|
499
|
+
type ChainOptions = {
|
|
500
|
+
app: QueryRouterServer;
|
|
501
|
+
};
|
|
502
|
+
declare class Chain {
|
|
503
|
+
object: RouteOpts;
|
|
504
|
+
app?: QueryRouterServer;
|
|
505
|
+
constructor(object: RouteOpts, opts?: ChainOptions);
|
|
506
|
+
get key(): string;
|
|
507
|
+
get path(): string;
|
|
508
|
+
setDescription(desc: string): this;
|
|
509
|
+
setMeta(metadata: {
|
|
510
|
+
[key: string]: any;
|
|
511
|
+
}): this;
|
|
512
|
+
setPath(path: string): this;
|
|
513
|
+
setMiddleware(middleware: RouteMiddleware[]): this;
|
|
514
|
+
setKey(key: string): this;
|
|
515
|
+
setId(key: string): this;
|
|
516
|
+
setRun<U extends SimpleObject = {}>(run: Run<U>): this;
|
|
517
|
+
define<U extends SimpleObject = {}>(run: Run<U>): this;
|
|
518
|
+
createRoute(): this;
|
|
519
|
+
}
|
|
520
|
+
type QueryChainOptions = {
|
|
521
|
+
query?: Query;
|
|
522
|
+
omitKeys?: string[];
|
|
523
|
+
};
|
|
524
|
+
declare class QueryChain {
|
|
525
|
+
obj: SimpleObject;
|
|
526
|
+
query: Query;
|
|
527
|
+
omitKeys: string[];
|
|
528
|
+
constructor(value?: SimpleObject, opts?: QueryChainOptions);
|
|
529
|
+
omit(obj: SimpleObject, key?: string[]): {
|
|
530
|
+
[x: string]: any;
|
|
531
|
+
};
|
|
532
|
+
/**
|
|
533
|
+
* 生成
|
|
534
|
+
* @param queryData
|
|
535
|
+
* @returns
|
|
536
|
+
*/
|
|
537
|
+
getKey(queryData?: SimpleObject): Pick<RouteOpts, 'path' | 'key' | 'metadata' | 'description'>;
|
|
538
|
+
post<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
539
|
+
get<R = SimpleObject, P = SimpleObject>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
540
|
+
}
|
|
541
|
+
declare const util: {
|
|
542
|
+
getChain: (obj: RouteOpts, opts?: ChainOptions) => Chain;
|
|
543
|
+
};
|
|
544
|
+
declare class QueryUtil<T extends RouteObject = RouteObject> {
|
|
545
|
+
obj: T;
|
|
546
|
+
app: QueryRouterServer;
|
|
547
|
+
query: Query;
|
|
548
|
+
constructor(object: T, opts?: ChainOptions & QueryChainOptions);
|
|
549
|
+
static createFormObj<U extends RouteObject>(object: U, opts?: ChainOptions): QueryUtil<U>;
|
|
550
|
+
static create<U extends Record<string, RouteOpts>>(value: U, opts?: ChainOptions): QueryUtil<U>;
|
|
551
|
+
get<K extends keyof T>(key: K): RouteOpts;
|
|
552
|
+
chain<K extends keyof T>(key: K, opts?: ChainOptions): Chain;
|
|
553
|
+
queryChain<K extends keyof T>(key: K, opts?: QueryChainOptions): QueryChain;
|
|
554
|
+
static Chain: typeof Chain;
|
|
555
|
+
static QueryChain: typeof QueryChain;
|
|
556
|
+
get routeObject(): T;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export { QueryUtil, define, util };
|
|
560
|
+
export type { RouteArray, RouteObject, RouteOpts };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// src/router-define.ts
|
|
2
|
+
function define(value) {
|
|
3
|
+
return value;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
class Chain {
|
|
7
|
+
object;
|
|
8
|
+
app;
|
|
9
|
+
constructor(object, opts) {
|
|
10
|
+
this.object = object;
|
|
11
|
+
this.app = opts?.app;
|
|
12
|
+
}
|
|
13
|
+
get key() {
|
|
14
|
+
return this.object.key;
|
|
15
|
+
}
|
|
16
|
+
get path() {
|
|
17
|
+
return this.object.path;
|
|
18
|
+
}
|
|
19
|
+
setDescription(desc) {
|
|
20
|
+
this.object.description = desc;
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
setMeta(metadata) {
|
|
24
|
+
this.object.metadata = metadata;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
setPath(path) {
|
|
28
|
+
this.object.path = path;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
setMiddleware(middleware) {
|
|
32
|
+
this.object.middleware = middleware;
|
|
33
|
+
return this;
|
|
34
|
+
}
|
|
35
|
+
setKey(key) {
|
|
36
|
+
this.object.key = key;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
setId(key) {
|
|
40
|
+
this.object.id = key;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
setRun(run) {
|
|
44
|
+
this.object.run = run;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
define(run) {
|
|
48
|
+
this.object.run = run;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
createRoute() {
|
|
52
|
+
this.app.route(this.object).addTo(this.app);
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class QueryChain {
|
|
58
|
+
obj = {};
|
|
59
|
+
query;
|
|
60
|
+
omitKeys = ["metadata", "description", "validator"];
|
|
61
|
+
constructor(value, opts) {
|
|
62
|
+
this.obj = value || {};
|
|
63
|
+
this.query = opts?.query;
|
|
64
|
+
if (opts?.omitKeys)
|
|
65
|
+
this.omitKeys = opts.omitKeys;
|
|
66
|
+
}
|
|
67
|
+
omit(obj, key = []) {
|
|
68
|
+
const newObj = { ...obj };
|
|
69
|
+
key.forEach((k) => {
|
|
70
|
+
delete newObj[k];
|
|
71
|
+
});
|
|
72
|
+
return newObj;
|
|
73
|
+
}
|
|
74
|
+
getKey(queryData) {
|
|
75
|
+
const obj = this.omit(this.obj, this.omitKeys);
|
|
76
|
+
return {
|
|
77
|
+
...obj,
|
|
78
|
+
...queryData
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
post(data, options) {
|
|
82
|
+
const _queryData = this.getKey(data);
|
|
83
|
+
return this.query.post(_queryData, options);
|
|
84
|
+
}
|
|
85
|
+
get(data, options) {
|
|
86
|
+
const _queryData = this.getKey(data);
|
|
87
|
+
return this.query.get(_queryData, options);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
var util = {
|
|
91
|
+
getChain: (obj, opts) => {
|
|
92
|
+
return new Chain(obj, opts);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
class QueryUtil {
|
|
97
|
+
obj;
|
|
98
|
+
app;
|
|
99
|
+
query;
|
|
100
|
+
constructor(object, opts) {
|
|
101
|
+
this.obj = object;
|
|
102
|
+
this.app = opts?.app;
|
|
103
|
+
this.query = opts?.query;
|
|
104
|
+
}
|
|
105
|
+
static createFormObj(object, opts) {
|
|
106
|
+
return new QueryUtil(object, opts);
|
|
107
|
+
}
|
|
108
|
+
static create(value, opts) {
|
|
109
|
+
const obj = value;
|
|
110
|
+
return new QueryUtil(obj, opts);
|
|
111
|
+
}
|
|
112
|
+
get(key) {
|
|
113
|
+
return this.obj[key];
|
|
114
|
+
}
|
|
115
|
+
chain(key, opts) {
|
|
116
|
+
const obj = this.obj[key];
|
|
117
|
+
let newOpts = { app: this.app, ...opts };
|
|
118
|
+
return new QueryUtil.Chain(obj, newOpts);
|
|
119
|
+
}
|
|
120
|
+
queryChain(key, opts) {
|
|
121
|
+
const value = this.obj[key];
|
|
122
|
+
let newOpts = { query: this.query, ...opts };
|
|
123
|
+
return new QueryUtil.QueryChain(value, newOpts);
|
|
124
|
+
}
|
|
125
|
+
static Chain = Chain;
|
|
126
|
+
static QueryChain = QueryChain;
|
|
127
|
+
get routeObject() {
|
|
128
|
+
return this.obj;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export {
|
|
132
|
+
util,
|
|
133
|
+
define,
|
|
134
|
+
QueryUtil
|
|
135
|
+
};
|