fastify-flash with typescript version

34 views Asked by At

I am using https://github.com/fastify/fastify-flash for my cli generated typescript project but unable to use it.

Getting below error whenever i am trying to use request.flash method.

Property 'flash' does not exist on type 'FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, FastifySchema, FastifyTypeProviderDefault, unknown, FastifyBaseLogger, ResolveFastifyRequestType<...>>'.ts

Here is my code of src/app.ts file

import type { FastifyCookieOptions } from '@fastify/cookie' 

fastify.register(import('@fastify/cookie'), {
          secret: 'my-secret',
          parseOptions: {}
} as FastifyCookieOptions)

fastify.register(import('@fastify/secure-session'), { 
       secret: 'my-secret',
       salt: 'my-secret',
      cookie: { path: '/', httpOnly: true }
})

fastify.register(import('@fastify/flash'))

here is the error handler i am trying to use req.flash method

fastify.setErrorHandler(async function (error, request: FastifyRequest, reply: FastifyReply) {
        request.log.error(error)
        let errors = [];
        const locales = await fastify.getLocaleMsg(request.headers.lang as string || 'en');
        if (error.statusCode === 429) {
            reply.code(429)
            error.message = locales.RATE_LIMIT_ERROR
        }
        if(error.code == 'FST_ERR_VALIDATION'){
            reply.code(400)
            error.message = locales.VALIDATION_ERROR
            errors = error.validation ? error.validation.map((error) => locales[error.message as string] ? locales[error.message as string] : error.message ) : []
        }

                request.flash('error', errors)

        error.message = error.message ? error.message : locales.SOMETHING_WENT_WRONG
        const accept = request.accepts()
        switch(accept.type(['json', 'html'])) {
            case 'json':
                reply.type('application/json').send({ msg: error.message , errors: errors })
                break
            case 'html':
                reply.type('text/html').send(`<b>${locales.SOMETHING_WENT_WRONG}</b> <br /> ${error.message}`)
                break
            default:
                reply.send(Boom.notAcceptable('unacceptable'))
                break
        }
        // reply.send(error)
    })
0

There are 0 answers