2026-04-19 10:54:12 +09:00
|
|
|
import { useAuthStore } from '../stores/auth'
|
|
|
|
|
|
2026-04-07 18:55:06 +09:00
|
|
|
export async function api(url, options = {}) {
|
2026-04-13 14:27:00 +09:00
|
|
|
const headers = { 'Content-Type': 'application/json', ...options.headers }
|
|
|
|
|
|
2026-04-19 10:54:12 +09:00
|
|
|
// 관리자 API에는 로그인 다이얼로그에서 저장한 키를 자동으로 헤더에 포함
|
2026-04-13 14:27:00 +09:00
|
|
|
if (url.startsWith('/api/admin')) {
|
2026-04-19 10:54:12 +09:00
|
|
|
const adminKey = useAuthStore.getState().apiKey
|
2026-04-13 14:27:00 +09:00
|
|
|
if (adminKey) headers['x-admin-key'] = adminKey
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-07 18:55:06 +09:00
|
|
|
const res = await fetch(url, {
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
...options,
|
2026-04-13 14:27:00 +09:00
|
|
|
headers,
|
2026-04-07 18:55:06 +09:00
|
|
|
body: options.body ? JSON.stringify(options.body) : undefined,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
const error = await res.json().catch(() => ({}))
|
2026-04-16 08:45:50 +09:00
|
|
|
const e = new Error(error.error || `HTTP ${res.status}`)
|
|
|
|
|
Object.assign(e, error, { status: res.status })
|
|
|
|
|
throw e
|
2026-04-07 18:55:06 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res.json()
|
|
|
|
|
}
|