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

fix: propagate generics from FastifyRegister to plugin type #4034

Merged
merged 7 commits into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
33 changes: 31 additions & 2 deletions test/types/register.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { expectAssignable, expectError, expectType } from 'tsd'
import fastify, { FastifyInstance, FastifyPluginAsync } from '../../fastify'
import fastify, { FastifyInstance, FastifyError, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, RawServerDefault } from '../../fastify'

const testPluginOptsAsync: FastifyPluginAsync = async function (_instance, _opts) { }
const testPluginCallback: FastifyPluginCallback = function (instance, opts, done) { }
const testPluginAsync: FastifyPluginAsync = async function (instance, opts) { }

const testPluginOpts: FastifyPluginCallback = function (instance, opts, done) { }
const testPluginOptsAsync: FastifyPluginAsync = async function (instance, opts) { }

const testPluginOptsWithType = (instance: FastifyInstance, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
const testPluginOptsWithTypeAsync = async (instance: FastifyInstance, opts: FastifyPluginOptions) => { }

// Type validation
expectError(fastify().register(testPluginOptsAsync, { prefix: 1 }))
Expand All @@ -26,3 +33,25 @@ expectAssignable<FastifyInstance>(
expectType<FastifyInstance>(instance)
})
)

// With Type Provider
interface TestOptions extends FastifyPluginOptions {
option1: string;
option2: boolean;
}
type TestTypeProvider = { input: 'test', output: 'test' }
const serverWithTypeProvider = fastify().withTypeProvider<TestTypeProvider>()
const testPluginWithTypeProvider: FastifyPluginCallback<TestOptions, RawServerDefault, TestTypeProvider> = function (instance, opts, done) { }
const testPluginWithTypeProviderAsync: FastifyPluginAsync<TestOptions, RawServerDefault, TestTypeProvider> = async function (instance, opts) { }
const testPluginWithTypeProviderWithType = (instance: typeof serverWithTypeProvider, opts: FastifyPluginOptions, done: (error?: FastifyError) => void) => { }
const testPluginWithTypeProviderWithTypeAsync = async (instance: typeof serverWithTypeProvider, opts: FastifyPluginOptions) => { }
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginCallback))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginAsync))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginOpts))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsAsync))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsWithType))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginOptsWithTypeAsync))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProvider))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderAsync))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderWithType))
expectAssignable<typeof serverWithTypeProvider>(serverWithTypeProvider.register(testPluginWithTypeProviderWithTypeAsync))
16 changes: 9 additions & 7 deletions types/register.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { FastifyPluginOptions, FastifyPluginCallback, FastifyPluginAsync } from './plugin'
import { LogLevel } from './logger'
import { FastifyInstance } from './instance'
import { RawServerBase } from './utils'
import { FastifyTypeProvider, RawServerDefault } from '../fastify'

export interface RegisterOptions {
prefix?: string;
Expand All @@ -15,17 +17,17 @@ export type FastifyRegisterOptions<Options> = (RegisterOptions & Options) | ((in
*
* Function for adding a plugin to fastify. The options are inferred from the passed in FastifyPlugin parameter.
*/
export interface FastifyRegister<T = void> {
<Options extends FastifyPluginOptions>(
plugin: FastifyPluginCallback<Options>,
export interface FastifyRegister<T = void, RawServer extends RawServerBase = RawServerDefault, TypeProviderDefault extends FastifyTypeProvider = FastifyTypeProvider> {
<Options extends FastifyPluginOptions, Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault>(
plugin: FastifyPluginCallback<Options, Server, TypeProvider>,
opts?: FastifyRegisterOptions<Options>
): T;
<Options extends FastifyPluginOptions>(
plugin: FastifyPluginAsync<Options>,
<Options extends FastifyPluginOptions, Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault>(
plugin: FastifyPluginAsync<Options, Server, TypeProvider>,
opts?: FastifyRegisterOptions<Options>
): T;
<Options extends FastifyPluginOptions>(
plugin: FastifyPluginCallback<Options> | FastifyPluginAsync<Options> | Promise<{ default: FastifyPluginCallback<Options> }> | Promise<{ default: FastifyPluginAsync<Options> }>,
<Options extends FastifyPluginOptions, Server extends RawServerBase = RawServer, TypeProvider extends FastifyTypeProvider = TypeProviderDefault>(
plugin: FastifyPluginCallback<Options, Server, TypeProvider> | FastifyPluginAsync<Options, Server, TypeProvider> | Promise<{ default: FastifyPluginCallback<Options, Server, TypeProvider> }> | Promise<{ default: FastifyPluginAsync<Options, Server, TypeProvider> }>,
opts?: FastifyRegisterOptions<Options>
): T;
}