Compare commits

...

1 Commits

Author SHA1 Message Date
xiongxiao
3ce92c8ffc chore: 更新版本号至 0.1.6,并添加认证相关中间件功能 2026-03-18 22:52:46 +08:00
2 changed files with 41 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
{ {
"$schema": "https://json.schemastore.org/package", "$schema": "https://json.schemastore.org/package",
"name": "@kevisual/router", "name": "@kevisual/router",
"version": "0.1.5", "version": "0.1.6",
"description": "", "description": "",
"type": "module", "type": "module",
"main": "./dist/router.js", "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====:', e);
console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`); console.error('=====debug====:[path:key]:', `${route.path}-${route.key}`);
} }
if (e instanceof CustomError) { if (e instanceof CustomError || e?.code) {
ctx.code = e.code; ctx.code = e.code;
ctx.message = e.message; ctx.message = e.message;
} else { } else {
@@ -782,6 +782,45 @@ export class QueryRouterServer<C extends SimpleObject = SimpleObject> extends Qu
const { path, key, id } = api as any; const { path, key, id } = api as any;
return this.run({ path, key, id, payload }, ctx); 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 });
}
} }