refactor: Add HTTP headers to API requests.

- Add headers to the fetch and refresh access token functions
- Allow for optional headers in request data interface
- Fix syntax error in headers object_literals in request method
- Use default configuration instead of creating a new object in request method
This commit is contained in:
zhl 2023-03-27 21:36:49 +08:00
parent 8d0a054a44
commit 1aa0e1b6d0
2 changed files with 4 additions and 1 deletions

View File

@ -3,6 +3,7 @@ export interface IReqData {
url: string
method?: string
data?: any
headers?: any
}
export class NetClient {
httpGet(reqData: IReqData | string): Promise<any> {
@ -25,7 +26,7 @@ export class NetClient {
async request(data: AxiosRequestConfig): Promise<any> {
let defaultCfg: AxiosRequestConfig = {
method: 'get',
headers: {'Content-Type': 'application/json'}
headers: { 'Content-Type': 'application/json' },
}
Object.assign(defaultCfg, data)
console.log(defaultCfg)

View File

@ -18,6 +18,7 @@ export function fetchAccessToken(code: string) {
return new NetClient().httpPost({
url: url_access_token,
method: 'post',
headers: { 'Content-Type': 'text/html;' },
})
}
@ -29,5 +30,6 @@ export function refreshAccessToken(refresh_token: string) {
return new NetClient().httpPost({
url: url_refresh_token,
method: 'post',
headers: { 'Content-Type': 'text/html;' },
})
}