Skip to content

Commit

Permalink
[#24] api: useAxios hook
Browse files Browse the repository at this point in the history
  • Loading branch information
MyungJiwoo committed Apr 10, 2024
1 parent b557c12 commit e0d4b6f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 68 deletions.
108 changes: 58 additions & 50 deletions app/hooks/useAxios.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
import { useState, useEffect } from 'react';
import { AxiosError, AxiosInstance } from 'axios';
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { axiosInstance } from '../lib/api';
import { useState, useEffect } from "react";
import { AxiosError, AxiosInstance } from "axios";
import axios, { AxiosResponse, AxiosRequestConfig } from "axios";
import { axiosInstance } from "../lib/api";

const useAxios = <T>(
url: string,
method: 'get' | 'post' | 'delete' | 'patch',
requestBody?: any,
params?: AxiosRequestConfig
url: string,
method: "get" | "post" | "delete" | "patch",
requestBody?: any,
params?: AxiosRequestConfig
) => {
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [data, setData] = useState<T | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
const fetchData = async () => {
try {
let response: AxiosResponse<T>;
setLoading(true);
switch (method) {
case 'get':
response = await axiosInstance.get(url, params);
break;
case 'post':
response = await axiosInstance.post(url, requestBody, params);
break;
case 'delete':
response = await axiosInstance.delete(url, {
data: requestBody,
params,
});
break;
case 'patch':
response = await axiosInstance.patch(url, requestBody, params);
break;
}
setData(response.data);
setLoading(false);
} catch (e) {
setError('네트워크 요청에 실패했어요');
if (e instanceof AxiosError) {
setError(e.message);
}
useEffect(() => {
const fetchData = async () => {
try {
let response: AxiosResponse<T>;
setLoading(true);
switch (method) {
case "get":
response = await axiosInstance.get(url, params);
break;
case "post":
response = await axiosInstance.post(
url,
requestBody,
params
);
break;
case "delete":
response = await axiosInstance.delete(url, {
data: requestBody,
params,
});
break;
case "patch":
response = await axiosInstance.patch(
url,
requestBody,
params
);
break;
}
setData(response.data);
setLoading(false);
} catch (e) {
setError("네트워크 요청에 실패했어요");
if (e instanceof AxiosError) {
setError(e.message);
}

setLoading(false);
}
};
fetchData();
}, [url, method, requestBody]);
setLoading(false);
}
};
fetchData();
}, []);

return {
data,
loading,
error,
};
return {
data,
loading,
error,
};
};

export { useAxios };
36 changes: 18 additions & 18 deletions app/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import axios, { AxiosError, AxiosRequestConfig } from 'axios';
import axios, { AxiosError, AxiosRequestConfig } from "axios";

export const axiosInstance = axios.create({
baseURL: `${process.env.NEXTAUTH_URL}`,
withCredentials: true,
timeout: 3000,
baseURL: `http://localhost:3000`,
withCredentials: true,
timeout: 3000,
});

const handleTokenError = async (err: AxiosError) => {
try {
await fetch('/api/refresh_token');
return axiosInstance.request(err.config as AxiosRequestConfig<any>);
} catch (e) {
return Promise.reject(err);
}
try {
await fetch("/api/refresh_token");
return axiosInstance.request(err.config as AxiosRequestConfig<any>);
} catch (e) {
return Promise.reject(err);
}
};

axiosInstance.interceptors.response.use(
(res) => {
return res;
},
(error: AxiosError) => {
if (error.response && error.response.status === 401) {
return handleTokenError(error);
(res) => {
return res;
},
(error: AxiosError) => {
if (error.response && error.response.status === 401) {
return handleTokenError(error);
}
return Promise.reject(error);
}
return Promise.reject(error);
}
);

0 comments on commit e0d4b6f

Please sign in to comment.