Creating API for getting profile

This commit is contained in:
Jamie Hewitt 2022-08-18 17:09:42 +03:00
parent 61dd21b2f1
commit 9a36f47c9b
2 changed files with 19 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { getEnsName } from './name'
import { getEnsTextRecords } from './text'
@ -21,7 +22,7 @@ function getEnsAvatar(ensName: string): string {
}
export async function getEnsProfile(accountId: string): Promise<Profile> {
const name = await getEnsName(accountId)
const name = (await getEnsName(accountId)).name
if (!name) return { name: null }
const records = await getEnsTextRecords(name)
@ -52,6 +53,21 @@ export async function getEnsProfile(accountId: string): Promise<Profile> {
...(description && { description }),
...(links.length > 0 && { links })
}
return profile
}
export default async function EnsProfileApi(
request: NextApiRequest,
response: NextApiResponse
) {
try {
const accountId = String(request.query.address)
const profile = await getEnsProfile(accountId)
console.log('profile', profile)
response.setHeader('Cache-Control', 's-maxage=86400')
response.status(200).send(profile)
} catch (error) {
response.status(500).send(`${error}`)
}
}

View File

@ -29,7 +29,7 @@ export async function getEnsTextRecords(
requestPolicy: 'cache-and-network'
}
)
if (!result?.data?.domains[0]?.resolver) return
if (!result?.data?.domains[0]?.resolver) throw 'No ENS text records found'
// 2. Retrieve the text records.
const { texts } = result.data.domains[0].resolver
@ -40,7 +40,6 @@ export async function getEnsTextRecords(
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 })
}