diff --git a/test/types/hooks.test-d.ts b/test/types/hooks.test-d.ts index fdf7719422..7b16bc9c0f 100644 --- a/test/types/hooks.test-d.ts +++ b/test/types/hooks.test-d.ts @@ -9,9 +9,11 @@ import fastify, { RawServerBase, RouteOptions, RegisterOptions, - FastifyPluginOptions + FastifyPluginOptions, + FastifyContextConfig } from '../../fastify' import { preHandlerAsyncHookHandler, RequestPayload } from '../../types/hooks' +import { RouteGenericInterface } from '../../types/route' const server = fastify() @@ -226,3 +228,51 @@ Record server.register(async (instance) => { instance.addHook('preHandler', customTypedHook) }) + +// Test custom Context Config types for hooks +type CustomContextConfig = FastifyContextConfig & { + foo: string; + bar: number; +} + +server.route({ + method: 'GET', + url: '/', + handler: () => {}, + onRequest: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + preParsing: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + preValidation: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + preHandler: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + preSerialization: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + onSend: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + onResponse: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + onTimeout: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + }, + onError: (request, reply) => { + expectType(request.context.config) + expectType(reply.context.config) + } +}) diff --git a/types/hooks.d.ts b/types/hooks.d.ts index 814277919d..f133e3c253 100644 --- a/types/hooks.d.ts +++ b/types/hooks.d.ts @@ -30,7 +30,7 @@ export interface onRequestHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction ): void; @@ -45,7 +45,7 @@ export interface onRequestAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, ): Promise; } @@ -63,7 +63,7 @@ export interface preParsingHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, payload: RequestPayload, done: (err?: TError | null, res?: RequestPayload) => void @@ -79,7 +79,7 @@ export interface preParsingAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, payload: RequestPayload, ): Promise; @@ -97,7 +97,7 @@ export interface preValidationHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction ): void; @@ -112,7 +112,7 @@ export interface preValidationAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, ): Promise; } @@ -129,7 +129,7 @@ export interface preHandlerHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction ): void; @@ -144,7 +144,7 @@ export interface preHandlerAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, ): Promise; } @@ -170,7 +170,7 @@ export interface preSerializationHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, payload: PreSerializationPayload, done: DoneFuncWithErrOrRes @@ -187,7 +187,7 @@ export interface preSerializationAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, payload: PreSerializationPayload ): Promise; @@ -207,7 +207,7 @@ export interface onSendHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, payload: OnSendPayload, done: DoneFuncWithErrOrRes @@ -224,7 +224,7 @@ export interface onSendAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, payload: OnSendPayload, ): Promise; @@ -243,7 +243,7 @@ export interface onResponseHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction ): void; @@ -258,7 +258,7 @@ export interface onResponseAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply ): Promise; } @@ -276,7 +276,7 @@ export interface onTimeoutHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, done: HookHandlerDoneFunction ): void; @@ -291,7 +291,7 @@ export interface onTimeoutAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply ): Promise; } @@ -312,7 +312,7 @@ export interface onErrorHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, error: TError, done: () => void @@ -329,7 +329,7 @@ export interface onErrorAsyncHookHandler< > { ( this: FastifyInstance, - request: FastifyRequest, + request: FastifyRequest, reply: FastifyReply, error: TError ): Promise;