fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
497 stars 70 forks source link

typescript types no longer work after fastify 4.18.0 to 4.19.2 update #305

Closed FabianFrank closed 1 year ago

FabianFrank commented 1 year ago

Prerequisites

Fastify version

4.19.2

Plugin version

8.0.1

Node.js version

18.16.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

Ventura 13.4.1 (22F82)

Description

Upgrading from fastify 4.18.0 to 4.19.2 results in the following type error:

Type '{ rateLimit: { max: number; timeWindow: string; }; }' is not assignable to type 'Omit<FastifyContextConfig & FastifyRouteConfig, "url" | "method">'.
  Object literal may only specify known properties, and 'rateLimit' does not exist in type 'Omit<FastifyContextConfig & FastifyRouteConfig, "url" | "method">'.

Somehow the rate limit types no longer get applied to the route config options. The issues appears first with fastify 4.19.0.

Steps to Reproduce

import rateLimit from "@fastify/rate-limit"

  await fastify.register(rateLimit, {
    global: false,
    redis: new Redis(redisUrl, {
      connectionName: "rate-limit",
      password: redisAuthKey,
    }),
  })

  fastify.get(
    "/",
    {
      config: {
        rateLimit: {
          max: 3,
          timeWindow: "1 minute",
        },
      },
    },
    async (req, reply) => {
      await reply.send({ hello: "from ... root" })
    }
  )

Expected Behavior

the types should work as they do with fastify 4.18.0

KirillTregubov commented 1 year ago

Same here. My type error (censored imports):

error TS2769: No overload matches this call.
  Overload 1 of 3, '(plugin: FastifyPluginCallback<{ max: number; timeWindow: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'typeof import(".../node_modules/@fastify/rate-limit/types/index")' is not assignable to parameter of type 'FastifyPluginCallback<{ max: number; timeWindow: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'.
      Type 'typeof import(".../node_modules/@fastify/rate-limit/types/index")' provides no match for the signature '(instance: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProvider>, opts: { ...; }, done: (err?: Error | undefined) => void): void'.
  Overload 2 of 3, '(plugin: FastifyPluginAsync<{ max: number; timeWindow: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'typeof import(".../node_modules/@fastify/rate-limit/types/index")' is not assignable to paramet      Type 'typeof import(".../node_modules/@fastify/rate-limit/types/index")' provides no match for the signature '(instance: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProvider>, opts: { ...; }): Promise<...>'.
  Overload 3 of 3, '(plugin: FastifyPluginCallback<{ max: number; timeWindow: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'typeof import(".../node_modules/@fastify/rate-limit/types/index")' is not assignable to parameter of type 'FastifyPluginCallback<{ max: number; timeWindow: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.

fastify.register(rateLimit, {
                 ~~~~~~~~~
climba03003 commented 1 year ago

I didn't see the types properly set the FastifyRouteConfig interface in this plugin. It is correct that typescript throw error in this case.

Would you like to send PR to update the types?

climba03003 commented 1 year ago

@KirillTregubov For your issue, please try version 8.0.2

FabianFrank commented 1 year ago

Would you like to send PR to update the types?

Sure, I'll take a look today

FabianFrank commented 1 year ago

https://github.com/fastify/fastify-rate-limit/pull/306

KirillTregubov commented 1 year ago

Fixed, thanks so much!