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", //'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. */ const axiosInstance = setupInterceptorsTo(axiosClient.create(defaultAxiosRequestConfig)); /** * Handle all responses. */ /*axiosInstance.interceptors.response.use( (response) => { if (!response) { return Promise.resolve({ statusCode: 500, body: null, }); } const { data, headers } = response; const DTOBody = !isArray(data) ? data : { items: data, totalCount: headers["x-total-count"] ? parseInt(headers["x-total-count"]) : data.length, }; const result = { statusCode: response.status, body: DTOBody, }; //console.log('Axios OK => ', result); return Promise.resolve(result); }, (error) => { console.group("Axios error:"); if (error.response) { // La respuesta fue hecha y el servidor respondió con un código de estado // que esta fuera del rango de 2xx console.log("1 => El servidor respondió con un código de estado > 200"); console.log(error.response.data); console.log(error.response.status); } else if (error.request) { // La petición fue hecha pero no se recibió respuesta console.log("2 => El servidor no respondió"); console.log(error.request); } else { // Algo paso al preparar la petición que lanzo un Error console.log("3 => Error desconocido"); console.log("Error", error.message); } const customError = { message: error.response?.data?.message, statusCode: error.response?.status, }; console.log("Axios BAD => ", error, customError); console.groupEnd(); return Promise.reject(customError); } );*/ /** * Replaces main `axios` instance with the custom-one. * * @param config - Axios configuration object. * @returns A promise object of a response of the HTTP request with the 'data' object already * destructured. */ /*const axios = (config: AxiosRequestConfig) => axiosInstance.request(config); export default axios;*/ export const createAxiosInstance = () => axiosInstance;