From efdeaca1c138f5b187d3ea9dfe449995cfeec2ef Mon Sep 17 00:00:00 2001 From: Marco Reni Date: Mon, 21 Nov 2022 18:13:10 +0100 Subject: [PATCH] fix: use generic for Logger to register plugins when using a custom logger (#4435) --- test/types/register.test-d.ts | 43 +++++++++++++++++++++++++++++++++++ types/plugin.d.ts | 12 +++++++--- types/register.d.ts | 16 ++++++------- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/test/types/register.test-d.ts b/test/types/register.test-d.ts index 8cfd6b0711..3519d3c3b4 100644 --- a/test/types/register.test-d.ts +++ b/test/types/register.test-d.ts @@ -101,3 +101,46 @@ expectAssignable(serverWithTypeProvider.register(async ( expectAssignable(serverWithTypeProvider.register(async (instance: ServerWithTypeProvider) => { expectAssignable(instance) })) + +// With Type Provider and logger +const customLogger = { + level: 'info', + info: () => { }, + warn: () => { }, + error: () => { }, + fatal: () => { }, + trace: () => { }, + debug: () => { }, + child: () => customLogger, + silent: () => { } +} +const serverWithTypeProviderAndLogger = fastify({ + logger: customLogger +}).withTypeProvider() +type ServerWithTypeProviderAndLogger = FastifyInstance +const testPluginWithTypeProviderAndLogger: FastifyPluginCallback = function (instance, opts, done) { } +const testPluginWithTypeProviderAndLoggerAsync: FastifyPluginAsync = async function (instance, opts) { } +const testPluginWithTypeProviderAndLoggerWithType = (instance: ServerWithTypeProviderAndLogger, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { } +const testPluginWithTypeProviderAndLoggerWithTypeAsync = async (instance: ServerWithTypeProviderAndLogger, opts: FastifyPluginOptions) => { } +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginCallback)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginAsync)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginOpts)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginOptsAsync)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginOptsWithType)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginOptsWithTypeAsync)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLogger)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerAsync)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithType)) +expectAssignable(serverWithTypeProviderAndLogger.register(testPluginWithTypeProviderAndLoggerWithTypeAsync)) +expectAssignable(serverWithTypeProviderAndLogger.register((instance) => { + expectAssignable(instance) +})) +expectAssignable(serverWithTypeProviderAndLogger.register((instance: ServerWithTypeProviderAndLogger) => { + expectAssignable(instance) +})) +expectAssignable(serverWithTypeProviderAndLogger.register(async (instance) => { + expectAssignable(instance) +})) +expectAssignable(serverWithTypeProviderAndLogger.register(async (instance: ServerWithTypeProviderAndLogger) => { + expectAssignable(instance) +})) diff --git a/types/plugin.d.ts b/types/plugin.d.ts index 77bce4e118..74c0a2b49f 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -10,8 +10,13 @@ export type FastifyPluginOptions = Record * * Fastify allows the user to extend its functionalities with plugins. A plugin can be a set of routes, a server decorator or whatever. To activate plugins, use the `fastify.register()` method. */ -export type FastifyPluginCallback, Server extends RawServerBase = RawServerDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault> = ( - instance: FastifyInstance, RawReplyDefaultExpression, FastifyBaseLogger, TypeProvider>, +export type FastifyPluginCallback< + Options extends FastifyPluginOptions = Record, + Server extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, + Logger extends FastifyBaseLogger = FastifyBaseLogger, +> = ( + instance: FastifyInstance, RawReplyDefaultExpression, Logger, TypeProvider>, opts: Options, done: (err?: Error) => void ) => void @@ -25,8 +30,9 @@ export type FastifyPluginAsync< Options extends FastifyPluginOptions = Record, Server extends RawServerBase = RawServerDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, + Logger extends FastifyBaseLogger = FastifyBaseLogger, > = ( - instance: FastifyInstance, RawReplyDefaultExpression, FastifyBaseLogger, TypeProvider>, + instance: FastifyInstance, RawReplyDefaultExpression, Logger, TypeProvider>, opts: Options ) => Promise; diff --git a/types/register.d.ts b/types/register.d.ts index 5cca479df1..de274836d6 100644 --- a/types/register.d.ts +++ b/types/register.d.ts @@ -2,7 +2,7 @@ import { FastifyPluginOptions, FastifyPluginCallback, FastifyPluginAsync } from import { LogLevel } from './logger' import { FastifyInstance } from './instance' import { RawServerBase } from './utils' -import { FastifyTypeProvider, RawServerDefault } from '../fastify' +import { FastifyBaseLogger, FastifyTypeProvider, RawServerDefault } from '../fastify' export interface RegisterOptions { prefix?: string; @@ -17,17 +17,17 @@ export type FastifyRegisterOptions = (RegisterOptions & Options) | ((in * * Function for adding a plugin to fastify. The options are inferred from the passed in FastifyPlugin parameter. */ -export interface FastifyRegister { - ( - plugin: FastifyPluginCallback, +export interface FastifyRegister { + ( + plugin: FastifyPluginCallback, opts?: FastifyRegisterOptions ): T; - ( - plugin: FastifyPluginAsync, + ( + plugin: FastifyPluginAsync, opts?: FastifyRegisterOptions ): T; - ( - plugin: FastifyPluginCallback | FastifyPluginAsync | Promise<{ default: FastifyPluginCallback }> | Promise<{ default: FastifyPluginAsync }>, + ( + plugin: FastifyPluginCallback | FastifyPluginAsync | Promise<{ default: FastifyPluginCallback }> | Promise<{ default: FastifyPluginAsync }>, opts?: FastifyRegisterOptions ): T; }