25 lines
632 B
TypeScript
25 lines
632 B
TypeScript
import { useLoginDialogStore } from "@/store/login-dialog"
|
|
|
|
type RequestExecutor<T> = () => Promise<T>
|
|
|
|
interface RequestOptions {
|
|
onUnauthorized?: () => void
|
|
}
|
|
|
|
export async function requestWithAuth<T>(executor: RequestExecutor<T>, options?: RequestOptions) {
|
|
try {
|
|
return await executor()
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === "UNAUTHORIZED") {
|
|
if (options?.onUnauthorized) {
|
|
useLoginDialogStore.getState().openLoginDialog(options.onUnauthorized)
|
|
} else {
|
|
useLoginDialogStore.getState().openLoginDialog()
|
|
}
|
|
return null
|
|
}
|
|
|
|
throw error
|
|
}
|
|
}
|