Files
olympics-calendar/ui/app/api/data/[[...slug]]/route.ts
Fabrice Lamant fef4690b0b add docker
2025-12-06 14:55:13 +01:00

25 lines
695 B
TypeScript

import { promises as fs } from "fs";
import { NextResponse } from "next/server";
import path from "path";
const DATA_FOLDER = path.resolve("data");
export async function GET(
request: Request,
{ params }: { params: Promise<{ slug?: string[] | undefined }> }
): Promise<NextResponse> {
try {
const { slug } = await params || [];
const filePath = slug ? path.join(DATA_FOLDER, ...slug) : null;
if (!filePath) throw new Error()
const content = await fs.readFile(filePath);
if (!content) throw new Error()
return new NextResponse(content, { status: 200 });
} catch (ex) {
console.log(ex);
return new NextResponse("File not found", { status: 404 });
}
}