2024-01-02 17:12:26 +01:00
|
|
|
import { cache } from "react";
|
|
|
|
|
import { headers } from "next/headers";
|
2025-05-02 19:23:15 +02:00
|
|
|
import { createTRPCOptionsProxy } from "@trpc/tanstack-react-query";
|
2024-01-02 17:12:26 +01:00
|
|
|
|
2025-05-02 19:23:15 +02:00
|
|
|
import { appRouter, createCaller, createTRPCContext } from "@homarr/api";
|
2024-05-01 21:17:28 +02:00
|
|
|
import { auth } from "@homarr/auth/next";
|
2024-01-02 17:12:26 +01:00
|
|
|
|
2025-05-02 19:23:15 +02:00
|
|
|
import { makeQueryClient } from "./shared";
|
|
|
|
|
|
2024-01-02 17:12:26 +01:00
|
|
|
/**
|
|
|
|
|
* This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when
|
|
|
|
|
* handling a tRPC call from a React Server Component.
|
|
|
|
|
*/
|
|
|
|
|
const createContext = cache(async () => {
|
2025-01-04 19:47:23 +01:00
|
|
|
const heads = new Headers(await headers());
|
2024-01-02 17:12:26 +01:00
|
|
|
heads.set("x-trpc-source", "rsc");
|
|
|
|
|
|
|
|
|
|
return createTRPCContext({
|
2024-02-17 12:52:25 +01:00
|
|
|
session: await auth(),
|
2024-01-02 17:12:26 +01:00
|
|
|
headers: heads,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2024-02-17 12:52:25 +01:00
|
|
|
export const api = createCaller(createContext);
|
2025-05-02 19:23:15 +02:00
|
|
|
|
|
|
|
|
// IMPORTANT: Create a stable getter for the query client that
|
|
|
|
|
// will return the same client during the same request.
|
|
|
|
|
export const getQueryClient = cache(makeQueryClient);
|
|
|
|
|
export const trpc = createTRPCOptionsProxy({
|
|
|
|
|
ctx: createContext,
|
|
|
|
|
router: appRouter,
|
|
|
|
|
queryClient: getQueryClient,
|
|
|
|
|
});
|