ens-proxy/pages/api/text.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-08-18 15:04:49 +02:00
import { NextApiRequest, NextApiResponse } from 'next'
import { gql, OperationResult } from 'urql'
2022-08-18 15:51:38 +02:00
import { fetchData, getEns } from './_utils'
2022-08-18 15:04:49 +02:00
const ProfileTextRecordsQuery = gql<{
domains: [{ resolver: { texts: string[] } }]
}>`
query ProfileTextRecords($name: String!) {
domains(where: { name: $name }) {
resolver {
texts
}
}
}
`
2022-08-18 15:51:38 +02:00
export async function getEnsTextRecords(
ensName: string
): Promise<{ key: string; value: string }[]> {
// 1. Check which text records are set for the domain with ENS subgraph,
// to prevent unnecessary contract calls.
const result: OperationResult<{
domains: [{ resolver: { texts: string[] } }]
}> = await fetchData(
ProfileTextRecordsQuery,
{ name: ensName },
{
url: `https://api.thegraph.com/subgraphs/name/ensdomains/ens`,
requestPolicy: 'cache-and-network'
}
)
if (!result?.data?.domains[0]?.resolver) return
// 2. Retrieve the text records.
const { texts } = result.data.domains[0].resolver
const records = []
const ens = await getEns()
for (let index = 0; index < texts?.length; index++) {
const key = texts[index]
const value = await ens.name(ensName).getText(key)
console.log(value)
records.push({ key, value })
}
return records
}
export default async function ensTextApi(
2022-08-18 15:04:49 +02:00
request: NextApiRequest,
response: NextApiResponse
2022-08-18 15:51:38 +02:00
) {
2022-08-18 15:04:49 +02:00
try {
const ensName = String(request.query.name)
2022-08-18 15:51:38 +02:00
const records = await getEnsTextRecords(ensName)
2022-08-18 15:04:49 +02:00
response.setHeader('Cache-Control', 's-maxage=86400')
response.status(200).send(records)
} catch (error) {
response.status(500).send(`${error}`)
}
}