You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the feature has not already been requested
馃殌 Feature Proposal
Allow the FastifyRequest type to be passed as a generic parameter directly into FastifyInstance.
Motivation
When decorating the request with additional data, such as user info or other specific properties, there's no straightforward way to modify FastifyRequest locally in a scoped manner. We are faced with either the option to modify global typings (which is not appropriate if we use decorateRequest within a specific prefixed controller) or use a lot of typecasting in every route handler.
Example
import{FastifyInstance,FastifyRequest,FastifyPluginOptions}from'fastify';interfaceCustomRequestextendsFastifyRequest{user?: {id: string;name: string;};}constuserController=(fastify: FastifyInstance)=>{// Adding FastiyRequest as a type parameter to FastifyInstance would allow us to modify the request objectconstmodifiedFastify=fastifyasFastifyInstance<{},{},{},{},CustomRequest>;modifiedFastify.decorateRequest('user',null);modifiedFastify.addHook('preHandler',async(request,reply)=>{request.user={id: 'abc123',name: 'John Doe'};});modifiedFastify.get('/profile',async(request,reply)=>{// Type for request here should be CustomRequestif(request.user){reply.send(`User ID: ${request.user.id}, Name: ${request.user.name}`);}else{reply.send('User not authenticated');}});};awaitfastifyServer.register(userController,{prefix: '/user'});
The text was updated successfully, but these errors were encountered:
Prerequisites
馃殌 Feature Proposal
Allow the FastifyRequest type to be passed as a generic parameter directly into FastifyInstance.
Motivation
When decorating the request with additional data, such as user info or other specific properties, there's no straightforward way to modify FastifyRequest locally in a scoped manner. We are faced with either the option to modify global typings (which is not appropriate if we use decorateRequest within a specific prefixed controller) or use a lot of typecasting in every route handler.
Example
The text was updated successfully, but these errors were encountered: