|
2 | 2 |
|
3 | 3 | var xml2js = require('xml2js');
|
4 | 4 |
|
| 5 | +var DEFAULT_TYPES = ['*/xml', '+xml']; |
| 6 | + |
5 | 7 | module.exports = function(bodyParser) {
|
6 |
| - var xmlTypes = ['*/xml', '+xml']; |
7 | 8 | if(bodyParser.xml) {
|
8 | 9 | // We already setup the XML parser.
|
9 | 10 | // End early.
|
10 | 11 | return;
|
11 | 12 | }
|
12 | 13 |
|
13 | 14 | function xml(options) {
|
14 |
| - var textParser; |
15 | 15 | options = options || {};
|
16 |
| - if(options.type){ |
17 |
| - if(Array.isArray(options.type)){ |
18 |
| - xmlTypes = options.type.concat(xmlTypes); |
19 |
| - }else if(typeof options.type === 'string'){ |
20 |
| - xmlTypes.push(options.type); |
21 |
| - } |
22 |
| - options.type = xmlTypes; |
| 16 | + |
| 17 | + options.type = options.type || DEFAULT_TYPES; |
| 18 | + if(!Array.isArray(options.type)) { |
| 19 | + options.type = [options.type]; |
23 | 20 | }
|
24 | 21 |
|
25 |
| - options.type = xmlTypes; |
26 |
| - textParser= bodyParser.text(options); |
| 22 | + var textParser = bodyParser.text(options); |
27 | 23 | return function xmlParser(req, res, next) {
|
28 |
| - var parser; |
29 | 24 | // First, run the body through the text parser.
|
30 | 25 | textParser(req, res, function(err) {
|
31 | 26 | if(err) { return next(err); }
|
32 | 27 | if(typeof req.body !== 'string') { return next(); }
|
33 | 28 |
|
34 | 29 | // Then, parse as XML.
|
35 |
| - parser = new xml2js.Parser(options.xmlParseOptions); |
| 30 | + var parser = new xml2js.Parser(options.xmlParseOptions); |
36 | 31 | parser.parseString(req.body, function(err, xml) {
|
37 | 32 | if(err) {
|
38 | 33 | err.status = 400;
|
|
0 commit comments