Skip to content

V3#21

Merged
adevinwild merged 25 commits intodevelopfrom
v3
Oct 19, 2025
Merged

V3#21
adevinwild merged 25 commits intodevelopfrom
v3

Conversation

@adevinwild
Copy link
Member

V3

This PR introduces new way to use rate limit, by being inspired by how modern libraries handle it (which I think is the way to go and really allows customizability)

Before

import { defaultRateLimit, configureDefaults } from '@perseidesjs/medusa-plugin-rate-limit'

configureDefaults({
  limit: 100,
  window: 900,
})

export default defineMiddlewares({
  routes: [
    {
      matcher: "/store/custom*",
      middlewares: [defaultRateLimit()],
    },
  ],
})

After

import { defineMiddlewares } from "@medusajs/medusa"
import { RateLimit } from '@perseidesjs/medusa-plugin-rate-limit'
import { Modules } from "@medusajs/framework/utils"

export default defineMiddlewares({
  routes: [
    {
      matcher: "/store/custom*",
      middlewares: [
        async (req: MedusaRequest, res: MedusaResponse, next: MedusaNextFunction) => {
          const cacheService = req.scope.resolve(Modules.CACHE)
          const rateLimit = new RateLimit({
            cacheService,
            options: {
              limit: 50, // 50 requests per minute
              window: 60
            }
          })

          const ip = req.headers['x-forwarded-for'] as string
          const { success } = await rateLimit.limit(ip)
          if (!success) {
            res.status(429).send('Too many requests, please try again later.')
            return
          }
          next()
        }
      ],
    },
  ],
})

As you can see the main changes resides in the export of a new class RateLimit which will be our new way to throttle endpoints, with much more freedom.
This really unlocks new ways/scenarios to rate limit your endpoints in our Medusa apps, this still uses the cache module under the hood

@adevinwild adevinwild self-assigned this Oct 8, 2025
@adevinwild adevinwild added the enhancement New feature or request label Oct 8, 2025
@changeset-bot
Copy link

changeset-bot bot commented Oct 8, 2025

🦋 Changeset detected

Latest commit: 2ab43cf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@perseidesjs/medusa-plugin-rate-limit Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@adevinwild adevinwild merged commit c4b4b08 into develop Oct 19, 2025
1 check passed
@adevinwild adevinwild deleted the v3 branch January 7, 2026 07:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments