-
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] fetch instance 기능 보강 (#450)
* feat: error 처리 로직 세분화를 위한 custom error 생성 * feat: params 처리 * feat: form data 처리 * refactor: instance 적용 * chore: fetcher로 이름 변경
- Loading branch information
1 parent
cf8a51f
commit f6b0527
Showing
15 changed files
with
95 additions
and
97 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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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'; | ||
|
||
export class CustomError extends Error { | ||
status: number; | ||
|
||
constructor(message: string, status: number) { | ||
super(message); | ||
this.status = status; | ||
} | ||
} | ||
|
||
interface FetchOptions extends Omit<RequestInit, 'body'> { | ||
method?: RequestMethod; | ||
headers?: Partial<Record<StandardHeaders, string>>; | ||
body?: Record<string, unknown>; | ||
params?: Record<string, any>; | ||
} | ||
|
||
const fetcher = async (url: string, options: FetchOptions = {}) => { | ||
const { body, params, headers = {}, ...rest } = options; | ||
const urlWithParams = params ? `${url}?${new URLSearchParams(params).toString()}` : url; | ||
const isFormData = body instanceof FormData; | ||
|
||
if (!isFormData) { | ||
headers['Content-Type'] = 'application/json'; | ||
} | ||
|
||
const response = await fetch(`${baseURL}${urlWithParams}`, { | ||
headers, | ||
body: isFormData ? body : JSON.stringify(body), | ||
...rest, | ||
}); | ||
|
||
if (!response.ok) { | ||
const errMsg = await response.json(); | ||
throw new CustomError(errMsg.userMessage, response.status); | ||
} | ||
|
||
return response.json(); | ||
}; | ||
|
||
export default fetcher; |
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'; | ||
import fetcher from '@apis/fetcher'; | ||
|
||
export const getRecruitingInfo = async () => { | ||
const res = await instance('/recruiting-season/latest', { method: 'GET' }); | ||
const res = await fetcher('/recruiting-season/latest', { method: 'GET' }); | ||
|
||
return res; | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.