nestjs-graphql-rate-limit
Version:
Rate Limit module with Redis storage support for NestJS GraphQL APIs
44 lines • 2.25 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ThrottlerStorageRedisService = void 0;
const common_1 = require("@nestjs/common");
const ioredis_1 = __importDefault(require("ioredis"));
let ThrottlerStorageRedisService = class ThrottlerStorageRedisService {
constructor(redisOrOptions, scanCount) {
this.scanCount = typeof scanCount === 'undefined' ? 1000 : scanCount;
if (redisOrOptions instanceof ioredis_1.default) {
this.redis = redisOrOptions;
}
else if (typeof redisOrOptions === 'string') {
this.redis = new ioredis_1.default(redisOrOptions);
}
else {
this.redis = new ioredis_1.default(redisOrOptions);
}
}
async getRecord(key) {
const ttls = (await this.redis.scan(0, 'MATCH', `${key}:*`, 'COUNT', this.scanCount)).pop();
return ttls.map((k) => parseInt(k.split(':').pop())).sort();
}
async addRecord(key, ttl) {
await this.redis.set(`${key}:${Date.now() + ttl * 1000}`, ttl, 'EX', ttl);
}
};
ThrottlerStorageRedisService = __decorate([
common_1.Injectable(),
__metadata("design:paramtypes", [Object, Number])
], ThrottlerStorageRedisService);
exports.ThrottlerStorageRedisService = ThrottlerStorageRedisService;
//# sourceMappingURL=throttler-storage-redis.service.js.map