@kevisual/router 0.0.26-alpha.4 → 0.0.26

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/auto.d.ts ADDED
@@ -0,0 +1,472 @@
1
+ import { z } from 'zod';
2
+ export { Schema } from 'zod';
3
+
4
+ type GlobOptions = {
5
+ cwd?: string;
6
+ load?: (args?: any) => Promise<any>;
7
+ };
8
+ declare const getMatchFiles: (match?: string, { cwd }?: GlobOptions) => Promise<string[]>;
9
+ declare const loadTS: (match?: string, { cwd, load }?: GlobOptions) => Promise<any[]>;
10
+
11
+ type BaseRule = {
12
+ value?: any;
13
+ required?: boolean;
14
+ message?: string;
15
+ };
16
+ type RuleString = {
17
+ type: 'string';
18
+ min?: number;
19
+ max?: number;
20
+ regex?: string;
21
+ } & BaseRule;
22
+ type RuleNumber = {
23
+ type: 'number';
24
+ min?: number;
25
+ max?: number;
26
+ } & BaseRule;
27
+ type RuleBoolean = {
28
+ type: 'boolean';
29
+ } & BaseRule;
30
+ type RuleArray = {
31
+ type: 'array';
32
+ items: Rule;
33
+ } & BaseRule;
34
+ type RuleObject = {
35
+ type: 'object';
36
+ properties: {
37
+ [key: string]: Rule;
38
+ };
39
+ } & BaseRule;
40
+ type RuleAny = {
41
+ type: 'any';
42
+ } & BaseRule;
43
+ type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
44
+ declare const createSchema: (rule: Rule) => z.ZodType<any, any, any>;
45
+
46
+ type RouterContextT = {
47
+ code?: number;
48
+ [key: string]: any;
49
+ };
50
+ type RouteContext<T = {
51
+ code?: number;
52
+ }, S = any> = {
53
+ query?: {
54
+ [key: string]: any;
55
+ };
56
+ /** return body */
57
+ body?: number | string | Object;
58
+ /** return code */
59
+ code?: number;
60
+ /** return msg */
61
+ message?: string;
62
+ state?: S;
63
+ /**
64
+ * 当前路径
65
+ */
66
+ currentPath?: string;
67
+ /**
68
+ * 当前key
69
+ */
70
+ currentKey?: string;
71
+ /**
72
+ * 当前route
73
+ */
74
+ currentRoute?: Route;
75
+ /**
76
+ * 进度
77
+ */
78
+ progress?: [string, string][];
79
+ nextQuery?: {
80
+ [key: string]: any;
81
+ };
82
+ end?: boolean;
83
+ /**
84
+ * 请求 route的返回结果,包函ctx
85
+ */
86
+ queryRouter?: QueryRouter;
87
+ error?: any;
88
+ /** 请求 route的返回结果,包函ctx */
89
+ call?: (message: {
90
+ path: string;
91
+ key?: string;
92
+ payload?: any;
93
+ [key: string]: any;
94
+ } | {
95
+ id: string;
96
+ apyload?: any;
97
+ [key: string]: any;
98
+ }, ctx?: RouteContext & {
99
+ [key: string]: any;
100
+ }) => Promise<any>;
101
+ /** 请求 route的返回结果,不包函ctx */
102
+ queryRoute?: (message: {
103
+ path: string;
104
+ key?: string;
105
+ payload?: any;
106
+ }, ctx?: RouteContext & {
107
+ [key: string]: any;
108
+ }) => Promise<any>;
109
+ index?: number;
110
+ throw?: (code?: number | string, message?: string, tips?: string) => void;
111
+ /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
112
+ needSerialize?: boolean;
113
+ } & T;
114
+ type SimpleObject = Record<string, any>;
115
+ type Run<T extends SimpleObject = {}> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
116
+ type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
117
+ type RouteMiddleware = {
118
+ path: string;
119
+ key?: string;
120
+ id?: string;
121
+ } | string;
122
+ type RouteOpts = {
123
+ path?: string;
124
+ key?: string;
125
+ id?: string;
126
+ run?: Run;
127
+ nextRoute?: NextRoute;
128
+ description?: string;
129
+ metadata?: {
130
+ [key: string]: any;
131
+ };
132
+ middleware?: RouteMiddleware[];
133
+ type?: 'route' | 'middleware';
134
+ /**
135
+ * validator: {
136
+ * packageName: {
137
+ * type: 'string',
138
+ * required: true,
139
+ * },
140
+ * }
141
+ */
142
+ validator?: {
143
+ [key: string]: Rule;
144
+ };
145
+ schema?: {
146
+ [key: string]: any;
147
+ };
148
+ isVerify?: boolean;
149
+ verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
150
+ verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
151
+ /**
152
+ * $#$ will be used to split path and key
153
+ */
154
+ idUsePath?: boolean;
155
+ /**
156
+ * id 合并的分隔符,默认为 $#$
157
+ */
158
+ delimiter?: string;
159
+ isDebug?: boolean;
160
+ };
161
+ type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
162
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
163
+ type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
164
+ declare class Route<U = {
165
+ [key: string]: any;
166
+ }> {
167
+ /**
168
+ * 一级路径
169
+ */
170
+ path?: string;
171
+ /**
172
+ * 二级路径
173
+ */
174
+ key?: string;
175
+ id?: string;
176
+ share?: boolean;
177
+ run?: Run;
178
+ nextRoute?: NextRoute;
179
+ description?: string;
180
+ metadata?: {
181
+ [key: string]: any;
182
+ };
183
+ middleware?: RouteMiddleware[];
184
+ type?: string;
185
+ private _validator?;
186
+ schema?: {
187
+ [key: string]: any;
188
+ };
189
+ data?: any;
190
+ /**
191
+ * 是否需要验证
192
+ */
193
+ isVerify?: boolean;
194
+ /**
195
+ * 是否开启debug,开启后会打印错误信息
196
+ */
197
+ isDebug?: boolean;
198
+ constructor(path: string, key?: string, opts?: RouteOpts);
199
+ private createSchema;
200
+ /**
201
+ * set validator and create schema
202
+ * @param validator
203
+ */
204
+ set validator(validator: {
205
+ [key: string]: Rule;
206
+ });
207
+ get validator(): {
208
+ [key: string]: Rule;
209
+ };
210
+ /**
211
+ * has code, body, message in ctx, return ctx if has error
212
+ * @param ctx
213
+ * @param dev
214
+ * @returns
215
+ */
216
+ verify(ctx: RouteContext, dev?: boolean): void;
217
+ /**
218
+ * Need to manully call return ctx fn and configure body, code, message
219
+ * @param key
220
+ * @param ctx
221
+ * @param dev
222
+ * @returns
223
+ */
224
+ verifyKey(key: string, ctx: RouteContext, dev?: boolean): {
225
+ message: string;
226
+ path: string;
227
+ key: string;
228
+ error: any;
229
+ } | {
230
+ message: string;
231
+ path: string;
232
+ key: string;
233
+ error?: undefined;
234
+ };
235
+ setValidator(validator: {
236
+ [key: string]: Rule;
237
+ }): this;
238
+ define<T extends {
239
+ [key: string]: any;
240
+ } = RouterContextT>(opts: DefineRouteOpts): this;
241
+ define<T extends {
242
+ [key: string]: any;
243
+ } = RouterContextT>(fn: Run<T & U>): this;
244
+ define<T extends {
245
+ [key: string]: any;
246
+ } = RouterContextT>(key: string, fn: Run<T & U>): this;
247
+ define<T extends {
248
+ [key: string]: any;
249
+ } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
250
+ addTo(router: QueryRouter | {
251
+ add: (route: Route) => void;
252
+ [key: string]: any;
253
+ }): void;
254
+ setData(data: any): this;
255
+ throw(code?: number | string, message?: string, tips?: string): void;
256
+ }
257
+ declare class QueryRouter {
258
+ routes: Route[];
259
+ maxNextRoute: number;
260
+ context?: RouteContext;
261
+ constructor();
262
+ add(route: Route): void;
263
+ /**
264
+ * remove route by path and key
265
+ * @param route
266
+ */
267
+ remove(route: Route | {
268
+ path: string;
269
+ key?: string;
270
+ }): void;
271
+ /**
272
+ * remove route by id
273
+ * @param uniqueId
274
+ */
275
+ removeById(unique: string): void;
276
+ /**
277
+ * 执行route
278
+ * @param path
279
+ * @param key
280
+ * @param ctx
281
+ * @returns
282
+ */
283
+ runRoute(path: string, key: string, ctx?: RouteContext): any;
284
+ /**
285
+ * 第一次执行
286
+ * @param message
287
+ * @param ctx
288
+ * @returns
289
+ */
290
+ parse(message: {
291
+ path: string;
292
+ key?: string;
293
+ payload?: any;
294
+ }, ctx?: RouteContext & {
295
+ [key: string]: any;
296
+ }): Promise<any>;
297
+ /**
298
+ * 返回的数据包含所有的context的请求返回的内容,可做其他处理
299
+ * @param message
300
+ * @param ctx
301
+ * @returns
302
+ */
303
+ call(message: {
304
+ id?: string;
305
+ path?: string;
306
+ key?: string;
307
+ payload?: any;
308
+ }, ctx?: RouteContext & {
309
+ [key: string]: any;
310
+ }): Promise<any>;
311
+ /**
312
+ * 请求 result 的数据
313
+ * @param message
314
+ * @param ctx
315
+ * @returns
316
+ */
317
+ queryRoute(message: {
318
+ path: string;
319
+ key?: string;
320
+ payload?: any;
321
+ }, ctx?: RouteContext & {
322
+ [key: string]: any;
323
+ }): Promise<{
324
+ code: any;
325
+ data: any;
326
+ message: any;
327
+ }>;
328
+ /**
329
+ * 设置上下文
330
+ * @description 这里的上下文是为了在handle函数中使用
331
+ * @param ctx
332
+ */
333
+ setContext(ctx: RouteContext): void;
334
+ getList(): RouteInfo[];
335
+ /**
336
+ * 获取handle函数, 这里会去执行parse函数
337
+ */
338
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
339
+ path: string;
340
+ key?: string;
341
+ [key: string]: any;
342
+ }, handleContext?: RouteContext) => Promise<{
343
+ [key: string]: any;
344
+ code: string;
345
+ data?: any;
346
+ message?: string;
347
+ } | {
348
+ code: any;
349
+ data: any;
350
+ message: any;
351
+ } | {
352
+ code: number;
353
+ message: any;
354
+ data?: undefined;
355
+ }>;
356
+ exportRoutes(): Route<{
357
+ [key: string]: any;
358
+ }>[];
359
+ importRoutes(routes: Route[]): void;
360
+ importRouter(router: QueryRouter): void;
361
+ throw(code?: number | string, message?: string, tips?: string): void;
362
+ hasRoute(path: string, key?: string): Route<{
363
+ [key: string]: any;
364
+ }>;
365
+ }
366
+ type QueryRouterServerOpts = {
367
+ handleFn?: HandleFn;
368
+ context?: RouteContext;
369
+ };
370
+ interface HandleFn<T = any> {
371
+ (msg: {
372
+ path: string;
373
+ [key: string]: any;
374
+ }, ctx?: any): {
375
+ code: string;
376
+ data?: any;
377
+ message?: string;
378
+ [key: string]: any;
379
+ };
380
+ (res: RouteContext<T>): any;
381
+ }
382
+ /**
383
+ * QueryRouterServer
384
+ * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
385
+ */
386
+ declare class QueryRouterServer extends QueryRouter {
387
+ handle: any;
388
+ constructor(opts?: QueryRouterServerOpts);
389
+ setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
390
+ use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
391
+ addRoute(route: Route): void;
392
+ Route: typeof Route;
393
+ route(opts: RouteOpts): Route<Required<RouteContext>>;
394
+ route(path: string, key?: string): Route<Required<RouteContext>>;
395
+ route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
396
+ route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
397
+ /**
398
+ * 等于queryRoute,但是调用了handle
399
+ * @param param0
400
+ * @returns
401
+ */
402
+ run({ path, key, payload }: {
403
+ path: string;
404
+ key?: string;
405
+ payload?: any;
406
+ }): Promise<any>;
407
+ }
408
+
409
+ type ListenSocketOptions = {
410
+ /**
411
+ * Unix socket path, defaults to './app.sock'
412
+ */
413
+ path?: string;
414
+ app?: QueryRouterServer;
415
+ /**
416
+ * Unix socket path, defaults to './app.pid'
417
+ */
418
+ pidPath?: string;
419
+ /**
420
+ * Timeout for the server, defaults to 15 minutes.
421
+ * If the server is not responsive for this duration, it will be terminated
422
+ */
423
+ timeout?: number;
424
+ };
425
+ declare const listenSocket: (options?: ListenSocketOptions) => Promise<any>;
426
+
427
+ /** 自定义错误 */
428
+ declare class CustomError extends Error {
429
+ code?: number;
430
+ data?: any;
431
+ message: string;
432
+ tips?: string;
433
+ constructor(code?: number | string, message?: string, tips?: string);
434
+ static fromCode(code?: number): CustomError;
435
+ static fromErrorData(code?: number, data?: any): CustomError;
436
+ static parseError(e: CustomError): {
437
+ code: number;
438
+ data: any;
439
+ message: string;
440
+ tips: string;
441
+ };
442
+ /**
443
+ * 判断 throw 的错误是否不是当前这个错误
444
+ * @param err
445
+ * @returns
446
+ */
447
+ static isError(err: any): boolean;
448
+ parse(e?: CustomError): {
449
+ code: number;
450
+ data: any;
451
+ message: string;
452
+ tips: string;
453
+ };
454
+ }
455
+
456
+ type QueryData = {
457
+ path?: string;
458
+ key?: string;
459
+ payload?: any;
460
+ [key: string]: any;
461
+ };
462
+ type CallSockOptions = {
463
+ socketPath?: string;
464
+ timeout?: number;
465
+ method?: 'GET' | 'POST';
466
+ };
467
+ declare const autoCall: (data: QueryData, options?: Omit<CallSockOptions, "method">) => Promise<any>;
468
+
469
+ declare const App: typeof QueryRouterServer;
470
+
471
+ export { App, CustomError, QueryRouter, QueryRouterServer, Route, autoCall, createSchema, getMatchFiles, listenSocket, loadTS };
472
+ export type { RouteContext, RouteOpts, Rule, Run };