( req: Request, apiUrl: string, apiPath: string, )
| 43 | } |
| 44 | |
| 45 | async function handleApiRoute( |
| 46 | req: Request, |
| 47 | apiUrl: string, |
| 48 | apiPath: string, |
| 49 | ): Promise<NextResponse> { |
| 50 | const headers = getClientHeaders(req); |
| 51 | |
| 52 | try { |
| 53 | const res = await fetch(`${apiUrl}${apiPath}`, { |
| 54 | method: req.method, |
| 55 | headers, |
| 56 | body: |
| 57 | req.method === 'POST' ? JSON.stringify(await req.json()) : undefined, |
| 58 | }); |
| 59 | |
| 60 | if (res.headers.get('content-type')?.includes('application/json')) { |
| 61 | return NextResponse.json(await res.json(), { status: res.status }); |
| 62 | } |
| 63 | return NextResponse.json(await res.text(), { status: res.status }); |
| 64 | } catch (e) { |
| 65 | return NextResponse.json( |
| 66 | { |
| 67 | error: 'Failed to proxy request', |
| 68 | message: e instanceof Error ? e.message : String(e), |
| 69 | }, |
| 70 | { status: 500 }, |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | async function handleScriptProxyRoute(req: Request): Promise<NextResponse> { |
| 76 | const url = new URL(req.url); |
no test coverage detected