Skip to content

Commit

Permalink
Simplify Content-Type setting
Browse files Browse the repository at this point in the history
  • Loading branch information
fiznool committed Mar 18, 2016
1 parent c0c3053 commit 8efa293
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ app.use(bodyParser.xml());

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

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:
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:

- `text/xml`
- `application/xml`
- `application/rss+xml`

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

### Options

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

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'`.


#### type

set your own valid Content-Type that will be parsed, can be a string or an array, already valid topics are '*/xml', '+xml'

The expected `Content-Type` of the XML request to be parsed. Overrides the default content types, can be a String or Array of Strings.

#### verify

Expand Down
21 changes: 8 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,32 @@

var xml2js = require('xml2js');

var DEFAULT_TYPES = ['*/xml', '+xml'];

module.exports = function(bodyParser) {
var xmlTypes = ['*/xml', '+xml'];
if(bodyParser.xml) {
// We already setup the XML parser.
// End early.
return;
}

function xml(options) {
var textParser;
options = options || {};
if(options.type){
if(Array.isArray(options.type)){
xmlTypes = options.type.concat(xmlTypes);
}else if(typeof options.type === 'string'){
xmlTypes.push(options.type);
}
options.type = xmlTypes;

options.type = options.type || DEFAULT_TYPES;
if(!Array.isArray(options.type)) {
options.type = [options.type];
}

options.type = xmlTypes;
textParser= bodyParser.text(options);
var textParser = bodyParser.text(options);
return function xmlParser(req, res, next) {
var parser;
// First, run the body through the text parser.
textParser(req, res, function(err) {
if(err) { return next(err); }
if(typeof req.body !== 'string') { return next(); }

// Then, parse as XML.
parser = new xml2js.Parser(options.xmlParseOptions);
var parser = new xml2js.Parser(options.xmlParseOptions);
parser.parseString(req.body, function(err, xml) {
if(err) {
err.status = 400;
Expand Down

0 comments on commit 8efa293

Please sign in to comment.