Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
add getFollowerCountFromHub method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazeeh21 committed Mar 15, 2024
1 parent 5e1fc1d commit b547184
Showing 1 changed file with 84 additions and 2 deletions.
86 changes: 84 additions & 2 deletions src/functions/getFollowerCount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Provider, HubProvider, NeynarProvider } from '../providers';

export const getFollowerCount = async (provider: Provider, fid: number): Promise<number> => {
export const getFollowerCount = async (
provider: Provider,
fid: number
): Promise<number> => {
let url: string;

if (provider instanceof HubProvider) {
Expand All @@ -20,4 +23,83 @@ export const getFollowerCount = async (provider: Provider, fid: number): Promise
const data = await response.json();

return data.followerCount;
}
};
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<User[]> {
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;
}

0 comments on commit b547184

Please sign in to comment.