Files
movie-advisor/app/api/tmdb/[...path]/route.ts
T
2024-10-28 17:20:05 +02:00

31 lines
833 B
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { MOVIE_DB_TOKEN } from '@/env';
type TMDBParams = {
params: {
path: string[];
};
};
export async function GET({ nextUrl: { searchParams } }: NextRequest, { params: { path } }: TMDBParams) {
try {
const response = await fetch(`https://api.themoviedb.org/3/${path.join('/')}?${searchParams.toString()}`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${MOVIE_DB_TOKEN}`
}
});
if (!response.ok) {
return NextResponse.json({ error: 'Failed to fetch data from TMDb' }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
return NextResponse.json({ error: 'An error occurred' }, { status: 500 });
}
}