Use Vite API base URL for web deploys
This commit is contained in:
1 parent
f80834c6ab
commit
ffcba26e64
5 files changed
+14
-2
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -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<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
let response: Response
|
||||
try {
|
||||
response = await fetch(path, init)
|
||||
response = await fetch(resolveApiUrl(path), init)
|
||||
} catch {
|
||||
throw new ApiError(0, '서버에 연결하지 못했어요. 네트워크를 확인하고 다시 시도해 주세요.')
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user