2
0
Fork 0
tcrud/lib/plugin/format/format-xml.js

76 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2017-06-10 14:53:29 +00:00
var xmlmapping = require('xml-mapping');
var rawbody = require('raw-body');
var typeis = require('type-is');
module.exports = (function () {
function bodyParserXml() {
return function(req, res, next) {
if (req._body) return next();
req.body = req.body || {};
if (!typeis(req, 'xml')) return next();
// flag as parsed
req._body = true;
// parse
rawbody(req, {
limit: options.limit || '100kb',
length: req.headers['content-length'],
encoding: 'utf8'
}, function (err, buf) {
if (err) return next(err);
if (0 == buf.length) {
return next(error(400, 'invalid xml, empty body'));
}
try {
req.body = xmlmapping.dump(buf/*, options.reviver*/);
} catch (err){
err.body = buf;
err.status = 400;
return next(err);
}
next();
})
}
}
return function FormatXmlPlugin() {
this.configPlugin = function (ctx) {
ctx.key = 'formatXML';
ctx.description = 'Export the tentity api in xml format.';
ctx.localDir = __dirname;
ctx.localConfigTemplate = 'format-xml.json';
};
this.configServer = function(ctx) {
ctx.server.use(bodyParserXml());
}
// flag that we support these all crud types.
this.configApiTListExport = function (ctx) {
return ctx.renderTemplateDataList(ctx.tview.tmeta.tplugin.formatXML.tmime);
};
this.configApiTCreateExport = function (ctx) {
};
this.configApiTReadExport = function (ctx) {
return ctx.renderTemplateDataRead(ctx.tview.tmeta.tplugin.formatXML.tmime);
};
this.configApiTEditExport = function (ctx) {
};
this.configApiTDeleteExport = function (ctx) {
};
this.configApiTCountExport = function (ctx) {
};
};
})();