Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support decorators on plugin definition #259

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
20 changes: 17 additions & 3 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FastifyTypeProvider,
FastifyTypeProviderDefault,
FastifyBaseLogger,
FastifyInstance,
} from 'fastify'

type FastifyPlugin = typeof fastifyPlugin
Expand Down Expand Up @@ -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<any, any, any, any, any> | FastifyPluginAsync<any, any, any, any, any>)[],
}

/**
* This function does three things for you:
* 1. Add the `skip-override` hidden property
Expand All @@ -48,14 +56,20 @@ declare namespace fastifyPlugin {
*/

declare function fastifyPlugin<
Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] },
Options extends FastifyPluginOptions = Record<never, never>,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
Logger extends FastifyBaseLogger = FastifyBaseLogger,
Fn extends FastifyPluginCallback<Options, RawServer, TypeProvider, Logger> | FastifyPluginAsync<Options, RawServer, TypeProvider, Logger> = FastifyPluginCallback<Options, RawServer, TypeProvider, Logger>
Fn extends FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators['decorators']> | FastifyPluginAsync<Options, RawServer, TypeProvider, Logger, Decorators['decorators']> = FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators['decorators']>
> (
fn: Fn extends unknown ? Fn extends (...args: any) => Promise<any> ? FastifyPluginAsync<Options, RawServer, TypeProvider, Logger> : FastifyPluginCallback<Options, RawServer, TypeProvider, Logger> : Fn,
fn: Fn extends unknown ? Fn extends (...args: any) => Promise<any> ? FastifyPluginAsync<Options, RawServer, TypeProvider, Logger, Decorators['decorators'] & GetSixthGenericOfFasityInstance<GetFirstParameter<Decorators['dependencies'][number] extends undefined ? {} : Decorators['dependencies'][number]>>> : FastifyPluginCallback<Options, RawServer, TypeProvider, Logger, Decorators['decorators'] & GetSixthGenericOfFasityInstance<GetFirstParameter<Decorators['dependencies'][number] extends undefined ? {} : Decorators['dependencies'][number]>>> : Fn,
options?: fastifyPlugin.PluginMetadata | string
): Fn

export = fastifyPlugin
export default fastifyPlugin

type GetSixthGenericOfFasityInstance<Instance> = Instance extends FastifyInstance<any, any, any, any, any, infer U> ? U : never
type GetFirstParameter<T> = T extends (...args: infer P) => any ? P[0] : never
type GetFastifyDecoratorsFromPlugins<Plugins extends (FastifyPluginCallback<any, any, any, any, any> | FastifyPluginAsync<any, any, any, any, any>)[]> = GetSixthGenericOfFasityInstance<GetFirstParameter<Plugins[number] extends never ? (instance: any) => {} : Plugins[number]>>
export type GetPluginTypes<Decorators extends FastifyPluginDecorators = { decorators: {}, dependencies: [] }, Options extends FastifyPluginOptions = {}> = FastifyPluginAsync<Options, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger, Decorators['decorators'] & GetFastifyDecoratorsFromPlugins<Decorators['dependencies']>> | FastifyPluginCallback<Options, RawServerDefault, FastifyTypeProviderDefault, FastifyBaseLogger, Decorators['decorators'] & GetFastifyDecoratorsFromPlugins<Decorators['dependencies']>>