Skip to main content

Creating rate-limiter adapters

Implementing your custom IRateLimiterAdapter

In order to create an adapter you need to implement the IRateLimiterAdapter contract.

Implementing your custom IRateLimiterStorageAdapter

We provide an additional contract IRateLimiterStorageAdapter for building custom rate-limiter storage adapters tailored to DatabaseRateLimiterAdapter and DatabaseRateLimiterProviderFactory.

Testing your custom IRateLimiterStorageAdapter

We provide a complete test suite to test your rate-limiter storage adapter implementation. Simply use the rateLimiterBreakerStorageTestSuite function:

  • Preconfigured Vitest test cases
  • Common edge case coverage

Usage example:

// filename: MyRateLimiterStorageAdapter.test.ts

import { beforeEach, describe, expect, test } from "vitest";
import { rateLimiterBreakerStorageTestSuite } from "@daiso-tech/core/rate-limiter/test-utilities";
import { MemoryRateLimiterStorageAdapter } from "./MemoryRateLimiterStorageAdapter.js";

describe("class: MyRateLimiterStorageAdapter", () => {
rateLimiterBreakerStorageTestSuite({
createAdapter: () => new MemoryRateLimiterStorageAdapter(),
test,
beforeEach,
expect,
describe,
});
});

Implementing your custom IRateLimiterProvider class

In some cases, you may need to implement a custom RateLimiterProvider class to optimize performance for your specific technology stack. You can then directly implement the IRateLimiterProvider contract.

Further information

For further information refer to @daiso-tech/core/rate-limiter API docs.