@kevisual/router 0.1.4 → 0.1.6

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/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.1.4",
4
+ "version": "0.1.6",
5
5
  "description": "",
6
6
  "type": "module",
7
7
  "main": "./dist/router.js",
package/src/route.ts CHANGED
@@ -434,7 +434,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> implements throw
434
434
  console.error('=====debug====:', e);
435
435
  console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
436
436
  }
437
- if (e instanceof CustomError) {
437
+ if (e instanceof CustomError || e?.code) {
438
438
  ctx.code = e.code;
439
439
  ctx.message = e.message;
440
440
  } else {
@@ -782,6 +782,45 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
782
782
  const { path, key, id } = api as any;
783
783
  return this.run({ path, key, id, payload }, ctx);
784
784
  }
785
+ /**
786
+ * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件
787
+ * @param fun 认证函数,接收 RouteContext 和认证类型
788
+ */
789
+ async createAuth(fun: (ctx: RouteContext<C>, type?: 'auth' | 'auth-admin' | 'auth-can') => any) {
790
+ this.route({
791
+ path: 'auth',
792
+ key: 'auth',
793
+ id: 'auth',
794
+ description: 'token验证',
795
+ }).define(async (ctx) => {
796
+ if (fun) {
797
+ await fun(ctx, 'auth');
798
+ }
799
+ }).addTo(this, { overwrite: false });
800
+
801
+ this.route({
802
+ path: 'auth-admin',
803
+ key: 'auth-admin',
804
+ id: 'auth-admin',
805
+ description: 'admin token验证',
806
+ middleware: ['auth']
807
+ }).define(async (ctx) => {
808
+ if (fun) {
809
+ await fun(ctx, 'auth-admin');
810
+ }
811
+ }).addTo(this, { overwrite: false });
812
+
813
+ this.route({
814
+ path: 'auth-can',
815
+ key: 'auth-can',
816
+ id: 'auth-can',
817
+ description: '权限验证'
818
+ }).define(async (ctx) => {
819
+ if (fun) {
820
+ await fun(ctx, 'auth-can');
821
+ }
822
+ }).addTo(this, { overwrite: false });
823
+ }
785
824
  }
786
825
 
787
826