|
|
@ -1,4 +1,4 @@
|
|
|
|
import type { AxiosResponse, GenericAbortSignal } from 'axios'
|
|
|
|
import type { AxiosProgressEvent, AxiosResponse, GenericAbortSignal } from 'axios'
|
|
|
|
import request from './axios'
|
|
|
|
import request from './axios'
|
|
|
|
|
|
|
|
|
|
|
|
export interface HttpOption {
|
|
|
|
export interface HttpOption {
|
|
|
@ -6,6 +6,7 @@ export interface HttpOption {
|
|
|
|
data?: any
|
|
|
|
data?: any
|
|
|
|
method?: string
|
|
|
|
method?: string
|
|
|
|
headers?: any
|
|
|
|
headers?: any
|
|
|
|
|
|
|
|
onDownloadProgress?: (progressEvent: AxiosProgressEvent) => void
|
|
|
|
signal?: GenericAbortSignal
|
|
|
|
signal?: GenericAbortSignal
|
|
|
|
beforeRequest?: () => void
|
|
|
|
beforeRequest?: () => void
|
|
|
|
afterRequest?: () => void
|
|
|
|
afterRequest?: () => void
|
|
|
@ -17,9 +18,11 @@ export interface Response<T = any> {
|
|
|
|
status: string
|
|
|
|
status: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function http<T = any>({ url, data, method, headers, signal, beforeRequest, afterRequest }: HttpOption) {
|
|
|
|
function http<T = any>(
|
|
|
|
|
|
|
|
{ url, data, method, headers, onDownloadProgress, signal, beforeRequest, afterRequest }: HttpOption,
|
|
|
|
|
|
|
|
) {
|
|
|
|
const successHandler = (res: AxiosResponse<Response<T>>) => {
|
|
|
|
const successHandler = (res: AxiosResponse<Response<T>>) => {
|
|
|
|
if (res.data.status === 'Success')
|
|
|
|
if (res.data.status === 'Success' || typeof res.data === 'string')
|
|
|
|
return res.data
|
|
|
|
return res.data
|
|
|
|
|
|
|
|
|
|
|
|
return Promise.reject(res.data)
|
|
|
|
return Promise.reject(res.data)
|
|
|
@ -37,17 +40,18 @@ function http<T = any>({ url, data, method, headers, signal, beforeRequest, afte
|
|
|
|
const params = Object.assign(typeof data === 'function' ? data() : data ?? {}, {})
|
|
|
|
const params = Object.assign(typeof data === 'function' ? data() : data ?? {}, {})
|
|
|
|
|
|
|
|
|
|
|
|
return method === 'GET'
|
|
|
|
return method === 'GET'
|
|
|
|
? request.get(url, { params, signal }).then(successHandler, failHandler)
|
|
|
|
? request.get(url, { params, signal, onDownloadProgress }).then(successHandler, failHandler)
|
|
|
|
: request.post(url, params, { headers, signal }).then(successHandler, failHandler)
|
|
|
|
: request.post(url, params, { headers, signal, onDownloadProgress }).then(successHandler, failHandler)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function get<T = any>(
|
|
|
|
export function get<T = any>(
|
|
|
|
{ url, data, method = 'GET', signal, beforeRequest, afterRequest }: HttpOption,
|
|
|
|
{ url, data, method = 'GET', onDownloadProgress, signal, beforeRequest, afterRequest }: HttpOption,
|
|
|
|
): Promise<Response<T>> {
|
|
|
|
): Promise<Response<T>> {
|
|
|
|
return http<T>({
|
|
|
|
return http<T>({
|
|
|
|
url,
|
|
|
|
url,
|
|
|
|
method,
|
|
|
|
method,
|
|
|
|
data,
|
|
|
|
data,
|
|
|
|
|
|
|
|
onDownloadProgress,
|
|
|
|
signal,
|
|
|
|
signal,
|
|
|
|
beforeRequest,
|
|
|
|
beforeRequest,
|
|
|
|
afterRequest,
|
|
|
|
afterRequest,
|
|
|
@ -55,13 +59,14 @@ export function get<T = any>(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function post<T = any>(
|
|
|
|
export function post<T = any>(
|
|
|
|
{ url, data, method = 'POST', headers, signal, beforeRequest, afterRequest }: HttpOption,
|
|
|
|
{ url, data, method = 'POST', headers, onDownloadProgress, signal, beforeRequest, afterRequest }: HttpOption,
|
|
|
|
): Promise<Response<T>> {
|
|
|
|
): Promise<Response<T>> {
|
|
|
|
return http<T>({
|
|
|
|
return http<T>({
|
|
|
|
url,
|
|
|
|
url,
|
|
|
|
method,
|
|
|
|
method,
|
|
|
|
data,
|
|
|
|
data,
|
|
|
|
headers,
|
|
|
|
headers,
|
|
|
|
|
|
|
|
onDownloadProgress,
|
|
|
|
signal,
|
|
|
|
signal,
|
|
|
|
beforeRequest,
|
|
|
|
beforeRequest,
|
|
|
|
afterRequest,
|
|
|
|
afterRequest,
|
|
|
|