feat: 프로젝트 세팅
This commit is contained in:
37
src/shared/lib/apiClient.ts
Normal file
37
src/shared/lib/apiClient.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* VibeRoom 공통 API 클라이언트
|
||||
* 환경 변수(NEXT_PUBLIC_API_BASE_URL)를 기반으로 자동으로 Base URL이 설정됩니다.
|
||||
*
|
||||
* - npm run dev 실행 시: https://api-dev.viberoom.io (from .env.development)
|
||||
* - 빌드 후 운영 환경: https://api.viberoom.io (from .env.production)
|
||||
*/
|
||||
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
export const apiClient = async <T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> => {
|
||||
// 엔드포인트 앞의 슬래시(/) 중복 방지
|
||||
const url = `${API_BASE_URL}${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`;
|
||||
|
||||
const defaultHeaders = {
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
...defaultHeaders,
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
// 향후 서비스 무드에 맞춰 "잠시 연결이 원활하지 않아요. 다시 시도해 주세요." 와 같이
|
||||
// 부드러운 에러 핸들링을 추가할 수 있는 진입점입니다.
|
||||
throw new Error(`API 요청 실패: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json() as Promise<T>;
|
||||
};
|
||||
Reference in New Issue
Block a user