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

4 changes: 2 additions & 2 deletions test/types/hooks.test-d.ts
Expand Up @@ -10,7 +10,7 @@ import fastify, {
RouteOptions,
RegisterOptions,
FastifyPluginOptions,
FastifyContextConfig
FastifyContextConfig, RawServerDefault
} from '../../fastify'
import { preHandlerAsyncHookHandler, RequestPayload } from '../../types/hooks'
import { RouteGenericInterface } from '../../types/route'
Expand Down Expand Up @@ -215,7 +215,7 @@ server.addHook('onClose', async (instance) => {
// Use case to monitor any regression on issue #3620
// ref.: https://github.com/fastify/fastify/issues/3620
const customTypedHook: preHandlerAsyncHookHandler<
RawServerBase,
RawServerDefault,
RawRequestDefaultExpression,
RawReplyDefaultExpression,
Record<string, unknown>
Expand Down
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, _reply) {
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)