import { API_ALLOWLIST, FRONTEND_PAGE_ALLOWLIST } from "common/config" import { pathToRegexp } from "common/utils/path" /** 公开 API 以只读为主,需配合服务端校验与限流 */ export function isPublicApiPath(path: string, method?: string) { if (!path.startsWith("/api/public/")) { return false; } const requestMethod = method?.toUpperCase() ?? "GET"; if (requestMethod === "GET") { return true; } return false; } /** * 检查前端页面是否在白名单中(允许未登录用户直接访问) * 已登录用户访问这些页面会被重定向 */ export function isFrontendPageAllowed(path: string): boolean { const cleanPath = path.split("?")[0]; return Array.from(FRONTEND_PAGE_ALLOWLIST).some((rule) => { const regex = pathToRegexp(rule); return regex.test(cleanPath!); }); } export function isAllowlistedApiPath(path: string, method?: string) { if (isPublicApiPath(path, method)) { return true; } const requestMethod = method?.toUpperCase() ?? "GET"; // 移除 query string const cleanPath = path.split("?")[0]; return API_ALLOWLIST.some((rule) => { const regex = pathToRegexp(rule.path); if (!regex.test(cleanPath!)) { return false; } if (!rule.methods || rule.methods.length === 0) { return true; } return rule.methods.includes(requestMethod); }); }