-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f049ba
commit fd790fa
Showing
8 changed files
with
70 additions
and
78 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
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 |
---|---|---|
@@ -1,53 +1,28 @@ | ||
import FormData from 'form-data'; | ||
import type { AxiosHeaderValue, InternalAxiosRequestConfig } from 'axios'; | ||
import type { ContentType } from './type'; | ||
import type { AxiosHeaderValue, AxiosResponse, InternalAxiosRequestConfig } from 'axios'; | ||
|
||
export function transformRequestData(data: any, contentType: AxiosHeaderValue) { | ||
if (typeof contentType !== 'string') return data; | ||
|
||
const $contentType = contentType as ContentType; | ||
|
||
// 1. "application/json": do not transform | ||
|
||
// 2 "application/x-www-form-urlencoded": transform to query string | ||
if ($contentType === 'application/x-www-form-urlencoded' && typeof data === 'object') { | ||
const params = new URLSearchParams(); | ||
Object.keys(data).forEach(key => { | ||
params.append(key, data[key]); | ||
}); | ||
|
||
return params; | ||
} | ||
|
||
// 3. "multipart/form-data": transform to FormData | ||
if ($contentType === 'multipart/form-data' && typeof data === 'object') { | ||
const formData = new FormData(); | ||
const entries = Object.entries(data); | ||
|
||
entries.forEach(([key, value]) => { | ||
if (isFiles(value)) { | ||
value.forEach(file => { | ||
formData.append(key, file); | ||
}); | ||
} else { | ||
formData.append(key, value); | ||
} | ||
}); | ||
} | ||
export function getContentType(config: InternalAxiosRequestConfig) { | ||
const contentType: AxiosHeaderValue = config.headers?.['Content-Type'] || 'application/json'; | ||
|
||
return data; | ||
return contentType; | ||
} | ||
|
||
function isFiles(data: File[] | unknown): data is File[] { | ||
const isArray = Array.isArray(data); | ||
const hasData = isArray && data.length > 0; | ||
const isFile = hasData && Object.prototype.toString.call(data[0]) === '[object File]'; | ||
|
||
return isFile; | ||
/** | ||
* check if http status is success | ||
* | ||
* @param status | ||
*/ | ||
export function isHttpSuccess(status: number) { | ||
const isSuccessCode = status >= 200 && status < 300; | ||
return isSuccessCode || status === 304; | ||
} | ||
|
||
export function getContentType(config: InternalAxiosRequestConfig) { | ||
const contentType: AxiosHeaderValue = config.headers?.['Content-Type'] || 'application/json'; | ||
/** | ||
* is response json | ||
* | ||
* @param response axios response | ||
*/ | ||
export function isResponseJson(response: AxiosResponse) { | ||
const { responseType } = response.config; | ||
|
||
return contentType; | ||
return responseType === 'json' || responseType === undefined; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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