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

Support RouteShorthandOptions to receive async hooks #4859

Closed
2 tasks done
MatanYadaev opened this issue Jun 27, 2023 · 9 comments · Fixed by #5147
Closed
2 tasks done

Support RouteShorthandOptions to receive async hooks #4859

MatanYadaev opened this issue Jun 27, 2023 · 9 comments · Fixed by #5147
Labels
bug Confirmed bug good first issue Good for newcomers typescript TypeScript related

Comments

@MatanYadaev
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Currently, RouteShorthandOptions type doesn't allow passing async hooks:

export interface RouteShorthandOptions<
  // ...
> {
  // ...
  onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
  | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];

It should be something like that:

export interface RouteShorthandOptions<
  // ...
> {
  // ...
  onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
  | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
  // Add these two:
  | onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
  | onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];
@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@mcollina mcollina added bug Confirmed bug good first issue Good for newcomers typescript TypeScript related labels Jun 27, 2023
@climba03003
Copy link
Member

For anyone who would like to works on this issue.
Please also check #4705

@gimnathperera
Copy link

Hi @mcollina assign this to me

@BrendenInhelder
Copy link
Contributor

I would like to work on solving this issue with a solution different from #4655 and #4862 to avoid regression.

@Uzlopak
Copy link
Contributor

Uzlopak commented Jul 31, 2023

Please provide a PR. Reassigning this to you.

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 1, 2023

I asked @webstrand on discord if he could help us.

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 1, 2023

Maybe somebody can open a question on stackoverflow? Usually @jcalz also answers there really fast.

@BrendenInhelder
Copy link
Contributor

Hopefully, this is not repetitive but I wanted to note something (feel free to delete/hide if not beneficial to issue efforts).
With the (reverted) change from #4655 in route.d.ts:

  onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
  | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[]
  | onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
  | onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>[];

Even if we specify the return as a Promise<void> in associated test in hooks.test-d.ts we still have any types. request and reply are any (not recognized):

server.get<RouteGenericInterface, CustomContextConfig>('/', {
  onRequest: async (request, reply):Promise<void> => {
    expectType<CustomContextConfigWithDefault>(request.context.config)
    expectType<CustomContextConfigWithDefault>(reply.context.config)
  },

And likewise, providing an additional argument will resolve the issue (not really because it is meant to be async) because it recognizes the three args as one that includes a hookHandlerDoneFunction parameter no matter what that parameter actually is.

Maybe this is indicative of a complicated issue; I am not an expert on typescript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug good first issue Good for newcomers typescript TypeScript related
Projects
None yet
6 participants