Compare commits
2 commits
ffede271a0
...
542e4b52e1
| Author | SHA1 | Date | |
|---|---|---|---|
| 542e4b52e1 | |||
| 24c2450ac1 |
1 changed files with 20 additions and 13 deletions
|
|
@ -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) {
|
||||
// 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>;
|
||||
|
||||
// 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) {
|
||||
// Retry
|
||||
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'],
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue