import * as http from 'node:http' import * as https from 'node:https' import { FastifyError } from '@fastify/error' import { expect } from 'tstyche' import fastify, { FastifyInstance, FastifyPluginOptions, SafePromiseLike } from '../../fastify.js' import { FastifyPluginCallback, FastifyPluginAsync } from '../../types/plugin.js' // FastifyPlugin & FastifyRegister interface TestOptions extends FastifyPluginOptions { option1: string; option2: boolean; } const testOptions: TestOptions = { option1: 'a', option2: false } const testPluginOpts: FastifyPluginCallback = function (instance, opts, done) { expect(opts).type.toBe() } const testPluginOptsAsync: FastifyPluginAsync = async function (instance, opts) { expect(opts).type.toBe() } const testPluginOptsWithType = ( instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void ) => { } const testPluginOptsWithTypeAsync = async ( instance: FastifyInstance, opts: FastifyPluginOptions ) => { } expect(fastify().register).type.not.toBeCallableWith(testPluginOpts, {}) // must provide required options expect(fastify().register).type.not.toBeCallableWith(testPluginOptsAsync, {}) // must provide required options expect(fastify().register(testPluginOpts, { option1: '', option2: true })).type.toBeAssignableTo() expect(fastify().register(testPluginOptsAsync, { option1: '', option2: true })).type.toBeAssignableTo() expect(fastify().register(function (instance, opts, done) { })).type.toBeAssignableTo() expect(fastify().register(function (instance, opts, done) { }, () => { })).type.toBeAssignableTo() expect(fastify().register(function (instance, opts, done) { }, { logLevel: 'info', prefix: 'foobar' })).type.toBeAssignableTo() expect(fastify().register(import('./dummy-plugin.mjs'))).type.toBeAssignableTo() expect(fastify().register(import('./dummy-plugin.mjs'), { foo: 1 })).type.toBeAssignableTo() const testPluginCallback: FastifyPluginCallback = function (instance, opts, done) { } expect(fastify().register(testPluginCallback, {})).type.toBeAssignableTo() const testPluginAsync: FastifyPluginAsync = async function (instance, opts) { } expect(fastify().register(testPluginAsync, {})).type.toBeAssignableTo() expect( fastify().register(function (instance, opts): Promise { return Promise.resolve() }) ).type.toBeAssignableTo() expect(fastify().register(async function (instance, opts) { }, () => { })).type.toBeAssignableTo() expect(fastify().register(async function (instance, opts) { }, { logLevel: 'info', prefix: 'foobar' })).type.toBeAssignableTo() expect(fastify().register).type.not.toBeCallableWith(function () { }, { logLevel: '', prefix: 'foobar' }) // must provide valid logLevel const httpsServer = fastify({ https: {} }) expect(httpsServer).type.not.toBeAssignableTo< FastifyInstance & Promise> >() expect(httpsServer).type.toBeAssignableTo< FastifyInstance & PromiseLike> >() expect(httpsServer).type.toBe< FastifyInstance & SafePromiseLike> >() // Chainable httpsServer .register(testPluginOpts, testOptions) .after((_error) => { }) .ready((_error) => { }) .close(() => { }) // Thenable expect(httpsServer.after()).type.toBeAssignableTo>() expect(httpsServer.close()).type.toBeAssignableTo>() expect(httpsServer.ready()).type.toBeAssignableTo>() expect(httpsServer.register(testPluginOpts, testOptions)).type.toBeAssignableTo>() expect(httpsServer.register(testPluginOptsWithType)).type.toBeAssignableTo>() expect(httpsServer.register(testPluginOptsWithTypeAsync)).type.toBeAssignableTo>() expect(httpsServer.register(testPluginOptsWithType, { prefix: '/test' })).type.toBeAssignableTo>() expect(httpsServer.register(testPluginOptsWithTypeAsync, { prefix: '/test' })).type.toBeAssignableTo>() async function testAsync (): Promise { await httpsServer .register(testPluginOpts, testOptions) .register(testPluginOpts, testOptions) }