blueeye-3184
feat: Inventory Action Center ๅช›์’–๊ฝ‘ ่ซ›?Vercel/GitHub ?๊ณ•๋ฃž ?๋บฃโ”
1248c60
Raw History Blame Contribute Delete
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;
}