#296

Proper middleware support

Requested

I am aware of the Convex Helpers package, which allows us to write custom context objects on queries, mutations, and actions, but that only allows us to write the CTX object before the request happens and not after a response is returned.

My biggest use for this would be to enable convex compatibility with the evlog (https://www.evlog.dev/getting-started/installation) package, which allows us to use wide logging on our events. That integration usually requires us to call .flush() on the object after the response is returned, to emit the log event. And I don't see a clean way to do this in convex.

Development Updates

Ian Macartney
Ian MacartneyApr 23, 2026

FYI the convex-helpers way of doing this currently is to use onSuccess like so:

const actionWithFlush = customAction(internalAction, {
  args: {},
  input: async () => {
    const logger = createLogger();
    return {
      ctx: { logger },
      args: {},
      onSuccess: ({ ctx, args, result }) => {
        logger.flush(result);
      },
    };
  },
});

const myAction = actionWithFlush({
  args: {},
  handler: async (ctx) => {
    ctx.logger.log("foo");
  },
});

Note: this won't fire if your action times out or otherwise exits un-cleanly, so putting a try/catch around it and having a Promise.race with a 9min timer and flushing your errors before re-throwing would be my recommendation. Acknowledged that (especially for actions) having a "finally" clause would be more ergonomic here.

Total backing

60

from 2 supporters

Back this request

Sign in to back this feature request with Convex Chips and help prioritize it.

Sign in to vote