Skip to content

Commit

Permalink
nodenext compatibility (#204)
Browse files Browse the repository at this point in the history
* nodenext compatibility

* add missing newline
  • Loading branch information
Uzlopak authored Dec 3, 2022
1 parent 5cb68c5 commit 1c20b66
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 102 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
73 changes: 0 additions & 73 deletions plugin.d.ts

This file was deleted.

3 changes: 2 additions & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ function plugin (fn, options = {}) {
return fn
}

plugin.default = plugin
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyPlugin = plugin
84 changes: 84 additions & 0 deletions types/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/// <reference types="fastify" />

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, RawServer, TypeProvider>,
options?: fastifyPlugin.PluginMetadata
): FastifyPluginAsync<Options, RawServer, TypeProvider>;

declare function fastifyPlugin<
Options extends FastifyPluginOptions,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginAsync<Options, RawServer, TypeProvider>,
options?: string
): FastifyPluginAsync<Options, RawServer, TypeProvider>;

declare function fastifyPlugin<
Options extends FastifyPluginOptions,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginCallback<Options, RawServer, TypeProvider>,
options?: fastifyPlugin.PluginMetadata
): FastifyPluginCallback<Options, RawServer, TypeProvider>;

declare function fastifyPlugin<
Options extends FastifyPluginOptions,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginCallback<Options>,
options?: string
): FastifyPluginCallback<Options>;

export = fastifyPlugin
54 changes: 27 additions & 27 deletions plugin.test-d.ts → types/plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -13,15 +13,15 @@ const testSymbol = Symbol('foobar')
// Callback

const pluginCallback: FastifyPluginCallback = (fastify, options, next) => { }
expectAssignable<FastifyPluginCallback>(fp(pluginCallback))
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback))

const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }
expectAssignable<FastifyPluginCallback>(fp(pluginCallbackWithTypes))
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallbackWithTypes))

expectAssignable<FastifyPluginCallback>(fp((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }))
expectAssignable<FastifyPluginCallback>(fastifyPlugin((fastify: FastifyInstance, options: FastifyPluginOptions, next: (error?: FastifyError) => void): void => { }))

expectAssignable<FastifyPluginCallback>(fp(pluginCallback, '' ))
expectAssignable<FastifyPluginCallback>(fp(pluginCallback, {
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback, '' ))
expectAssignable<FastifyPluginCallback>(fastifyPlugin(pluginCallback, {
fastify: '',
name: '',
decorators: {
Expand All @@ -37,29 +37,29 @@ const pluginCallbackWithOptions: FastifyPluginCallback<Options> = (fastify, opti
expectAssignable<string>(options.foo)
}

expectAssignable<FastifyPluginCallback<Options>>(fp(pluginCallbackWithOptions))
expectAssignable<FastifyPluginCallback<Options>>(fastifyPlugin(pluginCallbackWithOptions))

const pluginCallbackWithServer: FastifyPluginCallback<Options, Server> = (fastify, options, next) => {
expectAssignable<Server>(fastify.server)
}

expectAssignable<FastifyPluginCallback<Options, Server>>(fp(pluginCallbackWithServer))
expectAssignable<FastifyPluginCallback<Options, Server>>(fastifyPlugin(pluginCallbackWithServer))

const pluginCallbackWithTypeProvider: FastifyPluginCallback<Options, Server, TypeBoxTypeProvider> = (fastify, options, next) => {}

expectAssignable<FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>>(fp(pluginCallbackWithTypeProvider))
expectAssignable<FastifyPluginCallback<Options, Server, TypeBoxTypeProvider>>(fastifyPlugin(pluginCallbackWithTypeProvider))

// Async

const pluginAsync: FastifyPluginAsync = async (fastify, options) => { }
expectAssignable<FastifyPluginAsync>(fp(pluginAsync))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync))

const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }
expectAssignable<FastifyPluginAsync>(fp(pluginAsyncWithTypes))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsyncWithTypes))

expectAssignable<FastifyPluginAsync>(fp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }))
expectAssignable<FastifyPluginAsync>(fp(pluginAsync, '' ))
expectAssignable<FastifyPluginAsync>(fp(pluginAsync, {
expectAssignable<FastifyPluginAsync>(fastifyPlugin(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync, '' ))
expectAssignable<FastifyPluginAsync>(fastifyPlugin(pluginAsync, {
fastify: '',
name: '',
decorators: {
Expand All @@ -75,28 +75,28 @@ const pluginAsyncWithOptions: FastifyPluginAsync<Options> = async (fastify, opti
expectAssignable<string>(options.foo)
}

expectAssignable<FastifyPluginAsync<Options>>(fp(pluginAsyncWithOptions))
expectAssignable<FastifyPluginAsync<Options>>(fastifyPlugin(pluginAsyncWithOptions))

const pluginAsyncWithServer: FastifyPluginAsync<Options, Server> = async (fastify, options) => {
expectAssignable<Server>(fastify.server)
}

expectAssignable<FastifyPluginAsync<Options, Server>>(fp(pluginAsyncWithServer))
expectAssignable<FastifyPluginAsync<Options, Server>>(fastifyPlugin(pluginAsyncWithServer))

const pluginAsyncWithTypeProvider: FastifyPluginAsync<Options, Server, TypeBoxTypeProvider> = async (fastify, options) => {}

expectAssignable<FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>>(fp(pluginAsyncWithTypeProvider))
expectAssignable<FastifyPluginAsync<Options, Server, TypeBoxTypeProvider>>(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))

0 comments on commit 1c20b66

Please sign in to comment.