Skip to content

Update: Added req and res to context parameter of handlers on runMethod - #269

Merged
omegascorp merged 2 commits into
modelence:mainfrom
AtulYadav25:update-context-params
May 19, 2026
Merged

omegascorp merged 2 commits into
modelence:mainfrom
AtulYadav25:update-context-params

Conversation

@AtulYadav25

@AtulYadav25 AtulYadav25 commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Added a new type:

In src/methods/types.ts

export type HttpContext = Context & {
  req: Request;
  res: Response;
};

This provides guaranteed access to req and res in all runMethod handlers.

runLiveMethod handlers use sockets, so req and res are not available there and I intentionally excluded them.

--
Instead of manually accessing request data from a generic context, we can use HttpContext in handlers,

import { z } from 'zod';
import { Module, ObjectId } from 'modelence/server';
import { dbTodos } from './db';
import { HttpContext } from 'modelence/types';

export default new Module('todos', {
  stores: [dbTodos],
  queries: {
    async getAll() {
      return await dbTodos.fetch({});
    },
  },
  mutations: {
    async setCompleted(args, { req, res }: HttpContext) {
      console.log(req.method);
      console.log(req.cookies);
    },
  },
});

Testing

I Tested this update locally, accessing values from req inside handlers:

console.log(req.method);
console.log(req.cookies);

Verified that request method and cookies were available and returned expected values.
Screenshot (725)


Note

Medium Risk
Medium risk because it changes the shape and typing of the server-side method context and threads Express req/res through authentication and method execution paths; incorrect assumptions can cause runtime issues for non-HTTP (live) handlers.

Overview
Adds an HTTP-specific context type and threads Express request/response through server method execution. getCallContext now requires res and returns a HttpContext that includes req/res, and the internal method endpoint passes this through to runMethod so method handlers can access HTTP primitives.

OAuth flows were updated to call getCallContext(req, res), tests were adjusted accordingly, and route handler request params are tightened to Record<string, string> to match expected handler input.

Reviewed by Cursor Bugbot for commit 7979c56. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread packages/modelence/src/methods/types.ts Resolved
@omegascorp omegascorp self-assigned this May 15, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit f7f856a. Configure here.

Comment thread packages/modelence/src/methods/types.ts Resolved
@AtulYadav25
AtulYadav25 force-pushed the update-context-params branch from f7f856a to 7979c56 Compare May 16, 2026 03:05

@omegascorp omegascorp left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you

@omegascorp
omegascorp merged commit 161587a into modelence:main May 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants