feat: 接口统一优化
This commit is contained in:
@@ -23,18 +23,18 @@ export const publicApiRoutes: ApiRouteRule[] = [
|
||||
];
|
||||
|
||||
/** 路径匹配:支持精确匹配、`/**` 前缀通配和 RegExp */
|
||||
function matchPath(rulePath: string | RegExp, pathname: string): boolean {
|
||||
const matchPath = (rulePath: string | RegExp, pathname: string): boolean => {
|
||||
if (rulePath instanceof RegExp) return rulePath.test(pathname);
|
||||
if (rulePath.endsWith("/**")) {
|
||||
const prefix = rulePath.slice(0, -3);
|
||||
return pathname === prefix || pathname.startsWith(`${prefix}/`);
|
||||
}
|
||||
return pathname === rulePath;
|
||||
}
|
||||
};
|
||||
|
||||
/** 判断当前请求是否命中公开路由规则 */
|
||||
export function isPublicApiRoute(pathname: string, method: string): boolean {
|
||||
return publicApiRoutes.some((rule) => {
|
||||
export const isPublicApiRoute = (pathname: string, method: string): boolean =>
|
||||
publicApiRoutes.some((rule) => {
|
||||
const methods = Array.isArray(rule.method)
|
||||
? rule.method
|
||||
: rule.method
|
||||
@@ -43,4 +43,3 @@ export function isPublicApiRoute(pathname: string, method: string): boolean {
|
||||
const methodMatched = !methods || methods.includes(method as HttpMethod);
|
||||
return methodMatched && matchPath(rule.path, pathname);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user