From c897bb996ec5b560113fa5051d14b2aa3032fed2 Mon Sep 17 00:00:00 2001 From: DemonHa Date: Fri, 3 Jan 2025 18:08:08 -0500 Subject: [PATCH 1/5] feat: support decorators on plugin definition --- package.json | 2 +- types/plugin.d.ts | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) 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..bea8375 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,16 @@ 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)[] +} + +type GetSixthGenericOfFasityInstance = T extends FastifyInstance ? U : void; +type GetFirstParameter = T extends (...args: infer P) => any ? P[0] : void; + /** * This function does three things for you: * 1. Add the `skip-override` hidden property @@ -48,13 +59,14 @@ 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 From 8334beb6a5cdd3bbcbc3a7232be6650320d9c75c Mon Sep 17 00:00:00 2001 From: DemonHa Date: Mon, 3 Feb 2025 16:20:43 -0500 Subject: [PATCH 2/5] chore: export plugin type --- types/plugin.d.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/types/plugin.d.ts b/types/plugin.d.ts index bea8375..28cf5c3 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -43,11 +43,11 @@ declare namespace fastifyPlugin { interface FastifyDecorators { fastify?: object, request?: object, reply?: object } interface FastifyPluginDecorators { decorators: FastifyDecorators, - dependencies: (FastifyPluginCallback | FastifyPluginAsync)[] + dependencies: (FastifyPluginCallback | FastifyPluginAsync)[], } -type GetSixthGenericOfFasityInstance = T extends FastifyInstance ? U : void; -type GetFirstParameter = T extends (...args: infer P) => any ? P[0] : void; +type GetSixthGenericOfFasityInstance = T extends FastifyInstance ? U : void +type GetFirstParameter = T extends (...args: infer P) => any ? P[0] : void /** * This function does three things for you: @@ -66,8 +66,9 @@ declare function fastifyPlugin< Logger extends FastifyBaseLogger = FastifyBaseLogger, 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 +export type GetPluginTypes = FastifyPluginAsync>> From d86b1a687a52ebd5b5bdeba80b179a54432509e4 Mon Sep 17 00:00:00 2001 From: DemonHa Date: Mon, 3 Feb 2025 21:27:13 -0500 Subject: [PATCH 3/5] fix: manual plugin type --- types/plugin.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/plugin.d.ts b/types/plugin.d.ts index 28cf5c3..4511c65 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -71,4 +71,4 @@ declare function fastifyPlugin< ): Fn export default fastifyPlugin -export type GetPluginTypes = FastifyPluginAsync>> +export type GetPluginTypes = FastifyPluginAsync {} : Decorators['dependencies'][number]>>> | FastifyPluginCallback {} : Decorators['dependencies'][number]>>> From 32643e1b34ed1da97f6afd1dcc12b04e73be99c5 Mon Sep 17 00:00:00 2001 From: DemonHa Date: Sun, 9 Feb 2025 19:46:15 -0500 Subject: [PATCH 4/5] fix: correct GetPluginTypes type --- types/plugin.d.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/types/plugin.d.ts b/types/plugin.d.ts index 4511c65..dff669a 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -46,9 +46,6 @@ interface FastifyPluginDecorators { dependencies: (FastifyPluginCallback | FastifyPluginAsync)[], } -type GetSixthGenericOfFasityInstance = T extends FastifyInstance ? U : void -type GetFirstParameter = T extends (...args: infer P) => any ? P[0] : void - /** * This function does three things for you: * 1. Add the `skip-override` hidden property @@ -71,4 +68,8 @@ declare function fastifyPlugin< ): Fn export default fastifyPlugin -export type GetPluginTypes = FastifyPluginAsync {} : Decorators['dependencies'][number]>>> | FastifyPluginCallback {} : Decorators['dependencies'][number]>>> + +type GetSixthGenericOfFasityInstance = Instance extends FastifyInstance ? U : never +type GetFirstParameter = T extends (...args: infer P) => any ? P[0] : never +type GetFastifyDecoratorsFromPlugins | FastifyPluginAsync)[]> = GetSixthGenericOfFasityInstance> +export type GetPluginTypes = FastifyPluginAsync> | FastifyPluginCallback> From 0f6c5723aa3724861f9a0f2e14fc8d118cc51565 Mon Sep 17 00:00:00 2001 From: DemonHa Date: Sun, 9 Feb 2025 21:24:01 -0500 Subject: [PATCH 5/5] fix: handle no deps case --- types/plugin.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/plugin.d.ts b/types/plugin.d.ts index dff669a..d2e49a0 100644 --- a/types/plugin.d.ts +++ b/types/plugin.d.ts @@ -71,5 +71,5 @@ 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> +type GetFastifyDecoratorsFromPlugins | FastifyPluginAsync)[]> = GetSixthGenericOfFasityInstance {} : Plugins[number]>> export type GetPluginTypes = FastifyPluginAsync> | FastifyPluginCallback>