路由匹配模式增加严格匹配选项
This commit is contained in:
@ -23,6 +23,14 @@ interface IKoaRouterTypeParser {
|
||||
|
||||
type KoaRouterDefaultTypes = "int" | "boolean";
|
||||
|
||||
/**
|
||||
* 路由注册选项
|
||||
*/
|
||||
interface IKoaRouterRegisterOptions {
|
||||
/** 是否严格匹配,默认true */
|
||||
strict?: boolean
|
||||
}
|
||||
|
||||
export interface IKoaRouterOptions {
|
||||
/** 前缀 */
|
||||
prefix?: string;
|
||||
@ -92,8 +100,23 @@ export class KoaRouter {
|
||||
* @param method 路由方法
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
* @param option 路由注册选项
|
||||
*/
|
||||
public register(method: KoaRouterMethods[], pathname: string, middleware: Middleware): void {
|
||||
public register(method: KoaRouterMethods[], pathname: string, middleware: Middleware): void
|
||||
public register(method: KoaRouterMethods[], pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public register(method: KoaRouterMethods[], pathname: string, _middleware: Middleware | IKoaRouterRegisterOptions, _option?: Middleware | IKoaRouterRegisterOptions): void {
|
||||
//参数处理
|
||||
let middleware: Middleware;
|
||||
let option: IKoaRouterRegisterOptions | undefined = undefined;
|
||||
|
||||
if (_option) {
|
||||
option = _middleware as IKoaRouterRegisterOptions;
|
||||
middleware = _option as Middleware;
|
||||
}
|
||||
else {
|
||||
middleware = _middleware as Middleware;
|
||||
}
|
||||
|
||||
//处理路径参数
|
||||
const prefix = this.#option.prefix?.replace(/\/+$/, "") ?? "";
|
||||
pathname = prefix + (pathname[0] == "/" ? pathname : `/${pathname}`);
|
||||
@ -126,8 +149,14 @@ export class KoaRouter {
|
||||
offset = match.index + match[0].length;
|
||||
}
|
||||
|
||||
//得到正则表达式
|
||||
const regexpStr = "^" + regexpItems.join("") + pathname.slice(offset) + "$";
|
||||
//基础正则表达式
|
||||
let regexpStr = "^" + regexpItems.join("") + pathname.slice(offset);
|
||||
|
||||
//严格模式
|
||||
if (option?.strict) regexpStr += "$";
|
||||
else regexpStr += "\b";
|
||||
|
||||
//生成正则表达式
|
||||
const regexp = new RegExp(regexpStr);
|
||||
|
||||
//注册路由
|
||||
@ -139,42 +168,66 @@ export class KoaRouter {
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
*/
|
||||
public get(pathname: string, middleware: Middleware) { this.register(["get"], pathname, middleware); }
|
||||
public get(pathname: string, middleware: Middleware): void
|
||||
public get(pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public get(pathname: string, middleware: Middleware | IKoaRouterRegisterOptions, option?: Middleware) {
|
||||
this.register(["get"], pathname, middleware as IKoaRouterRegisterOptions, option as Middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册POST路由
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
*/
|
||||
public post(pathname: string, middleware: Middleware) { this.register(["post"], pathname, middleware); }
|
||||
public post(pathname: string, middleware: Middleware): void
|
||||
public post(pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public post(pathname: string, middleware: Middleware | IKoaRouterRegisterOptions, option?: Middleware) {
|
||||
this.register(["post"], pathname, middleware as IKoaRouterRegisterOptions, option as Middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册PUT路由
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
*/
|
||||
public put(pathname: string, middleware: Middleware) { this.register(["put"], pathname, middleware); }
|
||||
public put(pathname: string, middleware: Middleware): void
|
||||
public put(pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public put(pathname: string, middleware: Middleware | IKoaRouterRegisterOptions, option?: Middleware) {
|
||||
this.register(["put"], pathname, middleware as IKoaRouterRegisterOptions, option as Middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册DELETE路由
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
*/
|
||||
public delete(pathname: string, middleware: Middleware) { this.register(["delete"], pathname, middleware); }
|
||||
public delete(pathname: string, middleware: Middleware): void
|
||||
public delete(pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public delete(pathname: string, middleware: Middleware | IKoaRouterRegisterOptions, option?: Middleware) {
|
||||
this.register(["delete"], pathname, middleware as IKoaRouterRegisterOptions, option as Middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册PATCH路由
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
*/
|
||||
public patch(pathname: string, middleware: Middleware) { this.register(["patch"], pathname, middleware); }
|
||||
public patch(pathname: string, middleware: Middleware): void
|
||||
public patch(pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public patch(pathname: string, middleware: Middleware | IKoaRouterRegisterOptions, option?: Middleware) {
|
||||
this.register(["patch"], pathname, middleware as IKoaRouterRegisterOptions, option as Middleware);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册所有路由方法
|
||||
* @param pathname 路由路径
|
||||
* @param middleware 路由中间件
|
||||
*/
|
||||
public all(pathname: string, middleware: Middleware) { this.register(["get", "post", "put", "delete", "patch"], pathname, middleware); }
|
||||
public all(pathname: string, middleware: Middleware): void
|
||||
public all(pathname: string, option: IKoaRouterRegisterOptions, middleware: Middleware): void
|
||||
public all(pathname: string, middleware: Middleware | IKoaRouterRegisterOptions, option?: Middleware) {
|
||||
this.register(["get", "post", "put", "delete", "patch"], pathname, middleware as IKoaRouterRegisterOptions, option as Middleware);
|
||||
}
|
||||
|
||||
/** 路由匹配中间件 */
|
||||
public callback(): Middleware {
|
||||
|
Reference in New Issue
Block a user