Nestjs with fastify throws an error when registering fastify/secure-session

109 Views Asked by At

Following the documentation I am trying to register the fastify secure session, but for some reason I am getting an error regarding the type of the fastify secure session middleware.

The code in the main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { ConfigService } from '@nestjs/config';
import { ValidationPipe } from '@nestjs/common';
import session from '@fastify/secure-session';
import { session_key } from '@app/keys';

(async () => {
const app = await NestFactory.create\<NestFastifyApplication\>(AppModule, new FastifyAdapter());
const config = app.get(ConfigService);
const { port } = config.get('http');
app.useLogger(config.get('loglevels'));
app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
app.enableShutdownHooks();
app.register(session, { secret: 'asdawdasadgfhafjwefihdauhadiwudhaadoiuahwdasiefuhasefiu', salt: 'asdawdafowsuFIU' });
await app.listen(port);
})();

Error:

Argument of type 'FastifySecureSession' is not assignable to parameter of type 'FastifyPluginCallback<SecureSessionPluginOptions | (SecureSessionPluginOptions & Required<Pick<SecureSessionPluginOptions, "sessionName">>)[]> | FastifyPluginAsync<...> | Promise<...> | Promise<...>'.
  Type 'FastifySecureSession' is not assignable to type 'FastifyPluginCallback<SecureSessionPluginOptions | (SecureSessionPluginOptions & Required<Pick<SecureSessionPluginOptions, "sessionName">>)[]>'.
    Types of parameters 'instance' and 'instance' are incompatible.
      Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>' is missing the following properties from type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse<IncomingMessage>, FastifyBaseLogger, FastifyTypeProviderDefault>': serializeCookie, parseCookie, createSecureSession, decodeSecureSession, and 3 more.
    14 |   app.useGlobalPipes(new ValidationPipe({ whitelist: true }));
    15 |   app.enableShutdownHooks();
  > 16 |   app.register(session, { key: 'asdawdasadgfhafjwefihdauhadiwudhaadoiuahwdasiefuhasefiu', salt: 'asdawdafowsuFIU' });
       |                ^^^^^^^
    17 |   await app.listen(port);
    18 | })();
    19 |

According to the docs, everything should be fine...

2

There are 2 best solutions below

0
Andy95 On

The problem was the version of the @nestjs/platform-fastify.

Upgrading to version 10.3.1 solves the type inconsistency.

0
Regis Silva On

I dig into the source code of @nestjs/platform-fastify and looks like now we need to add the register type

So I did this:

import { contentParser } from 'fastify-multer';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(options),
  );

  await app.register(
    contentParser as unknown as Parameters<
      NestFastifyApplication['register']
    >[0],
  );

  ...
}

Hackish but worked for me!