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

add generic logger to route handler & FastifyRequest #3782

42 changes: 41 additions & 1 deletion test/types/request.test-d.ts
@@ -1,10 +1,23 @@
import { expectType } from 'tsd'
import fastify, { RouteHandler, RawRequestDefaultExpression, RequestBodyDefault, RequestGenericInterface, FastifyContext, ContextConfigDefault, FastifyContextConfig } from '../../fastify'
import fastify, {
RouteHandler,
RawRequestDefaultExpression,
RequestBodyDefault,
RequestGenericInterface,
FastifyContext,
ContextConfigDefault,
FastifyContextConfig,
FastifyLogFn,
RawServerDefault,
RawReplyDefaultExpression,
RouteHandlerMethod
} from '../../fastify'
import { RequestParamsDefault, RequestHeadersDefault, RequestQuerystringDefault } from '../../types/utils'
import { FastifyLoggerInstance } from '../../types/logger'
import { FastifyRequest } from '../../types/request'
import { FastifyReply } from '../../types/reply'
import { FastifyInstance } from '../../types/instance'
import { RouteGenericInterface } from '../../types/route'

interface RequestBody {
content: string;
Expand Down Expand Up @@ -38,6 +51,10 @@ type CustomRequest = FastifyRequest<{
Headers: RequestHeaders;
}>

interface CustomLoggerInterface extends FastifyLoggerInstance {
foo: FastifyLogFn; // custom severity logger method
}

const getHandler: RouteHandler = function (request, _reply) {
expectType<string>(request.url)
expectType<string>(request.method)
Expand All @@ -64,6 +81,10 @@ const getHandler: RouteHandler = function (request, _reply) {
expectType<FastifyInstance>(request.server)
}

const getHandlerWithCustomLogger: RouteHandlerMethod<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RouteGenericInterface, ContextConfigDefault, CustomLoggerInterface> = function (request: FastifyRequest<RouteGenericInterface, RawServerDefault, RawRequestDefaultExpression, ContextConfigDefault, CustomLoggerInterface>, _reply) {
MarcoLeko marked this conversation as resolved.
Show resolved Hide resolved
expectType<CustomLoggerInterface>(request.log)
}

const postHandler: Handler = function (request) {
expectType<RequestBody>(request.body)
expectType<RequestParams>(request.params)
Expand Down Expand Up @@ -96,3 +117,22 @@ const server = fastify()
server.get('/get', getHandler)
server.post('/post', postHandler)
server.put('/put', putHandler)

const customLogger: CustomLoggerInterface = {
info: () => { },
warn: () => { },
error: () => { },
fatal: () => { },
trace: () => { },
debug: () => { },
foo: () => { }, // custom severity logger method
child: () => customLogger
}

const serverWithCustomLogger = fastify({ logger: customLogger })
expectType<
FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>
& PromiseLike<FastifyInstance<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, CustomLoggerInterface>>
>(serverWithCustomLogger)

serverWithCustomLogger.get('/get', getHandlerWithCustomLogger)
101 changes: 60 additions & 41 deletions types/hooks.d.ts
Expand Up @@ -26,11 +26,12 @@ export interface onRequestHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -41,11 +42,12 @@ export interface onRequestAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
Expand All @@ -59,11 +61,12 @@ export interface preParsingHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
Expand All @@ -75,11 +78,12 @@ export interface preParsingAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
): Promise<RequestPayload | unknown>;
Expand All @@ -93,11 +97,12 @@ export interface preValidationHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -108,11 +113,12 @@ export interface preValidationAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
Expand All @@ -125,11 +131,12 @@ export interface preHandlerHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -140,11 +147,12 @@ export interface preHandlerAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
Expand All @@ -166,11 +174,12 @@ export interface preSerializationHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload,
done: DoneFuncWithErrOrRes
Expand All @@ -183,11 +192,12 @@ export interface preSerializationAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload
): Promise<unknown>;
Expand All @@ -203,11 +213,12 @@ export interface onSendHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
done: DoneFuncWithErrOrRes
Expand All @@ -220,11 +231,12 @@ export interface onSendAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
): Promise<unknown>;
Expand All @@ -239,11 +251,12 @@ export interface onResponseHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -254,11 +267,12 @@ export interface onResponseAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
}
Expand All @@ -272,11 +286,12 @@ export interface onTimeoutHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
Expand All @@ -287,11 +302,12 @@ export interface onTimeoutAsyncHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
}
Expand All @@ -308,11 +324,12 @@ export interface onErrorHookHandler<
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
TError extends Error = FastifyError
TError extends Error = FastifyError,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError,
done: () => void
Expand All @@ -325,11 +342,12 @@ export interface onErrorAsyncHookHandler<
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
TError extends Error = FastifyError
TError extends Error = FastifyError,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
request: FastifyRequest<RouteGeneric, RawServer, RawRequest, ContextConfig, Logger>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError
): Promise<unknown>;
Expand All @@ -345,10 +363,11 @@ export interface onRouteHookHandler<
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
ContextConfig = ContextConfigDefault,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance
> {
(
this: FastifyInstance,
this: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> & { routePath: string; path: string; prefix: string }
): Promise<unknown> | void;
}
Expand All @@ -362,7 +381,7 @@ export interface onRegisterHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
Logger = FastifyLoggerInstance,
Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
Options extends FastifyPluginOptions = FastifyPluginOptions
> {
(
Expand Down Expand Up @@ -394,7 +413,7 @@ export interface onCloseHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
Logger = FastifyLoggerInstance
Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
> {
(
instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
Expand All @@ -406,7 +425,7 @@ export interface onCloseAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
Logger = FastifyLoggerInstance
Logger extends FastifyLoggerInstance = FastifyLoggerInstance,
> {
(
instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>
Expand Down