1.0.0 โ€ข Published 4 months ago

@edirect/rate-limit-module v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
4 months ago

๐Ÿ“ˆ Rate Limiting Module

This module provides a flexible rate-limiting system where access to operations can be restricted based on dynamic rule expressions such as count_quote > 1. If the condition evaluates to true, access is blocked.

โœจ Features

  • Define rate-limiting rules with custom expressions.
  • Dynamically inject context-aware functions for rule evaluation.
  • Easily integrate with any resource via a decorator.
  • Built with extensibility and modularity in mind.

๐Ÿงฑ How It Works

  1. You define a rule using a simple expression (e.g., count_quote > 1).
  2. The rule is evaluated in runtime using data injected from the request context.
  3. If the expression is true, the action is blocked.

๐Ÿงฉ Usage

1. Install the Module

@Module({
  imports: [
    RateLimitingModule,
    RuleEngineModule.forFeatureAsync({
      useFactory: async (quoteService: QuoteService) => {
        return await quoteService.countQuote();
      },
      inject: [QuoteService],
    }),
  ],
})
export class YourFeatureModule {}

2. Decorate Your Handlers

Use the @RateLimitingRuleMetadata decorator to attach metadata used during rule evaluation.

import { RateLimitingRuleMetadata } from 'path-to-rate-limiting-module';

@RateLimitingRuleMetadata({
  action: 'create_quote',
  partnerPath: 'partner.partnerId',
  providerPath: 'provider',
  productTypePath: 'productType',
})
@Post('quotes')
createQuote(@Body() body: CreateQuoteDto) {
  return this.quoteService.create(body);
}
  • action: Name of the action to be controlled.
  • partnerPath: Path to extract the partner ID from the request context.
  • providerPath: Path to extract the provider.
  • productTypePath: Path to extract the product type.

๐Ÿ›  Rule Engine Integration

This module depends on an underlying rule engine to evaluate expressions. You must provide the functions used in expressions through:

RuleEngineModule.forFeatureAsync({
  useFactory: async (useCase: RateLimitingInjectFunctionsUseCase) => {
    return await useCase.getFunctions();
    // returns an object like { count_quote: async () => number }
  },
  inject: [RateLimitingInjectFunctionsUseCase],
});

๐Ÿงช Example Rule

A rule stored in your system might look like:

{
  "expression": "count_quote > 1",
  "action": "create_quote"
}

If the expression returns true, the rate limiter will block further create_quote actions for the user based on the context values.

๐Ÿงฐ Requirements

  • NestJS
  • RuleEngineModule (compatible expression parser/executor)
  • Custom service to inject context functions (like RateLimitingInjectFunctionsUseCase)

๐Ÿ“Ž Notes

  • Rules should be defined and stored externally (e.g., DB, config).
  • The context extractor paths must match the structure of your handler inputs (e.g., body, params).
  • Ensure getFunctions() returns async functions that provide the required values for the expression.

๐Ÿงพ License

MIT