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

ngx-rate-limiter

v1.0.1

Published

The `ngx-rate-limiter` library provides an interceptor for Angular applications that allows you to implement rate limiting for HTTP requests made by your application. This interceptor helps prevent excessive traffic to your server by restricting the numbe

Downloads

7

Readme

NGX Rate Limiter

The ngx-rate-limiter library provides an interceptor for Angular applications that allows you to implement rate limiting for HTTP requests made by your application. This interceptor helps prevent excessive traffic to your server by restricting the number of requests that can be made within a certain time interval. This can provide an extra layer of security and can be very impactful in today's world where creating bots becomes very easy and 100s of requests can be made within seconds.

Features

  • Global Rate Limiting: Set a global rate limit for all HTTP requests made by your Angular application.
  • Specific Endpoint Configuration: Define specific rate limits for individual API endpoints based on the endpoint ending with.
  • Dynamic Configuration: Easily configure rate limits by providing a configuration object to the interceptor.
  • Exceeding Rate Limit Handling: Automatically block requests that exceed the configured rate limit for sepcific time. Once an endpoint exceeds the limit specified requests from that endpoint can be blocked for a specific amount of time . The interceptor return a status code 429 with message "Too Many Requests".

Getting started

Hope you already have an angular project, if not please create one using the below commands

npm install -g @angular/cli
ng new my-app
cd my-app
ng serve --open

once the angular application setup is ready, install the Ngx Rate Limiter using the following command

npm i ngx-rate-limiter

Use the interceptor

Add the interceptor to app.config or app.module.ts depends on what Angular version consuming application has.

  { provide: HTTP_INTERCEPTORS, useClass: RateLimitInterceptor, multi: true },

Provide RATE_LIMIT_CONFIG

{
      provide: RATE_LIMIT_CONFIG,
      useValue: {
        specificEndPointsConfig: SPECIFIC_ENDPOINT_RATE_LIMIT_CONFIG,
        allEndPointsConfig: GLOBAL_RATE_LIMIT_CONFIG,
      },
    },

Config

export const SPECIFIC_ENDPOINT_RATE_LIMIT_CONFIG: SpecificEndPointConfig[] = [
  {
    urlEndsWith: '/launches',
    config: {
      maxRequests: 10,
      blockDuration: 10000,
      intervalDuration: 60000,
    },
  },
];
export const GLOBAL_RATE_LIMIT_CONFIG: RateLimitProperties = {
  maxRequests: 5,
  intervalDuration: 10000,
  blockDuration: 5000,
};

that's it. If allEndPointsConfig config not provided, Rate limiter will only rate limit the specific endpoint. If both are provided then all endpoints config will work for all endpoints except the ones defined in specific endpoint config.

So with the above provided config, if user makes more than 10 requests with a minute with endpoint ending with '/launches', the rate limiter will block all request for next 10 seconds.