UNPKG

1.11 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const xmlbuilder = require("xmlbuilder");
4/**
5 * To use this middleware with swagger, set xml root tag name in relevant definition in swagger.yml
6 */
7class ContentNegotiationMiddleware {
8 constructor(xmlRoot) {
9 this.xmlRoot = xmlRoot;
10 }
11 getMiddleware() {
12 const xmlRoot = this.xmlRoot;
13 if (!xmlRoot) {
14 return;
15 }
16 return (req, res, next) => {
17 const xmlType = 'application/xml';
18 if (xmlType === req.get('accept')) {
19 const originalSend = res.send;
20 res.send = function (...data) {
21 const obj = {};
22 obj[xmlRoot] = data[0];
23 data[0] = xmlbuilder.create(obj).end();
24 originalSend.apply(res, data);
25 };
26 res.header('content-type', xmlType);
27 }
28 next();
29 };
30 }
31}
32exports.ContentNegotiationMiddleware = ContentNegotiationMiddleware;
33//# sourceMappingURL=ContentNegotiationMiddleware.js.map
\No newline at end of file