Download web-development/frontend/lib/api.ts from apple-retail-study/Apple-Retail-Sales-Strategy: direct link, hf CLI and curl.
- Browser
- Download file 2.91 kB
-
https://huggingface.co/spaces/apple-retail-study/Apple-Retail-Sales-Strategy/resolve/main/web-development/frontend/lib/api.ts
- Command line
-
hf download hf://spaces/apple-retail-study/Apple-Retail-Sales-Strategy/web-development/frontend/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/apple-retail-study/Apple-Retail-Sales-Strategy/resolve/main/web-development/frontend/lib/api.ts
2.91 kB
| /** | |
| * ํด๋ผ์ด์ธํธ ์ ์ฉ: ๋ฐฑ์๋ ๋ฒ ์ด์ค URL. | |
| * API ๊ฒฝ๋ก๋ ๋ฐ๋์ '/api/...' ์๋ ๊ฒฝ๋ก๋ก ํธ์ถํ๋ฉด next.config.js rewrites ๊ท์น(source: /api/:path*)๊ณผ ์ผ์นํฉ๋๋ค. | |
| * NEXT_PUBLIC_API_URL ๋ฏธ์ค์ / localhost ์ vercel.app์์๋ fallback ์ฌ์ฉ. | |
| */ | |
| const { PRODUCTION_BACKEND_URL } = require('./backend-url.js'); | |
| const API_BASE_FALLBACK = | |
| (typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_FALLBACK_BACKEND_URL) || | |
| PRODUCTION_BACKEND_URL; | |
| function isUnsafeApiBase(url: string | undefined): boolean { | |
| if (url == null || typeof url !== 'string') return true; | |
| const u = url.trim().toLowerCase(); | |
| if (!u.startsWith('http')) return true; | |
| if (u.includes('localhost') || u.startsWith('http://127.0.0.1') || u.startsWith('http://[::1]')) return true; | |
| return false; | |
| } | |
| /** ํด๋ผ์ด์ธํธ์ฉ API ๋ฒ ์ด์ค URL (์๋๊ฒฝ๋ก ์ ''). export for page.tsx health/apple-data ๋ฑ. */ | |
| export function getApiBase(): string { | |
| if (typeof window === 'undefined') return ''; | |
| const env = process.env.NEXT_PUBLIC_API_URL; | |
| const isVercel = typeof window !== 'undefined' && window.location?.hostname?.includes('vercel.app'); | |
| if (env != null && typeof env === 'string' && env.trim() !== '' && !isUnsafeApiBase(env)) return env.trim().replace(/\/$/, ''); | |
| if (isVercel) return API_BASE_FALLBACK; | |
| return ''; | |
| } | |
| const API_TIMEOUT_MS = 25000; | |
| /** API ํธ์ถ: NEXT_PUBLIC_API_URL ์ฐ์ . ์ค์ ์ ํด๋น URL๋ง ์ฌ์ฉ, ๋ฏธ์ค์ ์ ์๋๊ฒฝ๋ก(rewrites)๋ง ์๋. localhost ๋ฏธ์ฌ์ฉ. */ | |
| export async function apiGet<T = unknown>(path: string): Promise<T | null> { | |
| const fixed = getApiBase(); | |
| const bases: string[] = fixed ? [fixed] : ['']; | |
| for (const base of bases) { | |
| try { | |
| const url = base ? `${base}${path}` : path; | |
| const ac = new AbortController(); | |
| const t = setTimeout(() => ac.abort(), API_TIMEOUT_MS); | |
| const res = await fetch(url, { signal: ac.signal, cache: 'no-store' }); | |
| clearTimeout(t); | |
| if (res?.ok) return (await res.json()) as T; | |
| } catch { | |
| continue; | |
| } | |
| } | |
| return null; | |
| } | |
| /** POST ์์ฒญ: NEXT_PUBLIC_API_URL ์ฌ์ฉ. ๋ฏธ์ค์ ์ ์๋๊ฒฝ๋ก๋ง. localhost ๋ฏธ์ฌ์ฉ. */ | |
| export async function apiPost<T = unknown>(path: string, body: object): Promise<T | null> { | |
| const fixed = getApiBase(); | |
| const bases: string[] = fixed ? [fixed] : ['']; | |
| for (const base of bases) { | |
| try { | |
| const url = base ? `${base}${path}` : path; | |
| const ac = new AbortController(); | |
| const t = setTimeout(() => ac.abort(), API_TIMEOUT_MS); | |
| const res = await fetch(url, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(body), | |
| signal: ac.signal, | |
| cache: 'no-store', | |
| }); | |
| clearTimeout(t); | |
| if (res?.ok) return (await res.json()) as T; | |
| } catch { | |
| continue; | |
| } | |
| } | |
| return null; | |
| } | |