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 <wollencarina@gmail.com>
This commit is contained in:
co-authored by
Udit Takkar
CarinaWolli
parent
6ef35e4dba
commit
ebca5c6409
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user