Skip to content

Commit 3bcdc50

Browse files
feat: Allow a custom user identifier to be passed in the rate limitter
1 parent 4ff0a48 commit 3bcdc50

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/utils/with-rate-limit.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,22 @@ import ms from "ms";
55
import isProd from "./is-prod";
66

77
interface RateLimitConfig {
8+
/**
9+
* The key to use for the rate limit. Usually an IP address or a user ID.
10+
* @default IP address of the request
11+
*/
12+
userIdentifier?: string;
13+
/**
14+
* A unique identifier for the rate limit.
15+
*/
816
identifier: string;
17+
/**
18+
* The maximum number of requests allowed within the window.
19+
*/
920
limit: number;
21+
/**
22+
* The time window in seconds.
23+
*/
1024
windowInSeconds: number;
1125
}
1226

@@ -22,7 +36,7 @@ export async function withRateLimit<T>(
2236
const ip = await getIP();
2337

2438
const rateLimitResult = await checkRateLimit({
25-
key: ip || "",
39+
key: config?.userIdentifier || ip || "",
2640
options: {
2741
identifier: config.identifier,
2842
limit: config.limit,

0 commit comments

Comments
 (0)