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

NestJS with Fastify - no overload matches this call #578

Closed
simplenotezy opened this issue Jun 11, 2023 · 3 comments
Closed

NestJS with Fastify - no overload matches this call #578

simplenotezy opened this issue Jun 11, 2023 · 3 comments

Comments

@simplenotezy
Copy link

simplenotezy commented Jun 11, 2023

I just stumbled upon #569 and wanted to integrate with a NestJS application, however, we're getting the following error:

 No overload matches this call.
  Overload 1 of 3, '(plugin: FastifyPluginCallback<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...>): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '(fastify: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, _opts: { ...; }, next: (err?: Error) => void) => void' is not assignable to parameter of type 'FastifyPluginCallback<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'.
      Types of parameters 'fastify' and 'instance' are incompatible.
        Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProvider>' is missing the following properties from type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>': listeningOrigin, multipartErrors
  Overload 2 of 3, '(plugin: FastifyPluginAsync<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>, opts?: FastifyRegisterOptions<...>): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '(fastify: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, _opts: { ...; }, next: (err?: Error) => void) => void' is not assignable to parameter of type 'FastifyPluginAsync<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'.
  Overload 3 of 3, '(plugin: FastifyPluginCallback<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>, opts?: FastifyRegisterOptions<...>): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type '(fastify: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, _opts: { ...; }, next: (err?: Error) => void) => void' is not assignable to parameter of type 'FastifyPluginCallback<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.
      Type '(fastify: FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>, _opts: { ...; }, next: (err?: Error) => void) => void' is not assignable to type 'FastifyPluginCallback<{ basePath: string; }, RawServerDefault, FastifyTypeProvider, FastifyBaseLogger>'.

41     instance.register(serverAdapter.registerPlugin(), {

This is our global queue module:

import { createBullBoard } from '@bull-board/api';
import { FastifyAdapter as BBFastifyAdapter } from '@bull-board/fastify';
import { BullModule, InjectQueue } from '@nestjs/bullmq';
import { HttpAdapterHost } from '@nestjs/core';

import { BullAdapter } from '@bull-board/api/bullAdapter';
import { DynamicModule, Global, MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { FastifyAdapter } from '@nestjs/platform-fastify';
import { Queue } from 'bullmq';
import { BullQueueJobs } from './identifiers';

@Global()
@Module({})
export class QueueProcessorModule implements NestModule {
  constructor(
    @InjectQueue(BullQueueJobs.TEST) private readonly testQueue: Queue,
    private adapterHost: HttpAdapterHost<FastifyAdapter>,
  ) {}

  configure(_: MiddlewareConsumer) {
    const serverAdapter = new BBFastifyAdapter();

    createBullBoard({
      queues: [new BullAdapter(this.testQueue)],
      serverAdapter,
    });

    serverAdapter.setBasePath('/ui');

    const instance = this.adapterHost.httpAdapter.getInstance();
    instance.register(serverAdapter.registerPlugin(), {
      basePath: '',
      prefix: 'ui',
      logLevel: 'trace',
    });
  }

  static register(): DynamicModule {
    return {
      module: QueueProcessorModule,
      imports: [
        BullModule.registerQueue({
          name: BullQueueJobs.TEST,
        }),
      ],
      providers: [BullModule],
      exports: [BullModule],
    };
  }
}


This is our dependencies versions:

```js
    "@bull-board/api": "^5.2.1",
    "@bull-board/fastify": "^5.2.1",
    "@bull-board/ui": "^5.2.1",
    "@nestjs/platform-fastify": "^9.3.9",
    "fastify": "^4.18.0",
    "@nestjs/common": "^9.3.9",
@felixmosh
Copy link
Owner

@simplenotezy
Copy link
Author

@felixmosh awesome! That worked, thanks.

However, the middleware does not seem to work, not sure why. I tried applying this middleware:

/* eslint-disable @typescript-eslint/no-floating-promises */
import { Injectable, NestMiddleware } from '@nestjs/common';
import { FastifyReply, FastifyRequest } from 'fastify';

@Injectable()
export class BasicAuthMiddleware implements NestMiddleware {
  use(req: FastifyRequest, res: FastifyReply, next: () => void) {
    const authHeader = req.headers.authorization;
    if (!authHeader || !authHeader.startsWith('Basic ')) {
      res.statusCode = 401;
      res.header('WWW-Authenticate', 'Basic realm="Restricted"');
      res.send('Unauthorized');
      return;
    }

    const credentials = Buffer.from(authHeader.split(' ')[1], 'base64').toString().split(':');
    const username = credentials[0];
    const password = credentials[1];

    // Replace this with your own authentication logic
    const isValid = username === 'admin' && password === 'password';
    if (!isValid) {
      res.statusCode = 401;
      res.send('Unauthorized');
      return;
    }

    next();
  }
}

Using:

BullBoardModule.forRoot({
  route: '/queues',
  middleware: [BasicAuthMiddleware],
  adapter: FastifyAdapter,
}),

@simplenotezy
Copy link
Author

Thanks for the help! Created a dedicated issue for it here: DennisSnijder/nestjs-bull-board#1

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