import fastify, { FastifyInstance, FastifyPluginOptions, SafePromiseLike } from '../../fastify' import * as http from 'node:http' import * as https from 'node:https' import { expectType, expectError, expectAssignable } from 'tsd' import { FastifyPluginCallback, FastifyPluginAsync } from '../../types/plugin' import { FastifyError } from '@fastify/error' // FastifyPlugin & FastifyRegister interface TestOptions extends FastifyPluginOptions { option1: string; option2: boolean; } const testOptions: TestOptions = { option1: 'a', option2: false } const testPluginOpts: FastifyPluginCallback = function (instance, opts, done) { expectType(opts) } const testPluginOptsAsync: FastifyPluginAsync = async function (instance, opts) { expectType(opts) } const testPluginOptsWithType = ( instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void ) => { } const testPluginOptsWithTypeAsync = async ( instance: FastifyInstance, opts: FastifyPluginOptions ) => { } expectError(fastify().register(testPluginOpts, {})) // error because missing required options from generic declaration expectError(fastify().register(testPluginOptsAsync, {})) // error because missing required options from generic declaration expectAssignable(fastify().register(testPluginOpts, { option1: '', option2: true })) expectAssignable(fastify().register(testPluginOptsAsync, { option1: '', option2: true })) expectAssignable(fastify().register(function (instance, opts, done) { })) expectAssignable(fastify().register(function (instance, opts, done) { }, () => { })) expectAssignable(fastify().register(function (instance, opts, done) { }, { logLevel: 'info', prefix: 'foobar' })) expectAssignable(fastify().register(import('./dummy-plugin'))) expectAssignable(fastify().register(import('./dummy-plugin'), { foo: 1 })) const testPluginCallback: FastifyPluginCallback = function (instance, opts, done) { } expectAssignable(fastify().register(testPluginCallback, {})) const testPluginAsync: FastifyPluginAsync = async function (instance, opts) { } expectAssignable(fastify().register(testPluginAsync, {})) expectAssignable( fastify().register(function (instance, opts): Promise { return Promise.resolve() }) ) expectAssignable(fastify().register(async function (instance, opts) { }, () => { })) expectAssignable(fastify().register(async function (instance, opts) { }, { logLevel: 'info', prefix: 'foobar' })) expectError(fastify().register(function (instance, opts, done) { }, { ...testOptions, logLevel: '' })) // must use a valid logLevel const httpsServer = fastify({ https: {} }) expectError< FastifyInstance & Promise> >(httpsServer) expectAssignable< FastifyInstance & PromiseLike> >(httpsServer) expectType< FastifyInstance & SafePromiseLike> >(httpsServer) // Chainable httpsServer .register(testPluginOpts, testOptions) .after((_error) => { }) .ready((_error) => { }) .close(() => { }) // Thenable expectAssignable>(httpsServer.after()) expectAssignable>(httpsServer.close()) expectAssignable>(httpsServer.ready()) expectAssignable>(httpsServer.register(testPluginOpts, testOptions)) expectAssignable>(httpsServer.register(testPluginOptsWithType)) expectAssignable>(httpsServer.register(testPluginOptsWithTypeAsync)) expectAssignable>(httpsServer.register(testPluginOptsWithType, { prefix: '/test' })) expectAssignable>(httpsServer.register(testPluginOptsWithTypeAsync, { prefix: '/test' })) /* eslint-disable @typescript-eslint/no-unused-vars */ async function testAsync (): Promise { await httpsServer .register(testPluginOpts, testOptions) .register(testPluginOpts, testOptions) }