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

types: Use full pino API as Logger default #5397

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

matt-smarsh
Copy link

Checklist

Fixes #5382

@jsumners jsumners requested a review from a team April 9, 2024 19:13
Signed-off-by: Matthew Pietz <89040187+matt-smarsh@users.noreply.github.com>
@climba03003
Copy link
Member

It's like changing the default is incompatible with the generic

@climba03003
Copy link
Member

More discussion on #5244 and #4960

@mcollina
Copy link
Member

This essentially reverts #2289 and #2289.

I guess we have some decisions to make on this one.

Things were separated to allow of other loggers to be passed in, but the generics are not really working well for this.

@matt-smarsh
Copy link
Author

Oof, yeah. Just looking at test/types/hooks.test-d.ts would lead me to believe that the types for server.addHook() are now causing problems. I don't know the history of these interfaces well enough to understand why addHook is completely duplicating the FastifyInstance generics rather than just passing this. I'll keep playing around with things locally but I might not be able to solve this on my own.

@matt-smarsh
Copy link
Author

matt-smarsh commented Apr 11, 2024

I think the hook handlers can be simplified quite a bit by using infer and passing the entire fastify instance to them as a generic. Something like the following:

export interface onRequestHookHandler<
  Server extends FastifyInstance<any, any, any, any, any>,
> {
  (
    this: Server,
    request: Server extends FastifyInstance<any, infer RawRequest, any, any, any> ? RawRequest : never,
    reply: Server extends FastifyInstance<any, any, infer RawReply, any, any> ? RawReply : never,
    done: HookHandlerDoneFunction
  ): void;
}

Then using it would allow each hook to be simplified to:

  addHook<
    Fn extends onRequestHookHandler<this> | onRequestAsyncHookHandler<this> = onRequestHookHandler<this>
  >(
    name: 'onRequest',
    hook: Fn extends unknown ? Fn extends AsyncFunction ? onRequestAsyncHookHandler<this> : onRequestHookHandler<this> : Fn,
  ): this;

I don't know if this will break other areas but it would seem to help with preserving any user-defined custom interfaces.

D'oh, I can see now that while that works, the request and reply properties aren't supposed to be the Raw versions, but wrapped in with Fastify* equivalents. I think I'm at my knowledge limit with this one. Hopefully someone else can figure it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fastify.log.isLevelEnabled() is missing from TypeScript types
3 participants