@kevisual/router 0.0.29 → 0.0.30
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/router-browser.d.ts +10 -4
- package/dist/router-browser.js +50 -4
- package/dist/router.d.ts +12 -5
- package/dist/router.js +50 -4
- package/package.json +1 -1
- package/src/app.ts +1 -1
- package/src/browser.ts +1 -1
- package/src/index.ts +4 -2
- package/src/route.ts +55 -6
package/dist/router-browser.d.ts
CHANGED
|
@@ -141,7 +141,7 @@ type RouteOpts = {
|
|
|
141
141
|
[key: string]: Rule;
|
|
142
142
|
};
|
|
143
143
|
schema?: {
|
|
144
|
-
[key: string]:
|
|
144
|
+
[key: string]: any;
|
|
145
145
|
};
|
|
146
146
|
isVerify?: boolean;
|
|
147
147
|
verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
|
|
@@ -182,7 +182,7 @@ declare class Route<U = {
|
|
|
182
182
|
type?: string;
|
|
183
183
|
private _validator?;
|
|
184
184
|
schema?: {
|
|
185
|
-
[key: string]:
|
|
185
|
+
[key: string]: any;
|
|
186
186
|
};
|
|
187
187
|
data?: any;
|
|
188
188
|
/**
|
|
@@ -233,6 +233,8 @@ declare class Route<U = {
|
|
|
233
233
|
setValidator(validator: {
|
|
234
234
|
[key: string]: Rule;
|
|
235
235
|
}): this;
|
|
236
|
+
prompt(description: string): this;
|
|
237
|
+
prompt(description: Function): this;
|
|
236
238
|
define<T extends {
|
|
237
239
|
[key: string]: any;
|
|
238
240
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
@@ -245,6 +247,7 @@ declare class Route<U = {
|
|
|
245
247
|
define<T extends {
|
|
246
248
|
[key: string]: any;
|
|
247
249
|
} = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
|
|
250
|
+
update(opts: DefineRouteOpts, checkList?: string[]): this;
|
|
248
251
|
addTo(router: QueryRouter | {
|
|
249
252
|
add: (route: Route) => void;
|
|
250
253
|
[key: string]: any;
|
|
@@ -313,6 +316,7 @@ declare class QueryRouter {
|
|
|
313
316
|
* @returns
|
|
314
317
|
*/
|
|
315
318
|
queryRoute(message: {
|
|
319
|
+
id?: string;
|
|
316
320
|
path: string;
|
|
317
321
|
key?: string;
|
|
318
322
|
payload?: any;
|
|
@@ -334,7 +338,8 @@ declare class QueryRouter {
|
|
|
334
338
|
* 获取handle函数, 这里会去执行parse函数
|
|
335
339
|
*/
|
|
336
340
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
337
|
-
|
|
341
|
+
id?: string;
|
|
342
|
+
path?: string;
|
|
338
343
|
key?: string;
|
|
339
344
|
[key: string]: any;
|
|
340
345
|
}, handleContext?: RouteContext) => Promise<{
|
|
@@ -419,6 +424,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
419
424
|
payload?: any;
|
|
420
425
|
}): Promise<any>;
|
|
421
426
|
}
|
|
427
|
+
declare const Mini: typeof QueryRouterServer;
|
|
422
428
|
|
|
423
429
|
/** 自定义错误 */
|
|
424
430
|
declare class CustomError extends Error {
|
|
@@ -529,5 +535,5 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
|
|
|
529
535
|
|
|
530
536
|
declare const App: typeof QueryRouterServer;
|
|
531
537
|
|
|
532
|
-
export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
|
|
538
|
+
export { App, CustomError, Mini, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
|
|
533
539
|
export type { RouteArray, RouteContext, RouteObject, RouteOpts, Rule, Run, Schema };
|
package/dist/router-browser.js
CHANGED
|
@@ -4818,6 +4818,9 @@ class Route {
|
|
|
4818
4818
|
*/
|
|
4819
4819
|
isDebug;
|
|
4820
4820
|
constructor(path, key = '', opts) {
|
|
4821
|
+
if (!path) {
|
|
4822
|
+
path = nanoid$1(8);
|
|
4823
|
+
}
|
|
4821
4824
|
path = path.trim();
|
|
4822
4825
|
key = key.trim();
|
|
4823
4826
|
this.path = path;
|
|
@@ -4946,6 +4949,16 @@ class Route {
|
|
|
4946
4949
|
this.validator = validator;
|
|
4947
4950
|
return this;
|
|
4948
4951
|
}
|
|
4952
|
+
prompt(...args) {
|
|
4953
|
+
const [description] = args;
|
|
4954
|
+
if (typeof description === 'string') {
|
|
4955
|
+
this.description = description;
|
|
4956
|
+
}
|
|
4957
|
+
else if (typeof description === 'function') {
|
|
4958
|
+
this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
4959
|
+
}
|
|
4960
|
+
return this;
|
|
4961
|
+
}
|
|
4949
4962
|
define(...args) {
|
|
4950
4963
|
const [path, key, opts] = args;
|
|
4951
4964
|
// 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
|
|
@@ -4985,6 +4998,26 @@ class Route {
|
|
|
4985
4998
|
}
|
|
4986
4999
|
return this;
|
|
4987
5000
|
}
|
|
5001
|
+
update(opts, checkList) {
|
|
5002
|
+
const keys = Object.keys(opts);
|
|
5003
|
+
const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
|
|
5004
|
+
checkList = checkList || defaultCheckList;
|
|
5005
|
+
for (let item of keys) {
|
|
5006
|
+
if (!checkList.includes(item)) {
|
|
5007
|
+
continue;
|
|
5008
|
+
}
|
|
5009
|
+
if (item === 'validator') {
|
|
5010
|
+
this.validator = opts[item];
|
|
5011
|
+
continue;
|
|
5012
|
+
}
|
|
5013
|
+
if (item === 'middleware') {
|
|
5014
|
+
this.middleware = this.middleware.concat(opts[item]);
|
|
5015
|
+
continue;
|
|
5016
|
+
}
|
|
5017
|
+
this[item] = opts[item];
|
|
5018
|
+
}
|
|
5019
|
+
return this;
|
|
5020
|
+
}
|
|
4988
5021
|
addTo(router) {
|
|
4989
5022
|
router.add(this);
|
|
4990
5023
|
}
|
|
@@ -5265,19 +5298,20 @@ class QueryRouter {
|
|
|
5265
5298
|
*/
|
|
5266
5299
|
async call(message, ctx) {
|
|
5267
5300
|
let path = message.path;
|
|
5268
|
-
message.key;
|
|
5301
|
+
let key = message.key;
|
|
5269
5302
|
if (message.id) {
|
|
5270
5303
|
const route = this.routes.find((r) => r.id === message.id);
|
|
5271
5304
|
if (route) {
|
|
5272
5305
|
path = route.path;
|
|
5273
|
-
route.key;
|
|
5306
|
+
key = route.key;
|
|
5274
5307
|
}
|
|
5275
5308
|
else {
|
|
5276
5309
|
return { code: 404, body: null, message: 'Not found route' };
|
|
5277
5310
|
}
|
|
5311
|
+
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
|
5278
5312
|
}
|
|
5279
5313
|
else if (path) {
|
|
5280
|
-
return await this.parse({ ...message, path }, { ...this.context, ...ctx });
|
|
5314
|
+
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
|
5281
5315
|
}
|
|
5282
5316
|
else {
|
|
5283
5317
|
return { code: 404, body: null, message: 'Not found path' };
|
|
@@ -5317,6 +5351,17 @@ class QueryRouter {
|
|
|
5317
5351
|
return async (msg, handleContext) => {
|
|
5318
5352
|
try {
|
|
5319
5353
|
const context = { ...ctx, ...handleContext };
|
|
5354
|
+
if (msg.id) {
|
|
5355
|
+
const route = router.routes.find((r) => r.id === msg.id);
|
|
5356
|
+
if (route) {
|
|
5357
|
+
msg.path = route.path;
|
|
5358
|
+
msg.key = route.key;
|
|
5359
|
+
}
|
|
5360
|
+
else {
|
|
5361
|
+
return { code: 404, message: 'Not found route' };
|
|
5362
|
+
}
|
|
5363
|
+
}
|
|
5364
|
+
// @ts-ignore
|
|
5320
5365
|
const res = await router.parse(msg, context);
|
|
5321
5366
|
if (wrapperFn) {
|
|
5322
5367
|
res.data = res.body;
|
|
@@ -5431,6 +5476,7 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5431
5476
|
}
|
|
5432
5477
|
}
|
|
5433
5478
|
}
|
|
5479
|
+
const Mini = QueryRouterServer;
|
|
5434
5480
|
|
|
5435
5481
|
const parseBody = async (req) => {
|
|
5436
5482
|
return new Promise((resolve, reject) => {
|
|
@@ -5635,4 +5681,4 @@ class QueryUtil {
|
|
|
5635
5681
|
|
|
5636
5682
|
const App = QueryRouterServer;
|
|
5637
5683
|
|
|
5638
|
-
export { App, CustomError, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
|
|
5684
|
+
export { App, CustomError, Mini, QueryRouter, QueryRouterServer, QueryUtil, Route, createSchema, define, parseBody, parseSearch, parseSearchValue, util };
|
package/dist/router.d.ts
CHANGED
|
@@ -145,7 +145,7 @@ type RouteOpts = {
|
|
|
145
145
|
[key: string]: Rule;
|
|
146
146
|
};
|
|
147
147
|
schema?: {
|
|
148
|
-
[key: string]:
|
|
148
|
+
[key: string]: any;
|
|
149
149
|
};
|
|
150
150
|
isVerify?: boolean;
|
|
151
151
|
verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
|
|
@@ -186,7 +186,7 @@ declare class Route<U = {
|
|
|
186
186
|
type?: string;
|
|
187
187
|
private _validator?;
|
|
188
188
|
schema?: {
|
|
189
|
-
[key: string]:
|
|
189
|
+
[key: string]: any;
|
|
190
190
|
};
|
|
191
191
|
data?: any;
|
|
192
192
|
/**
|
|
@@ -237,6 +237,8 @@ declare class Route<U = {
|
|
|
237
237
|
setValidator(validator: {
|
|
238
238
|
[key: string]: Rule;
|
|
239
239
|
}): this;
|
|
240
|
+
prompt(description: string): this;
|
|
241
|
+
prompt(description: Function): this;
|
|
240
242
|
define<T extends {
|
|
241
243
|
[key: string]: any;
|
|
242
244
|
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
@@ -249,6 +251,7 @@ declare class Route<U = {
|
|
|
249
251
|
define<T extends {
|
|
250
252
|
[key: string]: any;
|
|
251
253
|
} = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
|
|
254
|
+
update(opts: DefineRouteOpts, checkList?: string[]): this;
|
|
252
255
|
addTo(router: QueryRouter | {
|
|
253
256
|
add: (route: Route) => void;
|
|
254
257
|
[key: string]: any;
|
|
@@ -317,6 +320,7 @@ declare class QueryRouter {
|
|
|
317
320
|
* @returns
|
|
318
321
|
*/
|
|
319
322
|
queryRoute(message: {
|
|
323
|
+
id?: string;
|
|
320
324
|
path: string;
|
|
321
325
|
key?: string;
|
|
322
326
|
payload?: any;
|
|
@@ -338,7 +342,8 @@ declare class QueryRouter {
|
|
|
338
342
|
* 获取handle函数, 这里会去执行parse函数
|
|
339
343
|
*/
|
|
340
344
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
341
|
-
|
|
345
|
+
id?: string;
|
|
346
|
+
path?: string;
|
|
342
347
|
key?: string;
|
|
343
348
|
[key: string]: any;
|
|
344
349
|
}, handleContext?: RouteContext) => Promise<{
|
|
@@ -423,6 +428,7 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
423
428
|
payload?: any;
|
|
424
429
|
}): Promise<any>;
|
|
425
430
|
}
|
|
431
|
+
declare const Mini: typeof QueryRouterServer;
|
|
426
432
|
|
|
427
433
|
declare class Connect {
|
|
428
434
|
path: string;
|
|
@@ -718,7 +724,8 @@ declare class App<T = {}, U = AppReqRes> {
|
|
|
718
724
|
route(path: string, opts?: RouteOpts): Route<U>;
|
|
719
725
|
route(path: string, key?: string, opts?: RouteOpts): Route<U>;
|
|
720
726
|
call(message: {
|
|
721
|
-
|
|
727
|
+
id?: string;
|
|
728
|
+
path?: string;
|
|
722
729
|
key?: string;
|
|
723
730
|
payload?: any;
|
|
724
731
|
}, ctx?: RouteContext & {
|
|
@@ -744,5 +751,5 @@ declare class App<T = {}, U = AppReqRes> {
|
|
|
744
751
|
onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
|
|
745
752
|
}
|
|
746
753
|
|
|
747
|
-
export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
|
|
754
|
+
export { App, Connect, CustomError, Mini, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
|
|
748
755
|
export type { RouteArray, RouteContext, RouteMiddleware, RouteObject, RouteOpts, Rule, Run, Schema };
|
package/dist/router.js
CHANGED
|
@@ -4840,6 +4840,9 @@ class Route {
|
|
|
4840
4840
|
*/
|
|
4841
4841
|
isDebug;
|
|
4842
4842
|
constructor(path, key = '', opts) {
|
|
4843
|
+
if (!path) {
|
|
4844
|
+
path = nanoid$1(8);
|
|
4845
|
+
}
|
|
4843
4846
|
path = path.trim();
|
|
4844
4847
|
key = key.trim();
|
|
4845
4848
|
this.path = path;
|
|
@@ -4968,6 +4971,16 @@ class Route {
|
|
|
4968
4971
|
this.validator = validator;
|
|
4969
4972
|
return this;
|
|
4970
4973
|
}
|
|
4974
|
+
prompt(...args) {
|
|
4975
|
+
const [description] = args;
|
|
4976
|
+
if (typeof description === 'string') {
|
|
4977
|
+
this.description = description;
|
|
4978
|
+
}
|
|
4979
|
+
else if (typeof description === 'function') {
|
|
4980
|
+
this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
4981
|
+
}
|
|
4982
|
+
return this;
|
|
4983
|
+
}
|
|
4971
4984
|
define(...args) {
|
|
4972
4985
|
const [path, key, opts] = args;
|
|
4973
4986
|
// 全覆盖,所以opts需要准确,不能由idUsePath 需要check的变量
|
|
@@ -5007,6 +5020,26 @@ class Route {
|
|
|
5007
5020
|
}
|
|
5008
5021
|
return this;
|
|
5009
5022
|
}
|
|
5023
|
+
update(opts, checkList) {
|
|
5024
|
+
const keys = Object.keys(opts);
|
|
5025
|
+
const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
|
|
5026
|
+
checkList = checkList || defaultCheckList;
|
|
5027
|
+
for (let item of keys) {
|
|
5028
|
+
if (!checkList.includes(item)) {
|
|
5029
|
+
continue;
|
|
5030
|
+
}
|
|
5031
|
+
if (item === 'validator') {
|
|
5032
|
+
this.validator = opts[item];
|
|
5033
|
+
continue;
|
|
5034
|
+
}
|
|
5035
|
+
if (item === 'middleware') {
|
|
5036
|
+
this.middleware = this.middleware.concat(opts[item]);
|
|
5037
|
+
continue;
|
|
5038
|
+
}
|
|
5039
|
+
this[item] = opts[item];
|
|
5040
|
+
}
|
|
5041
|
+
return this;
|
|
5042
|
+
}
|
|
5010
5043
|
addTo(router) {
|
|
5011
5044
|
router.add(this);
|
|
5012
5045
|
}
|
|
@@ -5287,19 +5320,20 @@ class QueryRouter {
|
|
|
5287
5320
|
*/
|
|
5288
5321
|
async call(message, ctx) {
|
|
5289
5322
|
let path = message.path;
|
|
5290
|
-
message.key;
|
|
5323
|
+
let key = message.key;
|
|
5291
5324
|
if (message.id) {
|
|
5292
5325
|
const route = this.routes.find((r) => r.id === message.id);
|
|
5293
5326
|
if (route) {
|
|
5294
5327
|
path = route.path;
|
|
5295
|
-
route.key;
|
|
5328
|
+
key = route.key;
|
|
5296
5329
|
}
|
|
5297
5330
|
else {
|
|
5298
5331
|
return { code: 404, body: null, message: 'Not found route' };
|
|
5299
5332
|
}
|
|
5333
|
+
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
|
5300
5334
|
}
|
|
5301
5335
|
else if (path) {
|
|
5302
|
-
return await this.parse({ ...message, path }, { ...this.context, ...ctx });
|
|
5336
|
+
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
|
5303
5337
|
}
|
|
5304
5338
|
else {
|
|
5305
5339
|
return { code: 404, body: null, message: 'Not found path' };
|
|
@@ -5339,6 +5373,17 @@ class QueryRouter {
|
|
|
5339
5373
|
return async (msg, handleContext) => {
|
|
5340
5374
|
try {
|
|
5341
5375
|
const context = { ...ctx, ...handleContext };
|
|
5376
|
+
if (msg.id) {
|
|
5377
|
+
const route = router.routes.find((r) => r.id === msg.id);
|
|
5378
|
+
if (route) {
|
|
5379
|
+
msg.path = route.path;
|
|
5380
|
+
msg.key = route.key;
|
|
5381
|
+
}
|
|
5382
|
+
else {
|
|
5383
|
+
return { code: 404, message: 'Not found route' };
|
|
5384
|
+
}
|
|
5385
|
+
}
|
|
5386
|
+
// @ts-ignore
|
|
5342
5387
|
const res = await router.parse(msg, context);
|
|
5343
5388
|
if (wrapperFn) {
|
|
5344
5389
|
res.data = res.body;
|
|
@@ -5453,6 +5498,7 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5453
5498
|
}
|
|
5454
5499
|
}
|
|
5455
5500
|
}
|
|
5501
|
+
const Mini = QueryRouterServer;
|
|
5456
5502
|
|
|
5457
5503
|
class Connect {
|
|
5458
5504
|
path;
|
|
@@ -11355,4 +11401,4 @@ class App {
|
|
|
11355
11401
|
}
|
|
11356
11402
|
}
|
|
11357
11403
|
|
|
11358
|
-
export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
|
|
11404
|
+
export { App, Connect, CustomError, Mini, QueryConnect, QueryRouter, QueryRouterServer, QueryUtil, Route, Server, createSchema, define, handleServer, util };
|
package/package.json
CHANGED
package/src/app.ts
CHANGED
|
@@ -82,7 +82,7 @@ export class App<T = {}, U = AppReqRes> {
|
|
|
82
82
|
}
|
|
83
83
|
return new Route(path, key, opts);
|
|
84
84
|
}
|
|
85
|
-
async call(message: { path
|
|
85
|
+
async call(message: { id?: string, path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
|
86
86
|
const router = this.router;
|
|
87
87
|
return await router.call(message, ctx);
|
|
88
88
|
}
|
package/src/browser.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { Route, QueryRouter, QueryRouterServer } from './route.ts';
|
|
1
|
+
export { Route, QueryRouter, QueryRouterServer, Mini } from './route.ts';
|
|
2
2
|
export { Connect, QueryConnect } from './connect.ts';
|
|
3
3
|
|
|
4
4
|
export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
|
|
@@ -11,7 +11,9 @@ export { Server, handleServer } from './server/index.ts';
|
|
|
11
11
|
*/
|
|
12
12
|
export { CustomError } from './result/error.ts';
|
|
13
13
|
|
|
14
|
-
export {
|
|
14
|
+
export { createSchema } from './validator/index.ts';
|
|
15
|
+
|
|
16
|
+
export type { Rule, Schema, } from './validator/index.ts';
|
|
15
17
|
|
|
16
18
|
export { App } from './app.ts';
|
|
17
19
|
|
package/src/route.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { nanoid } from 'nanoid';
|
|
1
|
+
import { nanoid, random } from 'nanoid';
|
|
2
2
|
import { CustomError } from './result/error.ts';
|
|
3
3
|
import { Schema, Rule, createSchema } from './validator/index.ts';
|
|
4
4
|
import { pick } from './utils/pick.ts';
|
|
@@ -88,7 +88,7 @@ export type RouteOpts = {
|
|
|
88
88
|
* }
|
|
89
89
|
*/
|
|
90
90
|
validator?: { [key: string]: Rule };
|
|
91
|
-
schema?: { [key: string]:
|
|
91
|
+
schema?: { [key: string]: any };
|
|
92
92
|
isVerify?: boolean;
|
|
93
93
|
verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
|
|
94
94
|
verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
|
|
@@ -123,7 +123,7 @@ export class Route<U = { [key: string]: any }> {
|
|
|
123
123
|
middleware?: RouteMiddleware[]; // middleware
|
|
124
124
|
type? = 'route';
|
|
125
125
|
private _validator?: { [key: string]: Rule };
|
|
126
|
-
schema?: { [key: string]:
|
|
126
|
+
schema?: { [key: string]: any };
|
|
127
127
|
data?: any;
|
|
128
128
|
/**
|
|
129
129
|
* 是否需要验证
|
|
@@ -134,6 +134,9 @@ export class Route<U = { [key: string]: any }> {
|
|
|
134
134
|
*/
|
|
135
135
|
isDebug?: boolean;
|
|
136
136
|
constructor(path: string, key: string = '', opts?: RouteOpts) {
|
|
137
|
+
if (!path) {
|
|
138
|
+
path = nanoid(8)
|
|
139
|
+
}
|
|
137
140
|
path = path.trim();
|
|
138
141
|
key = key.trim();
|
|
139
142
|
this.path = path;
|
|
@@ -261,6 +264,17 @@ export class Route<U = { [key: string]: any }> {
|
|
|
261
264
|
this.validator = validator;
|
|
262
265
|
return this;
|
|
263
266
|
}
|
|
267
|
+
prompt(description: string): this;
|
|
268
|
+
prompt(description: Function): this;
|
|
269
|
+
prompt(...args: any[]) {
|
|
270
|
+
const [description] = args;
|
|
271
|
+
if (typeof description === 'string') {
|
|
272
|
+
this.description = description;
|
|
273
|
+
} else if (typeof description === 'function') {
|
|
274
|
+
this.description = description() || ''; // 如果是Promise,需要addTo App之前就要获取应有的函数了。
|
|
275
|
+
}
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
264
278
|
define<T extends { [key: string]: any } = RouterContextT>(opts: DefineRouteOpts): this;
|
|
265
279
|
define<T extends { [key: string]: any } = RouterContextT>(fn: Run<T & U>): this;
|
|
266
280
|
define<T extends { [key: string]: any } = RouterContextT>(key: string, fn: Run<T & U>): this;
|
|
@@ -304,6 +318,27 @@ export class Route<U = { [key: string]: any }> {
|
|
|
304
318
|
}
|
|
305
319
|
return this;
|
|
306
320
|
}
|
|
321
|
+
|
|
322
|
+
update(opts: DefineRouteOpts, checkList?: string[]): this {
|
|
323
|
+
const keys = Object.keys(opts);
|
|
324
|
+
const defaultCheckList = ['path', 'key', 'run', 'nextRoute', 'description', 'metadata', 'middleware', 'type', 'validator', 'isVerify', 'isDebug'];
|
|
325
|
+
checkList = checkList || defaultCheckList;
|
|
326
|
+
for (let item of keys) {
|
|
327
|
+
if (!checkList.includes(item)) {
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
if (item === 'validator') {
|
|
331
|
+
this.validator = opts[item];
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
if (item === 'middleware') {
|
|
335
|
+
this.middleware = this.middleware.concat(opts[item]);
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
this[item] = opts[item];
|
|
339
|
+
}
|
|
340
|
+
return this;
|
|
341
|
+
}
|
|
307
342
|
addTo(router: QueryRouter | { add: (route: Route) => void;[key: string]: any }) {
|
|
308
343
|
router.add(this);
|
|
309
344
|
}
|
|
@@ -583,8 +618,9 @@ export class QueryRouter {
|
|
|
583
618
|
} else {
|
|
584
619
|
return { code: 404, body: null, message: 'Not found route' };
|
|
585
620
|
}
|
|
621
|
+
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
|
586
622
|
} else if (path) {
|
|
587
|
-
return await this.parse({ ...message, path }, { ...this.context, ...ctx });
|
|
623
|
+
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
|
588
624
|
} else {
|
|
589
625
|
return { code: 404, body: null, message: 'Not found path' };
|
|
590
626
|
}
|
|
@@ -596,7 +632,7 @@ export class QueryRouter {
|
|
|
596
632
|
* @param ctx
|
|
597
633
|
* @returns
|
|
598
634
|
*/
|
|
599
|
-
async queryRoute(message: { path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
|
635
|
+
async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
|
600
636
|
const res = await this.parse(message, { ...this.context, ...ctx });
|
|
601
637
|
return {
|
|
602
638
|
code: res.code,
|
|
@@ -621,9 +657,19 @@ export class QueryRouter {
|
|
|
621
657
|
* 获取handle函数, 这里会去执行parse函数
|
|
622
658
|
*/
|
|
623
659
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext) {
|
|
624
|
-
return async (msg: { path
|
|
660
|
+
return async (msg: { id?: string; path?: string; key?: string;[key: string]: any }, handleContext?: RouteContext) => {
|
|
625
661
|
try {
|
|
626
662
|
const context = { ...ctx, ...handleContext };
|
|
663
|
+
if (msg.id) {
|
|
664
|
+
const route = router.routes.find((r) => r.id === msg.id);
|
|
665
|
+
if (route) {
|
|
666
|
+
msg.path = route.path;
|
|
667
|
+
msg.key = route.key;
|
|
668
|
+
} else {
|
|
669
|
+
return { code: 404, message: 'Not found route' };
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
// @ts-ignore
|
|
627
673
|
const res = await router.parse(msg, context);
|
|
628
674
|
if (wrapperFn) {
|
|
629
675
|
res.data = res.body;
|
|
@@ -751,3 +797,6 @@ export class QueryRouterServer extends QueryRouter {
|
|
|
751
797
|
}
|
|
752
798
|
}
|
|
753
799
|
}
|
|
800
|
+
|
|
801
|
+
|
|
802
|
+
export const Mini = QueryRouterServer
|