import fastifyPlugin from '..'; import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify'; import { expectAssignable, expectError, expectNotType, expectType } from 'tsd' import { Server } from "node:https" import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox" import fastifyExampleCallback from './example-callback.test-d'; import fastifyExampleAsync from './example-async.test-d'; interface Options { foo: string } const testSymbol = Symbol('foobar') // Callback const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { } expectType(fastifyPlugin(pluginCallback)) const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { } expectAssignable(fastifyPlugin(pluginCallbackWithTypes)) expectNotType(fastifyPlugin(pluginCallbackWithTypes)) expectAssignable(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { })) expectNotType(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { })) expectType(fastifyPlugin(pluginCallback, '')) expectType(fastifyPlugin(pluginCallback, { fastify: '', name: '', decorators: { fastify: ['', testSymbol], reply: ['', testSymbol], request: ['', testSymbol] }, dependencies: [''], encapsulate: true })) const pluginCallbackWithOptions: FastifyPluginCallback = (fastify, options, next) => { expectType(options.foo) } expectType>(fastifyPlugin(pluginCallbackWithOptions)) const pluginCallbackWithServer: FastifyPluginCallback = (fastify, options, next) => { expectType(fastify.server) } expectType>(fastifyPlugin(pluginCallbackWithServer)) const pluginCallbackWithTypeProvider: FastifyPluginCallback = (fastify, options, next) => { } expectType>(fastifyPlugin(pluginCallbackWithTypeProvider)) // Async const pluginAsync: FastifyPluginAsync = async (fastify, options) => { } expectType(fastifyPlugin(pluginAsync)) const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise => { } expectType>(fastifyPlugin(pluginAsyncWithTypes)) expectType>(fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise => { })) expectType(fastifyPlugin(pluginAsync, '')) expectType(fastifyPlugin(pluginAsync, { fastify: '', name: '', decorators: { fastify: ['', testSymbol], reply: ['', testSymbol], request: ['', testSymbol] }, dependencies: [''], encapsulate: true })) const pluginAsyncWithOptions: FastifyPluginAsync = async (fastify, options) => { expectType(options.foo) } expectType>(fastifyPlugin(pluginAsyncWithOptions)) const pluginAsyncWithServer: FastifyPluginAsync = async (fastify, options) => { expectType(fastify.server) } expectType>(fastifyPlugin(pluginAsyncWithServer)) const pluginAsyncWithTypeProvider: FastifyPluginAsync = async (fastify, options) => { } expectType>(fastifyPlugin(pluginAsyncWithTypeProvider)) // Fastify register const server = fastify() server.register(fastifyPlugin(pluginCallback)) server.register(fastifyPlugin(pluginCallbackWithTypes)) server.register(fastifyPlugin(pluginCallbackWithOptions)) server.register(fastifyPlugin(pluginCallbackWithServer)) server.register(fastifyPlugin(pluginCallbackWithTypeProvider)) server.register(fastifyPlugin(pluginAsync)) server.register(fastifyPlugin(pluginAsyncWithTypes)) server.register(fastifyPlugin(pluginAsyncWithOptions)) server.register(fastifyPlugin(pluginAsyncWithServer)) server.register(fastifyPlugin(pluginAsyncWithTypeProvider)) // properly handling callback and async fastifyPlugin(function (fastify, options, next) { expectType(fastify) expectType>(options) expectType<(err?: Error) => void>(next) }) fastifyPlugin(function (fastify, options, next) { expectType(fastify) expectType(options) expectType<(err?: Error) => void>(next) }) fastifyPlugin(async function (fastify, options) { expectType(fastify) expectType(options) }) expectAssignable>(fastifyPlugin(async function (fastify: FastifyInstance, options: Options) { })) expectNotType(fastifyPlugin(async function (fastify: FastifyInstance, options: Options) { })) fastifyPlugin(async function (fastify, options: Options) { expectType(fastify) expectType(options) }) fastifyPlugin(async function (fastify, options) { expectType(fastify) expectType>(options) }) expectError( fastifyPlugin(async function (fastify, options: Options, next) { expectType(fastify) expectType(options) }) ) expectAssignable>(fastifyPlugin(function (fastify, options, next) { })) expectNotType(fastifyPlugin(function (fastify, options, next) { })) fastifyPlugin(function (fastify, options: Options, next) { expectType(fastify) expectType(options) expectType<(err?: Error) => void>(next) }) expectError( fastifyPlugin(function (fastify, options: Options, next) { expectType(fastify) expectType(options) return Promise.resolve() }) ) server.register(fastifyExampleCallback, { foo: 'bar' }) expectError(server.register(fastifyExampleCallback, { foo: 'baz' })) server.register(fastifyExampleAsync, { foo: 'bar' }) expectError(server.register(fastifyExampleAsync, { foo: 'baz' }))