Compare commits
No commits in common. "2bf268cbef0672eadd7fe2cbae4a87ae54b1b910" and "27acfab0cda4cdb50df388a63f6bc86142d730c8" have entirely different histories.
2bf268cbef
...
27acfab0cd
1 changed files with 23 additions and 25 deletions
|
|
@ -10,13 +10,15 @@ async function getRefreshed(): Promise<boolean> {
|
|||
if (!refreshPromise) {
|
||||
refreshPromise = (async () => {
|
||||
try {
|
||||
const res = await refreshTokens({ throwOnError: true });
|
||||
const res = await refreshTokens();
|
||||
// consider refresh successful if res.data exists
|
||||
return !!res.data;
|
||||
} catch {
|
||||
return false;
|
||||
} catch (err) {
|
||||
return false; // failed to refresh
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
return refreshPromise;
|
||||
}
|
||||
|
||||
|
|
@ -25,33 +27,29 @@ const baseClient = createClient(createConfig<ClientOptions2>({ baseUrl: '/api/v1
|
|||
export const authClient: Client = {
|
||||
...baseClient,
|
||||
|
||||
// Force the function to match the expected signature exactly
|
||||
request: (async <
|
||||
request: function <
|
||||
TData = unknown,
|
||||
TError = unknown,
|
||||
ThrowOnError extends boolean = false,
|
||||
ThrowOnError extends boolean = true,
|
||||
TResponseStyle extends ResponseStyle = 'fields',
|
||||
>(
|
||||
options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'> &
|
||||
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);
|
||||
): RequestResult<TData, TError, ThrowOnError, TResponseStyle> {
|
||||
|
||||
// Check for 401
|
||||
if ((result as any)?.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";
|
||||
// Wrap logic inside a Promise to satisfy RequestResult type
|
||||
return baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options).catch(async (err: any) => {
|
||||
if (err?.status === 401) {
|
||||
const refreshed = await getRefreshed();
|
||||
if (!refreshed) {
|
||||
localStorage.clear();
|
||||
window.location.href = "/login";
|
||||
throw err;
|
||||
}
|
||||
// Retry original request
|
||||
return baseClient.request<TData, TError, ThrowOnError, TResponseStyle>(options);
|
||||
}
|
||||
}
|
||||
|
||||
return result as RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
||||
}) as Client['request'], // Cast the function itself to match the Client interface
|
||||
};
|
||||
throw err;
|
||||
}) as RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
|
||||
},
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue