@kevisual/router 0.0.15 → 0.0.16

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.
@@ -110,6 +110,11 @@ type RouteContext<T = {
110
110
  } & T;
111
111
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
112
112
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
113
+ type RouteMiddleware = {
114
+ path: string;
115
+ key?: string;
116
+ id?: string;
117
+ } | string;
113
118
  type RouteOpts = {
114
119
  path?: string;
115
120
  key?: string;
@@ -120,7 +125,7 @@ type RouteOpts = {
120
125
  metadata?: {
121
126
  [key: string]: any;
122
127
  };
123
- middleware?: Route[] | string[];
128
+ middleware?: RouteMiddleware[];
124
129
  type?: 'route' | 'middleware';
125
130
  /**
126
131
  * validator: {
@@ -171,7 +176,7 @@ declare class Route<U = {
171
176
  metadata?: {
172
177
  [key: string]: any;
173
178
  };
174
- middleware?: (Route | string)[];
179
+ middleware?: RouteMiddleware[];
175
180
  type?: string;
176
181
  private _validator?;
177
182
  schema?: {
@@ -5911,7 +5911,15 @@ class QueryRouter {
5911
5911
  route = this.routes.find((r) => r.id === item);
5912
5912
  }
5913
5913
  else {
5914
- route = this.routes.find((r) => r.path === item.path && r.key === item.key);
5914
+ route = this.routes.find((r) => {
5915
+ if (item.id) {
5916
+ return r.id === item.id;
5917
+ }
5918
+ else {
5919
+ // key 可以是空,所以可以不严格验证
5920
+ return r.path === item.path && r.key == item.key;
5921
+ }
5922
+ });
5915
5923
  }
5916
5924
  if (!route) {
5917
5925
  if (isString) {
@@ -1,4 +1,4 @@
1
- import * as zod from 'zod';
1
+ import * as node_modules_zod_lib_types_js from 'node_modules/zod/lib/types.js';
2
2
  import { Schema } from 'zod';
3
3
 
4
4
  type BaseRule = {
@@ -107,6 +107,11 @@ type RouteContext<T = {
107
107
  } & T;
108
108
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
109
109
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
110
+ type RouteMiddleware = {
111
+ path: string;
112
+ key?: string;
113
+ id?: string;
114
+ } | string;
110
115
  type RouteOpts = {
111
116
  path?: string;
112
117
  key?: string;
@@ -117,7 +122,7 @@ type RouteOpts = {
117
122
  metadata?: {
118
123
  [key: string]: any;
119
124
  };
120
- middleware?: Route[] | string[];
125
+ middleware?: RouteMiddleware[];
121
126
  type?: 'route' | 'middleware';
122
127
  /**
123
128
  * validator: {
@@ -168,7 +173,7 @@ declare class Route<U = {
168
173
  metadata?: {
169
174
  [key: string]: any;
170
175
  };
171
- middleware?: (Route | string)[];
176
+ middleware?: RouteMiddleware[];
172
177
  type?: string;
173
178
  private _validator?;
174
179
  schema?: {
@@ -410,7 +415,7 @@ declare class Chain {
410
415
  [key: string]: any;
411
416
  }): this;
412
417
  setPath(path: string): this;
413
- setMiddleware(middleware: string[] | Route[]): this;
418
+ setMiddleware(middleware: RouteMiddleware[]): this;
414
419
  setKey(key: string): this;
415
420
  setId(key: string): this;
416
421
  setRun(run: Run): this;
@@ -438,13 +443,13 @@ declare class QueryUtil<T extends RouteObject = RouteObject> {
438
443
  metadata?: {
439
444
  [key: string]: any;
440
445
  };
441
- middleware?: Route[] | string[];
446
+ middleware?: RouteMiddleware[];
442
447
  type?: "route" | "middleware";
443
448
  validator?: {
444
449
  [key: string]: Rule;
445
450
  };
446
451
  schema?: {
447
- [key: string]: zod.ZodType<any>;
452
+ [key: string]: node_modules_zod_lib_types_js.ZodType<any>;
448
453
  };
449
454
  isVerify?: boolean;
450
455
  verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
package/dist/router.d.ts CHANGED
@@ -113,6 +113,11 @@ type RouteContext<T = {
113
113
  } & T;
114
114
  type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
115
115
  type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
116
+ type RouteMiddleware = {
117
+ path: string;
118
+ key?: string;
119
+ id?: string;
120
+ } | string;
116
121
  type RouteOpts = {
117
122
  path?: string;
118
123
  key?: string;
@@ -123,7 +128,7 @@ type RouteOpts = {
123
128
  metadata?: {
124
129
  [key: string]: any;
125
130
  };
126
- middleware?: Route[] | string[];
131
+ middleware?: RouteMiddleware[];
127
132
  type?: 'route' | 'middleware';
128
133
  /**
129
134
  * validator: {
@@ -174,7 +179,7 @@ declare class Route<U = {
174
179
  metadata?: {
175
180
  [key: string]: any;
176
181
  };
177
- middleware?: (Route | string)[];
182
+ middleware?: RouteMiddleware[];
178
183
  type?: string;
179
184
  private _validator?;
180
185
  schema?: {
@@ -643,4 +648,4 @@ declare class App<T = {}, U = AppReqRes> {
643
648
  }
644
649
 
645
650
  export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, Server, createSchema, handleServer };
646
- export type { RouteContext, RouteOpts, Rule, Run };
651
+ export type { RouteContext, RouteMiddleware, RouteOpts, Rule, Run };
package/dist/router.js CHANGED
@@ -5933,7 +5933,15 @@ class QueryRouter {
5933
5933
  route = this.routes.find((r) => r.id === item);
5934
5934
  }
5935
5935
  else {
5936
- route = this.routes.find((r) => r.path === item.path && r.key === item.key);
5936
+ route = this.routes.find((r) => {
5937
+ if (item.id) {
5938
+ return r.id === item.id;
5939
+ }
5940
+ else {
5941
+ // key 可以是空,所以可以不严格验证
5942
+ return r.path === item.path && r.key == item.key;
5943
+ }
5944
+ });
5937
5945
  }
5938
5946
  if (!route) {
5939
5947
  if (isString) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@kevisual/router",
4
- "version": "0.0.15",
4
+ "version": "0.0.16",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
package/src/index.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { Route, QueryRouter, QueryRouterServer } from './route.ts';
2
2
  export { Connect, QueryConnect } from './connect.ts';
3
3
 
4
- export type { RouteContext, RouteOpts } from './route.ts';
4
+ export type { RouteContext, RouteOpts, RouteMiddleware } from './route.ts';
5
5
 
6
6
  export type { Run } from './route.ts';
7
7
 
package/src/route.ts CHANGED
@@ -61,6 +61,13 @@ export type RouteContext<T = { code?: number }, S = any> = {
61
61
  export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
62
62
 
63
63
  export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
64
+ export type RouteMiddleware =
65
+ | {
66
+ path: string;
67
+ key?: string;
68
+ id?: string;
69
+ }
70
+ | string;
64
71
  export type RouteOpts = {
65
72
  path?: string;
66
73
  key?: string;
@@ -69,7 +76,7 @@ export type RouteOpts = {
69
76
  nextRoute?: NextRoute; // route to run after this route
70
77
  description?: string;
71
78
  metadata?: { [key: string]: any };
72
- middleware?: Route[] | string[]; // middleware
79
+ middleware?: RouteMiddleware[]; // middleware
73
80
  type?: 'route' | 'middleware';
74
81
  /**
75
82
  * validator: {
@@ -112,7 +119,7 @@ export class Route<U = { [key: string]: any }> {
112
119
  nextRoute?: NextRoute; // route to run after this route
113
120
  description?: string;
114
121
  metadata?: { [key: string]: any };
115
- middleware?: (Route | string)[]; // middleware
122
+ middleware?: RouteMiddleware[]; // middleware
116
123
  type? = 'route';
117
124
  private _validator?: { [key: string]: Rule };
118
125
  schema?: { [key: string]: Schema<any> };
@@ -379,7 +386,14 @@ export class QueryRouter {
379
386
  if (isString) {
380
387
  route = this.routes.find((r) => r.id === item);
381
388
  } else {
382
- route = this.routes.find((r) => r.path === item.path && r.key === item.key);
389
+ route = this.routes.find((r) => {
390
+ if (item.id) {
391
+ return r.id === item.id;
392
+ } else {
393
+ // key 可以是空,所以可以不严格验证
394
+ return r.path === item.path && r.key == item.key;
395
+ }
396
+ });
383
397
  }
384
398
  if (!route) {
385
399
  if (isString) {
@@ -1,4 +1,5 @@
1
- import type { QueryRouterServer, Route, RouteOpts, Run } from './route.ts';
1
+ // import type { QueryRouterServer, Route, RouteOpts, Run } from '@kevisual/router';
2
+ import type { QueryRouterServer, RouteOpts, Run, RouteMiddleware } from './index.ts';
2
3
 
3
4
  // export type RouteObject<T extends readonly string[]> = {
4
5
  // [K in T[number]]: RouteOpts;
@@ -45,7 +46,7 @@ class Chain {
45
46
  this.object.path = path;
46
47
  return this;
47
48
  }
48
- setMiddleware(middleware: string[] | Route[]) {
49
+ setMiddleware(middleware: RouteMiddleware[]) {
49
50
  this.object.middleware = middleware;
50
51
  return this;
51
52
  }
@@ -0,0 +1,10 @@
1
+ import { App } from '@/app.ts';
2
+ import { QueryUtil } from '@/router-define.ts';
3
+ const v = QueryUtil.create({
4
+ a: {
5
+ path: 'a',
6
+ key: 'b',
7
+ },
8
+ });
9
+ const app = new App();
10
+ app.route(v.get('a'));