Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/shared/apis/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL;
const apiTeamId = process.env.NEXT_PUBLIC_API_TEAM_ID;

if (!apiBaseUrl) {
throw new Error('NEXT_PUBLIC_API_BASE_URL is not defined.');
}
if (!apiTeamId) {
throw new Error('NEXT_PUBLIC_API_TEAM_ID is not defined.');
}

export const BASE_URL = apiBaseUrl;
export const TEAM_ID = apiTeamId;
// 빌드 시점이 아닌 실제 API 호출 시점에 유효성 검사를 수행하기 위해 undefined 허용
export const BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL;
export const TEAM_ID = process.env.NEXT_PUBLIC_API_TEAM_ID;
7 changes: 5 additions & 2 deletions src/shared/apis/fetchApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BASE_URL, TEAM_ID } from './config';

const BODYLESS_METHODS = new Set(['GET', 'HEAD']);
const NORMALIZED_BASE_URL = normalizeBaseUrl(BASE_URL);
const DEV_ACCESS_TOKEN = process.env.NEXT_PUBLIC_DEV_ACCESS_TOKEN;
const IS_DEVELOPMENT = process.env.NODE_ENV === 'development';

Expand All @@ -14,8 +13,12 @@ function normalizePath(path: string) {
}

function buildApiUrl(path: string) {
// 실제 API 호출 시점에 환경변수 유효성 검사
if (!BASE_URL) throw new Error('NEXT_PUBLIC_API_BASE_URL is not defined.');
if (!TEAM_ID) throw new Error('NEXT_PUBLIC_API_TEAM_ID is not defined.');
Comment on lines +17 to +18

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

두 환경 변수를 각각 확인하는 대신, 한 번에 확인하여 누락된 모든 환경 변수를 에러 메시지에 포함시키는 것이 좋습니다. 이렇게 하면 설정 문제를 디버깅할 때 한 번에 모든 누락된 변수를 파악할 수 있어 더 효율적입니다.

  const missingVars: string[] = [];
  if (!BASE_URL) {
    missingVars.push('NEXT_PUBLIC_API_BASE_URL');
  }
  if (!TEAM_ID) {
    missingVars.push('NEXT_PUBLIC_API_TEAM_ID');
  }
  if (missingVars.length > 0) {
    throw new Error('Missing required environment variables: ' + missingVars.join(', '));
  }


const relativePath = `${TEAM_ID}/${normalizePath(path)}`;
return new URL(relativePath, NORMALIZED_BASE_URL).toString();
return new URL(relativePath, normalizeBaseUrl(BASE_URL)).toString();
}

function getMethod(options: RequestInit) {
Expand Down
Loading