1
- import axios , { AxiosError } from " axios" ;
2
- import { getCookie } from " ../utils/cookies" ;
3
- import { setCookie } from " ../utils/cookies" ;
4
- import { reIssueToken } from " ./auth" ;
1
+ import axios , { AxiosError } from ' axios' ;
2
+ import { getCookie } from ' ../utils/cookies' ;
3
+ import { setCookie } from ' ../utils/cookies' ;
4
+ import { reIssueToken } from ' ./auth' ;
5
5
6
6
export const instance = axios . create ( {
7
- baseURL : `https://dev-api.dms-dsm.com` ,
7
+ baseURL : process . env . REACT_APP_BASE_URL ,
8
8
timeout : 10000 ,
9
9
} ) ;
10
10
11
11
instance . interceptors . request . use (
12
12
( config ) => {
13
13
const accessToken = getCookie ( 'access_token' ) ;
14
- const returnConfig = {
15
- ...config ,
16
- } ;
17
14
if ( accessToken ) {
18
- // @ts -ignore
19
- returnConfig . headers = {
20
- Authorization : `Bearer ${ accessToken } ` ,
21
- } ;
15
+ config . headers = config . headers ?? { } ;
16
+ config . headers . Authorization = `Bearer ${ accessToken } ` ;
22
17
}
23
- return returnConfig ;
18
+ return config ;
24
19
} ,
25
20
( error : AxiosError ) => Promise . reject ( error ) ,
26
21
) ;
@@ -29,7 +24,7 @@ instance.interceptors.response.use(
29
24
( response ) => response ,
30
25
async ( error : AxiosError < AxiosError > ) => {
31
26
if ( axios . isAxiosError ( error ) && error . response ) {
32
- const { config} = error ;
27
+ const { config } = error ;
33
28
const refreshToken = getCookie ( 'refresh_token' ) ;
34
29
if ( error . response . data . message === 'Expired Token' ) {
35
30
if ( refreshToken ) {
@@ -47,8 +42,8 @@ instance.interceptors.response.use(
47
42
} ) ;
48
43
49
44
if ( config ?. headers )
50
- config . headers [ 'Authorization' ] = `Bearer ${ res . access_token } `
51
-
45
+ config . headers [ 'Authorization' ] = `Bearer ${ res . access_token } ` ;
46
+
52
47
return axios ( config ! ) ;
53
48
} catch ( error ) {
54
49
return Promise . reject ( error ) ;
@@ -61,5 +56,5 @@ instance.interceptors.response.use(
61
56
}
62
57
}
63
58
return Promise . reject ( error ) ;
64
- }
59
+ } ,
65
60
) ;
0 commit comments