Skip to content

Commit

Permalink
(v3.x) Allow custom Context Config types for hooks' request propert…
Browse files Browse the repository at this point in the history
…ies (#3787)

* Types hooks requests with custom Context Config

* Test custom Context Config types for hooks
  • Loading branch information
sumbad committed Mar 21, 2022
1 parent f66d2a8 commit f00f2a2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 19 deletions.
52 changes: 51 additions & 1 deletion test/types/hooks.test-d.ts
Expand Up @@ -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()

Expand Down Expand Up @@ -226,3 +228,51 @@ Record<string, unknown>
server.register(async (instance) => {
instance.addHook('preHandler', customTypedHook)
})

// Test custom Context Config types for hooks
type CustomContextConfig = FastifyContextConfig & {
foo: string;
bar: number;
}

server.route<RouteGenericInterface, CustomContextConfig>({
method: 'GET',
url: '/',
handler: () => {},
onRequest: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preParsing: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preValidation: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preHandler: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
preSerialization: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onSend: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onResponse: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onTimeout: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
},
onError: (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
}
})
36 changes: 18 additions & 18 deletions types/hooks.d.ts
Expand Up @@ -30,7 +30,7 @@ export interface onRequestHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -45,7 +45,7 @@ export interface onRequestAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
Expand All @@ -63,7 +63,7 @@ export interface preParsingHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
Expand All @@ -79,7 +79,7 @@ export interface preParsingAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
): Promise<RequestPayload | unknown>;
Expand All @@ -97,7 +97,7 @@ export interface preValidationHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -112,7 +112,7 @@ export interface preValidationAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
Expand All @@ -129,7 +129,7 @@ export interface preHandlerHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -144,7 +144,7 @@ export interface preHandlerAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
Expand All @@ -170,7 +170,7 @@ export interface preSerializationHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload,
done: DoneFuncWithErrOrRes
Expand All @@ -187,7 +187,7 @@ export interface preSerializationAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload
): Promise<unknown>;
Expand All @@ -207,7 +207,7 @@ export interface onSendHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
done: DoneFuncWithErrOrRes
Expand All @@ -224,7 +224,7 @@ export interface onSendAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
): Promise<unknown>;
Expand All @@ -243,7 +243,7 @@ export interface onResponseHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -258,7 +258,7 @@ export interface onResponseAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
}
Expand All @@ -276,7 +276,7 @@ export interface onTimeoutHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -291,7 +291,7 @@ export interface onTimeoutAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
}
Expand All @@ -312,7 +312,7 @@ export interface onErrorHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError,
done: () => void
Expand All @@ -329,7 +329,7 @@ export interface onErrorAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError
): Promise<unknown>;
Expand Down

0 comments on commit f00f2a2

Please sign in to comment.