21 lines
1.3 KiB
TypeScript
21 lines
1.3 KiB
TypeScript
import { cookies } from 'next/headers';
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const resources = new Set(['users', 'vehicles', 'rides']);
|
|
|
|
async function proxy(request: NextRequest, context: { params: Promise<{ path: string[] }> }) {
|
|
const { path } = await context.params;
|
|
if (!path.length || !resources.has(path[0])) return NextResponse.json({ detail: 'Unknown admin resource.' }, { status: 404 });
|
|
const token = (await cookies()).get('motorsignal_access_token')?.value;
|
|
if (!token) return NextResponse.json({ detail: 'Authentication required.' }, { status: 401 });
|
|
const baseUrl = process.env.MOTORSIGNAL_API_BASE_URL ?? 'https://api.motorsignal.app';
|
|
const body = request.method === 'GET' || request.method === 'DELETE' ? undefined : await request.text();
|
|
const response = await fetch(`${baseUrl}/api/v1/admin/${path.join('/')}${request.nextUrl.search}`, { method: request.method, headers: { authorization: `Bearer ${token}`, ...(body ? { 'content-type': 'application/json' } : {}) }, body, cache: 'no-store' });
|
|
return new NextResponse(await response.text(), { status: response.status, headers: { 'content-type': response.headers.get('content-type') ?? 'application/json' } });
|
|
}
|
|
|
|
export const GET = proxy;
|
|
export const POST = proxy;
|
|
export const PATCH = proxy;
|
|
export const DELETE = proxy;
|