UNPKG

@tidecloak/nextjs

Version:
31 lines (30 loc) 1.01 kB
/** * Convert a single RoutePattern into a test function */ export function normalizePattern(pattern) { if (typeof pattern === 'function') return pattern; if (pattern instanceof RegExp) return (path) => pattern.test(path); if (pattern === 'OPTIONS') return (_path, req) => req.method === 'OPTIONS'; if (typeof pattern === 'string' && pattern.includes('*')) { // Escape regex chars, then replace '*' with '.*' const escaped = pattern .replace(/[-/\\^$+?.()|[\]{}]/g, '\\$&') .replace(/\\\*/g, '.*'); const regex = new RegExp(`^${escaped}$`); return (path) => regex.test(path); } // Simple prefix match return (path) => path.startsWith(pattern); } /** * Convert a ProtectedRoutesMap into an array of { test, roles } */ export function normalizeProtectedRoutes(map = {}) { return Object.entries(map).map(([pattern, roles]) => ({ test: normalizePattern(pattern), roles, })); }