From faaf5a45af8dce3ea8f11c13e473a59ab1ef70b9 Mon Sep 17 00:00:00 2001 From: Turka Turki Date: Sun, 14 Sep 2025 05:40:54 +0000 Subject: [PATCH] chore: code cleanup and formatting for middleware files --- src/middleware/auth.ts | 3 +-- src/middleware/rateLimit.ts | 35 +++++++++++++++++++---------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/middleware/auth.ts b/src/middleware/auth.ts index 4ff8b95..45562a8 100644 --- a/src/middleware/auth.ts +++ b/src/middleware/auth.ts @@ -24,7 +24,7 @@ export async function verifyWalletSignature( } // Type guard for request.body - const { walletAddress, message, signature } = request.body as Record; + const { walletAddress, message, signature } = request.body as { walletAddress: string; message: string; signature: string }; if ( typeof walletAddress !== 'string' || typeof message !== 'string' || @@ -50,6 +50,5 @@ export async function verifyWalletSignature( error ? { reason: error } : undefined ); } - // If all validation passes, continue } diff --git a/src/middleware/rateLimit.ts b/src/middleware/rateLimit.ts index b4f733e..04959d0 100644 --- a/src/middleware/rateLimit.ts +++ b/src/middleware/rateLimit.ts @@ -1,10 +1,12 @@ + import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; +/** /** * Rate limiting configuration for different endpoint types */ export const rateLimitConfigs = { - // General API rate limit (most endpoints) + // General API rate limit general: { max: parseInt(process.env.RATE_LIMIT_GENERAL_MAX || '100'), // 100 requests timeWindow: 60 * 1000, // per minute @@ -12,7 +14,7 @@ export const rateLimitConfigs = { skipOnError: false, }, - // Authentication endpoints (stricter limits) + // Authentication endpoints auth: { max: parseInt(process.env.RATE_LIMIT_AUTH_MAX || '10'), // 10 requests timeWindow: 60 * 1000, // per minute @@ -20,7 +22,7 @@ export const rateLimitConfigs = { skipOnError: false, }, - // API key creation (very strict) + // API key creation apiKeyCreation: { max: parseInt(process.env.RATE_LIMIT_API_KEY_CREATION_MAX || '3'), // 3 API key creations timeWindow: 60 * 1000, // per minute @@ -28,7 +30,7 @@ export const rateLimitConfigs = { skipOnError: false, }, - // Registration (moderate limits) + // Registration registration: { max: parseInt(process.env.RATE_LIMIT_REGISTRATION_MAX || '5'), // 5 registration attempts timeWindow: 60 * 1000, // per minute @@ -36,7 +38,7 @@ export const rateLimitConfigs = { skipOnError: false, }, - // Health checks (more lenient) + // Health checks health: { max: parseInt(process.env.RATE_LIMIT_HEALTH_MAX || '200'), // 200 requests timeWindow: 60 * 1000, // per minute @@ -45,6 +47,7 @@ export const rateLimitConfigs = { } }; +/** /** * Custom error response for rate limit exceeded */ @@ -53,10 +56,11 @@ export const rateLimitErrorHandler = (request: FastifyRequest, context: any) => statusCode: 429, error: 'Rate limit exceeded', message: `Too many requests. Try again in ${Math.ceil(context.ttl / 1000)} seconds.`, - retryAfter: Math.ceil(context.ttl / 1000) + retryAfter: Math.ceil(context.ttl / 1000), }; }; +/** /** * Key generator for rate limiting - uses IP + wallet address if available */ @@ -64,41 +68,40 @@ export const rateLimitKeyGenerator = (request: FastifyRequest) => { const ip = request.ip; let walletAddress: string | undefined; if (request.body && typeof request.body === 'object' && 'walletAddress' in request.body) { - walletAddress = (request.body as any).walletAddress; + walletAddress = (request.body as { walletAddress?: string }).walletAddress; } if (!walletAddress && request.headers['x-wallet-address']) { walletAddress = String(request.headers['x-wallet-address']); } - // If wallet address is available, use IP + wallet for more granular control if (walletAddress) { return `${ip}:${walletAddress.toLowerCase()}`; } - // Fallback to just IP return ip; }; +/** /** * Register rate limiting plugin with different configurations */ export async function registerRateLimiting(fastify: FastifyInstance) { - // Register the rate limit plugin await fastify.register(import('@fastify/rate-limit'), { - global: false, // We'll apply different limits per route + global: false, errorResponseBuilder: rateLimitErrorHandler, keyGenerator: rateLimitKeyGenerator, addHeadersOnExceeding: { 'x-ratelimit-limit': true, 'x-ratelimit-remaining': true, - 'x-ratelimit-reset': true + 'x-ratelimit-reset': true, }, addHeaders: { 'x-ratelimit-limit': true, 'x-ratelimit-remaining': true, - 'x-ratelimit-reset': true - } + 'x-ratelimit-reset': true, + }, }); } +/** /** * Create rate limit preHandler for specific configurations */ @@ -106,8 +109,8 @@ export function createRateLimitHandler(config: typeof rateLimitConfigs.general) return { config, preHandler: async (request: FastifyRequest, reply: FastifyReply) => { - // You can add custom logic here if needed + // Custom logic can be added here if needed return; - } + }, }; }