File size: 3,653 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
 * Client-safe utilities for route matching that don't import server-side
 * utilities to avoid bundling issues with Turbopack
 */

import type {
  Key,
  TokensToRegexpOptions,
  ParseOptions,
  TokensToFunctionOptions,
} from 'next/dist/compiled/path-to-regexp'
import {
  pathToRegexp,
  compile,
  regexpToFunction,
} from 'next/dist/compiled/path-to-regexp'
import {
  hasAdjacentParameterIssues,
  normalizeAdjacentParameters,
  stripParameterSeparators,
} from '../../../../lib/route-pattern-normalizer'

/**
 * Client-safe wrapper around pathToRegexp that handles path-to-regexp 6.3.0+ validation errors.
 * This includes both "Can not repeat without prefix/suffix" and "Must have text between parameters" errors.
 */
export function safePathToRegexp(
  route: string | RegExp | Array<string | RegExp>,
  keys?: Key[],
  options?: TokensToRegexpOptions & ParseOptions
): RegExp {
  if (typeof route !== 'string') {
    return pathToRegexp(route, keys, options)
  }

  // Check if normalization is needed and cache the result
  const needsNormalization = hasAdjacentParameterIssues(route)
  const routeToUse = needsNormalization
    ? normalizeAdjacentParameters(route)
    : route

  try {
    return pathToRegexp(routeToUse, keys, options)
  } catch (error) {
    // Only try normalization if we haven't already normalized
    if (!needsNormalization) {
      try {
        const normalizedRoute = normalizeAdjacentParameters(route)
        return pathToRegexp(normalizedRoute, keys, options)
      } catch (retryError) {
        // If that doesn't work, fall back to original error
        throw error
      }
    }
    throw error
  }
}

/**
 * Client-safe wrapper around compile that handles path-to-regexp 6.3.0+ validation errors.
 * No server-side error reporting to avoid bundling issues.
 */
export function safeCompile(
  route: string,
  options?: TokensToFunctionOptions & ParseOptions
) {
  // Check if normalization is needed and cache the result
  const needsNormalization = hasAdjacentParameterIssues(route)
  const routeToUse = needsNormalization
    ? normalizeAdjacentParameters(route)
    : route

  try {
    return compile(routeToUse, options)
  } catch (error) {
    // Only try normalization if we haven't already normalized
    if (!needsNormalization) {
      try {
        const normalizedRoute = normalizeAdjacentParameters(route)
        return compile(normalizedRoute, options)
      } catch (retryError) {
        // If that doesn't work, fall back to original error
        throw error
      }
    }
    throw error
  }
}

/**
 * Client-safe wrapper around regexpToFunction that automatically cleans parameters.
 */
export function safeRegexpToFunction<
  T extends Record<string, any> = Record<string, any>,
>(regexp: RegExp, keys?: Key[]): (pathname: string) => { params: T } | false {
  const originalMatcher = regexpToFunction<T>(regexp, keys || [])

  return (pathname: string) => {
    const result = originalMatcher(pathname)
    if (!result) return false

    // Clean parameters before returning
    return {
      ...result,
      params: stripParameterSeparators(result.params as any) as T,
    }
  }
}

/**
 * Safe wrapper for route matcher functions that automatically cleans parameters.
 * This is client-safe and doesn't import path-to-regexp.
 */
export function safeRouteMatcher<T extends Record<string, any>>(
  matcherFn: (pathname: string) => false | T
): (pathname: string) => false | T {
  return (pathname: string) => {
    const result = matcherFn(pathname)
    if (!result) return false

    // Clean parameters before returning
    return stripParameterSeparators(result) as T
  }
}