Skip to content

Commit 3989c44

Browse files
committed
types(hooks): create asyncHook type for every hooks
1 parent 1d9dab9 commit 3989c44

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

test/types/hooks.test-d.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fastify, { RouteOptions, FastifyReply, FastifyRequest } from '../../fasti
22
import { expectType, expectError, expectAssignable } from 'tsd'
33
import { FastifyInstance } from '../../types/instance'
44
import { FastifyError } from 'fastify-error'
5-
import { HookHandlerDoneFunction, RequestPayload } from '../../types/hooks'
5+
import { RequestPayload } from '../../types/hooks'
66

77
const server = fastify()
88

@@ -15,16 +15,14 @@ type TestPayloadType = {
1515

1616
// Synchronous Tests
1717

18-
expectError(server.addHook('onRequest', (request: FastifyRequest, reply: FastifyReply) => {}))
19-
server.addHook('onRequest', (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => {
18+
server.addHook('onRequest', (request, reply, done) => {
2019
expectType<FastifyRequest>(request)
2120
expectType<FastifyReply>(reply)
2221
expectAssignable<(err?: FastifyError) => void>(done)
2322
expectAssignable<(err?: NodeJS.ErrnoException) => void>(done)
2423
expectType<void>(done(new Error()))
2524
})
2625

27-
expectError(server.addHook('preParsing', (request, reply, payload) => {}))
2826
server.addHook('preParsing', (request, reply, payload, done) => {
2927
expectType<FastifyRequest>(request)
3028
expectType<FastifyReply>(reply)
@@ -120,13 +118,11 @@ server.addHook('onClose', (instance, done) => {
120118

121119
// Asynchronous
122120

123-
expectError(server.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction) => {}))
124-
server.addHook('onRequest', async (request: FastifyRequest, reply: FastifyReply) => {
121+
server.addHook('onRequest', async (request, reply) => {
125122
expectType<FastifyRequest>(request)
126123
expectType<FastifyReply>(reply)
127124
})
128125

129-
expectError(server.addHook('preParsing', async (request: FastifyRequest, reply: FastifyReply, payload: RequestPayload, done: HookHandlerDoneFunction) => {}))
130126
server.addHook('preParsing', async (request, reply, payload) => {
131127
expectType<FastifyRequest>(request)
132128
expectType<FastifyReply>(reply)

types/hooks.d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ export interface onRequestHookHandler<
3131
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
3232
done: HookHandlerDoneFunction
3333
): void;
34+
}
35+
36+
export interface onRequestAsyncHookHandler<
37+
RawServer extends RawServerBase = RawServerDefault,
38+
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
39+
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
40+
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
41+
ContextConfig = ContextConfigDefault
42+
> {
3443
(
3544
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
3645
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,

types/instance.d.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FastifySchemaCompiler, FastifySchemaValidationError } from './schema'
44
import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
55
import { FastifyLoggerInstance } from './logger'
66
import { FastifyRegister } from './register'
7-
import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler, preParsingAsyncHookHandler, preValidationAsyncHookHandler, preHandlerAsyncHookHandler, preSerializationAsyncHookHandler, onSendAsyncHookHandler, onResponseAsyncHookHandler, onTimeoutAsyncHookHandler, onErrorAsyncHookHandler, onReadyAsyncHookHandler } from './hooks'
7+
import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler, preParsingAsyncHookHandler, preValidationAsyncHookHandler, preHandlerAsyncHookHandler, preSerializationAsyncHookHandler, onSendAsyncHookHandler, onResponseAsyncHookHandler, onTimeoutAsyncHookHandler, onErrorAsyncHookHandler, onReadyAsyncHookHandler, onRequestAsyncHookHandler } from './hooks'
88
import { FastifyRequest } from './request'
99
import { FastifyReply } from './reply'
1010
import { FastifyError } from 'fastify-error'
@@ -89,6 +89,14 @@ export interface FastifyInstance<
8989
hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
9090
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
9191

92+
addHook<
93+
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
94+
ContextConfig = ContextConfigDefault
95+
>(
96+
name: 'onRequest',
97+
hook: onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
98+
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
99+
92100
/**
93101
* `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
94102
* Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.

0 commit comments

Comments
 (0)