diff --git a/package.json b/package.json index c7da7a9..e47779e 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "@types/node": "^22.0.0", "c8": "^10.1.2", "eslint": "^9.17.0", - "fastify": "^5.0.0", + "fastify": "github:DemonHa/fastify#type-safe-plugin-definitions", "neostandard": "^0.12.0", "proxyquire": "^2.1.3", "tsd": "^0.31.0" diff --git a/types/plugin.d.ts b/types/plugin.d.ts index b04997b..d2e49a0 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -9,6 +9,7 @@ import { FastifyTypeProvider, FastifyTypeProviderDefault, FastifyBaseLogger, + FastifyInstance, } from 'fastify' type FastifyPlugin = typeof fastifyPlugin @@ -38,6 +39,13 @@ declare namespace fastifyPlugin { export { fastifyPlugin as default } } +// Todo: import decorators from fastify +interface FastifyDecorators { fastify?: object, request?: object, reply?: object } +interface FastifyPluginDecorators { + decorators: FastifyDecorators, + dependencies: (FastifyPluginCallback | FastifyPluginAsync)[], +} + /** * This function does three things for you: * 1. Add the `skip-override` hidden property @@ -48,14 +56,20 @@ declare namespace fastifyPlugin { */ declare function fastifyPlugin< + Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] }, Options extends FastifyPluginOptions = Record, RawServer extends RawServerBase = RawServerDefault, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger, - Fn extends FastifyPluginCallback | FastifyPluginAsync = FastifyPluginCallback + Fn extends FastifyPluginCallback | FastifyPluginAsync = FastifyPluginCallback > ( - fn: Fn extends unknown ? Fn extends (...args: any) => Promise ? FastifyPluginAsync : FastifyPluginCallback : Fn, + fn: Fn extends unknown ? Fn extends (...args: any) => Promise ? FastifyPluginAsync>> : FastifyPluginCallback>> : Fn, options?: fastifyPlugin.PluginMetadata | string ): Fn -export = fastifyPlugin +export default fastifyPlugin + +type GetSixthGenericOfFasityInstance = Instance extends FastifyInstance ? U : never +type GetFirstParameter = T extends (...args: infer P) => any ? P[0] : never +type GetFastifyDecoratorsFromPlugins | FastifyPluginAsync)[]> = GetSixthGenericOfFasityInstance {} : Plugins[number]>> +export type GetPluginTypes = FastifyPluginAsync> | FastifyPluginCallback>