Skip to content

feat: Support enhancing the context #6772

Open

Description

@dbartholomae

Describe the feature you'd like to request

In some situation, e.g. when using an awsLambdaRequestHandler, it is necessary to pass information from context generation to other parts of the application, e.g. to add specific headers in responseMeta. At the moment, there is no real way to pass that information without changing the router implementation itself, which would then couple the router to implementation details (e.g. in this case to the detail that the router is used via http or another transport that supports headers).

Describe the solution you'd like to see

Export a type EnhanceContext as follows:

type EnhanceContext<
  Router,
  EnhancedContext extends Partial<Context>,
> = Router & {
  _def: {
    _config: {
      $types: {
        ctx: EnhancedContext;
      };
    };
  };
};

This way, the router can be enhanced with additional context as needed without polluting the router and without sacrificing type safety:

export const handler =  awsLambdaRequestHandler({
    // Will fail if {foo: string} is not compatible with the Context type
    router: router as EnhanceContext<Router, {foo: string}>,
    createContext: async ({ event }: { event: APIGatewayEvent }) => {
      // Will fail if either createContext doesn't create the context of the router or foo isn't in the enhanced context
      return { ...createContext(event), foo: "bar" };
    },
    responseMeta: ({ ctx }) => {
      const headers = new Headers({
        // Will fail if ctx isn't actually enhanced with foo
        "Foo-Header": ctx.foo,
      });
      return { headers };
    },
  }),
);

The implementation of this is straight-forward, the main drawback is that the type needs to be kept up-to-date if the internal implementation changes (which is unlikely I would assume as so much depends on it, and even if it happens, changing it is again straight-forward).

Describe alternate solutions

One alternative could be to add another property to the parameter for createContext and responseMeta that isn't tied to the router at all. But that's a lot more implementation effort without any real benefits.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR implementing this feature!

Activity

  1. KATT commented on Nov 2, 2026

    @KATT
    Member

    🤖 In v12, resolvers and middleware set the response's headers and status directly through opts.response, so you don't need to pass data through context to responseMeta. https://v12.trpc.io/v12/docs/server/responses. v12 isn't released yet and there's no ETA.

  2. added
    next-major/definite
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    next-major/definite

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions