30 lines
739 B
TypeScript
30 lines
739 B
TypeScript
import { QueryClient, QueryKey } from "@tanstack/react-query";
|
|
import { ServerResult } from "../types";
|
|
|
|
export async function ensureServerQueryData<TData>(
|
|
queryClient: QueryClient,
|
|
query: {
|
|
queryKey: QueryKey;
|
|
queryFn: () => Promise<ServerResult<TData>>;
|
|
}
|
|
): Promise<TData> {
|
|
return queryClient.ensureQueryData({
|
|
queryKey: query.queryKey,
|
|
queryFn: async () => {
|
|
try {
|
|
const result = await query.queryFn();
|
|
|
|
if (!result.success) {
|
|
throw new Error(result.error.userMessage);
|
|
}
|
|
|
|
return result.data;
|
|
} catch (error: any) {
|
|
if (error?.options?.to && error?.options?.statusCode) {
|
|
throw error;
|
|
}
|
|
throw error;
|
|
}
|
|
}
|
|
});
|
|
} |