nest
nest copied to clipboard
Argument of type 'FastifyCookie' is not assignable to parameter of type 'FastifyPluginCallback<FastifyCookieOptions> | FastifyPluginAsync<FastifyCookieOptions> | Promise<...> | Promise<...>'.
Is there an existing issue for this?
- [X] I have searched the existing issues
Current behavior
error TS2345: Argument of type 'FastifyCookie' is not assignable to parameter of type 'FastifyPluginCallback<FastifyCookieOptions> | FastifyPluginAsync<FastifyCookieOptions> | Promise<...> | Promise<...>'. Type 'FastifyCookie' is not assignable to type 'FastifyPluginCallback<FastifyCookieOptions>'. 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, 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<NestFastifyApplication>( 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 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
- [X]
@nestjs/common - [X]
@nestjs/core - [ ]
@nestjs/microservices - [ ]
@nestjs/platform-express - [X]
@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?
- [X] macOS
- [ ] Windows
- [ ] Linux
Other
No response