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

Missing types in @hono-rate-limiter/cloudflare #27

Open
gabrielecirulli opened this issue Sep 1, 2024 · 1 comment
Open

Missing types in @hono-rate-limiter/cloudflare #27

gabrielecirulli opened this issue Sep 1, 2024 · 1 comment
Assignees

Comments

@gabrielecirulli
Copy link

Hi, I've tried setting up the rate limiter with Hono but I'm running into strange TypeScript errors that I can't explain.

Here's the code I started with:

import { cloudflareRateLimiter } from "@hono-rate-limiter/cloudflare";
import { Hono } from "hono";
import { Env } from "../schemas";

const app = new Hono<{ Bindings: Env }>();

app.use(
  cloudflareRateLimiter<{ Bindings: Env }>({
    rateLimitBinding: (c) => c.env.RATE_LIMITER,
    keyGenerator: (c) => c.req.header("cf-connecting-ip") ?? "", // Method to generate custom identifiers for clients.
  }),
);

Where ../schemas.d.ts is:

import type { Fetcher } from "@cloudflare/workers-types";
import type { RateLimitBinding } from "@hono-rate-limiter/cloudflare";

export interface Env {
  // Built-in (Cloudflare Pages)
  ASSETS: Fetcher;
  RATE_LIMITER: RateLimitBinding;
}

With this, I get this cryptic error on cloudflareRateLimiter within app.use:

No overload matches this call.
  Overload 1 of 21, '(...handlers: MiddlewareHandler<{ Bindings: Env; }, never, {}>[]): Hono<{ Bindings: Env; }, BlankSchema, "/">', gave the following error.
    Argument of type 'MiddlewareHandler<{ Bindings: Env; }, string, Input>' is not assignable to parameter of type 'MiddlewareHandler<{ Bindings: Env; }, never, {}>'.
      Types of parameters 'c' and 'c' are incompatible.
        Type 'Context<{ Bindings: Env; }, never, {}>' is not assignable to type 'Context<{ Bindings: Env; }, string, Input>'.
          Property '#private' in type 'Context' refers to a different member that cannot be accessed from within type 'Context'.
  Overload 2 of 21, '(handler: MiddlewareHandler<{ Bindings: Env; }, never, {}>): Hono<{ Bindings: Env; }, BlankSchema, "/">', gave the following error.
    Argument of type 'MiddlewareHandler<{ Bindings: Env; }, string, Input>' is not assignable to parameter of type 'MiddlewareHandler<{ Bindings: Env; }, never, {}>'.
  Overload 3 of 21, '(path: string, ...handlers: MiddlewareHandler<{ Bindings: Env; }, string, {}>[]): Hono<{ Bindings: Env; }, BlankSchema, "/">', gave the following error.
    Argument of type 'MiddlewareHandler<{ Bindings: Env; }, string, Input>' is not assignable to parameter of type 'string'.ts(2769)

I am not able to understand what this error is pointing at.


I tried the following approach to more closely match the documentation and exclude any mismatch in my configuration:

import { cloudflareRateLimiter, RateLimitBinding } from "@hono-rate-limiter/cloudflare";
import { Hono } from "hono";

type AppType = {
  Variables: {
    rateLimit: boolean;
  };
  Bindings: {
    RATE_LIMITER: RateLimitBinding;
  };
};

// Apply the rate limiting middleware to all requests.
const app = new Hono<AppType>().use(
  cloudflareRateLimiter<AppType>({
    rateLimitBinding: (c) => c.env.RATE_LIMITER,
    keyGenerator: (c) => c.req.header("cf-connecting-ip") ?? "", // Method to generate custom identifiers for clients.
  }),
);

Note how this code matches the sample at https://www.npmjs.com/package/@hono-rate-limiter/cloudflare.

I notice the following two Typescript errors:

  1. On the first line, Module '"@hono-rate-limiter/cloudflare"' has no exported member 'RateLimitBinding'.ts(2305)
  2. On cloudflareRateLimiter(...):
No overload matches this call.
  Overload 1 of 21, '(...handlers: MiddlewareHandler<AppType, never, {}>[]): Hono<AppType, BlankSchema, "/">', gave the following error.
    Argument of type 'MiddlewareHandler<AppType, string, Input>' is not assignable to parameter of type 'MiddlewareHandler<AppType, never, {}>'.
      Types of parameters 'c' and 'c' are incompatible.
        Type 'Context<AppType, never, {}>' is not assignable to type 'Context<AppType, string, Input>'.
          Property '#private' in type 'Context' refers to a different member that cannot be accessed from within type 'Context'.
  Overload 2 of 21, '(handler: MiddlewareHandler<AppType, never, {}>): Hono<AppType, BlankSchema, "/">', gave the following error.
    Argument of type 'MiddlewareHandler<AppType, string, Input>' is not assignable to parameter of type 'MiddlewareHandler<AppType, never, {}>'.
  Overload 3 of 21, '(path: string, ...handlers: MiddlewareHandler<AppType, string, {}>[]): Hono<AppType, BlankSchema, "/">', gave the following error.
    Argument of type 'MiddlewareHandler<AppType, string, Input>' is not assignable to parameter of type 'string'.ts(2769)

Any suggestions as to what may be causing these errors?

@MathurAditya724
Copy link
Member

My bad, I have updated the docs you can check it out here - https://github.com/rhinobase/hono-rate-limiter/blob/main/packages/cloudflare/README.md

Here is the code -

import {
  cloudflareRateLimiter,
} from "@hono-rate-limiter/cloudflare";
import { Hono } from "hono";

type AppType = {
  Variables: {
    rateLimit: boolean;
  };
  Bindings: {
    RATE_LIMITER: RateLimit;
  };
};

// Apply the rate limiting middleware to all requests.
const app = new Hono<AppType>().use(
  cloudflareRateLimiter<AppType>({
    rateLimitBinding: (c) => c.env.RATE_LIMITER,
    keyGenerator: (c) => c.req.header("cf-connecting-ip") ?? "", // Method to generate custom identifiers for clients.
  })
);

app.get("/", (c) => {
  return c.text("Hello Hono!");
});

export default app;

RateLimit is the type coming from cloudflare/types, you can access it if you are using Cloudflare runtime with Hono

@MathurAditya724 MathurAditya724 self-assigned this Sep 2, 2024
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