-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
How would one modify an incoming request #551
Comments
Meanwhile technically this is correct, it's pretty confusing in a sense. Because I'd rather decorate the request with a proper name like |
@CedricVanhaverbeke did you manage to solve your issue? |
Let me take a look on this today. |
afaik, we have two choices:
considering the usage of typescript, I'm thinking on the second approach. can a maintainer give some feedback on this? I'm available to work on it in any case. |
cc @fastify/typescript I think the latter would be amazing but I don't know if it's possible |
The latter should be feasible with the current type definitions! (Some genetics wizardry is required, though 😁). |
I managed to make what I believe to be a more proper way to do what you're wanting with typescript. First, I generated a new project with fastify-cli using typescript template: fastify generate hello-fastify-ts --lang=typescript and then I created another file on plugin folder (in this setup, anything there will be autoloaded): import fp from 'fastify-plugin'
export default fp(async (fastify, opts) => {
fastify.addHook('preHandler', async (req) => {
req.authenticatedUser = {
id: 42,
name: 'Marcell',
token: 'wadda.wadda.wadda',
isAdmin: true,
};
})
})
interface AuthenticatedUser {
id: number,
name: string;
token: string;
isAdmin: boolean;
}
declare module 'fastify' {
export interface FastifyRequest {
authenticatedUser?: AuthenticatedUser;
}
} it works pretty well, my type validations were working just fine, I got warnings when trying to use the new custom property in a wrong way both in controller and hook, tests ok, etc etc. so, after some thought, I don't think it'd be very useful to add a builtin API to support that, imho the current one seems very straightforward, maybe there's some place to put this example in a documentation? @CedricVanhaverbeke, do you think this is enough for what you're looking for? maintainers, any recommended course of action for this? |
I agree with your conclusions. If anything, all we need to do here is write some docs, or point at some resources, or both :) |
Would you like to send a PR improving the docs? |
@Mazuh ^ |
Sure! I'll be opening until EOD. |
Thanks for the effort! Looks great! Wishing you all the best :D |
one problem with this approach is typescript now will expect anyone have anysolution for that ? i tried
but that broke down my type provider |
Use case
incoming request looks like this:
I would like to change the user parameter to the current user's id. This one is fetched from a JWT token that is passed along with the request. Let's say
req.user
contains the user object.First solution
My first solution consists of creating a plugin (with
fastify-plugin
) to do this:But this feels not right. I assume modifying the
request
-object is not the way to goAlternative ideas
request
request
and redirecting to this modified request.Question
How should this be approached?
The text was updated successfully, but these errors were encountered: