| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-02-25 14:39:09 |
| 4 | * @LastEditTime: 2025-02-25 21:34:03 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: |
| 7 | */ |
| 8 | import { Injectable, NestMiddleware } from '@nestjs/common'; |
| 9 | import * as bodyParser from 'body-parser'; |
| 10 | import { NextFunction } from 'express'; |
| 11 | import * as xml2js from 'xml2js'; |
| 12 | |
| 13 | @Injectable() |
| 14 | export class XMLMiddleware implements NestMiddleware { |
| 15 | private xmlParser = bodyParser.text({ type: 'text/xml' }); |
| 16 | |
| 17 | use(req: any, res: any, next: NextFunction) { |
| 18 | this.xmlParser(req, res, (err) => { |
| 19 | if (err) return next(err); |
| 20 | |
| 21 | if (req.body) { |
| 22 | xml2js.parseString( |
| 23 | req.body, |
| 24 | { explicitArray: false }, |
| 25 | (parseErr, result) => { |
| 26 | if (parseErr) return next(parseErr); |
| 27 | req.body = result.xml; |
| 28 | }, |
| 29 | ); |
| 30 | } |
| 31 | next(); |
| 32 | }); |
| 33 | } |
| 34 | } |
| 35 |