Files
our_recipe_atlas/public/js/api.js
T
2026-08-13 18:36:31 +09:00

22 lines
619 B
JavaScript

export async function apiRequest(path, options = {}) {
const response = await fetch(path, {
...options,
credentials: 'same-origin',
headers: {
...(options.body ? { 'content-type': 'application/json' } : {}),
...options.headers,
},
});
if (response.status === 401) {
window.location.assign('/login');
throw new Error('로그인이 필요합니다.');
}
if (response.status === 204) return null;
const data = await response.json().catch(() => ({}));
if (!response.ok) throw new Error(data.error?.message ?? '요청을 처리하지 못했습니다.');
return data;
}