From ebca5c64096f0b132beb748a4ad0fa6c4cb68a2f Mon Sep 17 00:00:00 2001 From: Andreas Thomas Date: Wed, 17 Apr 2024 16:01:13 +0200 Subject: [PATCH] fix: use 'success' value for better accuracy (#14624) * fix: use 'success' value for better accuracy * add success to test --------- Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: CarinaWolli --- packages/lib/checkRateLimitAndThrowError.test.ts | 2 ++ packages/lib/checkRateLimitAndThrowError.ts | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/lib/checkRateLimitAndThrowError.test.ts b/packages/lib/checkRateLimitAndThrowError.test.ts index afe0c74214..c6789d9619 100644 --- a/packages/lib/checkRateLimitAndThrowError.test.ts +++ b/packages/lib/checkRateLimitAndThrowError.test.ts @@ -19,6 +19,7 @@ describe("checkRateLimitAndThrowError", () => { limit: 10, remaining: -1, reset: Date.now() + 10000, + success: false, } as RatelimitResponse; }); @@ -35,6 +36,7 @@ describe("checkRateLimitAndThrowError", () => { limit: 10, remaining: 5, reset: Date.now() + 10000, + success: true, } as RatelimitResponse; }); diff --git a/packages/lib/checkRateLimitAndThrowError.ts b/packages/lib/checkRateLimitAndThrowError.ts index bed136e2ee..d88845203f 100644 --- a/packages/lib/checkRateLimitAndThrowError.ts +++ b/packages/lib/checkRateLimitAndThrowError.ts @@ -10,11 +10,11 @@ export async function checkRateLimitAndThrowError({ opts, }: RateLimitHelper) { const response = await rateLimiter()({ rateLimitingType, identifier, opts }); - const { remaining, reset } = response; + const { success, reset } = response; if (onRateLimiterResponse) onRateLimiterResponse(response); - if (remaining < 1) { + if (!success) { const convertToSeconds = (ms: number) => Math.floor(ms / 1000); const secondsToWait = convertToSeconds(reset - Date.now()); throw new TRPCError({