UNPKG

next

Version:

The React Framework

107 lines (106 loc) 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { PARAM_SEPARATOR: null, hasAdjacentParameterIssues: null, normalizeAdjacentParameters: null, normalizeTokensForRegexp: null, stripNormalizedSeparators: null, stripParameterSeparators: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { PARAM_SEPARATOR: function() { return PARAM_SEPARATOR; }, hasAdjacentParameterIssues: function() { return hasAdjacentParameterIssues; }, normalizeAdjacentParameters: function() { return normalizeAdjacentParameters; }, normalizeTokensForRegexp: function() { return normalizeTokensForRegexp; }, stripNormalizedSeparators: function() { return stripNormalizedSeparators; }, stripParameterSeparators: function() { return stripParameterSeparators; } }); const PARAM_SEPARATOR = '_NEXTSEP_'; function hasAdjacentParameterIssues(route) { if (typeof route !== 'string') return false; // Check for interception route markers followed immediately by parameters // Pattern: /(.):param, /(..):param, /(...):param, /(.)(.):param etc. // These patterns cause "Must have text between two parameters" errors if (/\/\(\.{1,3}\):[^/\s]+/.test(route)) { return true; } // Check for basic adjacent parameters without separators // Pattern: :param1:param2 (but not :param* or other URL patterns) if (/:[a-zA-Z_][a-zA-Z0-9_]*:[a-zA-Z_][a-zA-Z0-9_]*/.test(route)) { return true; } return false; } function normalizeAdjacentParameters(route) { let normalized = route; // Handle interception route patterns: (.):param -> (.)_NEXTSEP_:param normalized = normalized.replace(/(\([^)]*\)):([^/\s]+)/g, `$1${PARAM_SEPARATOR}:$2`); // Handle other adjacent parameter patterns: :param1:param2 -> :param1_NEXTSEP_:param2 normalized = normalized.replace(/:([^:/\s)]+)(?=:)/g, `:$1${PARAM_SEPARATOR}`); return normalized; } function normalizeTokensForRegexp(tokens) { return tokens.map((token)=>{ // Token union type: Token = string | TokenObject // Literal path segments are strings, parameters/wildcards are objects if (typeof token === 'object' && token !== null && // Not all token objects have 'modifier' property (e.g., simple text tokens) 'modifier' in token && // Only repeating modifiers (* or +) cause the validation error // Other modifiers like '?' (optional) are fine (token.modifier === '*' || token.modifier === '+') && // Token objects can have different shapes depending on route pattern 'prefix' in token && 'suffix' in token && // Both prefix and suffix must be empty strings // This is what causes the validation error in path-to-regexp token.prefix === '' && token.suffix === '') { // Add minimal prefix to satisfy path-to-regexp validation // We use '/' as it's the most common path delimiter and won't break route matching // The prefix gets used in regex generation but doesn't affect parameter extraction return { ...token, prefix: '/' }; } return token; }); } function stripNormalizedSeparators(pathname) { // Remove separator after interception route markers // Pattern: (.)_NEXTSEP_ -> (.), (..)_NEXTSEP_ -> (..), etc. // The separator appears after the closing paren of interception markers return pathname.replace(new RegExp(`\\)${PARAM_SEPARATOR}`, 'g'), ')'); } function stripParameterSeparators(params) { const cleaned = {}; for (const [key, value] of Object.entries(params)){ if (typeof value === 'string') { // Remove the separator if it appears at the start of parameter values cleaned[key] = value.replace(new RegExp(`^${PARAM_SEPARATOR}`), ''); } else if (Array.isArray(value)) { // Handle array parameters (from repeated route segments) cleaned[key] = value.map((item)=>typeof item === 'string' ? item.replace(new RegExp(`^${PARAM_SEPARATOR}`), '') : item); } else { cleaned[key] = value; } } return cleaned; } //# sourceMappingURL=route-pattern-normalizer.js.map