Skip to content

[Issue]: Impossible to pass custom context to /store/products or /store/products/:id routes (needed for Payload CMS integration) #14069

@aroard

Description

@aroard

Package.json file

{
	"name": "medusa",
	"version": "0.0.1",
	"description": "A starter for Medusa projects.",
	"author": "Medusa (https://medusajs.com)",
	"license": "MIT",
	"keywords": [
		"sqlite",
		"postgres",
		"typescript",
		"ecommerce",
		"headless",
		"medusa"
	],
	"scripts": {
		"build": "medusa build",
		"seed": "medusa exec ./src/scripts/seed/seed.ts",
		"start": "medusa start",
		"dev": "medusa develop",
		"test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
		"test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
		"test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
		"dev:email": "email dev --dir ./src/modules/resend/emails"
	},
	"dependencies": {
		"@dermofluide/medusa-plugin-invoice": "workspace:*",
		"@dermofluide/medusa-plugin-payload": "workspace:*",
		"@dermofluide/medusa-plugin-product-review": "workspace:*",
		"@dermofluide/medusa-plugin-sendcloud": "workspace:*",
		"@dermofluide/medusa-plugin-wishlist": "workspace:*",
		"@medusajs/admin-sdk": "2.11.0",
		"@medusajs/cli": "2.11.0",
		"@medusajs/dashboard": "2.11.0",
		"@medusajs/draft-order": "2.11.0",
		"@medusajs/framework": "2.11.0",
		"@medusajs/icons": "2.11.0",
		"@medusajs/js-sdk": "2.11.0",
		"@medusajs/medusa": "2.11.0",
		"@medusajs/ui": "^4.0.23",
		"@medusajs/ui-preset": "^2.11.0",
		"@payloadcms/sdk": "^3.62.1",
		"@react-email/components": "0.5.7",
		"@rsc-labs/medusa-store-analytics-v2": "^0.1.4",
		"axios": "^1.13.1",
		"jsonwebtoken": "^9.0.2",
		"resend": "^6.4.0",
		"zod": "^3.25.67"
	},
	"devDependencies": {
		"@medusajs/test-utils": "2.11.0",
		"@react-email/preview-server": "4.3.2",
		"@swc/core": "1.5.7",
		"@swc/jest": "^0.2.36",
		"@tanstack/react-query": "5.64.2",
		"@types/jest": "^29.5.13",
		"@types/node": "^20.0.0",
		"@types/react": "^18.3.2",
		"@types/react-dom": "^18.2.25",
		"jest": "^29.7.0",
		"prop-types": "^15.8.1",
		"react": "^18.2.0",
		"react-dom": "^18.2.0",
		"react-email": "^4.3.2",
		"ts-node": "^10.9.2",
		"typescript": "^5.6.2",
		"vite": "^5.2.11",
		"yalc": "^1.0.0-pre.53"
	},
	"engines": {
		"node": ">=20"
	}
}

Node.js version

v24.11.0

Database and its version

PostgreSQL 17

Operating system name and version

Ubuntu 24.04

Browser name

Firefox

What happended?

Describe the issue

It is currently impossible to inject a custom context into Store API routes, such as:

  • /store/products
  • /store/products/:id

This context is required when extending Medusa with custom modules (in my case, a PayloadModuleService).
The service needs information like locale and collection, but these values cannot be passed through:

  • neither the request context (req.context)
  • nor req.filterableFields
  • nor any other mechanism available from middleware

Because of that, custom modules cannot access the correct context during calls like:

  const result = await this.find(collection_name, {
    locale: filter.context?.locale ?? "en",
    where: {
      medusa_id: { in: ids },
    },
    depth: 2,
  });

I attempt adding the context from a custom middleware:

  const locale = req.params?.locale || req.query?.locale || "fr";

  req.context = {
    ...req.context,
    payload_product: {
      locale,
      collection: "products",
    },
  };

  next();

However, the Store API router does not forward the context to the container resolution layer nor the query engine, so the service cannot access what the middleware added.

Because of this limitation, I was forced to copy Medusa's Store routes directly into my own code and manually inject the context before calling the query layer:

const query = req.scope.resolve(ContainerRegistrationKeys.QUERY);
const context = {
  payload_product: QueryContext({
    locale,
    collection: "products",
  }),
};

I also needed to modify the client path to inject the locale:
/store/${locale}/products

This should be possible through middleware, not by modifying Medusa's codebase.

Expected behavior

  • It should be possible to inject custom context into Store API routes (e.g., /store/products, /store/products/:id) from a middleware.
  • The context should be forwarded to the query layer and custom services, including:
    • context passed to module services (list, retrieve, etc.)
    • context available under filter.context
  • Custom modules should be able to read:
    • context.locale
    • context.collection
    • or any arbitrary context needed by custom business logic.

Example expected working middleware:

  locale: "fr",
  collection: "products",

And the service should receive it:

filter.context.locale === "fr"

Actual behavior

  • Any field added via middleware to req.context is ignored.
  • Custom modules cannot access the needed context unless the core Medusa Store API code is manually duplicated and modified.
  • This breaks extensibility and maintainability.

Link to reproduction repo

https://github.com/medusajs/medusa.git

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions