-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance.ts
More file actions
51 lines (44 loc) · 1.29 KB
/
instance.ts
File metadata and controls
51 lines (44 loc) · 1.29 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
import ky from 'ky';
import { setAuthorizationHeader } from '@/utils/ky/hooks/beforeRequest';
import {
deleteClientCokiesPath,
retryRequestOnUnauthorized,
throwServerErrorMessage,
redirectTokenError
} from '@/utils/ky/hooks/afterResponse';
/**
* client 사이드에서 httponly 쿠키 사용이 불가능하므로 객체에 저장
* beforeRequest 훅에서 여기 있는 store를 꺼내서 헤더에 넣어준다.
*/
export const store: { [key in string]: string } = {};
export const setStore = (key: string, value: string) => {
store[key] = value;
};
export const removeStore = (key: string) => {};
const api = ky.extend({
prefixUrl: process.env.NEXT_PUBLIC_API_ENDPOINT,
headers: {
'Content-Type': 'application/json'
},
hooks: {
beforeRequest: [setAuthorizationHeader(process)],
afterResponse: [
retryRequestOnUnauthorized(process),
redirectTokenError(process),
throwServerErrorMessage,
deleteClientCokiesPath
]
}
});
const url = (() => {
const branch = process?.env.NEXT_PUBLIC_VERCEL_BRANCH_URL || '';
if (branch.match(/feat/)) {
return `https://${process?.env.NEXT_PUBLIC_VERCEL_BRANCH_URL}/api`;
} else {
return process?.env.NEXT_PUBLIC_FRONT_ENDPOINT;
}
})();
export const fe = ky.extend({
prefixUrl: url
});
export default api;