Axios

Axios config


import axios, { AxiosRequestConfig, AxiosError } from "axios";
import { getCustomCookie, putCustomCookie } from "./customCookies";

const isDevEnv = import.meta.env.MODE === "development";
const baseURL = import.meta.env.VITE_API_URL;

function logWarning(...text: string[]) {
  if (isDevEnv) {
    console.log(text);
  }
}
function logError(...text: string[]) {
  if (isDevEnv) {
    console.error(text);
  }
}

interface ResponseSuccess<T> {
  data: T;
  error: null;
}

interface ResponseError {
  data: null;
  error: { status: number; data: string };
}

type ResponseData<T> = Promise<ResponseSuccess<T> | ResponseError>;

async function requestConstructor<T>(
  instance: AxiosInstance,
  options: AxiosRequestConfig
): ResponseData<T> {
  const token = getCustomCookie<string>("cookie_token");
  if (!token) {
    instance.defaults.headers.common["Authorization"] = token;
  }
  try {
    const { data } = await instance.request<T>(options);
    return { data, error: null };
  } catch (error) {
    if (axios.isAxiosError(error)) {
      if (error.response) {
        return {
          error: { status: error.response.status, data: error.response.data },
          data: null,
        };
      } else if (error.request) {
        return {
          error: { status: error.request.status, data: error.request.data },
          data: null,
        };
      } else {
        return { error: { status: 400, data: error.message }, data: null };
      }
    }
    console.log("Error", error?.config);
    return { data: null, error: { status: 400, data: "Nesto ne radi" } };
  }
}
const instance = axios.create({
  baseURL: "https://some-domain.com/api/",
  timeout: 10000,
});

type RequestConfig = Omit<
  AxiosRequestConfig,
  "url" | "data" | "baseUrl" | "method"
>;

async function get<T>(url: string, config: RequestConfig = {}) {
  return requestConstructor<T>(instance, { ...config, url, method: "get" });
}
async function post<T, K>(url: string, data: K, config: RequestConfig = {}) {
  return requestConstructor<T>(instance, {
    ...config,
    url,
    data,
    method: "post",
  });
}
async function put<T, K>(url: string, data: K, config: RequestConfig = {}) {
  return requestConstructor<T>(instance, {
    ...config,
    url,
    data,
    method: "put",
  });
}
async function remove<T, K>(url: string, data?: K, config: RequestConfig = {}) {
  return requestConstructor<T>(instance, {
    ...config,
    url,
    data,
    method: "delete",
  });
}

const fetchData = { get, post, put, delete: remove };
export const getCustomCookie = <T>(name: string) => {
  const cookies = document.cookie.split(";");

  for (let i = 0; i < cookies.length; i++) {
    const cookiePair = cookies[i].split("=");
    cookiePair[0] = cookiePair[0].trim();
    if (cookiePair[0] === name) {
      try {
        return JSON.parse(cookiePair[1]) as T;
      } catch (error) {
        return cookiePair[1] as T;
      }
    }
  }
  return null;
};

export const putCustomCookie = () => {};
interface Book {
  name: string;
  author: string;
}

const books = await fetchData.get<Book[]>("/book");
const newBook = await fetchData.post<Book, Book>("/book", {
  name: "",
  author: "",
});