// request
import axios, {
type AxiosInstance,
type AxiosResponse,
type CancelTokenSource,
} from 'axios'
import type { IResponse, InterceptorType, RequestConfig } from './type'
class Request {
Instance: AxiosInstance | null
constructor(
// Base path for instance requests
private readonly baseURL: string,
// Timeout for instance requests
private readonly timeOUT: number,
// Instance interceptors
private readonly interceptor?: InterceptorType,
// Cancel token
private cancelTokenSource: CancelTokenSource | null = null,
) {
// Create an instance
this.Instance = axios.create({
baseURL: this.baseURL,
timeout: this.timeOUT,
})
// Configure request interception for the instance
if (this.interceptor?.requestInterceptor) {
this.Instance.interceptors.request.use(
this.interceptor.requestInterceptor.onFulfilled,
this.interceptor.requestInterceptor.onRejected,
this.interceptor.requestInterceptor.options,
)
}
// Configure response interception for the instance
if (this.interceptor?.responseInterceptor) {
this.Instance.interceptors.response.use(
this.interceptor.responseInterceptor.onFulfilled,
this.interceptor.responseInterceptor.onRejected,
this.interceptor.responseInterceptor.options,
)
}
}
// Instance method request
public request<T = AxiosResponse<any>>(config: RequestConfig): Promise<T> {
return new Promise<T>((resolve, reject) => {
// Request interception for a single interface
if (config.requestInterceptor) {
this.Instance?.interceptors.request.use(
config.requestInterceptor.onFulfilled,
config.requestInterceptor.onRejected,
config.requestInterceptor.options,
)
}
// Response interception for a single interface
if (config.responseInterceptor) {
this.Instance?.interceptors.response.use(
config.responseInterceptor.onFulfilled,
config.responseInterceptor.onRejected,
config.responseInterceptor.options,
)
}
this.cancelTokenSource = axios.CancelToken.source()
this.Instance?.request<T>({
...config,
cancelToken: this.cancelTokenSource.token,
})
.then((res) => {
resolve(res as any)
})
.catch((err) => {
reject(err)
})
})
}
// Cancel request
public cancelRequest(): void {
if (this.cancelTokenSource) {
this.cancelTokenSource.cancel('Cancel request')
}
}
public get<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'GET', ...config })
}
public post<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'POST', ...config })
}
public delete<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'DELETE', ...config })
}
public patch<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'PATCH', ...config })
}
public put<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'PUT', ...config })
}
public head<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'HEAD', ...config })
}
public options<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'OPTIONS', ...config })
}
public trace<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'TRACE', ...config })
}
public connect<T = any>(config: RequestConfig) {
return this.request<T>({ method: 'CONNECT', ...config })
}
}