You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd want the global opts to be listed at the bottom of --help for any command. I'm just not sure what a nice API to process the parsed global options would be. Perhaps just merge them in the trpc root context?
I briefly played with something like this, but didn't go through at the time because I was aiming to keep this library's docs as close to "just use trpc" as possible. To achieve something like this you can already use multiple .input(...)s.
That way, the loglevel value, to use your example, is available to middleware and the procedure implementation too, instead of it being a trpc-cli only thing. It's not a "global" option per se but you can easily add the options to every procedures.
Am on mobile so this formatting will be bad but basically:
constt=trpcServer.initTRPC().create()exportconstprocedure=t.procedure.input(z.object({logLevel: z.string()})).use(({input, next})=>{// set log level via AsyncLocalStorage or similarreturnmyLogContext.run(input.level,()=>{returnnext()})})constfoo=procedure.input(z.object({abc: z.string()}).mutation(...)constbar=procedure.input(z.object({def: z.string()}).mutation(...)constrouter=t.router({foo, bar})createCli({router}).run()
I'll have to search for where trpc recommends to use this pattern for things like authed procedures etc., it might be deep in the issues.
I think that can cover a similar use case right?
Edit: if you don't want to use AsyncLocalStorage you could use other trpc-native features like injecting a logger into the context:
constt=trpcServer.initTRPC().create()exportconstprocedure=t.procedure.input(z.object({logLevel: z.string().default('info')})).use(({input, next})=>{constlogger=newLogger(input.logLevel)returnnext({ctx: {logger}})})constfoo=procedure.input(z.object({abc: z.string()}).mutation(({input, ctx})=>{ctx.logger.warn(`abc is ${input.abc} and log level is ${input.logLevel}`)})constbar=procedure.input(z.object({def: z.string()}).mutation(...)constrouter=t.router({foo, bar})createCli({router}).run()
Activity
mmkal commentedon May 29, 2025
I briefly played with something like this, but didn't go through at the time because I was aiming to keep this library's docs as close to "just use trpc" as possible. To achieve something like this you can already use multiple
.input(...)s.That way, the loglevel value, to use your example, is available to middleware and the procedure implementation too, instead of it being a trpc-cli only thing. It's not a "global" option per se but you can easily add the options to every procedures.
Am on mobile so this formatting will be bad but basically:
I'll have to search for where trpc recommends to use this pattern for things like authed procedures etc., it might be deep in the issues.
I think that can cover a similar use case right?
Edit: if you don't want to use AsyncLocalStorage you could use other trpc-native features like injecting a logger into the context: