#6203·trpc

Extending Meta with middlewares / concat / plugins

Author: KATTCreated Nov 8, 2024Updated Sep 20, 2026
Labels@trpc/server

Discussed in https://github.com/trpc/trpc/discussions/6199

Originally posted by HamishBuckmaster November 7, 2024

Context

Let's set the scene of the problem and some ideas of what could be done. Let's say we have a base product that is just for authenticating users and we have multiple contexts that can change and you may want to allow for adjusting the context / meta of the request by what products. Say you want the Base Meta to look something like this

typescript
type BaseMeta = {
   application: "application-xyz" | "application-abc"
}

Now it would be great if we could create a plugin called "permissionPlugin" that could be "hooked" in to allow the base meta to be extended to look something like this:

typescript
type PermissionPluginMeta = {
  permissions: string;
} & BaseMeta;

So that when we use said plugin using concat it would require the developer to update the meta of the router to include this

typescript
foo: protectedProcedure
    .unstable_concat(permissionPlugin.pluginProc)
    .meta({ 
       application: "application-xyz"
       permission: "foo:bar"  // <- This would come into the picture when using the permissionPlugin that has extended the meta
    })
    // etc

Be great to get some other ideas on how others have done this in the past other then creating heaps of procedures, this would allow for a more of a "plug & play" what you need into the router

Currently when doing this approach the meta can't be extended because of "meta" mismatch