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

Plugin type mismatch when using fastify-metrics in an ESM project #92

Open
FredTreg opened this issue Oct 19, 2023 · 3 comments
Open

Plugin type mismatch when using fastify-metrics in an ESM project #92

FredTreg opened this issue Oct 19, 2023 · 3 comments
Labels
help wanted Extra attention is needed

Comments

@FredTreg
Copy link

FredTreg commented Oct 19, 2023

Since some 3rd party librairies I use in my project are now only packaged for ESM, I decided to migrate my TypeScript project from CommonJS to ESM ("module": "nodenext" and "moduleResolution": "nodenext" in tsconfig.json)

I have an issue with fastify-metrics: when registering, the type of metricsPlugin is no longer considered as matching the expected type :

import metricsPlugin from 'fastify-metrics'
...
fastify.register(metricsPlugin, { endpoint: '/metrics' })

=>

src/app.ts:52:18 - error TS2769: No overload matches this call.
  Overload 1 of 3, '(plugin: FastifyPluginCallback<{ endpoint: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'typeof import("/workspace/coucou/server/node_modules/fastify-metrics/dist/index")' is not assignable to parameter of type 'FastifyPluginCallback<{ endpoint: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'.
      Type 'typeof import("/workspace/coucou/server/node_modules/fastify-metrics/dist/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<{ endpoint: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'typeof import("/workspace/coucou/server/node_modules/fastify-metrics/dist/index")' is not assignable to parameter of type 'FastifyPluginAsync<{ endpoint: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'.
      Type 'typeof import("/workspace/coucou/server/node_modules/fastify-metrics/dist/index")' provides no match for the signature '(instance: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProvider>, opts: { ...; }): Promise<...>'.
  Overload 3 of 3, '(plugin: FastifyPluginCallback<{ endpoint: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>, opts?: FastifyRegisterOptions<...> | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'typeof import("/workspace/coucou/server/node_modules/fastify-metrics/dist/index")' is not assignable to parameter of type 'FastifyPluginCallback<{ endpoint: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.

52 fastify.register(metricsPlugin, { endpoint: '/metrics' })

Am I doing something wrong? It worked when my project was CommonJS.

As a workaround I did this:

fastify.register(metricsPlugin as unknown as FastifyPluginAsync<Partial<IMetricsPluginOptions>>, {
  endpoint: '/metrics'
})
@SkeLLLa
Copy link
Owner

SkeLLLa commented Oct 31, 2023

Hi. This plugin doesn't support ESM. And for now I don't have a plans for making it ESM-compatible. However if you want to try implement ESM support, you're more than welcome.

@SkeLLLa SkeLLLa added the help wanted Extra attention is needed label Oct 31, 2023
@dannycobussen
Copy link

I'm using the following as a "workaround".

import fastifyMetrics from "fastify-metrics";
...
fastify.register(fastifyMetrics.default, { endpoint: "/metrics" });

@FredTreg
Copy link
Author

FredTreg commented Nov 3, 2023

Thanks @dannycobussen for the workaround, my syntax was not working in fact.

@SkeLLLa SkeLLLa pinned this issue Nov 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants