Skip to content

Commit

Permalink
Allow custom Context Config types for hooks' request properties (#3786
Browse files Browse the repository at this point in the history
)

* Types hooks requests with custom Context Config

* Test custom Context Config types for hooks
  • Loading branch information
sumbad committed Mar 21, 2022
1 parent 538fe63 commit 2afc0d7
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface onRequestHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -52,7 +52,7 @@ export interface onRequestAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
): Promise<unknown>;
}
Expand All @@ -72,7 +72,7 @@ export interface preParsingHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
payload: RequestPayload,
done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
Expand All @@ -90,7 +90,7 @@ export interface preParsingAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
payload: RequestPayload,
): Promise<RequestPayload | unknown>;
Expand All @@ -110,7 +110,7 @@ export interface preValidationHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -127,7 +127,7 @@ export interface preValidationAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
): Promise<unknown>;
}
Expand All @@ -146,7 +146,7 @@ export interface preHandlerHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -163,7 +163,7 @@ export interface preHandlerAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
): Promise<unknown>;
}
Expand Down Expand Up @@ -191,7 +191,7 @@ export interface preSerializationHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
payload: PreSerializationPayload,
done: DoneFuncWithErrOrRes
Expand All @@ -210,7 +210,7 @@ export interface preSerializationAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
payload: PreSerializationPayload
): Promise<unknown>;
Expand All @@ -232,7 +232,7 @@ export interface onSendHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
payload: OnSendPayload,
done: DoneFuncWithErrOrRes
Expand All @@ -251,7 +251,7 @@ export interface onSendAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
payload: OnSendPayload,
): Promise<unknown>;
Expand All @@ -272,7 +272,7 @@ export interface onResponseHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -289,7 +289,7 @@ export interface onResponseAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
): Promise<unknown>;
}
Expand All @@ -309,7 +309,7 @@ export interface onTimeoutHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -326,7 +326,7 @@ export interface onTimeoutAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
): Promise<unknown>;
}
Expand All @@ -349,7 +349,7 @@ export interface onErrorHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
error: TError,
done: () => void
Expand All @@ -368,7 +368,7 @@ export interface onErrorAsyncHookHandler<
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
error: TError
): Promise<unknown>;
Expand Down

0 comments on commit 2afc0d7

Please sign in to comment.