260206_01_ENH : api 통신 모듈에 캐싱 기능 추가

This commit is contained in:
2026-02-06 11:11:16 +09:00
parent 046351da7a
commit 4b22120f45
3 changed files with 18 additions and 4 deletions

View File

@@ -25,9 +25,9 @@ const RootLayout = ({ children }: { children: React.ReactNode}) => {
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
<AppProvider>{children}</AppProvider>
<Toaster />
</body>
<AppProvider>{children}</AppProvider>
<Toaster />
</body>
</html>
);
}

View File

@@ -33,6 +33,11 @@ export type QueryValue = string | string[] | undefined;
export type Query = Record<string, QueryValue>;
export type PathParams = Record<string, string | number | boolean>;
export type NextFetchOptions = {
revalidate?: number;
tags?: string[];
};
export type RequestOptions = {
method?: HttpMethod;
pathParams?: PathParams;
@@ -47,6 +52,8 @@ export type RequestOptions = {
autoLogoutOnAuthError?: boolean;
expectText?: boolean;
expectBinary?: boolean;
cache?: RequestCache; // "force-cache" | "no-store" | ...
next?: NextFetchOptions; // Next 서버 fetch 확장 옵션
};
export type HttpClientOptions = {
@@ -140,12 +147,18 @@ export const createHttpClient = (baseURL: string, opts: HttpClientOptions = {})
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? defaultTimeoutMs);
const isServer = typeof window === "undefined";
const cacheMode: RequestCache = options.cache ?? (isServer && options.next ? "force-cache" : "no-store");
const nextOpt = isServer ? options.next : undefined;
try {
const res = await fetch(url.toString(), {
method: options.method ?? (body ? "POST" : "GET"),
headers,
body,
cache: "no-store",
cache: cacheMode,
// TS에서 RequestInit에 next가 없어서 캐스팅 필요
...(nextOpt ? ({ next: nextOpt } as any) : {}),
signal: controller.signal,
});