Skip to content

Commit

Permalink
fix cors issue in dev environment when accessing api.coingecko.com
Browse files Browse the repository at this point in the history
  • Loading branch information
iossocket committed Nov 4, 2024
1 parent 59f4b71 commit aae1802
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
26 changes: 26 additions & 0 deletions packages/nextjs/app/api/price/[symbol]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export async function GET(_: Request, { params: { symbol } }: { params: { symbol: string } }) {
let apiUrl = "";
if (symbol === "ETH") {
apiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd";
} else if (symbol === "STRK") {
apiUrl = "https://api.coingecko.com/api/v3/simple/price?ids=starknet&vs_currencies=usd";
} else {
return Response.json({
ethereum: { usd: 0 },
starknet: { usd: 0 }
});
}
try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`coingecko response status: ${response.status}`);
}
const json = await response.json();
return Response.json(json);
} catch (e) {
return Response.json({
ethereum: { usd: 0 },
starknet: { usd: 0 }
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ const updatePriceCache = async (symbol: string, retries = 3): Promise<number> =>
let attempt = 0;
while (attempt < retries) {
try {
let apiUrl = "";
if (symbol === "ETH") {
apiUrl = `https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd`;
} else if (symbol === "STRK") {
apiUrl = `https://api.coingecko.com/api/v3/simple/price?ids=starknet&vs_currencies=usd`;
}
const response = await fetch(apiUrl);
const response = await fetch(`/api/price/${symbol}`);
const data = await response.json();
const price = symbol === "ETH" ? data.ethereum.usd : data.starknet.usd;
priceCache[symbol] = price;
Expand Down

0 comments on commit aae1802

Please sign in to comment.