Skip to content

Commit

Permalink
fix: return type of rateLimit (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
MunifTanjim authored Sep 2, 2024
1 parent 595a627 commit 4626bc7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
20 changes: 18 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
/// <reference types='node' />

import {
ContextConfigDefault,
FastifyPluginCallback,
FastifyRequest,
FastifySchema,
preHandlerAsyncHookHandler,
RouteGenericInterface,
RouteOptions
} from 'fastify';

declare module 'fastify' {
interface FastifyInstance {
rateLimit: (options?: fastifyRateLimit.RateLimitOptions) => preHandlerAsyncHookHandler;
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
rateLimit<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
SchemaCompiler extends FastifySchema = FastifySchema,
>(options?: fastifyRateLimit.RateLimitOptions): preHandlerAsyncHookHandler<
RawServer,
RawRequest,
RawReply,
RouteGeneric,
ContextConfig,
SchemaCompiler,
TypeProvider,
Logger
>;
}
interface FastifyContextConfig {
rateLimit?: fastifyRateLimit.RateLimitOptions | false;
Expand Down
13 changes: 13 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fastify, {
} from 'fastify'
import * as http2 from 'http2'
import { default as ioredis } from 'ioredis'
import pino from 'pino';
import fastifyRateLimit, {
errorResponseBuilderContext,
FastifyRateLimitOptions,
Expand Down Expand Up @@ -160,3 +161,15 @@ const errorResponseContext: errorResponseBuilderContext = {
max: 1000,
ttl: 123
}
const appWithCustomLogger = fastify({
logger: pino(),
}).withTypeProvider()

appWithCustomLogger.register(fastifyRateLimit, options1)

appWithCustomLogger.route({
method: 'GET',
url: '/',
preHandler: appWithCustomLogger.rateLimit({}),
handler: () => {},
})

0 comments on commit 4626bc7

Please sign in to comment.