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 = { export const authClient: Client = {
...baseClient, ...baseClient,
// Force the function to match the expected signature exactly
request: (async < request: (async <
TData = unknown, TData = unknown,
TError = unknown, TError = unknown,
@ -36,22 +35,30 @@ export const authClient: Client = {
Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'> Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'>
): Promise<RequestResult<TData, TError, ThrowOnError, TResponseStyle>> => { ): Promise<RequestResult<TData, TError, ThrowOnError, TResponseStyle>> => {
// Initial request
let result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options); let result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
// Check for 401 // 1. Cast to a Record to allow the 'in' operator check on a generic
if ((result as any)?.error?.response?.status === 401) { // We use 'unknown' instead of 'any' to maintain safety.
const resultObj = result as Record<string, unknown>;
// 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(); const refreshed = await getRefreshed();
if (refreshed) { if (refreshed) {
// Retry
result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options); result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
} else { } else {
localStorage.clear(); localStorage.clear();
window.location.href = "/login"; window.location.href = "/login";
} }
} }
}
return result as RequestResult<TData, TError, ThrowOnError, TResponseStyle>; return result;
}) as Client['request'], // Cast the function itself to match the Client interface }) as Client['request'],
}; };