Replies: 2 comments
-
|
There isn't built-in support for passing custom headers per-request directly via the second argument to client calls (like client.myProcedure(data, { headers })) or through mutationOptions/mutate in the TanStack Query integration. However, you can achieve per-request headers by leveraging the context mechanism and customizing your RPCLink's headers function. Here's how you can do it:
const client = createORPCClient<RouterContract>(
new RPCLink({
url: "/rpc",
fetch: (url, init) => fetch(url, { ...init, credentials: "include" }),
headers: (options) => options.context?.headers ?? {},
}),
);
await client.myProcedure(data, {
context: { headers: { "x-captcha-response": token } }
});
const mutation = useMutation(
orpc.myProcedure.mutationOptions({
context: { headers: { "x-captcha-response": token } }
})
);
// Or when calling mutate:
mutation.mutate(data, {
context: { headers: { "x-captcha-response": token } }
});This approach keeps type safety and lets you inject headers per call. The key is that your RPCLink's headers function must pull from options.context. This pattern is the recommended workaround for now, as direct per-request header support in mutationOptions/mutate isn't currently available or planned in the immediate roadmap (reference, reference). Let me know if you want a more detailed example or have questions about advanced header handling! To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
|
Can this solve your problem? https://orpc.dev/docs/client/rpc-link#using-client-context |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using
@orpc/clientwith@orpc/tanstack-queryand need to pass custom headers on a per-request basis (specifically for Turnstile captcha verification viax-captcha-responseheader).Current setup:
What I'm trying to do:
Current workaround:
I'm bypassing ORPC entirely and using ky/fetch directly for endpoints that need custom headers:
This works but loses type safety and the nice DX of the ORPC client.
Questions:
Is there a way to pass per-request headers with the client directly?
Is there a way to pass headers through mutationOptions or the mutate call?
If not currently supported, is this on the roadmap?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions