Skip to content

Commit

Permalink
feat: Add Generics RawServer and TypeProvider to fp and tests. (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
olyop committed Aug 13, 2022
1 parent 3a04d1a commit b532e76
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
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))

0 comments on commit b532e76

Please sign in to comment.