Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use rate-limit-redis #10

Closed
mathysth opened this issue Apr 23, 2024 · 4 comments
Closed

Can't use rate-limit-redis #10

mathysth opened this issue Apr 23, 2024 · 4 comments
Assignees

Comments

@mathysth
Copy link

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);
  }
}
@MathurAditya724 MathurAditya724 self-assigned this Apr 23, 2024
@MathurAditya724
Copy link
Member

Hey, can you please provide me a reproduction of this, like on CodeSandbox or Stackblitz?

@mathysth
Copy link
Author

mathysth commented Apr 23, 2024

Hey it will take sometime so I will give you instead the file url, i'm also using bun.sh here

@MathurAditya724
Copy link
Member

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
});

@mathysth
Copy link
Author

It's working for me thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants