All files / src/util httpClient.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 711x   1x   1x 1x 2x         2x     1x 3x         3x     1x                 1x                 1x                 1x                 1x   5x   1x 1x     1x   1x  
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;