-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
47 lines (38 loc) · 1.15 KB
/
route.ts
File metadata and controls
47 lines (38 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { NextRequest, NextResponse } from "next/server";
const API_URL = process.env.API_URL;
const API_KEY = process.env.API_KEY;
async function proxyRequest(req: NextRequest) {
const url = new URL(req.url);
const path = url.pathname;
const search = url.search;
const destination = `${API_URL}/v1/${path}${search}`;
const headers = new Headers();
headers.set("Accept", "application/json");
headers.set("Content-Type", "application/json");
if (API_KEY) {
headers.set("Authorization", `Bearer ${API_KEY}`);
}
const body =
req.method !== "GET" && req.method !== "HEAD"
? await req.text()
: undefined;
const response = await fetch(destination, {
method: req.method,
headers,
body,
});
const data = await response.text();
return new NextResponse(data, {
status: response.status,
statusText: response.statusText,
headers: {
"Content-Type":
response.headers.get("Content-Type") || "application/json",
},
});
}
export const GET = proxyRequest;
export const POST = proxyRequest;
export const PATCH = proxyRequest;
export const PUT = proxyRequest;
export const DELETE = proxyRequest;