UNPKG

@trpc/react-query

Version:

The tRPC React library

140 lines (137 loc) 4.84 kB
import { dehydrate } from '@tanstack/react-query'; import { TRPCUntypedClient, getUntypedClient } from '@trpc/client'; import { getTransformer } from '@trpc/client/unstable-internals'; import { callProcedure, createRecursiveProxy, createFlatProxy, run } from '@trpc/server/unstable-core-do-not-import'; import { getQueryKeyInternal } from '../internals/getQueryKey.mjs'; import { getQueryType } from '../shared/proxy/utilsProxy.mjs'; import 'react'; import '../internals/context.mjs'; import { getQueryClient } from '../shared/queryClient.mjs'; /** * Create functions you can use for server-side rendering / static generation * @see https://trpc.io/docs/v11/client/nextjs/server-side-helpers */ function createServerSideHelpers(opts) { const queryClient = getQueryClient(opts); const transformer = getTransformer(opts.transformer); const resolvedOpts = (()=>{ if ('router' in opts) { const { ctx, router } = opts; return { serialize: transformer.output.serialize, query: (queryOpts)=>{ return callProcedure({ router, path: queryOpts.path, getRawInput: async ()=>queryOpts.input, ctx, type: 'query', signal: undefined }); } }; } const { client } = opts; const untypedClient = client instanceof TRPCUntypedClient ? client : getUntypedClient(client); return { query: (queryOpts)=>untypedClient.query(queryOpts.path, queryOpts.input), serialize: (obj)=>transformer.output.serialize(obj) }; })(); function _dehydrate(opts = { shouldDehydrateQuery (query) { if (query.state.status === 'pending') { return false; } // makes sure to serialize errors return true; } }) { const before = run(()=>{ const dehydrated = dehydrate(queryClient, opts); return { ...dehydrated, queries: dehydrated.queries.map((query)=>{ if (query.promise) { const { promise: _, ...rest } = query; return rest; } return query; }) }; }); const after = resolvedOpts.serialize(before); return after; } const proxy = createRecursiveProxy((opts)=>{ const args = opts.args; const input = args[0]; const arrayPath = [ ...opts.path ]; const utilName = arrayPath.pop(); const queryFn = ()=>resolvedOpts.query({ path: arrayPath.join('.'), input }); const queryKey = getQueryKeyInternal(arrayPath, input, getQueryType(utilName)); const helperMap = { queryOptions: ()=>{ const args1 = args[1]; return { ...args1, queryKey, queryFn }; }, infiniteQueryOptions: ()=>{ const args1 = args[1]; return { ...args1, queryKey, queryFn }; }, fetch: ()=>{ const args1 = args[1]; return queryClient.fetchQuery({ ...args1, queryKey, queryFn }); }, fetchInfinite: ()=>{ const args1 = args[1]; return queryClient.fetchInfiniteQuery({ ...args1, queryKey, queryFn, initialPageParam: args1?.initialCursor ?? null }); }, prefetch: ()=>{ const args1 = args[1]; return queryClient.prefetchQuery({ ...args1, queryKey, queryFn }); }, prefetchInfinite: ()=>{ const args1 = args[1]; return queryClient.prefetchInfiniteQuery({ ...args1, queryKey, queryFn, initialPageParam: args1?.initialCursor ?? null }); } }; return helperMap[utilName](); }); return createFlatProxy((key)=>{ if (key === 'queryClient') return queryClient; if (key === 'dehydrate') return _dehydrate; return proxy[key]; }); } export { createServerSideHelpers };