Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿

refactor: enhance getLensAccount to use a default account structure and improve error handling

yoginth.com cfbd4b8c 72988a3e

verified
+29 -20
+1 -7
apps/api/src/routes/ens/resolver/query.ts
··· 8 8 let res: string; 9 9 const account = await getLensAccount(name.replace(".hey.xyz", "")); 10 10 11 - const texts = { 12 - avatar: account.avatar, 13 - bio: account.bio, 14 - name: account.name 15 - }; 16 - 17 11 switch (functionName) { 18 12 case "addr": { 19 13 res = account.address ?? zeroAddress; ··· 21 15 } 22 16 case "text": { 23 17 const key = args[1]; 24 - res = texts[key as keyof typeof texts] ?? ""; 18 + res = account.texts[key as keyof typeof account.texts] ?? ""; 25 19 break; 26 20 } 27 21 default: {
+28 -13
apps/api/src/utils/getLensAccount.ts
··· 4 4 import apolloClient from "@hey/indexer/apollo/client"; 5 5 import { type Hex, zeroAddress } from "viem"; 6 6 7 - const getLensAccount = async ( 8 - handle: string 9 - ): Promise<{ 7 + interface LensAccount { 10 8 address: Hex; 11 - name: string; 12 - avatar: string; 13 - bio: string; 14 - }> => { 9 + texts: { 10 + avatar: string; 11 + description: string; 12 + name: string; 13 + url: string; 14 + }; 15 + } 16 + 17 + const defaultAccount: LensAccount = { 18 + address: zeroAddress, 19 + texts: { 20 + avatar: "", 21 + description: "", 22 + name: "", 23 + url: "" 24 + } 25 + }; 26 + 27 + const getLensAccount = async (handle: string): Promise<LensAccount> => { 15 28 try { 16 29 const { data } = await apolloClient.query<{ 17 30 account: AccountFragment; ··· 22 35 }); 23 36 24 37 const address = data.account.owner; 25 - if (!address) 26 - return { address: zeroAddress, avatar: "", bio: "", name: "" }; 38 + if (!address) return defaultAccount; 27 39 return { 28 40 address: address.toLowerCase() as Hex, 29 - avatar: getAvatar(data.account), 30 - bio: data.account.metadata?.bio ?? "", 31 - name: getAccount(data.account).name 41 + texts: { 42 + avatar: getAvatar(data.account), 43 + description: data.account.metadata?.bio ?? "", 44 + name: getAccount(data.account).name, 45 + url: `https://hey.xyz${getAccount(data.account).link}` 46 + } 32 47 }; 33 48 } catch { 34 - return { address: zeroAddress, avatar: "", bio: "", name: "" }; 49 + return defaultAccount; 35 50 } 36 51 }; 37 52