Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

v3.1.0 #85

Merged
merged 2 commits into from
Dec 1, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Adding getIpFromRequest method to get the request's IP (#84)
* Adding getIpFromRequest method to get the request's IP
  • Loading branch information
Borales authored Dec 1, 2021
commit 47aa10d4b0c1efe125e79f1cdfea3a4e975a7768
6 changes: 5 additions & 1 deletion lib/rate-limiter.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,16 @@ export class RateLimiterGuard implements CanActivate {
const response = this.httpHandler(context).res

const rateLimiter: RateLimiterAbstract = await this.getRateLimiter(reflectedOptions)
const key = request.ip?.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)?.[0]
const key = this.getIpFromRequest(request)

await this.responseHandler(response, key, rateLimiter, points, pointsConsumed)
return true
}

protected getIpFromRequest(request: { ip: string }): string {
return request.ip?.match(/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/)?.[0]
}

private httpHandler(context: ExecutionContext) {
if (this.options.for === 'ExpressGraphql') {
return {
Expand Down