JinZHouXiYiJi_DaPin2/examples/utils/http/config.ts

47 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2023-12-05 13:23:01 +08:00
import type { AxiosRequestConfig, Canceler } from 'axios'
/**
*
*/
export const defaultConfig: AxiosRequestConfig = {
baseURL: import.meta.env.VITE_APP_BASE_URL ? import.meta.env.VITE_APP_BASE_URL : '',
//10秒超时
timeout: 10000,
headers: { 'Content-Type': 'application/json;charset=UTF-8' }
}
export function httpConfig(config?: AxiosRequestConfig): AxiosRequestConfig {
if (!config) {
return defaultConfig
}
const { headers } = config
if (headers && typeof headers === 'object') {
defaultConfig.headers = {
...defaultConfig.headers,
...headers
}
}
return { ...excludeProps(config, 'headers'), ...defaultConfig }
}
// 取消指定的属性
export function excludeProps<T extends { [key: string]: any }>(
origin: T,
prop: string
): { [key: string]: T } {
return Object.keys(origin)
.filter((key) => !prop.includes(key))
.reduce((res, key) => {
res[key] = origin[key]
return res
}, {} as { [key: string]: T })
}
export type CancelTokenType = { cancelKey: string; cancelExecutor: Canceler }
export interface ResultType<T = any> {
data: T
code: number
message: string
}