-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute.ts
More file actions
82 lines (71 loc) · 1.75 KB
/
Copy pathroute.ts
File metadata and controls
82 lines (71 loc) · 1.75 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { type NextRequest, NextResponse } from "next/server";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
const LIFI_QUOTE_URL = "https://li.quest/v1/quote";
const PASS_THROUGH_PARAMS = [
"fromChain",
"toChain",
"fromToken",
"toToken",
"fromAddress",
"toAddress",
"fromAmount",
"slippage",
"order",
"integrator",
"referrer",
"allowBridges",
"allowExchanges",
] as const;
export async function GET(request: NextRequest) {
const apiKey = process.env.LIFI_API_KEY;
if (!apiKey) {
return NextResponse.json(
{ error: "missing_api_key" },
{ status: 500 },
);
}
const incoming = request.nextUrl.searchParams;
const upstream = new URL(LIFI_QUOTE_URL);
for (const key of PASS_THROUGH_PARAMS) {
const value = incoming.get(key);
if (value !== null && value !== "") {
upstream.searchParams.set(key, value);
}
}
try {
const upstreamResponse = await fetch(upstream.toString(), {
headers: {
accept: "application/json",
"x-lifi-api-key": apiKey,
},
cache: "no-store",
});
const text = await upstreamResponse.text();
if (!upstreamResponse.ok) {
return NextResponse.json(
{
error: "upstream_error",
status: upstreamResponse.status,
message: text,
},
{ status: upstreamResponse.status },
);
}
return new NextResponse(text, {
status: 200,
headers: {
"content-type": "application/json",
"cache-control": "no-store",
},
});
} catch (error) {
return NextResponse.json(
{
error: "proxy_failed",
message: error instanceof Error ? error.message : "unknown",
},
{ status: 502 },
);
}
}