@kevisual/router 0.0.70 → 0.0.72

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.
@@ -1,7 +1,398 @@
1
- import { RouteOpts, QueryRouterServer, RouteMiddleware, Run } from '@kevisual/router';
2
- export { RouteOpts } from '@kevisual/router';
1
+ import { EventEmitter } from 'eventemitter3';
3
2
  import { Query, DataOpts, Result } from '@kevisual/query/query';
4
3
 
4
+ declare class MockProcess {
5
+ emitter?: EventEmitter;
6
+ process?: NodeJS.Process;
7
+ constructor(opts?: {
8
+ emitter?: EventEmitter;
9
+ isNode?: boolean;
10
+ });
11
+ send(data?: any, callback?: (err?: Error) => void): void;
12
+ exit(flag?: number): void;
13
+ on(fn: (msg?: any) => any): void;
14
+ desctroy(): void;
15
+ }
16
+
17
+ type RouterContextT = {
18
+ code?: number;
19
+ [key: string]: any;
20
+ };
21
+ type RouteContext<T = {
22
+ code?: number;
23
+ }, S = any> = {
24
+ /**
25
+ * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
26
+ * 或者不需要登录的,直接调用
27
+ */
28
+ appId?: string;
29
+ query?: {
30
+ [key: string]: any;
31
+ };
32
+ args?: {
33
+ [key: string]: any;
34
+ };
35
+ /** return body */
36
+ body?: number | string | Object;
37
+ forward?: (response: {
38
+ code: number;
39
+ data?: any;
40
+ message?: any;
41
+ }) => void;
42
+ /** return code */
43
+ code?: number;
44
+ /** return msg */
45
+ message?: string;
46
+ state?: S;
47
+ /**
48
+ * 当前路径
49
+ */
50
+ currentPath?: string;
51
+ /**
52
+ * 当前key
53
+ */
54
+ currentKey?: string;
55
+ /**
56
+ * 当前route
57
+ */
58
+ currentRoute?: Route;
59
+ /**
60
+ * 进度
61
+ */
62
+ progress?: [string, string][];
63
+ nextQuery?: {
64
+ [key: string]: any;
65
+ };
66
+ end?: boolean;
67
+ app?: QueryRouter;
68
+ error?: any;
69
+ /** 请求 route的返回结果,不解析body为data */
70
+ call?: (message: {
71
+ path: string;
72
+ key?: string;
73
+ payload?: any;
74
+ [key: string]: any;
75
+ } | {
76
+ id: string;
77
+ apyload?: any;
78
+ [key: string]: any;
79
+ }, ctx?: RouteContext & {
80
+ [key: string]: any;
81
+ }) => Promise<any>;
82
+ /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
83
+ run?: (message: {
84
+ path: string;
85
+ key?: string;
86
+ payload?: any;
87
+ }, ctx?: RouteContext & {
88
+ [key: string]: any;
89
+ }) => Promise<any>;
90
+ index?: number;
91
+ throw?: (code?: number | string, message?: string, tips?: string) => void;
92
+ /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
93
+ needSerialize?: boolean;
94
+ } & T;
95
+ type SimpleObject$1 = Record<string, any>;
96
+ type Run<T extends SimpleObject$1 = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
97
+ type RunMessage = {
98
+ path?: string;
99
+ key?: string;
100
+ id?: string;
101
+ payload?: any;
102
+ };
103
+ type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
104
+ type RouteMiddleware = {
105
+ path: string;
106
+ key?: string;
107
+ id?: string;
108
+ } | string;
109
+ type RouteOpts<U = {}, T = SimpleObject$1> = {
110
+ path?: string;
111
+ key?: string;
112
+ id?: string;
113
+ run?: Run<U>;
114
+ nextRoute?: NextRoute;
115
+ description?: string;
116
+ metadata?: T;
117
+ middleware?: RouteMiddleware[];
118
+ type?: 'route' | 'middleware';
119
+ /**
120
+ * $#$ will be used to split path and key
121
+ */
122
+ idUsePath?: boolean;
123
+ /**
124
+ * id 合并的分隔符,默认为 $#$
125
+ */
126
+ delimiter?: string;
127
+ isDebug?: boolean;
128
+ };
129
+ type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
130
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
131
+ type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
132
+ declare class Route<U = {
133
+ [key: string]: any;
134
+ }, T extends SimpleObject$1 = SimpleObject$1> {
135
+ /**
136
+ * 一级路径
137
+ */
138
+ path?: string;
139
+ /**
140
+ * 二级路径
141
+ */
142
+ key?: string;
143
+ id?: string;
144
+ run?: Run;
145
+ nextRoute?: NextRoute;
146
+ description?: string;
147
+ metadata?: T;
148
+ middleware?: RouteMiddleware[];
149
+ type?: string;
150
+ data?: any;
151
+ /**
152
+ * 是否开启debug,开启后会打印错误信息
153
+ */
154
+ isDebug?: boolean;
155
+ constructor(path?: string, key?: string, opts?: RouteOpts);
156
+ prompt(description: string): this;
157
+ prompt(description: Function): this;
158
+ define<T extends {
159
+ [key: string]: any;
160
+ } = RouterContextT>(opts: DefineRouteOpts): this;
161
+ define<T extends {
162
+ [key: string]: any;
163
+ } = RouterContextT>(fn: Run<T & U>): this;
164
+ define<T extends {
165
+ [key: string]: any;
166
+ } = RouterContextT>(key: string, fn: Run<T & U>): this;
167
+ define<T extends {
168
+ [key: string]: any;
169
+ } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
170
+ update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
171
+ addTo(router: QueryRouter | {
172
+ add: (route: Route) => void;
173
+ [key: string]: any;
174
+ }, opts?: AddOpts): void;
175
+ setData(data: any): this;
176
+ throw(code?: number | string, message?: string, tips?: string): void;
177
+ }
178
+ /**
179
+ * @parmas overwrite 是否覆盖已存在的route,默认true
180
+ */
181
+ type AddOpts = {
182
+ overwrite?: boolean;
183
+ };
184
+ declare class QueryRouter {
185
+ appId: string;
186
+ routes: Route[];
187
+ maxNextRoute: number;
188
+ context?: RouteContext;
189
+ constructor();
190
+ /**
191
+ * add route
192
+ * @param route
193
+ * @param opts
194
+ */
195
+ add(route: Route, opts?: AddOpts): void;
196
+ /**
197
+ * remove route by path and key
198
+ * @param route
199
+ */
200
+ remove(route: Route | {
201
+ path: string;
202
+ key?: string;
203
+ }): void;
204
+ /**
205
+ * remove route by id
206
+ * @param uniqueId
207
+ */
208
+ removeById(uniqueId: string): void;
209
+ /**
210
+ * 执行route
211
+ * @param path
212
+ * @param key
213
+ * @param ctx
214
+ * @returns
215
+ */
216
+ runRoute(path: string, key: string, ctx?: RouteContext): any;
217
+ /**
218
+ * 第一次执行
219
+ * @param message
220
+ * @param ctx
221
+ * @returns
222
+ */
223
+ parse(message: {
224
+ path: string;
225
+ key?: string;
226
+ payload?: any;
227
+ }, ctx?: RouteContext & {
228
+ [key: string]: any;
229
+ }): Promise<any>;
230
+ /**
231
+ * 返回的数据包含所有的context的请求返回的内容,可做其他处理
232
+ * @param message
233
+ * @param ctx
234
+ * @returns
235
+ */
236
+ call(message: {
237
+ id?: string;
238
+ path?: string;
239
+ key?: string;
240
+ payload?: any;
241
+ }, ctx?: RouteContext & {
242
+ [key: string]: any;
243
+ }): Promise<any>;
244
+ /**
245
+ * 请求 result 的数据
246
+ * @param message
247
+ * @param ctx
248
+ * @deprecated use run or call instead
249
+ * @returns
250
+ */
251
+ queryRoute(message: {
252
+ id?: string;
253
+ path: string;
254
+ key?: string;
255
+ payload?: any;
256
+ }, ctx?: RouteContext & {
257
+ [key: string]: any;
258
+ }): Promise<{
259
+ code: any;
260
+ data: any;
261
+ message: any;
262
+ }>;
263
+ /**
264
+ * Router Run获取数据
265
+ * @param message
266
+ * @param ctx
267
+ * @returns
268
+ */
269
+ run(message: {
270
+ id?: string;
271
+ path?: string;
272
+ key?: string;
273
+ payload?: any;
274
+ }, ctx?: RouteContext & {
275
+ [key: string]: any;
276
+ }): Promise<{
277
+ code: any;
278
+ data: any;
279
+ message: any;
280
+ }>;
281
+ /**
282
+ * 设置上下文
283
+ * @description 这里的上下文是为了在handle函数中使用
284
+ * @param ctx
285
+ */
286
+ setContext(ctx: RouteContext): void;
287
+ getList(filter?: (route: Route) => boolean): RouteInfo[];
288
+ /**
289
+ * 获取handle函数, 这里会去执行parse函数
290
+ */
291
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
292
+ id?: string;
293
+ path?: string;
294
+ key?: string;
295
+ [key: string]: any;
296
+ }, handleContext?: RouteContext) => Promise<{
297
+ [key: string]: any;
298
+ code: string;
299
+ data?: any;
300
+ message?: string;
301
+ } | {
302
+ code: any;
303
+ data: any;
304
+ message: any;
305
+ } | {
306
+ code: number;
307
+ message: any;
308
+ data?: undefined;
309
+ }>;
310
+ exportRoutes(): Route<{
311
+ [key: string]: any;
312
+ }, SimpleObject$1>[];
313
+ importRoutes(routes: Route[]): void;
314
+ importRouter(router: QueryRouter): void;
315
+ throw(code?: number | string, message?: string, tips?: string): void;
316
+ hasRoute(path: string, key?: string): Route<{
317
+ [key: string]: any;
318
+ }, SimpleObject$1>;
319
+ findRoute(opts?: {
320
+ path?: string;
321
+ key?: string;
322
+ id?: string;
323
+ }): Route<{
324
+ [key: string]: any;
325
+ }, SimpleObject$1>;
326
+ createRouteList(force?: boolean, filter?: (route: Route) => boolean): void;
327
+ /**
328
+ * 等待程序运行, 获取到message的数据,就执行
329
+ * params 是预设参数
330
+ * emitter = process
331
+ * -- .exit
332
+ * -- .on
333
+ * -- .send
334
+ */
335
+ wait(params?: {
336
+ message: RunMessage;
337
+ }, opts?: {
338
+ mockProcess?: MockProcess;
339
+ timeout?: number;
340
+ getList?: boolean;
341
+ force?: boolean;
342
+ filter?: (route: Route) => boolean;
343
+ }): Promise<void>;
344
+ toJSONSchema: (route: RouteInfo) => Pick<RouteInfo, "path" | "key" | "id" | "description" | "type" | "middleware" | "metadata">;
345
+ fromJSONSchema: (route: RouteInfo) => RouteInfo;
346
+ }
347
+ type QueryRouterServerOpts = {
348
+ handleFn?: HandleFn;
349
+ context?: RouteContext;
350
+ appId?: string;
351
+ };
352
+ interface HandleFn<T = any> {
353
+ (msg: {
354
+ path: string;
355
+ [key: string]: any;
356
+ }, ctx?: any): {
357
+ code: string;
358
+ data?: any;
359
+ message?: string;
360
+ [key: string]: any;
361
+ };
362
+ (res: RouteContext<T>): any;
363
+ }
364
+ /**
365
+ * QueryRouterServer
366
+ * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
367
+ */
368
+ declare class QueryRouterServer extends QueryRouter {
369
+ appId: string;
370
+ handle: any;
371
+ constructor(opts?: QueryRouterServerOpts);
372
+ setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
373
+ addRoute(route: Route, opts?: AddOpts): void;
374
+ Route: typeof Route;
375
+ route(opts: RouteOpts): Route<Required<RouteContext>>;
376
+ route(path: string, key?: string): Route<Required<RouteContext>>;
377
+ route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
378
+ route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
379
+ prompt(description: string): Route<Required<RouteContext>>;
380
+ prompt(description: Function): Route<Required<RouteContext>>;
381
+ /**
382
+ * 调用了handle
383
+ * @param param0
384
+ * @returns
385
+ */
386
+ run(msg: {
387
+ id?: string;
388
+ path?: string;
389
+ key?: string;
390
+ payload?: any;
391
+ }, ctx?: RouteContext & {
392
+ [key: string]: any;
393
+ }): Promise<any>;
394
+ }
395
+
5
396
  type RouteObject = {
6
397
  [key: string]: RouteOpts;
7
398
  };
