-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuser-api.server.ts
More file actions
46 lines (37 loc) · 1.05 KB
/
user-api.server.ts
File metadata and controls
46 lines (37 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { cookies } from "next/headers";
import { fetch as apiFetch } from "@/shared/api/server";
import { API_ENDPOINTS } from "@/shared/config/endpoints";
interface UserApiResponse {
username: string;
email: string;
profileImage: string | null;
rating: number;
totalReviews: number;
isOwner: boolean;
}
export const getUserProfileServer = async (targetUserId: number) => {
const cookieStore = await cookies();
const accessToken = cookieStore.get("accessToken")?.value;
const path = API_ENDPOINTS.userInfo(targetUserId);
try {
const response = await apiFetch<UserApiResponse>(path, {
method: "GET",
headers: {
...(accessToken && { Authorization: `Bearer ${accessToken}` }),
},
cache: "no-store",
});
const { data } = response;
if (!data) return null;
return {
name: data.username,
email: data.email,
avatarUrl: data.profileImage || undefined,
rating: data.rating,
reviewCount: data.totalReviews,
isOwner: data.isOwner,
};
} catch {
return null;
}
};