Repository navigation
Expand file tree
/
Copy pathclient.js
More file actions
122 lines (108 loc) · 4.28 KB
/
Copy pathclient.js
File metadata and controls
122 lines (108 loc) · 4.28 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import "client-only";
import axios from "axios";
import { applyApiLanguageHeader } from "./request-language";
import { MOCK_USER, useAuthStore } from "@/stores/use-auth-store";
export const apiClient = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || "/api/v1",
timeout: 15_000,
withCredentials: true,
headers: {
Accept: "application/json",
},
});
apiClient.interceptors.request.use((config) => {
applyApiLanguageHeader(config.headers, document.cookie);
if (typeof FormData !== "undefined" && config.data instanceof FormData) {
if (typeof config.headers?.delete === "function") {
config.headers.delete("Content-Type");
} else if (config.headers) {
delete config.headers["Content-Type"];
delete config.headers["content-type"];
}
}
const isExplicitlyLoggedOut =
typeof window !== "undefined" &&
window.sessionStorage?.getItem("ditto_logged_out") === "true";
const authUser = useAuthStore.getState()?.user;
const isAuthLoginUrl = String(config.url || "").includes("/auth/login");
const isAdminRoute =
typeof window !== "undefined" && window.location.pathname.startsWith("/admin");
const isAdminProfile =
String(authUser?.role || "")
.trim()
.replace(/^ROLE_/i, "")
.toUpperCase() === "ADMIN" ||
String(authUser?.email || "").trim().toLowerCase() === "test1234@naver.com" ||
String(authUser?.nickname || authUser?.name || "").trim() === "구본희";
const requestUser = !isAdminRoute && isAdminProfile ? MOCK_USER : authUser;
const userId = !isExplicitlyLoggedOut
? requestUser?.id ||
requestUser?.userId ||
(authUser ? process.env.NEXT_PUBLIC_LOCAL_USER_ID?.trim() : null)
: requestUser?.id || requestUser?.userId || null;
const userRole = requestUser?.role || "ROLE_CUSTOMER";
const userEmail = requestUser?.email || "local-user@example.com";
const hasUserIdHeader =
typeof config.headers?.has === "function"
? config.headers.has("X-User-Id")
: Boolean(config.headers?.["X-User-Id"] || config.headers?.["x-user-id"]);
const hasUserRoleHeader =
typeof config.headers?.has === "function"
? config.headers.has("X-User-Role")
: Boolean(config.headers?.["X-User-Role"] || config.headers?.["x-user-role"]);
const hasUserEmailHeader =
typeof config.headers?.has === "function"
? config.headers.has("X-User-Email")
: Boolean(config.headers?.["X-User-Email"] || config.headers?.["x-user-email"]);
if (isAuthLoginUrl) {
if (typeof config.headers?.delete === "function") {
config.headers.delete("X-User-Id");
config.headers.delete("X-User-Role");
config.headers.delete("X-User-Email");
} else if (config.headers) {
delete config.headers["X-User-Id"];
delete config.headers["X-User-Role"];
delete config.headers["X-User-Email"];
}
return config;
}
if (userId && !hasUserIdHeader) {
if (typeof config.headers?.set === "function") {
config.headers.set("X-User-Id", String(userId));
} else if (config.headers) {
config.headers["X-User-Id"] = String(userId);
}
} else if (!userId && typeof config.headers?.delete === "function") {
config.headers.delete("X-User-Id");
} else if (!userId && config.headers) {
delete config.headers["X-User-Id"];
}
if (userId && !hasUserRoleHeader) {
if (typeof config.headers?.set === "function") {
config.headers.set("X-User-Role", String(userRole));
} else if (config.headers) {
config.headers["X-User-Role"] = String(userRole);
}
} else if (!userId && typeof config.headers?.delete === "function") {
config.headers.delete("X-User-Role");
} else if (!userId && config.headers) {
delete config.headers["X-User-Role"];
}
if (userId && !hasUserEmailHeader) {
if (typeof config.headers?.set === "function") {
config.headers.set("X-User-Email", String(userEmail));
} else if (config.headers) {
config.headers["X-User-Email"] = String(userEmail);
}
} else if (!userId && typeof config.headers?.delete === "function") {
config.headers.delete("X-User-Email");
} else if (!userId && config.headers) {
delete config.headers["X-User-Email"];
}
return config;
});
apiClient.interceptors.response.use(
(response) => response,
(error) => Promise.reject(error),
);
export default apiClient;