@canmertinyo/rate-limit-express 1.3.4 → 1.3.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,15 +1,15 @@
1
1
  # Express Rate Limiter
2
+
2
3
  ### A simple in-memory rate-limiting middleware for Express.js to restrict the number of requests a client can make within a given time. Ideal for lightweight use cases
3
4
 
5
+ To install :
4
6
 
5
- To install :
6
7
  ```typescript
7
8
  npm i @canmertinyo/rate-limit-express
8
9
  ```
9
10
 
11
+ # Example usage :
10
12
 
11
-
12
- # Example usage :
13
13
  ```typescript
14
14
  import express from "express";
15
15
  import { rateLimiter } from "@canmertinyo/rate-limit-express";
@@ -21,6 +21,7 @@ app.use(
21
21
  rateLimiter({
22
22
  ms: 60000, // Time in milliseconds
23
23
  maxRequest: 5, // Maximum requests allowed within the time
24
+ //DEFAULT IS IN MEMORY
24
25
  })
25
26
  );
26
27
 
@@ -31,5 +32,64 @@ app.get("/", (req, res) => {
31
32
  app.listen(3000, () => {
32
33
  console.log("Server is running on http://localhost:3000");
33
34
  });
35
+ ```
36
+
37
+ # Using Redis As A Store Manager
38
+
39
+ ```typescript
40
+ import express from "express";
41
+ import { rateLimiter, RedisStorage } from "@canmertinyo/rate-limit-express";
42
+
43
+ const app = express();
44
+ const port = 3001;
45
+
46
+ // Configure the rate limiter with Redis storage
47
+ app.use(
48
+ rateLimiter({
49
+ ms: 5000, // Time window in milliseconds
50
+ maxRequest: 2, // Maximum requests allowed in the time window
51
+ storage: new RedisStorage({ host: "127.0.0.1", port: 6379 }), // Redis configuration
52
+ })
53
+ );
54
+
55
+ // Sample route
56
+ app.get("/", (req, res) => {
57
+ res.send("Hello World!");
58
+ });
59
+
60
+ // Start the server
61
+ app.listen(port, () => {
62
+ console.log(`Server listening on port ${port}`);
63
+ });
64
+ ```
65
+
66
+ # Using Mongo As A Store Manager
67
+
68
+ ```typescript
69
+ import express from "express";
70
+ import { MongoStorage, rateLimiter } from "@canmertinyo/rate-limit-express";
71
+
72
+ const app = express();
73
+ const port = 3001;
74
+
75
+ // MongoDB connection string (replace with your MongoDB URL)
76
+ const mongoUrl = "mongodb://your-mongodb-url";
77
+
78
+ app.use(
79
+ rateLimiter({
80
+ ms: 5000, // Time window in milliseconds
81
+ maxRequest: 2, // Maximum requests allowed in the time window
82
+ storage: new MongoStorage(mongoUrl), // MongoDB configuration
83
+ })
84
+ );
85
+
86
+ // Sample route
87
+ app.get("/", (req, res) => {
88
+ res.send("Hello World!");
89
+ });
34
90
 
91
+ // Start the server
92
+ app.listen(port, () => {
93
+ console.log(`Server listening on port ${port}`);
94
+ });
35
95
  ```
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@canmertinyo/rate-limit-express",
3
3
  "description": "A simple rate-limiting middleware for Express.js",
4
- "version": "1.3.4",
4
+ "version": "1.3.6",
5
5
  "main": "dist/index.js",
6
6
  "license": "MIT",
7
7
  "types": "dist/index.d.ts",
@@ -18,8 +18,10 @@
18
18
  "author": "c4nzin",
19
19
  "devDependencies": {
20
20
  "@types/express": "^5.0.0",
21
- "typescript": "^5.7.2",
21
+ "@types/mongoose": "^5.11.97",
22
22
  "ioredis": "^5.4.1",
23
- "mongoose": "^8.8.3"
23
+ "lerna": "^8.1.9",
24
+ "mongoose": "^8.8.3",
25
+ "typescript": "^5.7.2"
24
26
  }
25
27
  }
@@ -5,8 +5,6 @@ import { RateLimitModel } from "./schemas/rate-limit.schema";
5
5
  export class MongoStorage implements RateLimiter {
6
6
  private model: mongoose.Model<RateLimitRecord>;
7
7
 
8
- //DEFAULT DB URI VERRIRSEK PATLICAZ GİBİ AMA BAKARIZ, AZ KÖTÜ🤨
9
- //connect-mongo kütüphanesini kullanarak yapsak?
10
8
  constructor(public mongoDbUrl: string = "mongodb://127.0.0.1:27017") {
11
9
  mongoose
12
10
  .connect(mongoDbUrl)
@@ -28,6 +28,7 @@ export class RedisStorage implements RateLimiter {
28
28
  public async createRateLimitRecord(
29
29
  record: RateLimitRecord
30
30
  ): Promise<RateLimitRecord> {
31
+ //json objesine döndürmeden başka bir yol gibi..
31
32
  await this.redis.set(record.key, JSON.stringify(record));
32
33
  return record;
33
34
  }