Files
gridpilot.gg/apps/website/lib/routing/RouteConfig.ts
2026-01-03 02:42:47 +01:00

328 lines
8.1 KiB
TypeScript

/**
* @file RouteConfig.ts
* Centralized routing configuration for clean, maintainable paths
*
* Design Principles:
* - Single source of truth for all routes
* - i18n-ready: paths can be localized
* - Type-safe: compile-time checking
* - Easy to refactor: change in one place
* - Environment-specific: can vary by mode
*/
export interface RouteDefinition {
path: string;
name: string;
description?: string;
}
export interface RouteGroup {
auth: {
login: string;
signup: string;
forgotPassword: string;
resetPassword: string;
};
public: {
home: string;
leagues: string;
drivers: string;
teams: string;
leaderboards: string;
races: string;
sponsorSignup: string;
};
protected: {
dashboard: string;
onboarding: string;
profile: string;
profileSettings: string;
profileLeagues: string;
profileLiveries: string;
profileLiveryUpload: string;
profileSponsorshipRequests: string;
};
sponsor: {
root: string;
dashboard: string;
billing: string;
campaigns: string;
leagues: string;
leagueDetail: (id: string) => string;
settings: string;
};
admin: {
root: string;
users: string;
};
league: {
detail: (id: string) => string;
rosterAdmin: (id: string) => string;
rulebook: (id: string) => string;
schedule: (id: string) => string;
scheduleAdmin: (id: string) => string;
settings: (id: string) => string;
sponsorships: (id: string) => string;
standings: (id: string) => string;
stewarding: (id: string) => string;
wallet: (id: string) => string;
create: string;
};
race: {
root: string;
all: string;
detail: (id: string) => string;
results: (id: string) => string;
stewarding: (id: string) => string;
};
team: {
root: string;
leaderboard: string;
detail: (id: string) => string;
};
driver: {
root: string;
detail: (id: string) => string;
};
error: {
notFound: string;
serverError: string;
};
}
/**
* Route configuration with i18n support
*
* Usage:
* ```typescript
* import { routes } from '@/lib/routing/RouteConfig';
*
* // Navigate to login
* router.push(routes.auth.login);
*
* // Navigate to league detail
* router.push(routes.league.detail('league-123'));
*
* // Check if current path is protected
* if (currentPath.startsWith(routes.protected.dashboard)) {
* // Handle protected route
* }
* ```
*/
export const routes: RouteGroup = {
auth: {
login: '/auth/login',
signup: '/auth/signup',
forgotPassword: '/auth/forgot-password',
resetPassword: '/auth/reset-password',
},
public: {
home: '/',
leagues: '/leagues',
drivers: '/drivers',
teams: '/teams',
leaderboards: '/leaderboards',
races: '/races',
sponsorSignup: '/sponsor/signup',
},
protected: {
dashboard: '/dashboard',
onboarding: '/onboarding',
profile: '/profile',
profileSettings: '/profile/settings',
profileLeagues: '/profile/leagues',
profileLiveries: '/profile/liveries',
profileLiveryUpload: '/profile/liveries/upload',
profileSponsorshipRequests: '/profile/sponsorship-requests',
},
sponsor: {
root: '/sponsor',
dashboard: '/sponsor/dashboard',
billing: '/sponsor/billing',
campaigns: '/sponsor/campaigns',
leagues: '/sponsor/leagues',
leagueDetail: (id: string) => `/sponsor/leagues/${id}`,
settings: '/sponsor/settings',
},
admin: {
root: '/admin',
users: '/admin/users',
},
league: {
detail: (id: string) => `/leagues/${id}`,
rosterAdmin: (id: string) => `/leagues/${id}/roster/admin`,
rulebook: (id: string) => `/leagues/${id}/rulebook`,
schedule: (id: string) => `/leagues/${id}/schedule`,
scheduleAdmin: (id: string) => `/leagues/${id}/schedule/admin`,
settings: (id: string) => `/leagues/${id}/settings`,
sponsorships: (id: string) => `/leagues/${id}/sponsorships`,
standings: (id: string) => `/leagues/${id}/standings`,
stewarding: (id: string) => `/leagues/${id}/stewarding`,
wallet: (id: string) => `/leagues/${id}/wallet`,
create: '/leagues/create',
},
race: {
root: '/races',
all: '/races/all',
detail: (id: string) => `/races/${id}`,
results: (id: string) => `/races/${id}/results`,
stewarding: (id: string) => `/races/${id}/stewarding`,
},
team: {
root: '/teams',
leaderboard: '/teams/leaderboard',
detail: (id: string) => `/teams/${id}`,
},
driver: {
root: '/drivers',
detail: (id: string) => `/drivers/${id}`,
},
error: {
notFound: '/404',
serverError: '/500',
},
};
/**
* Route matcher utilities for pattern matching
*/
export const routeMatchers = {
/**
* Check if path matches a pattern
*/
matches(path: string, pattern: string): boolean {
// Exact match
if (pattern === path) return true;
// Wildcard match (starts with)
if (pattern.endsWith('/*') && path.startsWith(pattern.slice(0, -2))) {
return true;
}
// Parameterized match (e.g., /leagues/[id])
const paramPattern = pattern.replace(/\[([^\]]+)\]/g, '([^/]+)');
const regex = new RegExp(`^${paramPattern}$`);
return regex.test(path);
},
/**
* Check if path is in a route group
*/
isInGroup(path: string, group: keyof RouteGroup): boolean {
const groupRoutes = routes[group];
// Handle nested objects (like sponsor.leagueDetail)
const values = Object.values(groupRoutes);
return values.some(value => {
if (typeof value === 'function') {
// For parameterized routes, check pattern
const pattern = value('placeholder');
return path.startsWith(pattern.replace('/placeholder', ''));
}
return path.startsWith(value as string);
});
},
/**
* Get all public route patterns
*/
getPublicPatterns(): string[] {
return [
routes.public.home,
routes.public.leagues,
routes.public.drivers,
routes.public.teams,
routes.public.leaderboards,
routes.public.races,
routes.public.sponsorSignup,
routes.auth.login,
routes.auth.signup,
routes.auth.forgotPassword,
routes.auth.resetPassword,
routes.error.notFound,
routes.error.serverError,
];
},
/**
* Check if path is public
*/
isPublic(path: string): boolean {
const publicPatterns = this.getPublicPatterns();
// Check exact matches
if (publicPatterns.includes(path)) return true;
// Check parameterized patterns
return publicPatterns.some(pattern => {
if (pattern.includes('[')) {
const paramPattern = pattern.replace(/\[([^\]]+)\]/g, '([^/]+)');
const regex = new RegExp(`^${paramPattern}$`);
return regex.test(path);
}
return false;
});
},
/**
* Check if path requires authentication
*/
requiresAuth(path: string): boolean {
return !this.isPublic(path);
},
/**
* Check if path requires specific role
*/
requiresRole(path: string): string[] | null {
if (this.isInGroup(path, 'admin')) {
return ['owner', 'admin'];
}
if (this.isInGroup(path, 'sponsor')) {
return ['sponsor'];
}
return null;
},
};
/**
* i18n-ready path builder
*
* Usage:
* ```typescript
* // With current locale
* const path = buildPath('leagueDetail', { id: '123' });
*
* // With specific locale
* const path = buildPath('leagueDetail', { id: '123' }, 'de');
* ```
*/
export function buildPath(
routeName: string,
params: Record<string, string> = {},
locale?: string
): string {
// This is a placeholder for future i18n implementation
// For now, it just builds the path using the route config
const parts = routeName.split('.');
let route: any = routes;
for (const part of parts) {
route = route[part];
if (!route) {
throw new Error(`Unknown route: ${routeName}`);
}
}
if (typeof route === 'function') {
const paramKeys = Object.keys(params);
if (paramKeys.length === 0) {
throw new Error(`Route ${routeName} requires parameters`);
}
return route(params[paramKeys[0]]);
}
return route as string;
}