Compare commits
No commits in common. "542e4b52e12beb95bd471f3527517bdc01a239e8" and "ffede271a0ac56718ae03a48cc7791f1e2b72e35" have entirely different histories.
542e4b52e1
...
ffede271a0
1 changed files with 13 additions and 20 deletions
|
|
@ -25,6 +25,7 @@ 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,
|
||||||
|
|
@ -35,30 +36,22 @@ 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);
|
||||||
|
|
||||||
// 1. Cast to a Record to allow the 'in' operator check on a generic
|
// Check for 401
|
||||||
// We use 'unknown' instead of 'any' to maintain safety.
|
if ((result as any)?.error?.response?.status === 401) {
|
||||||
const resultObj = result as Record<string, unknown>;
|
const refreshed = await getRefreshed();
|
||||||
|
|
||||||
// 2. Check if the object is valid and contains the error key
|
if (refreshed) {
|
||||||
if (result && typeof result === 'object' && 'error' in resultObj) {
|
// Retry
|
||||||
|
result = await baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
|
||||||
// 3. Narrow the error property specifically
|
} else {
|
||||||
const error = resultObj.error as { response?: { status?: number } } | null | undefined;
|
localStorage.clear();
|
||||||
|
window.location.href = "/login";
|
||||||
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;
|
return result as RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
||||||
}) as Client['request'],
|
}) as Client['request'], // Cast the function itself to match the Client interface
|
||||||
};
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue