Skip to content

Commit 3acfe2a

Browse files
committed
Revert "fix: use TS type inference for hooks"
This reverts commit f948814.
1 parent b15115f commit 3acfe2a

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

test/types/hooks.test-d.ts

-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,6 @@ server.addHook('onRequest', (request: FastifyRequest, reply: FastifyReply, done:
2323
expectType<void>(done(new Error()))
2424
})
2525

26-
expectError(server.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => {}))
27-
28-
server.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply) => {
29-
expectType<FastifyRequest>(request)
30-
expectType<FastifyReply>(reply)
31-
})
32-
3326
server.addHook('preParsing', (request, reply, payload, done) => {
3427
expectType<FastifyRequest>(request)
3528
expectType<FastifyReply>(reply)

types/hooks.d.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Readable } from 'stream'
2-
import { CallbackOrPromise } from './callback-or-promise'
32
import { FastifyInstance } from './instance'
43
import { RouteOptions, RouteGenericInterface } from './route'
54
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
@@ -20,14 +19,23 @@ interface RequestPayload extends Readable {
2019
* `onRequest` is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be `preParsing`.
2120
* Notice: in the `onRequest` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
2221
*/
23-
export type onRequestHookHandler<
22+
export interface onRequestHookHandler<
2423
RawServer extends RawServerBase = RawServerDefault,
2524
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
2625
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
2726
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
28-
ContextConfig = ContextConfigDefault,
29-
T extends (...args: any[]) => void = (...args: any[]) => void
30-
> = CallbackOrPromise<T, [ FastifyRequest<RouteGeneric, RawServer, RawRequest>, FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> ], HookHandlerDoneFunction>
27+
ContextConfig = ContextConfigDefault
28+
> {
29+
(
30+
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
31+
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
32+
done: HookHandlerDoneFunction
33+
): void;
34+
(
35+
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
36+
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
37+
): Promise<unknown>;
38+
}
3139

3240
/**
3341
* `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.

types/instance.d.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,10 @@ export interface FastifyInstance<
8383
*/
8484
addHook<
8585
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
86-
ContextConfig = ContextConfigDefault,
87-
T extends (...args: any[]) => void = (...args: any[]) => void
86+
ContextConfig = ContextConfigDefault
8887
>(
8988
name: 'onRequest',
90-
hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, T>
89+
hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
9190
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
9291

9392
/**

0 commit comments

Comments
 (0)