From 9a36f47c9b89f5af198075b154dc2f08b72a388b Mon Sep 17 00:00:00 2001 From: Jamie Hewitt Date: Thu, 18 Aug 2022 17:09:42 +0300 Subject: [PATCH] Creating API for getting profile --- pages/api/profile.ts | 20 ++++++++++++++++++-- pages/api/text.ts | 3 +-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pages/api/profile.ts b/pages/api/profile.ts index 46728e8..e7cf1e1 100644 --- a/pages/api/profile.ts +++ b/pages/api/profile.ts @@ -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 { - 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 { ...(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}`) + } +} diff --git a/pages/api/text.ts b/pages/api/text.ts index 3f19560..1ffc1cd 100644 --- a/pages/api/text.ts +++ b/pages/api/text.ts @@ -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 }) }