-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: delete 메소드에 refreshToken 로직 추가
- Loading branch information
Showing
1 changed file
with
24 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
// const API_URL = | ||
// process.env.NODE_ENV === 'production' | ||
// ? process.env.REACT_APP_API_DEFAULT_PROD | ||
// : process.env.REACT_APP_API_DEFAULT_DEV; | ||
|
||
const API_URL = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : DEFAULT_PROD_URL | ||
|
||
import { DEFAULT_PROD_URL } from '../constants'; | ||
import { ContentTypeType } from '../types/Api'; | ||
import withTokenRefresh from './utils'; | ||
|
||
interface Headers { | ||
'content-type': string; | ||
[key: string]: string; | ||
} | ||
|
||
export const deleteApi = async (url: string, contentType?: ContentTypeType) => { | ||
const apiUrl = `${DEFAULT_PROD_URL + url}`; | ||
const userToken = localStorage.getItem('userToken'); | ||
const headers: Headers = { | ||
'content-type': 'application/json', | ||
}; | ||
|
||
if (userToken) { | ||
headers['Authorization'] = `Bearer ${userToken}`; | ||
} | ||
|
||
if (contentType) { | ||
headers['content-type'] = contentType; | ||
} | ||
|
||
const response = await fetch(apiUrl, { | ||
method: 'DELETE', | ||
headers, | ||
return await withTokenRefresh(async () => { | ||
const apiUrl = `${DEFAULT_PROD_URL + url}`; | ||
const userToken = localStorage.getItem('userToken'); | ||
const headers: Headers = { | ||
'content-type': 'application/json', | ||
}; | ||
|
||
if (userToken) { | ||
headers['Authorization'] = `Bearer ${userToken}`; | ||
} | ||
|
||
if (contentType) { | ||
headers['content-type'] = contentType; | ||
} | ||
|
||
const response = await fetch(apiUrl, { | ||
method: 'DELETE', | ||
headers, | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error('[SERVER] DELETE 요청에 실패했습니다.'); | ||
} | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new Error('[SERVER] DELETE 요청에 실패했습니다.'); | ||
} | ||
}; |