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
{{ message }}
Repository navigation
Idea: query/mutation options similar to Fastify's route configs #1731
It would be nice to have some sort of per query/mutation options, just like Fastify's route configs
Could work like this:
router<Context>({hasAuth: true}/* default options, also used for type inference */).middleware(async({ options })=>{/* only check if route has auth */if(options.hasAuth===true){// check auth}}).mutation('login',{input: ...,/* optional options object */options: {hasAuth: false},resolve: ...
})
changed the title [-]query/mutation options similar to Fastify's route configs[/-][+]Idea: query/mutation options similar to Fastify's route configs[/+]on Apr 5, 2022
Hey, great idea! I've been toying with a similar idea to decorate procedures with metadata, similarly useful for #1724 to distinguish that a certain mutation would be a PUT or DELETE in the OpenAPI-output.
How I would like to see this designed in terms of usage:
With this new feature, is there any way to get Context Swapping to work?
For example, if the hasAuth meta parameter is true, the middleware will check to make sure the user has a token. Now that this check was done, the resolver should know the the token is nonnullable.
typeMeta={hasAuth: boolean}trpc.router<Context,Meta>().middleware(async({ meta, ctx })=>{if(meta.hasAuth&&(!ctx.token||!verifyTokenIsValid(ctx.token))){thrownewTRPCError({code: "UNAUTHORIZED"});}returnnext({ctx: {
...ctx,token: ctx.token,// At this point, `ctx.token` is still nullable because `meta.hasAuth` can be false, which means there does not need to be a token.},});}).mutation("test",{meta: {hasAuth: true},asyncresolve({ ctx }){ctx.token// Here, `ctx.token` should not be nullable because the middleware will reject any requests that do not have a token.},});
Using this pattern, the token in the resolver is still nullable. Is there any way that tRPC can infer the values of ctx based on how the middlewares affect the ctx based on the code flow which is controlled by the meta object?
If the example code is not clear enough, I can make a full example that reproduces this.
Activity
[-]query/mutation options similar to Fastify's route configs[/-][+]Idea: query/mutation options similar to Fastify's route configs[/+]KATT commentedon Apr 5, 2022
Hey, great idea! I've been toying with a similar idea to decorate procedures with metadata, similarly useful for #1724 to distinguish that a certain mutation would be a
PUTorDELETEin the OpenAPI-output.How I would like to see this designed in terms of usage:
When using it:
Notes:
Metaneeds to extendRecord<string, any>and default to{}Metais optional on each procedureLouisHaftmann commentedon Apr 5, 2022
wouldn't it be easier if meta was available in the middleware args?
KATT commentedon Apr 5, 2022
It could be available in the middleware as well
LouisHaftmann commentedon Apr 7, 2022
In the future we should add a way of changing the meta type from middleware. This would make it easier for plugin devs to add custom route options.
LouisHaftmann commentedon Apr 7, 2022
This would also work:
LouisHaftmann commentedon Apr 7, 2022
i really like this approach
We would need a function (
router.plugin()) similar torouter.merge()that also merges context and metaKATT commentedon Apr 7, 2022
@LouisHaftmann could you re-open the PR? I reverted it, wanna merge it with a squash https://github.com/trpc/trpc/compare/main...feat/route-metadata?expand=1
TheBestMoshe commentedon Apr 11, 2022
With this new feature, is there any way to get Context Swapping to work?
For example, if the
hasAuthmeta parameter istrue, the middleware will check to make sure the user has a token. Now that this check was done, the resolver should know the the token is nonnullable.Note: Example code is from a Next.js codebase
Context
Router
Using this pattern, the token in the resolver is still nullable. Is there any way that tRPC can infer the values of
ctxbased on how the middlewares affect thectxbased on the code flow which is controlled by themetaobject?If the example code is not clear enough, I can make a full example that reproduces this.
github-actions commentedon Oct 4, 2022
This issue has been locked because it had no new activity for 14 days. If you are running into a similar issue, please create a new issue. Thank you.