burger-api
Version:
<div align="center"> <a href="https://burger-api.com"> <img src="https://github.com/user-attachments/assets/0d9b376e-1d89-479a-aa7f-e7ee3c6b2342" alt="BurgerAPI"/> </a> </div>
92 lines (91 loc) • 3.29 kB
JavaScript
import * as path from 'path';
import { cleanPrefix } from './index';
import { ROUTE_CONSTANTS } from './routing';
/**
* Converts a file path to an API route path.
* @param filePath The file path to convert.
* @param prefix The prefix to prepend to the route path.
* @returns The API route path.
*/
export function filePathToApiRoutePath(filePath, prefix) {
if (filePath.endsWith('route.ts')) {
filePath = filePath.slice(0, -'route.ts'.length);
}
const segments = filePath.split(path.sep);
const resultSegments = [];
for (const segment of segments) {
if (!segment)
continue;
if (segment.startsWith(ROUTE_CONSTANTS.GROUPING_FOLDER_START) &&
segment.endsWith(ROUTE_CONSTANTS.GROUPING_FOLDER_END)) {
continue;
}
if (segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_START) &&
segment.endsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END) &&
!segment.startsWith(ROUTE_CONSTANTS.WILDCARD_START)) {
resultSegments.push(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX + segment.slice(1, -1));
}
else if (segment === ROUTE_CONSTANTS.WILDCARD_SIMPLE) {
resultSegments.push(ROUTE_CONSTANTS.WILDCARD_SEGMENT_PREFIX);
}
else {
resultSegments.push(segment);
}
}
let route = '/' + resultSegments.join('/');
const clean = cleanPrefix(prefix);
if (clean) {
route = '/' + clean + route;
}
if (route !== '/' && route.endsWith('/')) {
route = route.slice(0, -1);
}
return route;
}
/**
* Converts a file path to a page route path.
* @param filePath The file path to convert.
* @param prefix The prefix to prepend to the route path.
* @returns The page route path.
*/
export function filePathToPageRoutePath(filePath, prefix) {
const segments = filePath.split(path.sep);
const resultSegments = [];
for (const segment of segments) {
if (!segment)
continue;
if (segment.startsWith(ROUTE_CONSTANTS.GROUPING_FOLDER_START) &&
segment.endsWith(ROUTE_CONSTANTS.GROUPING_FOLDER_END)) {
continue;
}
if (segment.startsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_START) &&
segment.endsWith(ROUTE_CONSTANTS.DYNAMIC_FOLDER_END)) {
resultSegments.push(ROUTE_CONSTANTS.DYNAMIC_SEGMENT_PREFIX + segment.slice(1, -1));
}
else {
resultSegments.push(segment);
}
}
let route = '/' + resultSegments.join('/');
const clean = cleanPrefix(prefix);
if (clean) {
route = '/' + clean + route;
}
if (route !== '/' && route.endsWith('/')) {
route = route.slice(0, -1);
}
const pathSegments = route.split('/');
const lastSegment = pathSegments.at(-1);
if (typeof lastSegment === 'string') {
if (ROUTE_CONSTANTS.PAGE_INDEX_FILES.includes(lastSegment)) {
pathSegments.pop();
}
else {
const extensionIndex = lastSegment.lastIndexOf('.');
if (extensionIndex > 0) {
pathSegments[pathSegments.length - 1] = lastSegment.slice(0, extensionIndex);
}
}
}
return pathSegments.join('/') === '' ? '/' : pathSegments.join('/');
}