npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@j-ho/nestjs-rate-limiter

v0.1.4

Published

A rate limiter for NestJS with support for Redis and in-memory storage.

Downloads

37

Readme

Nestjs-rate-limiter

This project provides a flexible and extensible rate limiter implementation for NestJS, allowing easy application of rate limits on routes using a decorator. It supports multiple storage backends and allows users to customize the key used for rate limiting.

Features

  • Easy-to-use decorator for applying rate limits on routes
  • Supports multiple storage backends (Redis, in-memory, and easily extendable to others)
  • Allows custom key generation functions for rate limiting
  • Configurable through environment variables
  • Thoroughly tested with unit and E2E tests

Installation

To get started, install the package using npm:

npm install @j-ho/nestjs-rate-limiter

Configuration

Set up environment variables to choose the storage backend. By default, it uses in-memory storage. To use Redis, set the following environment variables:

CACHE_TYPE=redis
REDIS_HOST=localhost
REDIS_PORT=6379

Usage

  1. Import the RateLimiterModule in your app.module.ts:
import { Module } from '@nestjs/common';
import { RateLimiterModule } from '@j-ho/nestjs-rate-limiter';

@Module({
  imports: [RateLimiterModule],
  // ...
})
export class AppModule {}
  1. Use the @RateLimit decorator on your controller methods:
import { Controller, Get } from '@nestjs/common';
import { RateLimit } from '@j-ho/nestjs-rate-limiter';

@Controller()
export class TestController {
  @Get('test')
  @RateLimit(5, 60000) // 5 requests per minute, default key (IP)
  getTest() {
    return 'This is a rate-limited endpoint';
  }

  @Get('custom')
  @RateLimit(5, 60000, (req) => req.headers['user-agent']) // 5 requests per minute, custom key (User-Agent)
  getCustomTest() {
    return 'This is a custom rate-limited endpoint';
  }
}

Extending the Rate Limiter

Adding a New Cache Service

To add a new cache service:

  1. Implement the ICacheService interface:
import { ICacheService } from '@j-ho/nestjs-rate-limiter';

export class NewCacheService implements ICacheService {
  async get(key: string): Promise<string | null> {
    // Implementation
  }

  async set(key: string, value: string, ttl: number): Promise<void> {
    // Implementation
  }

  async clear(): Promise<void> {
    // Implementation
  }
}
  1. Add the new service to the CacheServiceFactory:
import { CacheServiceFactory } from '@j-ho/nestjs-rate-limiter';

CacheServiceFactory.prototype.create = function(type: string): ICacheService {
  switch (type) {
    case 'new-cache':
      return new NewCacheService();
    // ... other cases
  }
};

Testing

To run the tests, use the following command:

npm run test

This will run both unit tests and E2E tests, ensuring the rate limiter functions correctly across different scenarios.

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss any changes or improvements.

License

This project is licensed under the MIT License.