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

fix (types): this is FastifyInstance #3622

Merged
merged 9 commits into from
Jan 17, 2022
2 changes: 1 addition & 1 deletion test/404s.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ test('preHandler option for setNotFoundHandler', t => {
})

// https://github.com/fastify/fastify/issues/2229
t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', t => {
t.test('preHandler hook in setNotFoundHandler should be called when callNotFound', { timeout: 40000 }, t => {
t.plan(2)
const fastify = Fastify()

Expand Down
32 changes: 28 additions & 4 deletions test/types/hooks.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import fastify, { RouteOptions, FastifyReply, FastifyRequest } from '../../fastify'
import { expectType, expectError, expectAssignable } from 'tsd'
import { FastifyInstance } from '../../types/instance'
import { FastifyError } from 'fastify-error'
import { RequestPayload } from '../../types/hooks'
import { expectAssignable, expectError, expectType } from 'tsd'
import fastify, {
FastifyInstance,
FastifyReply,
FastifyRequest,
RawReplyDefaultExpression,
RawRequestDefaultExpression,
RawServerBase,
RouteOptions
} from '../../fastify'
import { preHandlerAsyncHookHandler, RequestPayload } from '../../types/hooks'

const server = fastify()

Expand Down Expand Up @@ -198,3 +205,20 @@ server.addHook('onReady', async function () {
server.addHook('onClose', async (instance) => {
expectType<FastifyInstance>(instance)
})

// Use case to monitor any regression on issue #3620
// ref.: https://github.com/fastify/fastify/issues/3620
const customTypedHook: preHandlerAsyncHookHandler<
RawServerBase,
RawRequestDefaultExpression,
RawReplyDefaultExpression,
Record<string, unknown>
> = async function (request, reply) {
expectType<FastifyInstance>(this)
darkgl0w marked this conversation as resolved.
Show resolved Hide resolved
expectAssignable<FastifyRequest>(request)
expectAssignable<FastifyReply>(reply)
}

server.register(async (instance) => {
instance.addHook('preHandler', customTypedHook)
})
38 changes: 19 additions & 19 deletions types/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface onRequestHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
Expand All @@ -42,7 +42,7 @@ export interface onRequestAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
Expand All @@ -60,7 +60,7 @@ export interface preParsingHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
Expand All @@ -76,7 +76,7 @@ export interface preParsingAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
Expand All @@ -94,7 +94,7 @@ export interface preValidationHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
Expand All @@ -109,7 +109,7 @@ export interface preValidationAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
Expand All @@ -126,7 +126,7 @@ export interface preHandlerHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
Expand All @@ -141,7 +141,7 @@ export interface preHandlerAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
Expand All @@ -167,7 +167,7 @@ export interface preSerializationHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload,
Expand All @@ -184,7 +184,7 @@ export interface preSerializationAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload
Expand All @@ -204,7 +204,7 @@ export interface onSendHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
Expand All @@ -221,7 +221,7 @@ export interface onSendAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
Expand All @@ -240,7 +240,7 @@ export interface onResponseHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
Expand All @@ -255,7 +255,7 @@ export interface onResponseAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
Expand All @@ -273,7 +273,7 @@ export interface onTimeoutHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
Expand All @@ -288,7 +288,7 @@ export interface onTimeoutAsyncHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
Expand All @@ -309,7 +309,7 @@ export interface onErrorHookHandler<
TError extends Error = FastifyError
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError,
Expand All @@ -326,7 +326,7 @@ export interface onErrorAsyncHookHandler<
TError extends Error = FastifyError
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError
Expand All @@ -346,7 +346,7 @@ export interface onRouteHookHandler<
ContextConfig = ContextConfigDefault
> {
(
this: FastifyInstance<RawServer, RawRequest, RawReply>,
this: FastifyInstance,
opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> & { routePath: string; path: string; prefix: string }
): Promise<unknown> | void;
}
Expand Down