ens-proxy/api/text.ts
2022-08-22 16:03:45 +03:00

44 lines
1.0 KiB
TypeScript

import type { VercelRequest, VercelResponse } from '@vercel/node'
import { getProvider } from './_utils'
export async function getEnsTextRecords(
ensName: string
): Promise<{ key: string; value: string }[]> {
const texts = [
'url',
'avatar',
'com.twitter',
'com.github',
'org.telegram',
'com.discord',
'com.reddit'
]
const records = []
const provider = await getProvider()
const resolver = await provider.getResolver(ensName)
for (let index = 0; index < texts?.length; index++) {
const key = texts[index]
const value = await resolver.getText(key)
value && records.push({ key, value })
}
return records
}
export default async function ensTextApi(
request: VercelRequest,
response: VercelResponse
) {
try {
const ensName = String(request.query.name)
const records = await getEnsTextRecords(ensName)
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')
response.status(200).send(records)
} catch (error) {
response.status(500).send(`${error}`)
}
}