import { createORPCClient } from '@orpc/client' import { RPCLink } from '@orpc/client/fetch' import { createRouterClient } from '@orpc/server' import { createTanstackQueryUtils } from '@orpc/tanstack-query' import { createIsomorphicFn } from '@tanstack/react-start' import { getRequestHeaders } from '@tanstack/react-start/server' import { router } from './router' import type { RouterClient } from './types' const getORPCClient = createIsomorphicFn() .server(() => createRouterClient(router, { context: () => ({ headers: getRequestHeaders(), }), }), ) .client(() => { const link = new RPCLink({ url: `${window.location.origin}/api/rpc`, }) return createORPCClient(link) }) const client: RouterClient = getORPCClient() export const orpc = createTanstackQueryUtils(client, { experimental_defaults: { todo: { create: { mutationOptions: { onSuccess: (_, __, ___, ctx) => { ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) }, }, }, update: { mutationOptions: { onSuccess: (_, __, ___, ctx) => { ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) }, }, }, remove: { mutationOptions: { onSuccess: (_, __, ___, ctx) => { ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() }) }, }, }, }, }, })