diff --git a/apps/web/.env.production b/apps/web/.env.production new file mode 100644 index 0000000..8c65d40 --- /dev/null +++ b/apps/web/.env.production @@ -0,0 +1 @@ +VITE_API_BASE_URL=https://persona-lenz-api.dahyeon.us diff --git a/apps/web/.env.staging b/apps/web/.env.staging new file mode 100644 index 0000000..03db7e1 --- /dev/null +++ b/apps/web/.env.staging @@ -0,0 +1 @@ +VITE_API_BASE_URL=https://persona-lenz-api-staging.dahyeon.us diff --git a/apps/web/src/config.ts b/apps/web/src/config.ts new file mode 100644 index 0000000..0c1b744 --- /dev/null +++ b/apps/web/src/config.ts @@ -0,0 +1,9 @@ +export const API_BASE_URL = ( + import.meta.env.VITE_API_BASE_URL ?? '' +).replace(/\/$/, '') + +export function resolveApiUrl(path: string): string { + if (/^https?:\/\//i.test(path)) return path + const normalizedPath = path.startsWith('/') ? path : `/${path}` + return API_BASE_URL ? `${API_BASE_URL}${normalizedPath}` : normalizedPath +} diff --git a/apps/web/src/lib/api.ts b/apps/web/src/lib/api.ts index 9767783..be45f27 100644 --- a/apps/web/src/lib/api.ts +++ b/apps/web/src/lib/api.ts @@ -5,6 +5,7 @@ import type { PlayerAnalysisDto, SimilarPlayersDto, } from '@persona-lenz/shared' +import { resolveApiUrl } from '../config' export class ApiError extends Error { readonly status: number @@ -19,7 +20,7 @@ export class ApiError extends Error { async function request(path: string, init?: RequestInit): Promise { let response: Response try { - response = await fetch(path, init) + response = await fetch(resolveApiUrl(path), init) } catch { throw new ApiError(0, '서버에 연결하지 못했어요. 네트워크를 확인하고 다시 시도해 주세요.') } diff --git a/scripts/deploy-cloudflare-pages.mjs b/scripts/deploy-cloudflare-pages.mjs index 926a080..f2145ab 100644 --- a/scripts/deploy-cloudflare-pages.mjs +++ b/scripts/deploy-cloudflare-pages.mjs @@ -237,7 +237,7 @@ function validateEnv(mode, env) { function resolveRequiredEnvKeys(env) { const rawKeys = env.CF_PAGES_REQUIRED_ENV_KEYS?.trim(); - if (!rawKeys) return []; + if (!rawKeys) return ["VITE_API_BASE_URL"]; return rawKeys .split(",") .map((key) => key.trim())