forked from mogua-station/FE
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Typo(mogua-station#123): fetch 모듈 함수 이름 변경
- Loading branch information
Showing
2 changed files
with
29 additions
and
55 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
interface FetchOptions extends RequestInit { | ||
auth?: boolean; | ||
} | ||
|
||
/** | ||
* 토큰 주입 방식 | ||
* 1. 유저가 필요한 요청: | ||
* await fetcher('/user/profile/me', token, {auth: true}); | ||
* 2. 유저가 필요 없는 요청: | ||
* await fetcher('/user/userId, token); | ||
*/ | ||
export async function fetcher( | ||
url: string, | ||
token: string, | ||
{ auth, headers, ...options }: FetchOptions = {}, | ||
) { | ||
const config = { | ||
...options, | ||
headers: { | ||
...(!(options.body instanceof FormData) && { | ||
"Content-Type": "application/json", | ||
}), | ||
...(auth && token && { Authorization: `Bearer ${token}` }), | ||
...headers, | ||
}, | ||
}; | ||
|
||
return fetch(`${process.env.NEXT_PUBLIC_BASE_URL}${url}`, config); | ||
} |