sinclairzx81 / fastify-typebox

Enhanced TypeBox support for Fastify
Other
31 stars 0 forks source link

Module '"fastify-typebox"' has no exported member 'FastifyTypeBoxPluginAsync' #2

Open rickerp opened 3 years ago

rickerp commented 3 years ago

There is no equivalent async function of FastifyTypeBoxPluginCallback

sinclairzx81 commented 3 years ago

@rickerp Have updated the library with better support for async on 0.8.15. All of the following should now work. Example here

import Fastify, { FastifyTypeBoxInstance, FastifyInstance } from 'fastify-typebox'
import fastifySwagger from 'fastify-swagger'

const fastify = Fastify()

async function ft_async(instance: FastifyTypeBoxInstance, options: { config: number }) {
    // ...   
}

function ft_sync(instance: FastifyTypeBoxInstance, options: { config: number }, done: Function) {
    // ...
}

async function t_async(instance: FastifyInstance, options: { config: number }, done: Function) {
    // ...
}

function t_sync(instance: FastifyInstance, options: { config: number }, done: Function) {
    // ...
}

fastify.register(ft_async, { config: 1 })
fastify.register(ft_sync, { config: 1 })
fastify.register(t_async, { config: 1 })
fastify.register(t_sync, { config: 1 })
fastify.register(fastifySwagger, {})

Unfortunately, this has come at the cost of breaking the opt in type assertion...

const fastify = Fastify() as FastifyTypeBoxInstance // no longer works

const fastify = Fastify() as unknown as FastifyTypeBoxInstance // workaround

...

import Fastify from 'fastify-typebox'

const fastify = Fastify()  // this is still fine

This is somewhat related to issue #3 where I'm trying to resolve the fastify Server type from the serverFactory option (a seeming requirement for plugins that are specific about the server type). The inference of the server is causing some issues with default inference.

Let me know if you're still experiencing problems with the async.