ens-proxy/api/text.ts

45 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2022-08-22 14:42:40 +02:00
import type { VercelRequest, VercelResponse } from '@vercel/node'
2022-08-22 14:54:07 +02:00
import { getProvider } from './_utils'
2022-08-18 15:04:49 +02:00
2022-08-18 15:51:38 +02:00
export async function getEnsTextRecords(
ensName: string
2022-08-24 16:05:52 +02:00
): Promise<{ key: string; value: string }[] | null> {
2022-08-22 14:54:07 +02:00
const texts = [
'url',
2022-08-22 15:03:45 +02:00
'avatar',
2022-08-22 14:54:07 +02:00
'com.twitter',
'com.github',
'org.telegram',
'com.discord',
'com.reddit'
]
2022-08-18 15:51:38 +02:00
const records = []
2022-08-22 14:09:50 +02:00
const provider = await getProvider()
const resolver = await provider.getResolver(ensName)
2022-08-24 16:05:52 +02:00
if (!resolver) return null
2022-08-18 15:51:38 +02:00
for (let index = 0; index < texts?.length; index++) {
const key = texts[index]
2022-08-22 14:09:50 +02:00
const value = await resolver.getText(key)
2022-08-22 14:54:07 +02:00
value && records.push({ key, value })
2022-08-18 15:51:38 +02:00
}
return records
}
export default async function ensTextApi(
2022-08-22 14:42:40 +02:00
request: VercelRequest,
response: VercelResponse
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
2022-08-22 12:57:25 +02:00
response.setHeader('Cache-Control', 'max-age=0, s-maxage=86400')
2022-08-24 15:49:17 +02:00
response.status(200).send({ records })
2022-08-18 15:04:49 +02:00
} catch (error) {
2022-08-24 15:49:17 +02:00
response.send({ error })
2022-08-18 15:04:49 +02:00
}
}