Crspy
Crspy
Explore posts from servers
TTCTheo's Typesafe Cult
Created by Crspy on 2/4/2024 in #questions
Add parameters to tRPC Context
I would like to add the parameters of the type CreateNextContextOptions to my tRPC context in my t3-app project. I've looked through the documentation, but I cannot find a way to get the parameters to pass them to the createContext function in my createTRPCProxyClient definition in server.ts. In the docs, they just pass the typeof createTRPCContext as a generic in the initTRPC.context() function, which was done in a separate file when the project was generated. Does anyone know how I can get this to work?
const createContext = cache((opts: CreateNextContextOptions) => {
const heads = new Headers(headers());
heads.set("x-trpc-source", "rsc");

return createTRPCContext({
headers: heads,
}, opts);
});

export const api = createTRPCProxyClient<AppRouter>({
transformer,
links: [
loggerLink({
enabled: (op) =>
process.env.NODE_ENV === "development" ||
(op.direction === "down" && op.result instanceof Error),
}),
() =>
({ op }) =>
observable((observer) => {
createContext()
// ^^ TS2554: Expected 1 arguments, but got 0
.then((ctx) => {
return callProcedure({
procedures: appRouter._def.procedures,
path: op.path,
rawInput: op.input,
ctx,
type: op.type,
});
})
.then((data) => {
observer.next({ result: { data } });
observer.complete();
})
.catch((cause: TRPCErrorResponse) => {
observer.error(TRPCClientError.from(cause));
});
}),
],
});
const createContext = cache((opts: CreateNextContextOptions) => {
const heads = new Headers(headers());
heads.set("x-trpc-source", "rsc");

return createTRPCContext({
headers: heads,
}, opts);
});

export const api = createTRPCProxyClient<AppRouter>({
transformer,
links: [
loggerLink({
enabled: (op) =>
process.env.NODE_ENV === "development" ||
(op.direction === "down" && op.result instanceof Error),
}),
() =>
({ op }) =>
observable((observer) => {
createContext()
// ^^ TS2554: Expected 1 arguments, but got 0
.then((ctx) => {
return callProcedure({
procedures: appRouter._def.procedures,
path: op.path,
rawInput: op.input,
ctx,
type: op.type,
});
})
.then((data) => {
observer.next({ result: { data } });
observer.complete();
})
.catch((cause: TRPCErrorResponse) => {
observer.error(TRPCClientError.from(cause));
});
}),
],
});
2 replies