Skip to content

Commit

Permalink
🎨 ApiInstance 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
woobottle committed Dec 26, 2023
1 parent f458dc4 commit f464d6f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// axios의 인스턴스 구현체가 있고 전반적으로 instance를 이용해서 api 콜하는 함수들

export {};
import apiInstance from './instance.api';

const APIS = {
// 아래는 예시
getPosts: async () => {
const { data } = await apiInstance.get('/posts');
return data;
},
};

export default APIS;
35 changes: 35 additions & 0 deletions src/apis/instance.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import axios, { type AxiosInstance } from 'axios';

const BASE_URL = 'https://api.10mm.today';
const BASE_TIMEOUT = 10000;

const setInterceptors = (instance: AxiosInstance) => {
instance.interceptors.request.use(
(config) => {
return config;
},
(error) => {
return Promise.reject(error);
},
);

instance.interceptors.response.use(
(response) => {
return response;
},
(error) => {
return Promise.reject(error);
},
);

return instance;
};

const axiosInstance = setInterceptors(
axios.create({
baseURL: BASE_URL,
timeout: BASE_TIMEOUT,
}),
);

export default axiosInstance;

0 comments on commit f464d6f

Please sign in to comment.