260206_01_ENH : api 통신 모듈에 캐싱 기능 추가
This commit is contained in:
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user