@kevisual/router 0.0.5-alpha-0 → 0.0.6-alpha-1
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 +40 -10
- package/dist/router-browser.js +28 -4
- package/dist/router-simple.d.ts +1 -1
- package/dist/router-simple.js +1 -7
- package/dist/router.d.ts +48 -11
- package/dist/router.js +31 -4
- package/package.json +1 -1
package/dist/router-browser.d.ts
CHANGED
|
@@ -65,9 +65,18 @@ type RouteContext<T = {
|
|
|
65
65
|
end?: boolean;
|
|
66
66
|
queryRouter?: QueryRouter;
|
|
67
67
|
error?: any;
|
|
68
|
+
/** 请求 route的返回结果,包函ctx */
|
|
68
69
|
call?: (message: {
|
|
69
70
|
path: string;
|
|
70
|
-
key
|
|
71
|
+
key?: string;
|
|
72
|
+
payload?: any;
|
|
73
|
+
}, ctx?: RouteContext & {
|
|
74
|
+
[key: string]: any;
|
|
75
|
+
}) => Promise<any>;
|
|
76
|
+
/** 请求 route的返回结果,不包函ctx */
|
|
77
|
+
queryRoute?: (message: {
|
|
78
|
+
path: string;
|
|
79
|
+
key?: string;
|
|
71
80
|
payload?: any;
|
|
72
81
|
}, ctx?: RouteContext & {
|
|
73
82
|
[key: string]: any;
|
|
@@ -225,13 +234,36 @@ declare class QueryRouter {
|
|
|
225
234
|
}, ctx?: RouteContext & {
|
|
226
235
|
[key: string]: any;
|
|
227
236
|
}): Promise<any>;
|
|
237
|
+
/**
|
|
238
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
239
|
+
* @param message
|
|
240
|
+
* @param ctx
|
|
241
|
+
* @returns
|
|
242
|
+
*/
|
|
228
243
|
call(message: {
|
|
229
244
|
path: string;
|
|
230
|
-
key
|
|
245
|
+
key?: string;
|
|
231
246
|
payload?: any;
|
|
232
247
|
}, ctx?: RouteContext & {
|
|
233
248
|
[key: string]: any;
|
|
234
249
|
}): Promise<any>;
|
|
250
|
+
/**
|
|
251
|
+
* 请求 result 的数据
|
|
252
|
+
* @param message
|
|
253
|
+
* @param ctx
|
|
254
|
+
* @returns
|
|
255
|
+
*/
|
|
256
|
+
queryRoute(message: {
|
|
257
|
+
path: string;
|
|
258
|
+
key?: string;
|
|
259
|
+
payload?: any;
|
|
260
|
+
}, ctx?: RouteContext & {
|
|
261
|
+
[key: string]: any;
|
|
262
|
+
}): Promise<{
|
|
263
|
+
code: any;
|
|
264
|
+
data: any;
|
|
265
|
+
message: any;
|
|
266
|
+
}>;
|
|
235
267
|
setContext(ctx: RouteContext): Promise<void>;
|
|
236
268
|
getList(): RouteInfo[];
|
|
237
269
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
@@ -284,16 +316,14 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
284
316
|
route(path: string, key?: string): Route;
|
|
285
317
|
route(path: string, opts?: RouteOpts): Route;
|
|
286
318
|
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
[key: string]: any;
|
|
293
|
-
}): Promise<any>;
|
|
319
|
+
/**
|
|
320
|
+
* 等于queryRoute,但是调用了handle
|
|
321
|
+
* @param param0
|
|
322
|
+
* @returns
|
|
323
|
+
*/
|
|
294
324
|
run({ path, key, payload }: {
|
|
295
325
|
path: string;
|
|
296
|
-
key
|
|
326
|
+
key?: string;
|
|
297
327
|
payload?: any;
|
|
298
328
|
}): Promise<any>;
|
|
299
329
|
}
|
package/dist/router-browser.js
CHANGED
|
@@ -5745,10 +5745,11 @@ class QueryRouter {
|
|
|
5745
5745
|
catch (e) {
|
|
5746
5746
|
if (route?.isDebug) {
|
|
5747
5747
|
console.error('=====debug====:middlerware error');
|
|
5748
|
+
console.error('=====debug====:', e);
|
|
5748
5749
|
console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
|
|
5749
5750
|
console.error('=====debug====:', e.message);
|
|
5750
5751
|
}
|
|
5751
|
-
if (e instanceof CustomError) {
|
|
5752
|
+
if (e instanceof CustomError || e?.code) {
|
|
5752
5753
|
ctx.code = e.code;
|
|
5753
5754
|
ctx.message = e.message;
|
|
5754
5755
|
ctx.body = null;
|
|
@@ -5864,12 +5865,33 @@ class QueryRouter {
|
|
|
5864
5865
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
|
5865
5866
|
ctx.queryRouter = this;
|
|
5866
5867
|
ctx.call = this.call.bind(this);
|
|
5868
|
+
ctx.queryRoute = this.queryRoute.bind(this);
|
|
5867
5869
|
ctx.index = 0;
|
|
5868
5870
|
return await this.runRoute(path, key, ctx);
|
|
5869
5871
|
}
|
|
5872
|
+
/**
|
|
5873
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
5874
|
+
* @param message
|
|
5875
|
+
* @param ctx
|
|
5876
|
+
* @returns
|
|
5877
|
+
*/
|
|
5870
5878
|
async call(message, ctx) {
|
|
5871
5879
|
return await this.parse(message, { ...this.context, ...ctx });
|
|
5872
5880
|
}
|
|
5881
|
+
/**
|
|
5882
|
+
* 请求 result 的数据
|
|
5883
|
+
* @param message
|
|
5884
|
+
* @param ctx
|
|
5885
|
+
* @returns
|
|
5886
|
+
*/
|
|
5887
|
+
async queryRoute(message, ctx) {
|
|
5888
|
+
const res = await this.parse(message, { ...this.context, ...ctx });
|
|
5889
|
+
return {
|
|
5890
|
+
code: res.code,
|
|
5891
|
+
data: res.body,
|
|
5892
|
+
message: res.message,
|
|
5893
|
+
};
|
|
5894
|
+
}
|
|
5873
5895
|
async setContext(ctx) {
|
|
5874
5896
|
this.context = ctx;
|
|
5875
5897
|
}
|
|
@@ -5946,9 +5968,11 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5946
5968
|
}
|
|
5947
5969
|
return new Route(path, key, opts);
|
|
5948
5970
|
}
|
|
5949
|
-
|
|
5950
|
-
|
|
5951
|
-
|
|
5971
|
+
/**
|
|
5972
|
+
* 等于queryRoute,但是调用了handle
|
|
5973
|
+
* @param param0
|
|
5974
|
+
* @returns
|
|
5975
|
+
*/
|
|
5952
5976
|
async run({ path, key, payload }) {
|
|
5953
5977
|
const handle = this.handle;
|
|
5954
5978
|
const resultError = (error, code = 500) => {
|
package/dist/router-simple.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ declare class SimpleRouter {
|
|
|
19
19
|
use(method: string, route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
|
|
20
20
|
get(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
|
|
21
21
|
post(route: string, ...fns: Array<(req: Req, res: ServerResponse) => Promise<void> | void>): this;
|
|
22
|
-
parse(req: Req, res: ServerResponse): Promise<void
|
|
22
|
+
parse(req: Req, res: ServerResponse): Promise<void> | "not_found";
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export { SimpleRouter };
|
package/dist/router-simple.js
CHANGED
|
@@ -450,13 +450,7 @@ class SimpleRouter {
|
|
|
450
450
|
const { handlers } = route;
|
|
451
451
|
return handlers.reduce((promiseChain, handler) => promiseChain.then(() => Promise.resolve(handler(req, res))), Promise.resolve());
|
|
452
452
|
}
|
|
453
|
-
|
|
454
|
-
res.statusCode = 404;
|
|
455
|
-
res.end('Not Found');
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
console.error('Not Found');
|
|
459
|
-
}
|
|
453
|
+
return 'not_found';
|
|
460
454
|
}
|
|
461
455
|
}
|
|
462
456
|
|
package/dist/router.d.ts
CHANGED
|
@@ -68,9 +68,18 @@ type RouteContext<T = {
|
|
|
68
68
|
end?: boolean;
|
|
69
69
|
queryRouter?: QueryRouter;
|
|
70
70
|
error?: any;
|
|
71
|
+
/** 请求 route的返回结果,包函ctx */
|
|
71
72
|
call?: (message: {
|
|
72
73
|
path: string;
|
|
73
|
-
key
|
|
74
|
+
key?: string;
|
|
75
|
+
payload?: any;
|
|
76
|
+
}, ctx?: RouteContext & {
|
|
77
|
+
[key: string]: any;
|
|
78
|
+
}) => Promise<any>;
|
|
79
|
+
/** 请求 route的返回结果,不包函ctx */
|
|
80
|
+
queryRoute?: (message: {
|
|
81
|
+
path: string;
|
|
82
|
+
key?: string;
|
|
74
83
|
payload?: any;
|
|
75
84
|
}, ctx?: RouteContext & {
|
|
76
85
|
[key: string]: any;
|
|
@@ -228,13 +237,36 @@ declare class QueryRouter {
|
|
|
228
237
|
}, ctx?: RouteContext & {
|
|
229
238
|
[key: string]: any;
|
|
230
239
|
}): Promise<any>;
|
|
240
|
+
/**
|
|
241
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
242
|
+
* @param message
|
|
243
|
+
* @param ctx
|
|
244
|
+
* @returns
|
|
245
|
+
*/
|
|
231
246
|
call(message: {
|
|
232
247
|
path: string;
|
|
233
|
-
key
|
|
248
|
+
key?: string;
|
|
234
249
|
payload?: any;
|
|
235
250
|
}, ctx?: RouteContext & {
|
|
236
251
|
[key: string]: any;
|
|
237
252
|
}): Promise<any>;
|
|
253
|
+
/**
|
|
254
|
+
* 请求 result 的数据
|
|
255
|
+
* @param message
|
|
256
|
+
* @param ctx
|
|
257
|
+
* @returns
|
|
258
|
+
*/
|
|
259
|
+
queryRoute(message: {
|
|
260
|
+
path: string;
|
|
261
|
+
key?: string;
|
|
262
|
+
payload?: any;
|
|
263
|
+
}, ctx?: RouteContext & {
|
|
264
|
+
[key: string]: any;
|
|
265
|
+
}): Promise<{
|
|
266
|
+
code: any;
|
|
267
|
+
data: any;
|
|
268
|
+
message: any;
|
|
269
|
+
}>;
|
|
238
270
|
setContext(ctx: RouteContext): Promise<void>;
|
|
239
271
|
getList(): RouteInfo[];
|
|
240
272
|
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
@@ -287,16 +319,14 @@ declare class QueryRouterServer extends QueryRouter {
|
|
|
287
319
|
route(path: string, key?: string): Route;
|
|
288
320
|
route(path: string, opts?: RouteOpts): Route;
|
|
289
321
|
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
[key: string]: any;
|
|
296
|
-
}): Promise<any>;
|
|
322
|
+
/**
|
|
323
|
+
* 等于queryRoute,但是调用了handle
|
|
324
|
+
* @param param0
|
|
325
|
+
* @returns
|
|
326
|
+
*/
|
|
297
327
|
run({ path, key, payload }: {
|
|
298
328
|
path: string;
|
|
299
|
-
key
|
|
329
|
+
key?: string;
|
|
300
330
|
payload?: any;
|
|
301
331
|
}): Promise<any>;
|
|
302
332
|
}
|
|
@@ -505,11 +535,18 @@ declare class App<T = {}> {
|
|
|
505
535
|
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
506
536
|
call(message: {
|
|
507
537
|
path: string;
|
|
508
|
-
key
|
|
538
|
+
key?: string;
|
|
509
539
|
payload?: any;
|
|
510
540
|
}, ctx?: RouteContext & {
|
|
511
541
|
[key: string]: any;
|
|
512
542
|
}): Promise<any>;
|
|
543
|
+
queryRoute(path: string, key?: string, payload?: any, ctx?: RouteContext & {
|
|
544
|
+
[key: string]: any;
|
|
545
|
+
}): Promise<{
|
|
546
|
+
code: any;
|
|
547
|
+
data: any;
|
|
548
|
+
message: any;
|
|
549
|
+
}>;
|
|
513
550
|
exportRoutes(): Route[];
|
|
514
551
|
importRoutes(routes: any[]): void;
|
|
515
552
|
importApp(app: App): void;
|
package/dist/router.js
CHANGED
|
@@ -5764,10 +5764,11 @@ class QueryRouter {
|
|
|
5764
5764
|
catch (e) {
|
|
5765
5765
|
if (route?.isDebug) {
|
|
5766
5766
|
console.error('=====debug====:middlerware error');
|
|
5767
|
+
console.error('=====debug====:', e);
|
|
5767
5768
|
console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
|
|
5768
5769
|
console.error('=====debug====:', e.message);
|
|
5769
5770
|
}
|
|
5770
|
-
if (e instanceof CustomError) {
|
|
5771
|
+
if (e instanceof CustomError || e?.code) {
|
|
5771
5772
|
ctx.code = e.code;
|
|
5772
5773
|
ctx.message = e.message;
|
|
5773
5774
|
ctx.body = null;
|
|
@@ -5883,12 +5884,33 @@ class QueryRouter {
|
|
|
5883
5884
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
|
5884
5885
|
ctx.queryRouter = this;
|
|
5885
5886
|
ctx.call = this.call.bind(this);
|
|
5887
|
+
ctx.queryRoute = this.queryRoute.bind(this);
|
|
5886
5888
|
ctx.index = 0;
|
|
5887
5889
|
return await this.runRoute(path, key, ctx);
|
|
5888
5890
|
}
|
|
5891
|
+
/**
|
|
5892
|
+
* 返回的数据包含所有的context的请求返回的内容,可做其他处理
|
|
5893
|
+
* @param message
|
|
5894
|
+
* @param ctx
|
|
5895
|
+
* @returns
|
|
5896
|
+
*/
|
|
5889
5897
|
async call(message, ctx) {
|
|
5890
5898
|
return await this.parse(message, { ...this.context, ...ctx });
|
|
5891
5899
|
}
|
|
5900
|
+
/**
|
|
5901
|
+
* 请求 result 的数据
|
|
5902
|
+
* @param message
|
|
5903
|
+
* @param ctx
|
|
5904
|
+
* @returns
|
|
5905
|
+
*/
|
|
5906
|
+
async queryRoute(message, ctx) {
|
|
5907
|
+
const res = await this.parse(message, { ...this.context, ...ctx });
|
|
5908
|
+
return {
|
|
5909
|
+
code: res.code,
|
|
5910
|
+
data: res.body,
|
|
5911
|
+
message: res.message,
|
|
5912
|
+
};
|
|
5913
|
+
}
|
|
5892
5914
|
async setContext(ctx) {
|
|
5893
5915
|
this.context = ctx;
|
|
5894
5916
|
}
|
|
@@ -5965,9 +5987,11 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5965
5987
|
}
|
|
5966
5988
|
return new Route(path, key, opts);
|
|
5967
5989
|
}
|
|
5968
|
-
|
|
5969
|
-
|
|
5970
|
-
|
|
5990
|
+
/**
|
|
5991
|
+
* 等于queryRoute,但是调用了handle
|
|
5992
|
+
* @param param0
|
|
5993
|
+
* @returns
|
|
5994
|
+
*/
|
|
5971
5995
|
async run({ path, key, payload }) {
|
|
5972
5996
|
const handle = this.handle;
|
|
5973
5997
|
const resultError = (error, code = 500) => {
|
|
@@ -6481,6 +6505,9 @@ class App {
|
|
|
6481
6505
|
const router = this.router;
|
|
6482
6506
|
return await router.call(message, ctx);
|
|
6483
6507
|
}
|
|
6508
|
+
async queryRoute(path, key, payload, ctx) {
|
|
6509
|
+
return await this.router.queryRoute({ path, key, payload }, ctx);
|
|
6510
|
+
}
|
|
6484
6511
|
exportRoutes() {
|
|
6485
6512
|
return this.router.exportRoutes();
|
|
6486
6513
|
}
|