From 24c2450ac15682dba8ef08c832bba699289a07e3 Mon Sep 17 00:00:00 2001 From: nihonium Date: Sat, 20 Dec 2025 03:28:49 +0300 Subject: [PATCH] fix: AuthClient --- .../frontend/src/api/AuthClient/AuthClient.ts | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/modules/frontend/src/api/AuthClient/AuthClient.ts b/modules/frontend/src/api/AuthClient/AuthClient.ts index 2619db9..c7c20dd 100644 --- a/modules/frontend/src/api/AuthClient/AuthClient.ts +++ b/modules/frontend/src/api/AuthClient/AuthClient.ts @@ -25,7 +25,6 @@ const baseClient = createClient(createConfig({ 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>, 'method'> ): Promise> => { - // Initial request let result = await baseClient.request(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; - if (refreshed) { - // Retry - result = await baseClient.request(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(options); + } else { + localStorage.clear(); + window.location.href = "/login"; + } } } - return result as RequestResult; - }) as Client['request'], // Cast the function itself to match the Client interface + return result; + }) as Client['request'], }; \ No newline at end of file