@@ -44,7 +44,11 @@ export function expressRateLimiter(
4444 const rateLimiter = setupRateLimiter ( rateLimiterAlgo , rateLimiterOptions , redisClient ) ;
4545
4646 // return the rate limiting middleware
47- return async ( req : Request , res : Response , next : NextFunction ) : Promise < void > => {
47+ return async (
48+ req : Request ,
49+ res : Response ,
50+ next : NextFunction
51+ ) : Promise < void | Response < any , Record < string , any > > > => {
4852 const requestTimestamp = new Date ( ) . valueOf ( ) ;
4953 const { query, variables } : { query : string ; variables : any } = req . body ;
5054 if ( ! query ) {
@@ -83,18 +87,17 @@ export function expressRateLimiter(
8387 requestTimestamp ,
8488 queryComplexity
8589 ) ;
86- if ( rateLimiterResponse . success === false ) {
90+ if ( ! rateLimiterResponse . success ) {
8791 // TODO: add a header 'Retry-After' with the time to wait untill next query will succeed
8892 // FIXME: send information about query complexity, tokens, etc, to the client on rejected query
89- res . status ( 429 ) . json ( { graphqlGate : rateLimiterResponse } ) ;
90- } else {
91- res . locals . graphqlGate = {
92- timestamp : requestTimestamp ,
93- complexity : queryComplexity ,
94- tokens : rateLimiterResponse . tokens ,
95- } ;
96- return next ( ) ;
93+ return res . status ( 429 ) . json ( { graphqlGate : rateLimiterResponse } ) ;
9794 }
95+ res . locals . graphqlGate = {
96+ timestamp : requestTimestamp ,
97+ complexity : queryComplexity ,
98+ tokens : rateLimiterResponse . tokens ,
99+ } ;
100+ return next ( ) ;
98101 } catch ( err ) {
99102 return next ( err ) ;
100103 }
0 commit comments