import type { FastifyInstance, FastifyPluginCallback, FastifyPluginOptions, FastifyRequest, } from 'fastify' import fp from 'fastify-plugin' import { IncomingMessage } from 'http' const kMultipart = Symbol('multipart') function setMultipart( req: FastifyRequest, _payload: IncomingMessage, done: (err: Error | null) => void, ) { // nothing to do, it will be done by multer in beforeHandler method ;(req as any)[kMultipart] = true done(null) } export function isMultipart(this: FastifyRequest): boolean { return (this.raw as any)[kMultipart] || false } const fastifyMulter: FastifyPluginCallback = ( instance: FastifyInstance, _options: FastifyPluginOptions, done: (err?: Error) => void, ): void => { instance.addContentTypeParser('multipart', setMultipart) instance.decorateRequest('isMultipart', isMultipart) done() } const multer = fp(fastifyMulter, { fastify: '4.x', name: 'fastify-multer', }) export default multer