import { useMemo } from 'react' import axios, { AxiosInstance, AxiosResponse, CreateAxiosDefaults } from 'axios' export interface AxiosAPI { get: (url: string) => Promise> post: (url: string, data?: D) => Promise> del: (url: string) => Promise> axiosInstance: AxiosInstance } const debugResponseHandler = (response: T) => { console.debug('Response', response) return response } export const useAxios = (defaultConfig: CreateAxiosDefaults): AxiosAPI => { const axiosInstance = useMemo(() => axios.create(defaultConfig), [defaultConfig]) return useMemo((): AxiosAPI => ({ get: (url: string) => { console.debug(`GET ${url}`) return axiosInstance.get, never>(url) .then(debugResponseHandler) }, post: (url: string, data?: D) => { console.debug(`POST ${url}`, data) return axiosInstance.post, D>(url, data) .then(debugResponseHandler) }, del: (url: string) => { console.debug(`DELETE ${url}`) return axiosInstance.delete, never>(url) .then(debugResponseHandler) }, axiosInstance }), [axiosInstance]) }