Skip to content

Commit fb57ad9

Browse files
authored
Merge pull request #22 from perseidesjs/changeset-release/develop
Version Packages
2 parents 8ccb51a + c401640 commit fb57ad9

File tree

3 files changed

+64
-57
lines changed

3 files changed

+64
-57
lines changed

.changeset/honest-points-pay.md

Lines changed: 0 additions & 55 deletions
This file was deleted.

CHANGELOG.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
11
# @perseidesjs/medusa-plugin-rate-limit
22

3+
## 3.0.0
4+
5+
### Major Changes
6+
7+
- 5e0ee7a: V3 - Revamped the whole way we rate limit apps. Introducing the `RateLimit` class for more granular control.
8+
9+
**Breaking Changes:**
10+
- The `defaultRateLimit` middleware has been removed
11+
- Global configuration has been removed
12+
13+
**New Features:**
14+
- Introduction of the `RateLimit` class for programmatic rate limiting
15+
- Built-in `ipRateLimit` middleware for common IP-based rate limiting
16+
- Support for custom identifiers beyond IP addresses
17+
- More granular control over rate limiting logic
18+
19+
## The RateLimit Class
20+
21+
The core of V3 is the new `RateLimit` class that gives you programmatic control over rate limiting. This class integrates directly with Medusa's cache service and allows you to implement custom rate limiting logic.
22+
23+
### Basic Usage
24+
25+
```ts title="src/api/middlewares.ts"
26+
import { defineMiddlewares } from "@medusajs/medusa";
27+
import { RateLimit } from "@perseidesjs/medusa-plugin-rate-limit";
28+
import { Modules } from "@medusajs/framework/utils";
29+
30+
export default defineMiddlewares({
31+
routes: [
32+
{
33+
matcher: "/store/custom*",
34+
middlewares: [
35+
async (
36+
req: MedusaRequest,
37+
res: MedusaResponse,
38+
next: MedusaNextFunction,
39+
) => {
40+
const cacheService = req.scope.resolve(Modules.CACHE);
41+
const rateLimit = new RateLimit({
42+
cacheService,
43+
options: {
44+
limit: 50, // 50 requests per minute
45+
window: 60,
46+
},
47+
});
48+
49+
const ip = req.headers["x-forwarded-for"] as string;
50+
const { success } = await rateLimit.limit(ip);
51+
if (!success) {
52+
res
53+
.status(429)
54+
.send("Too many requests, please try again later.");
55+
return;
56+
}
57+
next();
58+
},
59+
],
60+
},
61+
],
62+
});
63+
```
64+
365
## 2.2.0
466

567
### Minor Changes

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@perseidesjs/medusa-plugin-rate-limit",
3-
"version": "3.0.0-next.1",
3+
"version": "3.0.0",
44
"description": "A simple rate-limit middleware for Medusa",
55
"author": "adevinwild",
66
"license": "MIT",
@@ -31,7 +31,7 @@
3131
"build:publish": "rimraf dist && tsc --build ./tsconfig.build.json",
3232
"watch": "tsc --watch",
3333
"format": "biome format --write",
34-
"typecheck":"tsc -b --noEmit",
34+
"typecheck": "tsc -b --noEmit",
3535
"lint": "biome lint --write",
3636
"prepublishOnly": "npm run build",
3737
"version": "changeset version",

0 commit comments

Comments
 (0)