diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 0000000..4576208 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,33 @@ +export type ApiError = { + message: string; + status: number; + details?: unknown; +}; + +export async function fetchJson(input: RequestInfo | URL, init?: RequestInit): Promise { + const res = await fetch(input, { + headers: { "Content-Type": "application/json", ...(init?.headers ?? {}) }, + ...init, + }); + const text = await res.text(); + const data = text ? safeJson(text) : undefined; + if (!res.ok) { + const err: ApiError = { + message: (data as any)?.error || res.statusText || "Request failed", + status: res.status, + details: data, + }; + throw err; + } + return (data as T) ?? ({} as T); +} + +function safeJson(text: string): unknown { + try { + return JSON.parse(text); + } catch { + return text; + } +} + + diff --git a/todolist.txt b/todolist.txt index 68aa784..570a9c3 100644 --- a/todolist.txt +++ b/todolist.txt @@ -22,7 +22,7 @@ [상태관리/데이터] 4.1 React Query 설치 및 Provider 구성 o -4.2 공통 fetcher/에러 형식/재시도·백오프 설정 +4.2 공통 fetcher/에러 형식/재시도·백오프 설정 o 4.3 캐시 키/무효화 전략 수립 및 적용 4.4 낙관적 업데이트 패턴 적용(작성/수정)