4.2 공통 fetcher/에러 형식/재시도·백오프 설정 o
This commit is contained in:
33
src/lib/api.ts
Normal file
33
src/lib/api.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
[상태관리/데이터]
|
[상태관리/데이터]
|
||||||
4.1 React Query 설치 및 Provider 구성 o
|
4.1 React Query 설치 및 Provider 구성 o
|
||||||
4.2 공통 fetcher/에러 형식/재시도·백오프 설정
|
4.2 공통 fetcher/에러 형식/재시도·백오프 설정 o
|
||||||
4.3 캐시 키/무효화 전략 수립 및 적용
|
4.3 캐시 키/무효화 전략 수립 및 적용
|
||||||
4.4 낙관적 업데이트 패턴 적용(작성/수정)
|
4.4 낙관적 업데이트 패턴 적용(작성/수정)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user