From b54718467a08f8c33383cb244b3f8b31c0236af1 Mon Sep 17 00:00:00 2001 From: Nazeeh Vahora Date: Sat, 16 Mar 2024 02:04:25 +0530 Subject: [PATCH] add `getFollowerCountFromHub` method --- src/functions/getFollowerCount.ts | 86 ++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/src/functions/getFollowerCount.ts b/src/functions/getFollowerCount.ts index 5b9ef9c..5d5588e 100644 --- a/src/functions/getFollowerCount.ts +++ b/src/functions/getFollowerCount.ts @@ -1,6 +1,9 @@ import { Provider, HubProvider, NeynarProvider } from '../providers'; -export const getFollowerCount = async (provider: Provider, fid: number): Promise => { +export const getFollowerCount = async ( + provider: Provider, + fid: number +): Promise => { let url: string; if (provider instanceof HubProvider) { @@ -20,4 +23,83 @@ export const getFollowerCount = async (provider: Provider, fid: number): Promise const data = await response.json(); return data.followerCount; -} \ No newline at end of file +}; +type Bio = { + text: string; +}; + +type Profile = { + bio: Bio; +}; + +type VerifiedAddresses = { + eth_addresses: string[]; + sol_addresses: string[]; +}; + +type User = { + object: string; + fid: number; + custody_address: string; + username: string; + display_name: string; + pfp_url: string; + profile: Profile; + follower_count: number; + following_count: number; + verifications: string[]; + verified_addresses: VerifiedAddresses; + active_status: string; +}; + +type Follow = { + object: string; + user: User; +}; + +type UserDehydrated = { + object: string; + fid: number; +}; + +type FollowDehydrated = { + object: string; + user: UserDehydrated; +}; + +type ApiResponse = { + top_relevant_followers_hydrated: Follow[]; + all_relevant_followers_dehydrated: FollowDehydrated[]; +}; + +async function getFollowerCountFromHub( + provider: NeynarProvider, + targetFid: number, + viewerFid: number +): Promise { + const options = { + method: 'GET', + headers: { + accept: 'application/json', + api_key: provider.apiKey, + }, + }; + + const response = await fetch( + `${provider.endPoint}/v2/farcaster/followers/relevant?target_fid=${targetFid}&viewer_fid=${viewerFid}`, + options + ); + + if (!response.ok) { + throw new Error('Network response was not ok'); + } + + const data: ApiResponse = await response.json(); + + // Extract the User objects from the response + const users: User[] = data.top_relevant_followers_hydrated.map( + follow => follow.user + ); + + return users; +}