UNPKG

@envelop/rate-limiter

Version:

This plugins uses [`graphql-rate-limit`](https://github.com/teamplanes/graphql-rate-limit#readme) in order to limit the rate of calling queries and mutations.

24 lines (23 loc) 992 B
import { Plugin } from '@envelop/core'; import { GraphQLResolveInfo } from 'graphql'; import { getGraphQLRateLimiter } from 'graphql-rate-limit'; export * from './utils.js'; export declare class UnauthenticatedError extends Error { } export declare type IdentifyFn<ContextType = unknown> = (context: ContextType) => string; export declare const DIRECTIVE_SDL = "\n directive @rateLimit(max: Int, window: String, message: String) on FIELD_DEFINITION\n"; export declare type RateLimiterPluginOptions = { identifyFn: IdentifyFn; rateLimitDirectiveName?: 'rateLimit' | string; transformError?: (message: string) => Error; onRateLimitError?: (event: { error: string; identifier: string; context: unknown; info: GraphQLResolveInfo; }) => void; }; interface RateLimiterContext { rateLimiterFn: ReturnType<typeof getGraphQLRateLimiter>; } export declare const useRateLimiter: (options: RateLimiterPluginOptions) => Plugin<RateLimiterContext>;