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

Add Generics RawServer and TypeProvider #192

Merged
merged 1 commit into from Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -25,13 +25,13 @@
},
"homepage": "https://github.com/fastify/fastify-plugin#readme",
"devDependencies": {
"@fastify/type-provider-typebox": "^2.3.0",
"@types/node": "^18.0.0",
"fastify": "^4.0.1",
"proxyquire": "^2.1.3",
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.22.0",
"typescript": "^4.0.5"
},
"dependencies": {}
}
}
43 changes: 39 additions & 4 deletions plugin.d.ts
Expand Up @@ -3,6 +3,10 @@
import {
FastifyPluginCallback,
FastifyPluginAsync,
RawServerBase,
RawServerDefault,
FastifyTypeProvider,
FastifyTypeProviderDefault,
} from 'fastify'

/**
Expand All @@ -13,10 +17,41 @@ import {
* @param fn Fastify plugin function
* @param options Optional plugin options
*/
export default function fp<Options>(fn: FastifyPluginAsync<Options>, options?: PluginMetadata): FastifyPluginAsync<Options>;
export default function fp<Options>(fn: FastifyPluginAsync<Options>, options?: string): FastifyPluginAsync<Options>;
export default function fp<Options>(fn: FastifyPluginCallback<Options>, options?: PluginMetadata): FastifyPluginCallback<Options>;
export default function fp<Options>(fn: FastifyPluginCallback<Options>, options?: string): FastifyPluginCallback<Options>;
export default function fp<
Options,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginAsync<Options, RawServer, TypeProvider>,
options?: PluginMetadata
): FastifyPluginAsync<Options, RawServer, TypeProvider>;

export default function fp<
Options,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginAsync<Options, RawServer, TypeProvider>,
options?: string
): FastifyPluginAsync<Options, RawServer, TypeProvider>;

export default function fp<
Options,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginCallback<Options, RawServer, TypeProvider>,
options?: PluginMetadata
): FastifyPluginCallback<Options, RawServer, TypeProvider>;

export default function fp<
Options,
RawServer extends RawServerBase = RawServerDefault,
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
>(
fn: FastifyPluginCallback<Options>,
options?: string
): FastifyPluginCallback<Options>;

export interface PluginMetadata {
/** Bare-minimum version of Fastify for your plugin, just add the semver range that you need. */
Expand Down
26 changes: 26 additions & 0 deletions plugin.test-d.ts
@@ -1,6 +1,8 @@
import fp from './plugin';
import fastify, { FastifyPluginCallback, FastifyPluginAsync, FastifyError, FastifyInstance, FastifyPluginOptions } from 'fastify';
import { expectAssignable } from 'tsd'
import { Server } from "node:https"
import { TypeBoxTypeProvider } from "@fastify/type-provider-typebox"

interface Options {
foo: string
Expand Down Expand Up @@ -36,6 +38,16 @@ const pluginCallbackWithOptions: FastifyPluginCallback<Options> = (fastify, opti

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

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

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

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

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

// Async

const pluginAsync: FastifyPluginAsync = async (fastify, options) => { }
Expand Down Expand Up @@ -63,12 +75,26 @@ const pluginAsyncWithOptions: FastifyPluginAsync<Options> = async (fastify, opti

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

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

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

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

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