import { Props, NextFunc } from '@exobase/core'; import { Duration } from 'durhuman'; interface IRateLimitStore { /** * A function that should incrament the currently stored * number of requests in the current window and return * both the total number of requests made in the current * window and the beginning timestamp of the window. */ inc: (key: string) => Promise<{ count: number; timestamp: number; }>; reset: (key: string) => Promise; } interface IRateLimitLogger { log: (...args: any[]) => void; warn: (...args: any[]) => void; error: (...args: any[]) => void; } type UseRateLimitLimit = { /** * Time to live: The amount of time before we should consider an * item in the cache to be fresh. The hook will ignore an item in * the cache if the TTL has passed. */ window: Duration; /** * The maximum number of requests allowed for a single identity * during the window. */ max: number; }; type UseRateLimitOption = { /** * A string unique to the items you are using this hook to cache. * You'll likely want to use a unique key in every endpoint */ key: string; limit: UseRateLimitLimit | ((props: TProps) => UseRateLimitLimit | Promise); /** * A mapping function to convert the args into an object that should * be used to generate the cache key. */ toIdentity: (props: TProps) => string; /** * If passed, the hook will log information about the caching using * this object. If nothing is passed the hook will be silent. */ logger?: IRateLimitLogger; /** * An optional function telling the rate limit hook * where to get the store object. */ store?: IRateLimitStore | ((props: TProps) => IRateLimitStore | Promise); /** * Strict tells the rate limit hook if it should return an error * to the user when an error is thrown while interacting with * the store. If strict is false, errors will be ignored and the * will be called. */ strict?: boolean; }; declare const useRateLimit: (options: UseRateLimitOption) => (func: NextFunc>) => NextFunc; export { IRateLimitLogger, IRateLimitStore, UseRateLimitOption, useRateLimit };