MotorSignal-App/website/app/api/cms/[slug]/route.ts

45 lines
1.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
interface RouteContext {
params: Promise<{
slug: string;
}>;
}
export async function GET(request: NextRequest, context: RouteContext) {
const { slug } = await context.params;
const baseUrl = process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
try {
const response = await fetch(`${baseUrl}/api/v1/cms/documents/${encodeURIComponent(slug)}`, {
method: 'GET',
headers: {
accept: 'application/json',
},
next: { revalidate: 60, tags: ['cms', `cms-${slug}`] },
});
if (response.status === 404) {
return NextResponse.json({ detail: 'Document not found.' }, { status: 404 });
}
if (!response.ok) {
return NextResponse.json(
{ detail: 'CMS content temporarily unavailable.' },
{ status: response.status >= 500 ? response.status : 503 }
);
}
const data = await response.json();
return NextResponse.json(data, {
headers: {
'Cache-Control': 'public, s-maxage=60, stale-while-revalidate=300',
},
});
} catch {
return NextResponse.json(
{ detail: 'Unable to connect to CMS service.' },
{ status: 503 }
);
}
}