4.2 공통 fetcher/에러 형식/재시도·백오프 설정 o

This commit is contained in:
koreacomp5
2025-10-09 15:15:32 +09:00
parent 55c0f6abb1
commit 2b85741955
2 changed files with 34 additions and 1 deletions

33
src/lib/api.ts Normal file
View File

@@ -0,0 +1,33 @@
export type ApiError = {
message: string;
status: number;
details?: unknown;
};
export async function fetchJson<T>(input: RequestInfo | URL, init?: RequestInit): Promise<T> {
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;
}
}

View File

@@ -22,7 +22,7 @@
[상태관리/데이터]
4.1 React Query 설치 및 Provider 구성 o
4.2 공통 fetcher/에러 형식/재시도·백오프 설정
4.2 공통 fetcher/에러 형식/재시도·백오프 설정 o
4.3 캐시 키/무효화 전략 수립 및 적용
4.4 낙관적 업데이트 패턴 적용(작성/수정)