import LRU from 'lru-cache' ; import { NextApiResponse } from 'next' ; const rateLimit = ( options ) => { const tokenCache = new LRU ( { max : parseInt ( options . uniqueTokenPerInterval || 500 , 10 ) , maxAge : parseInt ( options . interval || 60000 , 10 ) , } ) ; return { check : ( res , limit , token ) => new Promise < void > ( ( resolve , reject ) => { const tokenCount = tokenCache . get ( token ) || [ 0 ] ; if ( tokenCount [ 0 ] === 0 ) { tokenCache . set ( token , tokenCount ) ; } tokenCount [ 0 ] += 1 ; const currentUsage = tokenCount [ 0 ] ; const isRateLimited = currentUsage >= parseInt ( limit , 10 ) ; res . setHeader ( 'X-RateLimit-Limit' , limit ) ; res . setHeader ( 'X-RateLimit-Remaining' , isRateLimited ? 0 : limit - currentUsage ) ; return isRateLimited ? reject ( ) : resolve ( ) ; } ) , } ; } ; export default rateLimit ;