Skip to content

Commit

Permalink
Add mapUserProfile function to fetchUserProfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dvque committed Feb 3, 2024
1 parent b1c521a commit 6670dbc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ThreadsUserProfileResponse } from "../types/threads-api";
import { ENDPOINTS_DOCUMENT_ID, GRAPHQL_ENDPOINT, THREADS_APP_ID } from "./consts";
import { IS_DEBUG } from "./env";
import { mapUserProfile } from "./map";

const fetchBase = ({ documentId, variables } : {documentId: string, variables: string} ) => {
return fetch (GRAPHQL_ENDPOINT, {
Expand Down Expand Up @@ -35,7 +37,11 @@ export const fetchUserProfile = async ({ userId, userName }: fetchUserParams) =>
userId = await fetchUserIdByName({ userName });
}
const variables = JSON.stringify({ userID: userId });
return fetchBase({ documentId: ENDPOINTS_DOCUMENT_ID.USER_PROFILE, variables })
const data = (await fetchBase(
{ documentId: ENDPOINTS_DOCUMENT_ID.USER_PROFILE, variables })
) as ThreadsUserProfileResponse

return mapUserProfile(data)
}

export const fetchUserThreads = async ({ userId, userName }: fetchUserParams) => {
Expand Down
25 changes: 25 additions & 0 deletions src/lib/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ThreadsUserProfileResponse } from "../types/threads-api";

export const mapUserProfile = (rawResponse: ThreadsUserProfileResponse) => {
const userApiResponse = rawResponse?.data?.userData?.user
if (!userApiResponse) return null

const { username, is_verified, biography, follower_count, bio_links, pk: id, full_name, profile_pic_url, hd_profile_pic_versions } = userApiResponse

const profile_pics = [{
height: 150,
width: 150,
url: profile_pic_url
}, ...hd_profile_pic_versions]

return {
id,
username,
isVerified: is_verified,
biography,
followerCount: follower_count,
bioLinks: bio_links,
fullName: full_name,
profilePics: profile_pics
}
}
42 changes: 42 additions & 0 deletions src/types/threads-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export type ThreadsUserProfileResponse = {
data: Data;
extensions: Extensions;
}

export type Data = {
userData: UserData;
}

export type UserData = {
user: User;
}

export type User = {
is_private: boolean;
profile_pic_url: string;
username: string;
hd_profile_pic_versions: HDProfilePicVersion[];
is_verified: boolean;
biography: string;
biography_with_entities: null;
follower_count: number;
profile_context_facepile_users: null;
bio_links: BioLink[];
pk: string;
full_name: string;
id: null;
}

export type BioLink = {
url: string;
}

export type HDProfilePicVersion = {
height: number;
url: string;
width: number;
}

export type Extensions = {
is_final: boolean;
}

0 comments on commit 6670dbc

Please sign in to comment.