import fastify, { FastifyInstance, FastifyPluginOptions, SafePromiseLike } from '../../fastify' import * as http from 'http' import * as https from '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 testPluginOpts: FastifyPluginCallback = function (instance, opts, done) { } const testPluginOptsAsync: FastifyPluginAsync = async function (instance, 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) { }, { logLevel: '' })) // must use a valid logLevel const httpsServer = fastify({ https: {} }) expectError & Promise>>(httpsServer) expectAssignable & PromiseLike>>(httpsServer) expectType & SafePromiseLike>>(httpsServer) // Chainable httpsServer .register(testPluginOpts) .after((_error) => { }) .ready((_error) => { }) .close(() => { }) // Thenable expectAssignable>(httpsServer.after()) expectAssignable>(httpsServer.close()) expectAssignable>(httpsServer.ready()) expectAssignable>(httpsServer.register(testPluginOpts)) 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) .register(testPluginOpts) }