@@ -71,4 +462,4 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
71
462
  }
72
463
 
73
464
  export { QueryUtil, define, util };
74
- export type { RouteArray, RouteObject };
465
+ export type { RouteArray, RouteObject, RouteOpts };
@@ -1,133 +1,135 @@
1
+ // src/router-define.ts
1
2
  function define(value) {
2
- return value;
3
+ return value;
3
4
  }
5
+
4
6
  class Chain {
5
- object;
6
- app;
7
- constructor(object, opts) {
8
- this.object = object;
9
- this.app = opts?.app;
10
- }
11
- get key() {
12
- return this.object.key;
13
- }
14
- get path() {
15
- return this.object.path;
16
- }
17
- setDescription(desc) {
18
- this.object.description = desc;
19
- return this;
20
- }
21
- setMeta(metadata) {
22
- this.object.metadata = metadata;
23
- return this;
24
- }
25
- setPath(path) {
26
- this.object.path = path;
27
- return this;
28
- }
29
- setMiddleware(middleware) {
30
- this.object.middleware = middleware;
31
- return this;
32
- }
33
- setKey(key) {
34
- this.object.key = key;
35
- return this;
36
- }
37
- setId(key) {
38
- this.object.id = key;
39
- return this;
40
- }
41
- setRun(run) {
42
- this.object.run = run;
43
- return this;
44
- }
45
- define(run) {
46
- this.object.run = run;
47
- return this;
48
- }
49
- createRoute() {
50
- this.app.route(this.object).addTo(this.app);
51
- return this;
52
- }
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
+ }
53
55
  }
