-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert fetch to SWR and use axios instead of fetch on server side ro…
…ute handler
- Loading branch information
1 parent
62f86ba
commit 3433eaf
Showing
6 changed files
with
124 additions
and
129 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
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
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
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 |
---|---|---|
@@ -1,27 +1,34 @@ | ||
import axios from "axios"; | ||
import type { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
try { | ||
// Base URL | ||
const url = `https://eth-sepolia.g.alchemy.com/nft/v3/${process.env.ALCHEMY_API_KEY}/getNFTsForContract?contractAddress=0x6a7415A8d287b343417150E7a74069D6D121372d&withMetadata=true`; | ||
const { contractAddress } = req.query; | ||
|
||
// Fetch data | ||
const response = await fetch(url); | ||
|
||
// Check if the response was successful | ||
if (!response.ok) { | ||
throw new Error(`API responded with status code ${response.status}`); | ||
// Check if contractAddress is provided | ||
if (!contractAddress) { | ||
return res.status(400).json({ error: "Contract address is required" }); | ||
} | ||
|
||
const data = await response.json(); | ||
const nftsArray = data.nfts; | ||
// Fetch data using axios | ||
const response = await axios.get( | ||
`https://eth-sepolia.g.alchemy.com/nft/v3/${process.env.ALCHEMY_API_KEY}/getNFTsForContract`, | ||
{ | ||
params: { | ||
contractAddress, | ||
withMetadata: true, | ||
}, | ||
}, | ||
); | ||
|
||
// Extract data from response | ||
const nftsArray = response.data.nfts; | ||
|
||
res.status(200).json(nftsArray); | ||
} catch (error) { | ||
// Log the error for server-side debugging | ||
console.error("Failed to fetch NFT data for contract", error); | ||
|
||
// Send a generic error response to the client | ||
// Send a response with the error message | ||
res.status(500).json({ error: "Failed to fetch NFT data for contract" }); | ||
} | ||
} |
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
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