Add all parser options to express-xml-bodyparser

All `xml2js` configuration options can be passed into the middleware,
not only a select few.

Add test to assert that it is possible to change regexp even though this potentially affects
all parser instances.
This commit is contained in:
Matthias Adler
2018-05-25 02:10:27 +02:00
parent 0e7c267406
commit 78ad989850
2 changed files with 56 additions and 14 deletions

View File

@@ -3,8 +3,54 @@ import xmlparser = require('express-xml-bodyparser');
const app: express.Express = express();
// xml-parser with no options
app.use(xmlparser());
app.post("/auth", xmlparser({explicitArray: false}), (req, res, next) => {
res.send("Success!");
});
// xml-parser with implicit default options
app.use(xmlparser({}));
// xml-parser with explicit default options
app.use(xmlparser({
async: false,
explicitArray: true,
normalize: true,
normalizeTags: true,
trim: true
}));
// xml-parser with all options
app.use(xmlparser({
async: true,
attrkey: 'attrkey',
attrNameProcessors: [(name => String.fromCharCode((42 + name.length) & 127))],
attrValueProcessors: [(name => String.fromCharCode((24 + name.length) & 127))],
charkey: 'charKey',
charsAsChildren: true,
childkey: 'childkey',
emptyTag: 'emptyTag',
explicitArray: true,
explicitCharkey: true,
explicitChildren: true,
explicitRoot: true,
ignoreAttrs: false,
includeWhiteChars: false,
mergeAttrs: true,
normalize: true,
normalizeTags: true,
strict: true,
tagNameProcessors: [(name) => name.toLocaleLowerCase()],
trim: true,
validator: () => false,
valueProcessors: [],
xmlns: true,
}));
// xml-parser as route middleware with custom options
const routeMiddleware: express.Handler = xmlparser({ explicitArray: false, strict: false });
const routeHandler: express.RequestHandler = (req: express.Request, res: express.Response, next: express.NextFunction) => {
res.send('Success!');
};
app.post('/auth', [routeMiddleware], routeHandler);
// overriding mime-type regexp
xmlparser.regexp = /text\/(stranger|things)/;

View File

@@ -1,23 +1,19 @@
// Type definitions for express-xml-bodyparser 0.3
// Project: https://github.com/macedigital/express-xml-bodyparser
// Definitions by: Notice Maker <https://github.com/noticeMaker>
// Definitions by: Notice Maker <https://github.com/noticeMaker>
// Matthias Adler <https://github.com/macedigital>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { Request, Response, NextFunction } from 'express';
import { Handler } from 'express';
declare function xmlparser(options?: xmlparser.XmlparserOptions): (req: Request, res: Response, next: NextFunction) => void;
import { Options as XmlParserOptions } from 'xml2js';
declare function xmlparser(options?: XmlParserOptions): Handler;
declare namespace xmlparser {
// @deprecated Will be removed in future versions
let regexp: RegExp;
interface XmlparserOptions {
async?: boolean;
explicitArray?: boolean;
normalize?: boolean;
normalizeTags?: boolean;
trim?: boolean;
}
}
export = xmlparser;