@@ -10,12 +10,28 @@ import {
1010 IRateLimiterStoreOptions ,
1111} from 'rate-limiter-flexible'
1212
13- export const createTrpcRedisLimiter = defineLimiterWithProps < {
14- redisClient : IRateLimiterStoreOptions [ 'storeClient' ]
15- limiter ?: (
16- opts : Required < BaseOpts < AnyRootConfig , any > >
17- ) => IRateLimiterStoreOptions [ 'insuranceLimiter' ]
18- } > (
13+ const isBlocked = async ( store : RateLimiterRedis , fingerprint : string ) => {
14+ try {
15+ await store . consume ( fingerprint )
16+ return null
17+ } catch ( error ) {
18+ if ( error instanceof RateLimiterRes ) {
19+ return Math . round ( error . msBeforeNext / 1000 ) || 1
20+ }
21+ // Should not happen with `insuranceLimiter`
22+ throw error
23+ }
24+ }
25+
26+ export const createTrpcRedisLimiter = defineLimiterWithProps <
27+ {
28+ redisClient : IRateLimiterStoreOptions [ 'storeClient' ]
29+ limiter ?: (
30+ opts : Required < BaseOpts < AnyRootConfig , any > >
31+ ) => IRateLimiterStoreOptions [ 'insuranceLimiter' ]
32+ } ,
33+ NonNullable < Awaited < ReturnType < typeof isBlocked > > >
34+ > (
1935 {
2036 store : ( opts ) => {
2137 return new RateLimiterRedis ( {
@@ -26,18 +42,7 @@ export const createTrpcRedisLimiter = defineLimiterWithProps<{
2642 insuranceLimiter : opts . limiter ( opts ) ,
2743 } )
2844 } ,
29- async isBlocked ( store , fingerprint ) {
30- try {
31- await store . consume ( fingerprint )
32- return null
33- } catch ( error ) {
34- if ( error instanceof RateLimiterRes ) {
35- return Math . round ( error . msBeforeNext / 1000 ) || 1
36- }
37- // Should not happen with `insuranceLimiter`
38- throw error
39- }
40- } ,
45+ isBlocked,
4146 } ,
4247 ( currentState ) => {
4348 return {
0 commit comments