22 lines
682 B
TypeScript
22 lines
682 B
TypeScript
import { BadRequestException, Injectable, NestMiddleware } from '@nestjs/common'
|
|
import { NextFunction, Request, Response } from 'express'
|
|
import * as getRawBody from 'raw-body'
|
|
|
|
@Injectable()
|
|
export class RawBodyMiddleware implements NestMiddleware {
|
|
use(req: Request, res: Response, next: NextFunction) {
|
|
if (!req.readable) {
|
|
return next(new BadRequestException('Неправильный запрос'))
|
|
}
|
|
|
|
getRawBody(req, { encoding: 'utf-8' })
|
|
.then((rawBody) => {
|
|
req.body = rawBody
|
|
next()
|
|
})
|
|
.catch((error: string) => {
|
|
throw new BadRequestException('Ошибка при получении', error)
|
|
})
|
|
}
|
|
}
|