37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import type { RouterClient } from "@orpc/server";
|
|
import { createORPCClient } from "@orpc/client";
|
|
import { RPCLink } from "@orpc/client/fetch";
|
|
import { createTanstackQueryUtils } from "@orpc/tanstack-query";
|
|
|
|
import type { router } from "@/server/rpc";
|
|
|
|
// Helper function to read CSRF token from cookie
|
|
function getCSRFToken(): string {
|
|
const cookieValue = document.cookie
|
|
.split('; ')
|
|
.find(row => row.startsWith('csrf-token='))
|
|
?.split('=')[1];
|
|
return cookieValue || '';
|
|
}
|
|
|
|
const link = new RPCLink({
|
|
url: `${window.location.origin}/rpc`,
|
|
headers: () => {
|
|
const csrfToken = getCSRFToken();
|
|
return csrfToken ? { 'X-CSRF-Token': csrfToken } : {};
|
|
},
|
|
fetch: (request, init) => {
|
|
return fetch(request, {
|
|
...init,
|
|
credentials: 'include' // Include cookies with all requests
|
|
});
|
|
}
|
|
});
|
|
|
|
export const rpcClient: RouterClient<typeof router> = createORPCClient(link);
|
|
|
|
export const queryClient = createTanstackQueryUtils(rpcClient);
|
|
|
|
// Export helper for potential use in other parts of the client code
|
|
export { getCSRFToken };
|