chore(FE-40): set opts.auth default to 'cookie' and export SWRHttpKey type

This commit is contained in:
ValdiANS
2025-10-01 14:08:02 +07:00
parent 3d3df42576
commit fa96d7a98a
+8 -4
View File
@@ -9,7 +9,9 @@ export async function httpClient<T, B = unknown>(
path: string,
opts: RequestOptions<B> = {}
): Promise<T> {
const isCookieAuth = opts.auth === 'cookie';
const isCookieAuth =
opts.auth === 'cookie' ||
(!opts.auth && opts.auth !== 'none' && opts.auth !== 'bearer');
const isBearerAuth = opts.auth === 'bearer' && !!opts.token;
const config: AxiosRequestConfig = {
@@ -18,11 +20,13 @@ export async function httpClient<T, B = unknown>(
params: opts.query,
data: opts.body,
timeout: opts.timeoutMs ?? 10_000,
withCredentials: isCookieAuth,
withCredentials: isCookieAuth && !isBearerAuth,
headers: {
'Content-Type': 'application/json',
...(opts.headers ?? {}),
...(isBearerAuth ? { Authorization: `Bearer ${opts.token}` } : {}),
...(isBearerAuth && !isCookieAuth
? { Authorization: `Bearer ${opts.token}` }
: {}),
},
};
@@ -34,7 +38,7 @@ export async function httpClient<T, B = unknown>(
}
}
type SWRHttpKey<B = unknown> =
export type SWRHttpKey<B = unknown> =
| string
| [path: string, opts?: RequestOptions<B>]
| { path: string; opts?: RequestOptions<B> };