chore: 更新版本号至 0.1.6,并添加认证相关中间件功能

This commit is contained in:
xiongxiao
2026-03-18 22:52:46 +08:00
committed by cnb
parent e0fe1bb476
commit 3ce92c8ffc
2 changed files with 41 additions and 2 deletions

View File

@@ -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",

View File

@@ -434,7 +434,7 @@ export class QueryRouter<T extends SimpleObject = SimpleObject> 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<C extends SimpleObject = SimpleObject> 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<C>, 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 });
}
}