import { Request, Response } from 'express';

/**
 * Custom express middleware that appends the raw body to the request object.
 * Mainly used for parsing csvs from bodies.
 */

const RawBodyMiddleware = (req: Request, res: Response, next: () => void) => {
    const body = []
    req.on('data', chunk => {
        body.push(chunk)
    })
    req.on('end', () => {
        const rawBody = Buffer.concat(body)
        req['rawBody'] = rawBody
        next()
    })
    req.on('error', () => {
        res.sendStatus(400)
    })
}

export default RawBodyMiddleware