UNPKG

next

Version:

The React Framework

116 lines (115 loc) 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "resolveParamValue", { enumerable: true, get: function() { return resolveParamValue; } }); const _invarianterror = require("../../invariant-error"); const _interceptionprefixfromparamtype = require("./interception-prefix-from-param-type"); /** * Extracts the param value from a path segment, handling interception markers * based on the expected param type. * * @param pathSegment - The path segment to extract the value from * @param params - The current params object for resolving dynamic param references * @param paramType - The expected param type which may include interception marker info * @returns The extracted param value */ function getParamValueFromSegment(pathSegment, params, paramType) { // If the segment is dynamic, resolve it from the params object if (pathSegment.type === 'dynamic') { return params[pathSegment.param.paramName]; } // If the paramType indicates this is an intercepted param, strip the marker // that matches the interception marker in the param type const interceptionPrefix = (0, _interceptionprefixfromparamtype.interceptionPrefixFromParamType)(paramType); if (interceptionPrefix === pathSegment.interceptionMarker) { return pathSegment.name.replace(pathSegment.interceptionMarker, ''); } // For static segments, use the name return pathSegment.name; } function resolveParamValue(paramName, paramType, depth, route, params) { switch(paramType){ case 'catchall': case 'optional-catchall': case 'catchall-intercepted-(..)(..)': case 'catchall-intercepted-(.)': case 'catchall-intercepted-(..)': case 'catchall-intercepted-(...)': // For catchall routes, derive from pathname using depth to determine // which segments to use const processedSegments = []; // Process segments to handle any embedded dynamic params for(let index = depth; index < route.segments.length; index++){ const pathSegment = route.segments[index]; if (pathSegment.type === 'static') { let value = pathSegment.name; // For intercepted catch-all params, strip the marker from the first segment const interceptionPrefix = (0, _interceptionprefixfromparamtype.interceptionPrefixFromParamType)(paramType); if (interceptionPrefix && index === depth && interceptionPrefix === pathSegment.interceptionMarker) { // Strip the interception marker from the value value = value.replace(pathSegment.interceptionMarker, ''); } processedSegments.push(value); } else { // If the segment is a param placeholder, check if we have its value if (!params.hasOwnProperty(pathSegment.param.paramName)) { // If the segment is an optional catchall, we can break out of the // loop because it's optional! if (pathSegment.param.paramType === 'optional-catchall') { break; } // Unknown param placeholder in pathname - can't derive full value return undefined; } // If the segment matches a param, use the param value // We don't encode values here as that's handled during retrieval. const paramValue = params[pathSegment.param.paramName]; if (Array.isArray(paramValue)) { processedSegments.push(...paramValue); } else { processedSegments.push(paramValue); } } } if (processedSegments.length > 0) { return processedSegments; } else if (paramType === 'optional-catchall') { return undefined; } else { // We shouldn't be able to match a catchall segment without any path // segments if it's not an optional catchall throw Object.defineProperty(new _invarianterror.InvariantError(`Unexpected empty path segments match for a route "${route.pathname}" with param "${paramName}" of type "${paramType}"`), "__NEXT_ERROR_CODE", { value: "E931", enumerable: false, configurable: true }); } case 'dynamic': case 'dynamic-intercepted-(..)(..)': case 'dynamic-intercepted-(.)': case 'dynamic-intercepted-(..)': case 'dynamic-intercepted-(...)': // For regular dynamic parameters, take the segment at this depth if (depth < route.segments.length) { const pathSegment = route.segments[depth]; // Check if the segment at this depth is a placeholder for an unknown param if (pathSegment.type === 'dynamic' && !params.hasOwnProperty(pathSegment.param.paramName)) { // The segment is a placeholder like [category] and we don't have the value return undefined; } // If the segment matches a param, use the param value from params object // Otherwise it's a static segment, just use it directly // We don't encode values here as that's handled during retrieval return getParamValueFromSegment(pathSegment, params, paramType); } return undefined; default: paramType; } } //# sourceMappingURL=resolve-param-value.js.map