Can't use rate-limit-redis
mathysth opened this issue · 4 comments
I'm trying to use rate-limit-redis
but it's not working and compiling because of the redisStore
.
Has the readme said I could use this store but I can't because of this error:
Type 'RedisStore' is not assignable to type 'Store'.
Here my setup
import { IocContainer, LoggerService, type ConfigService } from "@cosmoosjs/core";
import type { Server } from "@cosmoosjs/hono-openapi";
import { rateLimiter } from "hono-rate-limiter";
import RedisStore from "rate-limit-redis";
import { createClient } from "redis";
export async function setupRateLimit(server: Server, config: ConfigService) {
try {
const redisClient = createClient({
url: config.get<string>('REDIS_URL')
});
await redisClient.connect();
const limiter = rateLimiter({
windowMs: 10 * 60 * 1000, // 10 minutes
limit: 100,
standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers;
keyGenerator: (c) => {
console.log(c);
return "<unique_key>";
},
store: new RedisStore({
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
}),
});
// Apply the rate limiting middleware to all requests.
server.hono.use(limiter);
} catch (error) {
const logger = IocContainer.container.get(LoggerService);
logger.pino.error('An error occurred while connecting to redis');
logger.pino.error(error);
}
}
Hey, can you please provide me a reproduction of this, like on CodeSandbox or Stackblitz?
Hey it will take sometime so I will give you instead the file url, i'm also using bun.sh here
I tried creating a sample app, using pnpm. It did work out of the box (the rate-limiting stuff), but there is a type error. The error message is
Type 'ConfigType<any, any, {}>' is missing the following properties from type 'Options': legacyHeaders, validate
hono-rate-limiter
doesn't support legacyHeaders
and validate
properties, as these are something that express-rate-limit
itself suggests to void use for newer applications. So to avoid the type errors, we can do this
const limiter = rateLimiter({
windowMs: 60 * 1000, // 1 minute
limit: 5,
standardHeaders: "draft-6", // draft-6: `RateLimit-*` headers;
keyGenerator: (c) => {
return "<unique_key>";
},
store: new RedisStore({
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
}) as unknown as Store // Adding the correct type
});
It's working for me thanks for the help