-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f21521
commit 3b467c6
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import { PoolApiResponse } from "~/types/api.types"; | ||
|
||
// Cache times in seconds | ||
const S_MAXAGE_TIME = 60 * 4; // 4 minutes | ||
const STALE_WHILE_REVALIDATE_TIME = 60 * 10; // 10 minutes | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (!process.env.MARGINFI_API_URL) { | ||
return res.status(500).json({ error: "API URL is not set" }); | ||
} | ||
|
||
const { address } = req.query; | ||
|
||
console.log({ address }); | ||
|
||
try { | ||
const response = await fetch(`${process.env.MARGINFI_API_URL}/arena/positions/${address}`, { | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`API responded with status: ${response.status}`); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
// Set cache headers | ||
res.setHeader("Cache-Control", `s-maxage=${S_MAXAGE_TIME}, stale-while-revalidate=${STALE_WHILE_REVALIDATE_TIME}`); | ||
|
||
return res.status(200).json(data); | ||
} catch (error) { | ||
console.error("Error fetching pool data:", error); | ||
return res.status(500).json({ | ||
error: "Internal server error", | ||
message: process.env.NODE_ENV === "development" ? error : undefined, | ||
}); | ||
} | ||
} |