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

(v3.x) Allow custom Context Config types for hooks' request properties #3787

Merged
merged 2 commits into from Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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