refactoring: server actions => tmdb proxy

This commit is contained in:
ner committed 2024-10-28 20:04:36 +02:00
1 parent dfdfb79169
commit 97d55b24a1
13 files changed
+131 -113

No files matched your search

+29 -3
View File
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from 'next/server';
import { MOVIE_DB_TOKEN } from '@/env';
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN } from '@/env';
type TMDBParams = {
params: {
@@ -8,9 +8,9 @@ type TMDBParams = {
};
};
export async function GET({ nextUrl: { searchParams } }: NextRequest, { params: { path } }: TMDBParams) {
export async function GET({ nextUrl: { searchParams }, headers }: NextRequest, { params: { path } }: TMDBParams) {
try {
const response = await fetch(`https://api.themoviedb.org/3/${path.join('/')}?${searchParams.toString()}`, {
const response = await fetch(`${MOVIE_DB_API_URL}/${path.join('/')}?${searchParams.toString()}`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${MOVIE_DB_TOKEN}`
@@ -28,3 +28,29 @@ export async function GET({ nextUrl: { searchParams } }: NextRequest, { params:
return NextResponse.json({ error: 'An error occurred' }, { status: 500 });
}
}
export async function POST(request: NextRequest, { params: { path } }: TMDBParams) {
try {
const requestBody = await request.json();
const searchParams = request.nextUrl.searchParams.toString();
const response = await fetch(`${MOVIE_DB_API_URL}/${path.join('/')}?${searchParams}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${MOVIE_DB_TOKEN}`
},
body: JSON.stringify(requestBody)
});
if (!response.ok) {
return NextResponse.json({ error: 'Failed to post data to TMDb' }, { status: response.status });
}
const data = await response.json();
return NextResponse.json(data);
} catch (error) {
return NextResponse.json({ error: 'An error occurred' }, { status: 500 });
}
}