-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: default content-type #30
Open
yougyung
wants to merge
4
commits into
main
Choose a base branch
from
fix/content-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,7 +109,7 @@ export type FetchAXDefaultOptions = { | |
}; | ||
const parseResponseData = async <T>( | ||
response: Response, | ||
type: ResponseType, | ||
type?: ResponseType, | ||
): Promise<T> => { | ||
switch (type) { | ||
case 'arraybuffer': | ||
|
@@ -204,6 +204,15 @@ export interface RequestInit extends Omit<globalThis.RequestInit, 'body'> { | |
/** Resposne data's type */ | ||
responseType?: ResponseType; | ||
} | ||
|
||
const isJson = (data: any) => { | ||
try { | ||
return typeof JSON.parse(data) === 'object'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
|
||
const isArrayBufferView = (data: any): data is ArrayBufferView => { | ||
return ( | ||
data && | ||
|
@@ -218,17 +227,10 @@ const isArrayBufferView = (data: any): data is ArrayBufferView => { | |
}; | ||
|
||
const isBodyInit = (data: any): data is BodyInit => { | ||
const isJson = (data: any) => { | ||
try { | ||
return typeof JSON.parse(data) === 'object'; | ||
} catch (e) { | ||
return false; | ||
} | ||
}; | ||
return ( | ||
isJson(data) || // data === 'string' 을 통해서도 JSON인지를 확인할 수 있지만 명시적으로 따지기 위해서 | ||
typeof data === 'string' || | ||
data instanceof ReadableStream || | ||
(typeof ReadableStream !== 'undefined' && data instanceof ReadableStream) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 코드는 어떤 의도로 추가된걸까요 !? 혹시 어떤 오류를 발견하셨나용? |
||
data instanceof Blob || | ||
data instanceof ArrayBuffer || | ||
data instanceof FormData || | ||
|
@@ -288,6 +290,7 @@ const applyDefaultOptionsArgs = ( | |
); | ||
|
||
const requestHeaders: Record<string, string> = {}; | ||
|
||
if (defaultOptions?.headers) { | ||
new Headers(defaultOptions.headers).forEach((value, key) => { | ||
requestHeaders[key] = value; | ||
|
@@ -299,6 +302,10 @@ const applyDefaultOptionsArgs = ( | |
}); | ||
} | ||
|
||
if (requestInit?.data) { | ||
appendJsonContentType(requestInit.data, requestHeaders); | ||
} | ||
|
||
let requestArgs = { | ||
...defaultOptions, | ||
...requestInit, | ||
|
@@ -334,6 +341,16 @@ function isHttpError(response: Response) { | |
return response.status >= 300; | ||
} | ||
|
||
function appendJsonContentType( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저희 rollup 사용한 이후로 파일 분리를 안했네요!! 이 부분 제가 이슈에 추가하겠습니다 |
||
data: Record<string, any> | BodyInit, | ||
requestHeaders: Record<string, string>, | ||
) { | ||
if (isJson(JSON.stringify(data)) && !requestHeaders['content-type']) { | ||
requestHeaders['content-type'] = 'application/json'; | ||
} | ||
return requestHeaders; | ||
} | ||
|
||
function ensureBodyInit(data: BodyInit | Record<string, any>): BodyInit { | ||
if (isBodyInit(data)) { | ||
return data; | ||
|
@@ -565,8 +582,6 @@ const fetchAX = { | |
}; | ||
export default fetchAX; | ||
export const presetOptions: FetchAXDefaultOptions = { | ||
headers: { 'Content-Type': 'application/json' }, | ||
|
||
throwError: true, | ||
|
||
// baseURL: '' | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data가 json인 경우에는 저희가 application/json으로 "설정"해주는 것이기에
is 대신 is set to 요론식으로 작성하면 어떨까요 ~?