diff --git a/package.json b/package.json index 07d42ea..574b93e 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "4.3.0", "description": "Plugin helper for Fastify", "main": "plugin.js", - "types": "plugin.d.ts", + "types": "types/plugin.d.ts", "scripts": { "lint": "standard", "test": "npm run test:unit && npm run test:typescript", diff --git a/plugin.d.ts b/plugin.d.ts deleted file mode 100644 index 1b33011..0000000 --- a/plugin.d.ts +++ /dev/null @@ -1,73 +0,0 @@ -/// - -import { - FastifyPluginCallback, - FastifyPluginAsync, - FastifyPluginOptions, - RawServerBase, - RawServerDefault, - FastifyTypeProvider, - FastifyTypeProviderDefault, -} from 'fastify' - -/** - * This function does three things for you: - * 1. Add the `skip-override` hidden property - * 2. Check bare-minimum version of Fastify - * 3. Pass some custom metadata of the plugin to Fastify - * @param fn Fastify plugin function - * @param options Optional plugin options - */ -export default function fp< - Options extends FastifyPluginOptions, - RawServer extends RawServerBase = RawServerDefault, - TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault ->( - fn: FastifyPluginAsync, - options?: PluginMetadata -): FastifyPluginAsync; - -export default function fp< - Options extends FastifyPluginOptions, - RawServer extends RawServerBase = RawServerDefault, - TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault ->( - fn: FastifyPluginAsync, - options?: string -): FastifyPluginAsync; - -export default function fp< - Options extends FastifyPluginOptions, - RawServer extends RawServerBase = RawServerDefault, - TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault ->( - fn: FastifyPluginCallback, - options?: PluginMetadata -): FastifyPluginCallback; - -export default function fp< - Options extends FastifyPluginOptions, - 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. */ - fastify?: string, - name?: string, - /** Decorator dependencies for this plugin */ - decorators?: { - fastify?: (string | symbol)[], - reply?: (string | symbol)[], - request?: (string | symbol)[] - }, - /** The plugin dependencies */ - dependencies?: string[], - encapsulate?: boolean -} - -// Exporting PluginOptions for backward compatibility after renaming it to PluginMetadata -export interface PluginOptions extends PluginMetadata {} diff --git a/plugin.js b/plugin.js index 3939b4d..e355697 100644 --- a/plugin.js +++ b/plugin.js @@ -62,5 +62,6 @@ function plugin (fn, options = {}) { return fn } -plugin.default = plugin module.exports = plugin +module.exports.default = plugin +module.exports.fastifyPlugin = plugin diff --git a/types/plugin.d.ts b/types/plugin.d.ts new file mode 100644 index 0000000..33883d8 --- /dev/null +++ b/types/plugin.d.ts @@ -0,0 +1,84 @@ +/// + +import { + FastifyPluginCallback, + FastifyPluginAsync, + FastifyPluginOptions, + RawServerBase, + RawServerDefault, + FastifyTypeProvider, + FastifyTypeProviderDefault, +} from 'fastify' + +type FastifyPlugin = typeof fastifyPlugin + +declare namespace fastifyPlugin { + export interface PluginMetadata { + /** Bare-minimum version of Fastify for your plugin, just add the semver range that you need. */ + fastify?: string, + name?: string, + /** Decorator dependencies for this plugin */ + decorators?: { + fastify?: (string | symbol)[], + reply?: (string | symbol)[], + request?: (string | symbol)[] + }, + /** The plugin dependencies */ + dependencies?: string[], + encapsulate?: boolean + } + // Exporting PluginOptions for backward compatibility after renaming it to PluginMetadata + /** + * @deprecated Use PluginMetadata instead + */ + export interface PluginOptions extends PluginMetadata {} + + export const fastifyPlugin: FastifyPlugin + export { fastifyPlugin as default } +} + +/** + * This function does three things for you: + * 1. Add the `skip-override` hidden property + * 2. Check bare-minimum version of Fastify + * 3. Pass some custom metadata of the plugin to Fastify + * @param fn Fastify plugin function + * @param options Optional plugin options + */ + declare function fastifyPlugin< + Options extends FastifyPluginOptions, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginAsync, + options?: fastifyPlugin.PluginMetadata +): FastifyPluginAsync; + +declare function fastifyPlugin< + Options extends FastifyPluginOptions, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginAsync, + options?: string +): FastifyPluginAsync; + +declare function fastifyPlugin< + Options extends FastifyPluginOptions, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginCallback, + options?: fastifyPlugin.PluginMetadata +): FastifyPluginCallback; + +declare function fastifyPlugin< + Options extends FastifyPluginOptions, + RawServer extends RawServerBase = RawServerDefault, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault +>( + fn: FastifyPluginCallback, + options?: string +): FastifyPluginCallback; + +export = fastifyPlugin diff --git a/plugin.test-d.ts b/types/plugin.test-d.ts similarity index 52% rename from plugin.test-d.ts rename to types/plugin.test-d.ts index 40e346d..8593c34 100644 --- a/plugin.test-d.ts +++ b/types/plugin.test-d.ts @@ -1,4 +1,4 @@ -import fp from './plugin'; +import fastifyPlugin from '..'; import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify'; import { expectAssignable } from 'tsd' import { Server } from "node:https" @@ -13,15 +13,15 @@ const testSymbol = Symbol('foobar') // Callback const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { } -expectAssignable(fp(pluginCallback)) +expectAssignable(fastifyPlugin(pluginCallback)) const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { } -expectAssignable(fp(pluginCallbackWithTypes)) +expectAssignable(fastifyPlugin(pluginCallbackWithTypes)) -expectAssignable(fp((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { })) +expectAssignable(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { })) -expectAssignable(fp(pluginCallback, '' )) -expectAssignable(fp(pluginCallback, { +expectAssignable(fastifyPlugin(pluginCallback, '' )) +expectAssignable(fastifyPlugin(pluginCallback, { fastify: '', name: '', decorators: { @@ -37,29 +37,29 @@ const pluginCallbackWithOptions: FastifyPluginCallback = (fastify, opti expectAssignable(options.foo) } -expectAssignable>(fp(pluginCallbackWithOptions)) +expectAssignable>(fastifyPlugin(pluginCallbackWithOptions)) const pluginCallbackWithServer: FastifyPluginCallback = (fastify, options, next) => { expectAssignable(fastify.server) } -expectAssignable>(fp(pluginCallbackWithServer)) +expectAssignable>(fastifyPlugin(pluginCallbackWithServer)) const pluginCallbackWithTypeProvider: FastifyPluginCallback = (fastify, options, next) => {} -expectAssignable>(fp(pluginCallbackWithTypeProvider)) +expectAssignable>(fastifyPlugin(pluginCallbackWithTypeProvider)) // Async const pluginAsync: FastifyPluginAsync = async (fastify, options) => { } -expectAssignable(fp(pluginAsync)) +expectAssignable(fastifyPlugin(pluginAsync)) const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise => { } -expectAssignable(fp(pluginAsyncWithTypes)) +expectAssignable(fastifyPlugin(pluginAsyncWithTypes)) -expectAssignable(fp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise => { })) -expectAssignable(fp(pluginAsync, '' )) -expectAssignable(fp(pluginAsync, { +expectAssignable(fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise => { })) +expectAssignable(fastifyPlugin(pluginAsync, '' )) +expectAssignable(fastifyPlugin(pluginAsync, { fastify: '', name: '', decorators: { @@ -75,28 +75,28 @@ const pluginAsyncWithOptions: FastifyPluginAsync = async (fastify, opti expectAssignable(options.foo) } -expectAssignable>(fp(pluginAsyncWithOptions)) +expectAssignable>(fastifyPlugin(pluginAsyncWithOptions)) const pluginAsyncWithServer: FastifyPluginAsync = async (fastify, options) => { expectAssignable(fastify.server) } -expectAssignable>(fp(pluginAsyncWithServer)) +expectAssignable>(fastifyPlugin(pluginAsyncWithServer)) const pluginAsyncWithTypeProvider: FastifyPluginAsync = async (fastify, options) => {} -expectAssignable>(fp(pluginAsyncWithTypeProvider)) +expectAssignable>(fastifyPlugin(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)) +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))