From 3ce92c8ffcc36e10ec9c749bc9bef1ba900c8f99 Mon Sep 17 00:00:00 2001 From: xiongxiao Date: Wed, 18 Mar 2026 22:52:46 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E8=87=B3=200.1.6=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=AE=A4=E8=AF=81=E7=9B=B8=E5=85=B3=E4=B8=AD=E9=97=B4=E4=BB=B6?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/route.ts | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 2660705..eade401 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "$schema": "https://json.schemastore.org/package", "name": "@kevisual/router", - "version": "0.1.5", + "version": "0.1.6", "description": "", "type": "module", "main": "./dist/router.js", diff --git a/src/route.ts b/src/route.ts index a222e3b..47ad4e7 100644 --- a/src/route.ts +++ b/src/route.ts @@ -434,7 +434,7 @@ export class QueryRouter implements throw console.error('=====debug====:', e); console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`); } - if (e instanceof CustomError) { + if (e instanceof CustomError || e?.code) { ctx.code = e.code; ctx.message = e.message; } else { @@ -782,6 +782,45 @@ export class QueryRouterServer extends Qu const { path, key, id } = api as any; return this.run({ path, key, id, payload }, ctx); } + /** + * 创建认证相关的中间件,默认是 auth, auth-admin, auth-can 三个中间件 + * @param fun 认证函数,接收 RouteContext 和认证类型 + */ + async createAuth(fun: (ctx: RouteContext, type?: 'auth' | 'auth-admin' | 'auth-can') => any) { + this.route({ + path: 'auth', + key: 'auth', + id: 'auth', + description: 'token验证', + }).define(async (ctx) => { + if (fun) { + await fun(ctx, 'auth'); + } + }).addTo(this, { overwrite: false }); + + this.route({ + path: 'auth-admin', + key: 'auth-admin', + id: 'auth-admin', + description: 'admin token验证', + middleware: ['auth'] + }).define(async (ctx) => { + if (fun) { + await fun(ctx, 'auth-admin'); + } + }).addTo(this, { overwrite: false }); + + this.route({ + path: 'auth-can', + key: 'auth-can', + id: 'auth-can', + description: '权限验证' + }).define(async (ctx) => { + if (fun) { + await fun(ctx, 'auth-can'); + } + }).addTo(this, { overwrite: false }); + } }