UNPKG

@izzyjs/route

Version:

Use your AdonisJs routes in your Inertia.js application

50 lines (49 loc) 1.98 kB
/** * @izzyjs/route * * (c) IzzyJs - 2024 * For the full license information, please view the LICENSE file that was distributed with this source code. */ export function serializeRoute(route, domain = 'root') { const methods = route.methods.filter((method) => method !== 'HEAD').at(0); const allParams = route.pattern.match(/:\w+\??/g)?.map((param) => param.slice(1)); if (allParams) { const requiredParams = []; const optionalParams = []; // First, find all optional parameters (ending with ?) const optionalParamPattern = /:\w+\?/g; const optionalMatches = route.pattern.match(optionalParamPattern); if (optionalMatches) { optionalParams.push(...optionalMatches.map((param) => param.slice(1, -1))); // Remove : and ? } // Then, find all parameters (both required and optional) const allParamPattern = /:\w+/g; const allMatches = route.pattern.match(allParamPattern); if (allMatches) { // Filter out parameters that are optional const requiredMatches = allMatches.filter((param) => { const paramName = param.slice(1); // Remove : return !optionalParams.includes(paramName); }); requiredParams.push(...requiredMatches.map((param) => param.slice(1))); // Remove : } return { name: route.name, path: route.pattern, method: methods.toLowerCase(), params: requiredParams.length > 0 || optionalParams.length > 0 ? { required: requiredParams.length > 0 ? requiredParams : undefined, optional: optionalParams.length > 0 ? optionalParams : undefined, } : undefined, domain, }; } return { name: route.name, path: route.pattern, method: methods.toLowerCase(), domain, }; }