import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
axios.defaults.withCredentials = true;
class HttpClient {
public async get(url: string, config: AxiosRequestConfig): Promise<AxiosResponse> {
const requestConfig: AxiosRequestConfig = {
url,
method: 'get',
...config,
};
return this._sendRequest(requestConfig);
}
public async post(url: string, config: AxiosRequestConfig): Promise<AxiosResponse> {
const requestConfig: AxiosRequestConfig = {
url,
method: 'post',
...config,
};
return this._sendRequest(requestConfig);
}
public async put(url: string, config: AxiosRequestConfig): Promise<AxiosResponse> {
const requestConfig: AxiosRequestConfig = {
url,
method: 'put',
...config,
};
return this._sendRequest(requestConfig);
}
public async patch(url: string, config: AxiosRequestConfig): Promise<AxiosResponse> {
const requestConfig: AxiosRequestConfig = {
url,
method: 'patch',
...config,
};
return this._sendRequest(requestConfig);
}
public async delete(url: string, config: AxiosRequestConfig): Promise<AxiosResponse> {
const requestConfig: AxiosRequestConfig = {
url,
method: 'delete',
...config,
};
return this._sendRequest(requestConfig);
}
public async head(url: string, config: AxiosRequestConfig): Promise<AxiosResponse> {
const requestConfig: AxiosRequestConfig = {
url,
method: 'head',
...config,
};
return this._sendRequest(requestConfig);
}
private async _sendRequest(config: AxiosRequestConfig): Promise<AxiosResponse> {
try {
return await axios(config);
} catch (err) {
console.error(err);
throw err;
}
}
}
export default HttpClient;
|