diff --git a/src/hooks/useAxios.js b/src/hooks/useAxios.js index 9e188ba7..04f1f304 100644 --- a/src/hooks/useAxios.js +++ b/src/hooks/useAxios.js @@ -1,4 +1,4 @@ -import axios from 'axios' +import axios, { Axios } from 'axios' export const AxiosType = { INTERNAL: 'Internal', @@ -6,7 +6,10 @@ export const AxiosType = { } export function useAxios() { - const getInstances = (type) => { + const getInstances = (url) => { + let type = AxiosType.INTERNAL + url.startsWith('http') ? (type = AxiosType.EXTERNAL) : '' + return axios.create({ baseURL: type === AxiosType.INTERNAL ? process.env.NEXT_PUBLIC_API_SERVER_PATH : '', headers: { @@ -28,36 +31,36 @@ export function useAxios() { // } }) - const get = async ({ type, url }) => { - return await getInstances(type) + const get = async ({ url }) => { + return await getInstances(url) .get(url) .then((res) => res.data) .catch(console.error) } - const post = async ({ type, url, data }) => { - return await getInstances(type) + const post = async ({ url, data }) => { + return await getInstances(url) .post(url, data) .then((res) => res.data) .catch(console.error) } - const put = async ({ type, url, data }) => { - return await getInstances(type) + const put = async ({ url, data }) => { + return await getInstances(url) .put(url, data) .then((res) => res.data) .catch(console.error) } - const patch = async ({ type, url, data }) => { - return await getInstances(type) + const patch = async ({ url, data }) => { + return await getInstances(url) .patch(url, data) .then((res) => res.data) .catch(console.error) } - const del = async ({ type, url }) => { - return await getInstances(type) + const del = async ({ url }) => { + return await getInstances(url) .delete(url) .then((res) => res.data) .catch(console.error)