Files
DefinitelyTyped/types/koa-ratelimit/index.d.ts
2022-10-26 15:19:03 -07:00

96 lines
2.9 KiB
TypeScript

// Type definitions for koa-ratelimit 5.0
// Project: https://github.com/koajs/ratelimit#readme
// Definitions by: Ben Watkins <https://github.com/OutdatedVersion>
// Patrick Muff <https://github.com/dislick>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 4.2
import { Middleware, Context } from "koa";
import { Redis } from "ioredis";
declare function KoaRatelimit(options?: KoaRatelimit.MiddlewareOptions): Middleware;
declare namespace KoaRatelimit {
interface HeaderNameOptions {
/**
* The amount of requests remaining in the current limiting period.
*/
remaining: string;
/**
* The time, expressed as a UNIX epoch timestamp, at which your rate-limit expires.
*/
reset: string;
/**
* The total amount of requests a client may make during a limiting period.
*/
total: string;
}
interface MiddlewareOptions {
/**
* Driver to use ("redis" or "memory").
*/
driver: 'redis' | 'memory';
/**
* The database powering the backing rate-limiter package.
*/
db: Redis | Map<any, any>;
/**
* The length of a single limiting period. This value is expressed
* in milliseconds, defaulting to one hour.
*/
duration?: number | undefined;
/**
* The maximum amount of requests a client (see the `id` field) may
* make during a limiting period. (see `duration`)
*/
max?: number | undefined;
/**
* Get the unique-identifier for a request. This defaults to the
* client's IP address. Returning "false" will skip rate-limiting.
*/
id?: ((context: Context) => string | false) | undefined;
/**
* Whether or not to disable the usage of rate limit headers. This defaults
* to **false**.
*/
disableHeader?: boolean | undefined;
/**
* The message used on the response body if a client is rate-limited. There is
* a default message; which includes when they should try again.
*/
errorMessage?: string | undefined;
/**
* Whether or not to throw an error upon being rate-limited. This uses
* the Koa context function "throw".
*/
throw?: boolean | undefined;
/**
* A relation of header to the header's display name.
*/
headers?: HeaderNameOptions | undefined;
/**
* If function returns true, middleware exits before limiting
*/
whitelist?: ((context: Context) => boolean | Promise<boolean>) | undefined;
/**
* If function returns true, 403 error is thrown
*/
blacklist?: ((context: Context) => boolean | Promise<boolean>) | undefined;
}
}
export = KoaRatelimit;