44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
|
import axios, {AxiosInstance, AxiosRequestConfig} from "axios";
|
||
|
|
||
|
type TAxiosOption = {
|
||
|
baseURL: string;
|
||
|
timeout: number;
|
||
|
}
|
||
|
|
||
|
// const config = {
|
||
|
// baseURL: '/',
|
||
|
// timeout: 120000
|
||
|
// }
|
||
|
|
||
|
class Http {
|
||
|
service: AxiosInstance;
|
||
|
constructor(config:TAxiosOption) {
|
||
|
this.service = axios.create(config);
|
||
|
this.service.defaults.withCredentials = true
|
||
|
|
||
|
}
|
||
|
|
||
|
get<T>(url: string, params?: object, _object = {}): Promise<IResponseData<T>> {
|
||
|
return this.service.get(url, { params, ..._object })
|
||
|
}
|
||
|
post<T>(url: string, data?: object, _object:AxiosRequestConfig = {}): Promise<IResponseData<T>> {
|
||
|
return this.service.post(url, data, _object)
|
||
|
}
|
||
|
put<T>(url: string, params?: object, _object = {}): Promise<IResponseData<T>> {
|
||
|
return this.service.put(url, params, _object)
|
||
|
}
|
||
|
delete<T>(url: string, params?: any, _object = {}): Promise<IResponseData<T>> {
|
||
|
return this.service.delete(url, { params, ..._object })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default Http
|
||
|
|
||
|
export interface IResponseData<T> {
|
||
|
success: boolean;
|
||
|
message?:string;
|
||
|
data:T;
|
||
|
code: string;
|
||
|
error?:string
|
||
|
}
|