import axiosClient from "axios";
import { setupInterceptorsTo } from "./setupInterceptors";

// extend the AxiosRequestConfig interface and add two optional options raw and silent. I
// https://dev.to/mperon/axios-error-handling-like-a-boss-333d
declare module "axios" {
  export interface AxiosRequestConfig {
    raw?: boolean;
    silent?: boolean;
  }
}

export const defaultAxiosRequestConfig = {
  mode: "cors",
  cache: "no-cache",
  credentials: "same-origin",
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json; charset=utf-8",
    "Cache-Control": "no-cache",
    "Access-Control-Allow-Origin": "*", // Could work and fix the previous problem, but not in all APIs
    //'api-key': SERVER_API_KEY,
  },
  //timeout: 300,

  // `onUploadProgress` allows handling of progress events for uploads
  // browser only
  //onUploadProgress: function (progressEvent) {
  // Do whatever you want with the native progress event
  //},

  // `onDownloadProgress` allows handling of progress events for downloads
  // browser only
  //onDownloadProgress: function (progressEvent) {
  // Do whatever you want with the native progress event
  //},

  // `cancelToken` specifies a cancel token that can be used to cancel the request
  /*cancelToken: new CancelToken(function (cancel) {
      }),*/
};

/**
 * Creates an initial 'axios' instance with custom settings.
 */

export const createAxiosInstance = () =>
  setupInterceptorsTo(axiosClient.create(defaultAxiosRequestConfig));
