diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts index 4cffd79..eb3fd57 100644 --- a/src/lib/fetch.ts +++ b/src/lib/fetch.ts @@ -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, { @@ -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) => { diff --git a/src/lib/map.ts b/src/lib/map.ts new file mode 100644 index 0000000..a630450 --- /dev/null +++ b/src/lib/map.ts @@ -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 + } +} \ No newline at end of file diff --git a/src/types/threads-api.ts b/src/types/threads-api.ts new file mode 100644 index 0000000..3202070 --- /dev/null +++ b/src/types/threads-api.ts @@ -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; + } \ No newline at end of file