Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to manually check rate limit (#346) #392

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add type tests for manual rate limit check (#346)
Charioteer committed Feb 3, 2025
commit 0e8e5b17316bc876f9551a7e206bda3f06aa1037
58 changes: 58 additions & 0 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import * as http2 from 'node:http2'
import IORedis from 'ioredis'
import pino from 'pino'
import fastifyRateLimit, {
CreateRateLimitOptions,
errorResponseBuilderContext,
FastifyRateLimitOptions,
FastifyRateLimitStore,
@@ -217,3 +218,60 @@ appWithCustomLogger.route({
preHandler: appWithCustomLogger.rateLimit({}),
handler: () => {},
})

const options10: CreateRateLimitOptions = {
store: CustomStore,
skipOnError: true,
max: 0,
timeWindow: 5000,
allowList: ['127.0.0.1'],
keyGenerator: (req: FastifyRequest<RequestGenericInterface>) => req.ip,
ban: 10
}

appWithImplicitHttp.register(fastifyRateLimit, { global: false })
const checkRateLimit = appWithImplicitHttp.createRateLimit(options10)
appWithImplicitHttp.route({
method: 'GET',
url: '/',
handler: async (req, _reply) => {
const limit = await checkRateLimit(req)
expectType<{
isAllowed: true;
key: string;
} | {
isAllowed: false;
key: string;
max: number;
timeWindow: number;
remaining: number;
ttl: number;
ttlInSeconds: number;
isExceeded: boolean;
isBanned: boolean;
}>(limit)
},
})

const options11: CreateRateLimitOptions = {
max: (_req: FastifyRequest<RequestGenericInterface>, _key: string) => 42,
timeWindow: '10s',
allowList: (_req: FastifyRequest<RequestGenericInterface>) => true,
keyGenerator: (_req: FastifyRequest<RequestGenericInterface>) => 42,
}

const options12: CreateRateLimitOptions = {
max: (_req: FastifyRequest<RequestGenericInterface>, _key: string) => Promise.resolve(42),
timeWindow: (_req: FastifyRequest<RequestGenericInterface>, _key: string) => 5000,
allowList: (_req: FastifyRequest<RequestGenericInterface>) => Promise.resolve(true),
keyGenerator: (_req: FastifyRequest<RequestGenericInterface>) => Promise.resolve(42),
}

const options13: CreateRateLimitOptions = {
timeWindow: (_req: FastifyRequest<RequestGenericInterface>, _key: string) => Promise.resolve(5000),
keyGenerator: (_req: FastifyRequest<RequestGenericInterface>) => Promise.resolve('key'),
}

expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options11))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options12))
expectType<preHandlerAsyncHookHandler>(appWithImplicitHttp.rateLimit(options13))