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

Argument of type 'FastifyCookie' is not assignable to parameter of type 'FastifyPluginCallback<FastifyCookieOptions> | FastifyPluginAsync<FastifyCookieOptions> | Promise<...> | Promise<...>'. #13219

Closed
5 of 15 tasks
dev-mariana opened this issue Feb 16, 2024 · 2 comments
Labels
needs triage This issue has not been looked into

Comments

@dev-mariana
Copy link

dev-mariana commented Feb 16, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

error TS2345: Argument of type 'FastifyCookie' is not assignable to parameter of type 'FastifyPluginCallback | FastifyPluginAsync | Promise<...> | Promise<...>'.
Type 'FastifyCookie' is not assignable to type 'FastifyPluginCallback'.
Types of parameters 'instance' and 'instance' are incompatible.
Type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse, FastifyBaseLogger, FastifyTypeProviderDefault>' is missing the following properties from type 'FastifyInstance<RawServerDefault, IncomingMessage, ServerResponse, FastifyBaseLogger, FastifyTypeProviderDefault>': serializeCookie, parseCookie, signCookie, unsignCookie

main.ts:

import fastifyCookie from '@fastify/cookie';
import { NestFactory } from '@nestjs/core';
import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { ZodExceptionFilter } from '@shared/filters/zod-exception';
import { AppModule } from './app.module';

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

await app.register(fastifyCookie, {
secret: process.env.SECRET_KEY,
hook: 'onRequest',
});

app.useGlobalFilters(new ZodExceptionFilter());

await app.listen(4000);
}
bootstrap();

controller:

@post(':poll_id/votes')
async createVote(
@param() { poll_id }: CreateVoteDTOParam,
@Body() { poll_option_id }: CreateVoteDTOBody,
@Req() request: FastifyRequest,
@res({ passthrough: true }) response: FastifyReply,
): Promise {
const voteOnPollBody = createVoteSchemaBody.parse({
poll_option_id: String(poll_option_id),
});

const voteOnPollParams = createVoteSchemaParam.parse({
  poll_id: String(poll_id),
});

let sessionId = request.cookies.sessionId;

if (!sessionId) {
  sessionId = randomUUID();

  response.setCookie('sessionId', sessionId, {
    path: '/',
    maxAge: 60 * 60 * 24 * 30,
    signed: true,
    httpOnly: true,
  });

}

return { sessionId };

}

I already try put the fastifyCookie as any, works, but the cookie is not working as should be, the request.cookies is empty. Someone can help me?

Minimum reproduction code

https://github.com/dev-mariana/nlw-expert-node

Steps to reproduce

No response

Expected behavior

I would like to generate a cookie in my app by post request.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

No response

Packages versions

"dependencies": {
"@fastify/cookie": "^9.3.1",
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/mapped-types": "*",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-fastify": "^10.3.3",
"@prisma/client": "^5.9.1",
"fastify": "^4.26.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"zod": "^3.22.4"
},

Node.js version

20.10.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@dev-mariana dev-mariana added the needs triage This issue has not been looked into label Feb 16, 2024
@Tony133
Copy link
Contributor

Tony133 commented Feb 17, 2024

Remove the fastify dependency (https://github.com/dev-mariana/nlw-expert-node/blob/main/package.json#L30) from the package.json file and the error disappears. If using the fastify platform, it is best to also remove the express platform (see here: https://github.com/dev-mariana/nlw-expert-node/blob/main/package.json#L27) and also the @types/express package (see here: https://github.com/dev-mariana/nlw-expert-node/blob/main/package.json#L39)

I also rewrite it in this topic, the error is due to the fact that you "directly" installed both fastify and @nestjs/platform-fastify dependencies is they are not the same version.

If you install the same version of fastify that is inside the @nestjs/platform-fastify package, the error disappears.

See this issue here for reference: #11265

@dev-mariana
Copy link
Author

I tried it here and it solved my problem. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

3 participants