diff --git a/package.json b/package.json index 122d61f..59abe7b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ }, "homepage": "https://github.com/fastify/fastify-plugin#readme", "devDependencies": { + "@fastify/type-provider-typebox": "^2.3.0", "@types/node": "^18.0.0", "fastify": "^4.0.1", "proxyquire": "^2.1.3", @@ -32,6 +33,5 @@ "tap": "^16.0.1", "tsd": "^0.22.0", "typescript": "^4.0.5" - }, - "dependencies": {} + } } diff --git a/plugin.d.ts b/plugin.d.ts index 33bb636..1feabd8 100644 --- a/plugin.d.ts +++ b/plugin.d.ts @@ -3,6 +3,10 @@ import { FastifyPluginCallback, FastifyPluginAsync, + RawServerBase, + RawServerDefault, + FastifyTypeProvider, + FastifyTypeProviderDefault, } from 'fastify' /** @@ -13,10 +17,41 @@ import { * @param fn Fastify plugin function * @param options Optional plugin options */ -export default function fp(fn: FastifyPluginAsync, options?: PluginMetadata): FastifyPluginAsync; -export default function fp(fn: FastifyPluginAsync, options?: string): FastifyPluginAsync; -export default function fp(fn: FastifyPluginCallback, options?: PluginMetadata): FastifyPluginCallback; -export default function fp(fn: FastifyPluginCallback, options?: string): FastifyPluginCallback; +export default function fp< + Options, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginAsync, + options?: PluginMetadata +): FastifyPluginAsync; + +export default function fp< + Options, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginAsync, + options?: string +): FastifyPluginAsync; + +export default function fp< + Options, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginCallback, + options?: PluginMetadata +): FastifyPluginCallback; + +export default function fp< + Options, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginCallback, + options?: string +): FastifyPluginCallback; export interface PluginMetadata { /** Bare-minimum version of Fastify for your plugin, just add the semver range that you need. */ diff --git a/plugin.test-d.ts b/plugin.test-d.ts index db4b6f6..e77e1d1 100644 --- a/plugin.test-d.ts +++ b/plugin.test-d.ts @@ -1,6 +1,8 @@ import fp from './plugin'; import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify'; import { expectAssignable } from 'tsd' +import { Server } from "node:https" +import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox" interface Options { foo: string @@ -36,6 +38,16 @@ const pluginCallbackWithOptions: FastifyPluginCallback = (fastify, opti expectAssignable>(fp(pluginCallbackWithOptions)) +const pluginCallbackWithServer: FastifyPluginCallback = (fastify, options, next) => { + expectAssignable(fastify.server) +} + +expectAssignable>(fp(pluginCallbackWithServer)) + +const pluginCallbackWithTypeProvider: FastifyPluginCallback = (fastify, options, next) => {} + +expectAssignable>(fp(pluginCallbackWithTypeProvider)) + // Async const pluginAsync: FastifyPluginAsync = async (fastify, options) => { } @@ -63,12 +75,26 @@ const pluginAsyncWithOptions: FastifyPluginAsync = async (fastify, opti expectAssignable>(fp(pluginAsyncWithOptions)) +const pluginAsyncWithServer: FastifyPluginAsync = async (fastify, options) => { + expectAssignable(fastify.server) +} + +expectAssignable>(fp(pluginAsyncWithServer)) + +const pluginAsyncWithTypeProvider: FastifyPluginAsync = async (fastify, options) => {} + +expectAssignable>(fp(pluginAsyncWithTypeProvider)) + // Fastify register const server = fastify() server.register(fp(pluginCallback)) server.register(fp(pluginCallbackWithTypes)) server.register(fp(pluginCallbackWithOptions)) +server.register(fp(pluginCallbackWithServer)) +server.register(fp(pluginCallbackWithTypeProvider)) server.register(fp(pluginAsync)) server.register(fp(pluginAsyncWithTypes)) server.register(fp(pluginAsyncWithOptions)) +server.register(fp(pluginAsyncWithServer)) +server.register(fp(pluginAsyncWithTypeProvider))