I have a file src/app/api/trpc/[trpc]/route.ts
with
const handler = withAxiomRouteHandler((req: NextRequest) =>
// ...
export { handler as GET, handler as POST };
This works just fine. The problem is with another file that I have in this project, src/trpc/server.ts
.
export const api = createTRPCProxyClient<AppRouter>({
I don’t have the request here, I guess?
The issue is that I’m expecting the context to contain the request, because I want to inject axiom in the requests to log application errors.
So, in this boilerplate code:
links: [
loggerLink({
enabled: (op) =>
process.env.NODE_ENV === "development" || (op.direction === "down" && op.result instanceof Error),
}),
() => {
return ({ op }) => {
return observable((observer) => {
createContext() // <------- req?
.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));
});
});
};
},
],
Could I possibly get the request when creating the context there?
If it’s not possible, would it be significantly worse to switch to this?
unstable_httpBatchStreamLink({
url: getUrl(),
headers() {
return {
cookie: cookies().toString(),
"x-trpc-source": "rsc",
};
},
}),