56
+
54
57
  class QueryChain {
55
- obj = {};
56
- query;
57
- omitKeys = ['metadata', 'description', 'validator'];
58
- constructor(value, opts) {
59
- this.obj = value || {};
60
- this.query = opts?.query;
61
- if (opts?.omitKeys)
62
- this.omitKeys = opts.omitKeys;
63
- }
64
- omit(obj, key = []) {
65
- const newObj = { ...obj };
66
- key.forEach((k) => {
67
- delete newObj[k];
68
- });
69
- return newObj;
70
- }
71
- /**
72
- * 生成
73
- * @param queryData
74
- * @returns
75
- */
76
- getKey(queryData) {
77
- const obj = this.omit(this.obj, this.omitKeys);
78
- return {
79
- ...obj,
80
- ...queryData,
81
- };
82
- }
83
- post(data, options) {
84
- const _queryData = this.getKey(data);
85
- return this.query.post(_queryData, options);
86
- }
87
- get(data, options) {
88
- const _queryData = this.getKey(data);
89
- return this.query.get(_queryData, options);
90
- }
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
+ }
91
89
  }
92
- const util = {
93
- getChain: (obj, opts) => {
94
- return new Chain(obj, opts);
95
- },
90
+ var util = {
91
+ getChain: (obj, opts) => {
92
+ return new Chain(obj, opts);
93
+ }
96
94
  };
95
+
97
96
  class QueryUtil {
98
- obj;
99
- app;
100
- query;
101
- constructor(object, opts) {
102
- this.obj = object;
103
- this.app = opts?.app;
104
- this.query = opts?.query;
105
- }
106
- static createFormObj(object, opts) {
107
- return new QueryUtil(object, opts);
108
- }
109
- static create(value, opts) {
110
- const obj = value;
111
- return new QueryUtil(obj, opts);
112
- }
113
- get(key) {
114
- return this.obj[key];
115
- }
116
- chain(key, opts) {
117
- const obj = this.obj[key];
118
- let newOpts = { app: this.app, ...opts };
119
- return new QueryUtil.Chain(obj, newOpts);
120
- }
121
- queryChain(key, opts) {
122
- const value = this.obj[key];
123
- let newOpts = { query: this.query, ...opts };
124
- return new QueryUtil.QueryChain(value, newOpts);
125
- }
126
- static Chain = Chain;
127
- static QueryChain = QueryChain;
128
- get routeObject() {
129
- return this.obj;
130
- }
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
+ }
131
130
  }
132
-
133
- export { QueryUtil, define, util };
131
+ export {
132
+ util,
133
+ define,
134
+ QueryUtil
135
+ };