Compare commits

..

2 commits

Author SHA1 Message Date
542e4b52e1
Merge branch 'front' of ssh://meowgit.nekoea.red:22222/nihonium/nyanimedb into front
All checks were successful
Build (frontend build only) / build (push) Successful in 2m55s
2025-12-20 03:29:01 +03:00
24c2450ac1
fix: AuthClient 2025-12-20 03:28:49 +03:00

View file

@ -25,7 +25,6 @@ const baseClient = createClient(createConfig<ClientOptions2>({ baseUrl: '/api/v1
export const authClient: Client = {
...baseClient,
// Force the function to match the expected signature exactly
request: (async <
TData = unknown,
TError = unknown,
@ -36,22 +35,30 @@ export const authClient: Client = {
Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'>
): Promise<RequestResult<TData, TError, ThrowOnError, TResponseStyle>> => {
// Initial request
let result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
// Check for 401
if ((result as any)?.error?.response?.status === 401) {
const refreshed = await getRefreshed();
// 1. Cast to a Record to allow the 'in' operator check on a generic
// We use 'unknown' instead of 'any' to maintain safety.
const resultObj = result as Record<string, unknown>;
if (refreshed) {
// Retry
result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
} else {
localStorage.clear();
window.location.href = "/login";
// 2. Check if the object is valid and contains the error key
if (result && typeof result === 'object' && 'error' in resultObj) {
// 3. Narrow the error property specifically
const error = resultObj.error as { response?: { status?: number } } | null | undefined;
if (error?.response?.status === 401) {
const refreshed = await getRefreshed();
if (refreshed) {
result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
} else {
localStorage.clear();
window.location.href = "/login";
}
}
}
return result as RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
}) as Client['request'], // Cast the function itself to match the Client interface
return result;
}) as Client['request'],
};