@trpc/react-query
Version:
1 lines • 6.72 kB
Source Map (JSON)
{"version":3,"file":"rsc.mjs","names":["caller: AnyRouter extends TRouter\n ? TypeError<'Generic parameter missing in `createHydrationHelpers<HERE>`'>\n : Caller<TRouter>","getQueryClient: () => QueryClient","props: { children: React.ReactNode }"],"sources":["../src/rsc.tsx"],"sourcesContent":["/// <reference types=\"react/canary\" />\n\nimport {\n dehydrate,\n HydrationBoundary,\n type QueryClient,\n} from '@tanstack/react-query';\nimport type { TRPCClientError } from '@trpc/client';\nimport type { inferTransformedProcedureOutput } from '@trpc/server';\nimport {\n createRecursiveProxy,\n type AnyRouter,\n type inferProcedureInput,\n type RouterRecord,\n} from '@trpc/server/unstable-core-do-not-import';\nimport type {\n AnyProcedure,\n AnyRootTypes,\n inferProcedureOutput,\n inferRouterRootTypes,\n Maybe,\n RouterCaller,\n TypeError,\n} from '@trpc/server/unstable-core-do-not-import';\nimport * as React from 'react';\nimport { getQueryKeyInternal } from './internals/getQueryKey';\nimport type {\n TRPCFetchInfiniteQueryOptions,\n TRPCFetchQueryOptions,\n} from './shared';\n\nconst HELPERS = ['prefetch', 'prefetchInfinite'];\n\ntype DecorateProcedure<\n TRoot extends AnyRootTypes,\n TProcedure extends AnyProcedure,\n> = {\n (\n input: inferProcedureInput<TProcedure>,\n ): Promise<inferProcedureOutput<TProcedure>>;\n prefetch: (\n input: inferProcedureInput<TProcedure>,\n opts?: TRPCFetchQueryOptions<\n inferTransformedProcedureOutput<TRoot, TProcedure>,\n TRPCClientError<TRoot>\n >,\n ) => Promise<void>;\n prefetchInfinite: (\n input: inferProcedureInput<TProcedure>,\n opts?: TRPCFetchInfiniteQueryOptions<\n inferProcedureInput<TProcedure>,\n inferTransformedProcedureOutput<TRoot, TProcedure>,\n TRPCClientError<TRoot>\n >,\n ) => Promise<void>;\n};\n\ntype DecorateRouterRecord<\n TRoot extends AnyRootTypes,\n TRecord extends RouterRecord,\n> = {\n [TKey in keyof TRecord]: TRecord[TKey] extends infer $Value\n ? $Value extends AnyProcedure\n ? DecorateProcedure<TRoot, $Value>\n : $Value extends RouterRecord\n ? DecorateRouterRecord<TRoot, $Value>\n : never\n : never;\n};\n\ntype Caller<TRouter extends AnyRouter> = ReturnType<\n RouterCaller<inferRouterRootTypes<TRouter>, TRouter['_def']['record']>\n>;\n\n// ts-prune-ignore-next\n/**\n * @note This requires `@tanstack/react-query@^5.49.0`\n * @note Make sure to have `dehydrate.serializeData` and `hydrate.deserializeData`\n * set to your data transformer in your `QueryClient` factory.\n * @example\n * ```ts\n * export const createQueryClient = () =>\n * new QueryClient({\n * defaultOptions: {\n * dehydrate: {\n * serializeData: transformer.serialize,\n * },\n * hydrate: {\n * deserializeData: transformer.deserialize,\n * },\n * },\n * });\n * ```\n */\nexport function createHydrationHelpers<TRouter extends AnyRouter>(\n caller: AnyRouter extends TRouter\n ? TypeError<'Generic parameter missing in `createHydrationHelpers<HERE>`'>\n : Caller<TRouter>,\n getQueryClient: () => QueryClient,\n) {\n type RootTypes = inferRouterRootTypes<TRouter>;\n\n const wrappedProxy = createRecursiveProxy<\n DecorateRouterRecord<RootTypes, TRouter['_def']['record']>\n >(async (opts) => {\n const path = [...opts.path];\n const args = [...opts.args];\n const proc = path.reduce(\n (acc, key) =>\n // @ts-expect-error - ??\n HELPERS.includes(key) ? acc : acc[key],\n caller,\n ) as unknown as DecorateProcedure<RootTypes, AnyProcedure>;\n\n const input = args[0];\n const promise = proc(input);\n\n const helper = path.pop();\n if (helper === 'prefetch') {\n const args1 = args[1] as Maybe<\n TRPCFetchInfiniteQueryOptions<any, any, any>\n >;\n\n return getQueryClient().prefetchQuery({\n ...args1,\n queryKey: getQueryKeyInternal(path, input, 'query'),\n queryFn: () => promise,\n });\n }\n if (helper === 'prefetchInfinite') {\n const args1 = args[1] as Maybe<\n TRPCFetchInfiniteQueryOptions<any, any, any>\n >;\n\n return getQueryClient().prefetchInfiniteQuery({\n ...args1,\n queryKey: getQueryKeyInternal(path, input, 'infinite'),\n queryFn: () => promise,\n initialPageParam: args1?.initialCursor ?? null,\n });\n }\n\n return promise;\n });\n\n function HydrateClient(props: { children: React.ReactNode }) {\n const dehydratedState = dehydrate(getQueryClient());\n\n return (\n <HydrationBoundary state={dehydratedState}>\n {props.children}\n </HydrationBoundary>\n );\n }\n\n return {\n /***\n * Wrapped caller with prefetch helpers\n * Can be used as a regular [server-side caller](https://trpc.io/docs/server/server-side-calls)\n * or using prefetch helpers to put the promise into the QueryClient cache\n * @example\n * ```ts\n * const data = await trpc.post.get(\"postId\");\n *\n * // or\n * void trpc.post.get.prefetch(\"postId\");\n * ```\n */\n trpc: wrappedProxy,\n /**\n * HoC to hydrate the query client for a client component\n * to pick up the prefetched promise and skip an initial\n * client-side fetch.\n * @example\n * ```tsx\n * // MyRSC.tsx\n * const MyRSC = ({ params }) => {\n * void trpc.post.get.prefetch(params.postId);\n *\n * return (\n * <HydrateClient>\n * <MyCC postId={params.postId} />\n * </HydrateClient>\n * );\n * };\n *\n * // MyCC.tsx\n * \"use client\"\n * const MyCC = ({ postId }) => {\n * const { data: post } = trpc.post.get.useQuery(postId);\n * return <div>{post.title}</div>;\n * };\n * ```\n */\n HydrateClient,\n };\n}\n"],"mappings":";;;;;;;;AA+BA,MAAM,UAAU,CAAC,YAAY,kBAAmB;;;;;;;;;;;;;;;;;;;;AA+DhD,SAAgB,uBACdA,QAGAC,gBACA;CAGA,MAAM,eAAe,qBAEnB,OAAO,SAAS;EAChB,MAAM,OAAO,CAAC,GAAG,KAAK,IAAK;EAC3B,MAAM,OAAO,CAAC,GAAG,KAAK,IAAK;EAC3B,MAAM,OAAO,KAAK,OAChB,CAAC,KAAK,QAEJ,QAAQ,SAAS,IAAI,GAAG,MAAM,IAAI,MACpC,OACD;EAED,MAAM,QAAQ,KAAK;EACnB,MAAM,UAAU,KAAK,MAAM;EAE3B,MAAM,SAAS,KAAK,KAAK;AACzB,MAAI,WAAW,YAAY;GACzB,MAAM,QAAQ,KAAK;AAInB,UAAO,gBAAgB,CAAC,sFACnB;IACH,UAAU,oBAAoB,MAAM,OAAO,QAAQ;IACnD,SAAS,MAAM;MACf;EACH;AACD,MAAI,WAAW,oBAAoB;;GACjC,MAAM,QAAQ,KAAK;AAInB,UAAO,gBAAgB,CAAC,8FACnB;IACH,UAAU,oBAAoB,MAAM,OAAO,WAAW;IACtD,SAAS,MAAM;IACf,wFAAkB,MAAO,oFAAiB;MAC1C;EACH;AAED,SAAO;CACR,EAAC;CAEF,SAAS,cAAcC,OAAsC;EAC3D,MAAM,kBAAkB,UAAU,gBAAgB,CAAC;AAEnD,yBACE,IAAC;GAAkB,OAAO;aACvB,MAAM;IACW;CAEvB;AAED,QAAO;EAaL,MAAM;EA0BN;CACD;AACF"}