Skip to content

Commit

Permalink
feat: start proxying ipfs requests (#4242)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk authored Feb 1, 2024
1 parent d91c261 commit 7705105
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/hooks/JB721Delegate/useNftCollectionMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipfsGatewayFetch } from 'lib/api/ipfs'
import { ipfsFetch } from 'lib/api/ipfs'
import { NftCollectionMetadata } from 'models/nftRewards'
import { useQuery } from 'react-query'
import { cidFromUrl } from 'utils/ipfs'
Expand All @@ -17,7 +17,7 @@ export function useNftCollectionMetadata(uri: string | undefined) {
throw new Error('NFT Contract URI invalid.')
}

const response = await ipfsGatewayFetch<NftCollectionMetadata>(cid)
const response = await ipfsFetch<NftCollectionMetadata>(cid)
return response.data
},
{
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/useProjectMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ipfsGatewayFetch } from 'lib/api/ipfs'
import { ipfsFetch } from 'lib/api/ipfs'
import { AnyProjectMetadata, consolidateMetadata } from 'models/projectMetadata'
import { useQuery } from 'react-query'

Expand All @@ -10,13 +10,12 @@ export function useProjectMetadata(uri: string | null | undefined) {
throw new Error('Project URI not specified.')
}

const response = await ipfsGatewayFetch<AnyProjectMetadata>(uri)
const response = await ipfsFetch<AnyProjectMetadata>(uri)
const metadata = consolidateMetadata(response.data)
return metadata
},
{
enabled: !!uri,
staleTime: 60000,
},
)
}
14 changes: 14 additions & 0 deletions src/lib/api/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ const extractJsonFromBase64Data = (base64: string) => {
return JSON.parse(decoded.substring(jsonStart, jsonEnd + 1))
}

/**
* Fetch an IPFS hash from our proxy.
*/
export const ipfsFetch = async <T>(hash: string) => {
const response = await axios.get<T>(
`${process.env.NEXT_PUBLIC_BASE_URL}api/ipfs/${hash}`,
)

return response
}

/**
* Fetch an IPFS hash directly from the IPFS gateway.
*/
export const ipfsGatewayFetch = async <T>(
hash: string,
opts?: AxiosRequestConfig<T>,
Expand Down
27 changes: 27 additions & 0 deletions src/pages/api/ipfs/[cid].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ipfsGatewayFetch } from 'lib/api/ipfs'
import { getLogger } from 'lib/logger'
import { NextApiRequest, NextApiResponse } from 'next'

const logger = getLogger('ipfs/[cid]')

export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
if (req.method !== 'GET') {
return res.status(405).end()
}
if (!req.query.cid) {
return res.status(400).json({ error: 'cid not specified' })
}

try {
const ipfsRes = await ipfsGatewayFetch(req.query.cid as string)
// cache for 1 day
res.setHeader('Cache-Control', 's-maxage=86400, stale-while-revalidate')
return res.status(200).json(ipfsRes.data)
} catch (error) {
logger.error(error)
return res.status(500).json({ error: 'failed to fetch ipfs data' })
}
}

0 comments on commit 7705105

Please sign in to comment.