{"version":3,"file":"rsc.mjs","names":[],"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\nexport type 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\nexport type 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\nexport type Caller<TRouter extends AnyRouter> = ReturnType<\n  RouterCaller<inferRouterRootTypes<TRouter>, TRouter['_def']['record']>\n>;\n\n// ts-prune-ignore-next\n/**\n * Creates a server-side caller that can also prefetch queries, plus a\n * `HydrateClient` component that sends the prefetched data to the client.\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 *\n * @deprecated `@trpc/react-query` only gets bug fixes and will be removed in\n * the next major version. Use `@trpc/tanstack-react-query` instead.\n * @see https://trpc.io/docs/client/tanstack-react-query/migrating\n * @see https://trpc.io/docs/client/react/server-components\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 = async ({ params }) => {\n     *   await 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,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuE/C,SAAgB,uBACd,QAGA,gBACA;CAGA,MAAM,eAAe,qBAEnB,OAAO,SAAS;EAChB,MAAM,OAAO,CAAC,GAAG,KAAK,IAAI;EAC1B,MAAM,OAAO,CAAC,GAAG,KAAK,IAAI;EAC1B,MAAM,OAA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