@izzyjs/route
Version:
Use your AdonisJs routes in your Inertia.js application
65 lines (64 loc) • 2.09 kB
JavaScript
/**
* Check if a route name matches a pattern
* Supports wildcards like 'admin.*', 'posts.*'
*/
function matchesPattern(routeName, pattern) {
// Convert wildcard pattern to regex
const regexPattern = pattern
.replace(/\./g, '\\.') // Escape dots
.replace(/\*/g, '.*'); // Convert * to .*
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(routeName);
}
/**
* Check if a route should be included based on 'only' filter
*/
function shouldIncludeByOnly(routeName, onlyPatterns) {
return onlyPatterns.some((pattern) => matchesPattern(routeName, pattern));
}
/**
* Check if a route should be excluded based on 'except' filter
*/
function shouldExcludeByExcept(routeName, exceptPatterns) {
return exceptPatterns.some((pattern) => matchesPattern(routeName, pattern));
}
/**
* Filter routes based on configuration
*/
export function filterRoutes(routes, config) {
if (!config || (!config.only && !config.except)) {
return routes;
}
// If both 'only' and 'except' are set, return all routes (disable filtering)
if (config.only && config.except) {
console.warn('Both "only" and "except" filters are set. Route filtering is disabled.');
return routes;
}
return routes.filter((route) => {
// Apply 'only' filter
if (config.only) {
return shouldIncludeByOnly(route.name, config.only);
}
// Apply 'except' filter
if (config.except) {
return !shouldExcludeByExcept(route.name, config.except);
}
return true;
});
}
/**
* Get routes for a specific group
*/
export function getRoutesForGroup(routes, groupName, config) {
if (!config?.groups || !config.groups[groupName]) {
return [];
}
const groupPatterns = config.groups[groupName];
return routes.filter((route) => groupPatterns.some((pattern) => matchesPattern(route.name, pattern)));
}
/**
* Get available group names
*/
export function getAvailableGroups(config) {
return config?.groups ? Object.keys(config.groups) : [];
}