Code Drawer
1 min
Get User Profile (TypeScript)
type UserProfile = {
id: string;
email: string;
fullName: string;
plan: "free" | "pro" | "enterprise";
};
export async function getUserProfile(userId: string): Promise<UserProfile> {
const response = await fetch(`https://api.example.com/v1/users/${userId}`, {
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${process.env.API_TOKEN ?? ""}`
}
});
if (!response.ok) {
throw new Error(`Failed to fetch profile (${response.status})`);
}
return (await response.json()) as UserProfile;
}