-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] token 필요없는 fetch custom hook 만들기 (#445)
* feat: 기존 axios instace -> fetch instance로 대체 * refactor: 기존 코드 fetch instance로 변경 * refactor: headers type 제한 * feat: instace option method type 제한
- Loading branch information
1 parent
77532a9
commit 64c8410
Showing
6 changed files
with
76 additions
and
41 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,7 +1,7 @@ | ||
import instance from '@apis/instance'; | ||
|
||
export const getRecruitingInfo = async () => { | ||
const res = await instance.get('/recruiting-season/latest'); | ||
const res = await instance('/recruiting-season/latest', { method: 'GET' }); | ||
|
||
return res; | ||
}; |
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,10 +1,27 @@ | ||
import axios from 'axios'; | ||
|
||
const instance = axios.create({ | ||
baseURL: import.meta.env.VITE_BASE_URL, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
const baseURL = import.meta.env.VITE_BASE_URL; | ||
|
||
type StandardHeaders = 'Content-Type' | 'Authorization' | 'Accept' | 'Cache-Control' | 'User-Agent'; | ||
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; | ||
|
||
interface FetchOptions extends RequestInit { | ||
method?: RequestMethod; | ||
headers?: Record<StandardHeaders, string>; | ||
} | ||
|
||
const instance = async (url: string, options: FetchOptions = {}) => { | ||
const response = await fetch(`${baseURL}${url}`, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...options.headers, | ||
}, | ||
...options, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('network response가 도착하지 않았어요'); | ||
} | ||
|
||
return response.json(); | ||
}; | ||
|
||
export default instance; |
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
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
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
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