Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 26x 26x 26x 3x 23x 23x 23x 23x 23x 23x 44x | /**
* Route file parser
* Core logic - no Vite dependencies
*/
import { ROUTE_FILE_PATTERN } from '../../shared/constants.js';
import { isHttpMethod } from '../../shared/utils.js';
import { normalizeRoutePath, extractParamsFromPattern, patternToRegex } from '../normalize/normalize-path.js';
import type { Route, RouteHandler } from './route-types.js';
import type { HttpMethod } from '../../shared/constants.js';
/**
* Result of parsing a route file
*/
export interface ParsedRoute {
method: HttpMethod;
pattern: string;
params: string[];
file: string;
}
/**
* Extracts route information from a file path
*
* Examples:
* - src/api/users/[id].get.ts -> { method: 'get', pattern: 'users/:id', params: ['id'] }
* - src/api/posts/[...ids].get.ts -> { method: 'get', pattern: 'posts/*ids', params: ['ids'] }
*/
export function parseRouteFile(filePath: string, baseDir: string): ParsedRoute | null {
// Remove base directory from path
const relativePath = filePath.replace(baseDir, '').replace(/^[/\\]/, '');
// Try to match route file pattern
const match = relativePath.match(ROUTE_FILE_PATTERN);
if (!match) {
return null;
}
const [, pathPart, methodPart] = match;
const method = methodPart.toLowerCase();
Iif (!isHttpMethod(method)) {
return null;
}
// Normalize path and extract parameters
const pattern = normalizeRoutePath(pathPart);
const params = extractParamsFromPattern(pattern);
return {
method,
pattern,
params,
file: filePath,
};
}
/**
* Creates a route definition from a parsed route and handler
*/
export function createRoute(parsed: ParsedRoute, handler: RouteHandler, bodyType?: string, queryType?: string): Route {
return {
pattern: parsed.pattern,
method: parsed.method,
handler,
params: parsed.params,
file: parsed.file,
regex: patternToRegex(parsed.pattern),
bodyType,
queryType,
};
}
|