Skip to content

Commit 8efa293

Browse files
committed
Simplify Content-Type setting
1 parent c0c3053 commit 8efa293

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ app.use(bodyParser.xml());
3535

3636
This will parse any XML-based request and place it as a JavaScript object on `req.body` for your route handlers to use.
3737

38-
An XML-based request is determined by the value of the `Content-Type` header. If this ends in `/xml` or `/+xml`, it will be parsed as XML. For example, the following Content-Types will all match:
38+
An XML-based request is determined by the value of the `Content-Type` header. By default, any `Content-Type` header ending in `/xml` or `+xml` will be parsed as XML. For example, the following Content-Types will all match:
3939

4040
- `text/xml`
4141
- `application/xml`
4242
- `application/rss+xml`
4343

44-
But you can also set your own see Options:type
44+
If you need to match against a custom `Content-Type` header, pass in the `type` to match as an option (see below).
4545

4646
### Options
4747

@@ -65,11 +65,9 @@ When set to `true`, then deflated (compressed) bodies will be inflated; when `fa
6565

6666
Controls the maximum request body size. If this is a number, then the value specifies the number of bytes; if it is a string, the value is passed to the [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults to `'100kb'`.
6767

68-
6968
#### type
7069

71-
set your own valid Content-Type that will be parsed, can be a string or an array, already valid topics are '*/xml', '+xml'
72-
70+
The expected `Content-Type` of the XML request to be parsed. Overrides the default content types, can be a String or Array of Strings.
7371

7472
#### verify
7573

index.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,32 @@
22

33
var xml2js = require('xml2js');
44

5+
var DEFAULT_TYPES = ['*/xml', '+xml'];
6+
57
module.exports = function(bodyParser) {
6-
var xmlTypes = ['*/xml', '+xml'];
78
if(bodyParser.xml) {
89
// We already setup the XML parser.
910
// End early.
1011
return;
1112
}
1213

1314
function xml(options) {
14-
var textParser;
1515
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];
2320
}
2421

25-
options.type = xmlTypes;
26-
textParser= bodyParser.text(options);
22+
var textParser = bodyParser.text(options);
2723
return function xmlParser(req, res, next) {
28-
var parser;
2924
// First, run the body through the text parser.
3025
textParser(req, res, function(err) {
3126
if(err) { return next(err); }
3227
if(typeof req.body !== 'string') { return next(); }
3328

3429
// Then, parse as XML.
35-
parser = new xml2js.Parser(options.xmlParseOptions);
30+
var parser = new xml2js.Parser(options.xmlParseOptions);
3631
parser.parseString(req.body, function(err, xml) {
3732
if(err) {
3833
err.status = 400;

0 commit comments

Comments
 (0)