{"version":3,"file":"server.mjs","names":[],"sources":["../../src/tanstack-query/server.tsx"],"sourcesContent":["/* -------------------------------------------------------------------\n\n                       🗲 Storm Software - Stryke\n\n This code was released as part of the Stryke project. Stryke\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/stryke.\n\n Website:                  https://stormsoftware.com\n Repository:               https://github.com/storm-software/stryke\n Documentation:            https://docs.stormsoftware.com/projects/stryke\n Contact:                  https://stormsoftware.com/contact\n\n SPDX-License-Identifier:  Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport type { MaybePromise } from \"@stryke/types\";\nimport type { QueryClientConfig } from \"@tanstack/react-query\";\nimport { dehydrate, HydrationBoundary } from \"@tanstack/react-query\";\nimport type { AnyTRPCRouter, inferRouterContext } from \"@trpc/server\";\nimport type { TRPCQueryOptions } from \"@trpc/tanstack-react-query\";\nimport { createTRPCOptionsProxy } from \"@trpc/tanstack-react-query\";\nimport defu from \"defu\";\nimport React, { cache } from \"react\";\nimport \"server-only\";\nimport { createQueryClient } from \"../shared\";\n\n/**\n * Create a TRPC Tanstack Query server.\n *\n * @example\n * ```tsx title=\"app/api/trpc/route.tsx\"\n * import { createTRPCTanstackQueryServer } from \"@trpc-next/tanstack-query/server\";\n * import { z } from \"zod\";\n * import { createRouter } from \"@trpc/server\";\n *\n * const router = createRouter().query(\"hello\", {\n *   input: z.object({\n *     name: z.string()\n *   }),\n *   resolve({ input }) {\n *     return `Hello, ${input.name}!`;\n *   }\n * });\n *\n * const { trpc, HydrateClient } = createTRPCTanstackQueryServer({\n *   router,\n *   headers: async () => new Headers()\n * });\n * ```\n *\n * @param headers - A function that returns the headers for the TRPC context.\n * @param router - The TRPC router to use for the server.\n * @param createContext - A function that creates the context for the TRPC server.\n * @param queryClientConfig - Optional configuration for the Tanstack Query client.\n * @returns An object containing the TRPC context and a component to hydrate the client state.\n */\nexport function createTRPCTanstackQueryServer<\n  TRouter extends AnyTRPCRouter,\n  TContext extends inferRouterContext<TRouter> = inferRouterContext<TRouter>\n>(\n  headers: () => Promise<Headers>,\n  router: TRouter,\n  createContext: () => MaybePromise<TContext> = () => ({}) as TContext,\n  queryClientConfig: Partial<QueryClientConfig> = {}\n) {\n  const _createContext = cache(async (): Promise<TContext> => {\n    const context = await Promise.resolve(createContext());\n\n    const _headers = new Headers(await headers());\n    _headers.set(\"x-trpc-source\", \"rsc\");\n\n    return defu(\n      {\n        headers: Object.fromEntries(_headers)\n      },\n      context\n    ) as TContext;\n  });\n\n  /**\n   * Create a stable getter for the query client that\n   * will return the same client during the same request.\n   */\n  const getQueryClient = cache(() => createQueryClient(queryClientConfig));\n\n  const trpc = createTRPCOptionsProxy({\n    router,\n    queryClient: getQueryClient,\n    ctx: _createContext\n  });\n\n  return {\n    trpc,\n    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    prefetch: <T extends ReturnType<TRPCQueryOptions<any>>>(\n      queryOptions: T\n    ) => {\n      const queryClient = getQueryClient();\n      if (queryOptions.queryKey[1]?.type === \"infinite\") {\n        void queryClient.prefetchInfiniteQuery(queryOptions as any);\n      } else {\n        void queryClient.prefetchQuery(queryOptions);\n      }\n    }\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2DA,SAAgB,8BAId,SACA,QACA,uBAAqD,CAAC,IACtD,oBAAgD,CAAC,GACjD;CACA,MAAM,iBAAiB,MAAM,YAA+B;EAC1D,MAAM,UAAU,MAAM,QAAQ,QAAQ,cAAc,CAAC;EAErD,MAAM,WAAW,IAAI,QAAQ,MAAM,QAAQ,CAAC;EAC5C,SAAS,IAAI,iBAAiB,KAAK;EAEnC,OAAO,KACL,EACE,SAAS,OAAO,YAAY,QAAQ,EACtC,GACA,OACF;CACF,CAAC;;;;;CAMD,MAAM,iBAAiB,YAAY,kBAAkB,iBAAiB,CAAC;CAQvE,OAAO;EACL,MAPW,uBAAuB;GAClC;GACA,aAAa;GACb,KAAK;EACP,CAGK;EACH,gBAAgB,UAAyC;GACvD,MAAM,kBAAkB,UAAU,eAAe,CAAC;GAElD,OACE,oBAAC,mBAAD;IAAmB,OAAO;cACvB,MAAM;GACU;EAEvB;EACA,WACE,iBACG;GACH,MAAM,cAAc,eAAe;GACnC,IAAI,aAAa,SAAS,EAAE,EAAE,SAAS,YACrC,AAAK,YAAY,sBAAsB,YAAmB;QAE1D,AAAK,YAAY,cAAc,YAAY;EAE/C;CACF;AACF"}