UNPKG

rested

Version:

{REST}ed; {REST}ful Enterprise Data-as-a-Service (DaaS)

31 lines (27 loc) 1.28 kB
const stringify = require('csv-stringify'); const through = require('through2'); const flatten = require('flat'); module.exports = function () { this.setFormatter('text/csv', singleOrArray); this.setFormatter('application/csv', singleOrArray); this.setFormatter('text/comma-separated-values', singleOrArray); }; const singleOrArray = function () { let header = '\n'; return through.obj(function (doc, enc, callback) { if (doc.constructor.name === 'model') doc = JSON.parse(JSON.stringify(doc)); doc = doc instanceof Array ? doc : doc ? [doc] : undefined; doc = doc.map(d => d instanceof Object && Object.keys(d).length > 0 ? flatten(d) : { "": d }); stringify(doc, { header: true, cast: { date: v => v.toISOString() } }, (err, output = '') => { if (output.indexOf(header) !== 0) header = output.match(/^.*?\n/)[0]; else output = output.substr(header.length); // ensure dates without timestamp represented as iso date string this.emit('data', output.replace(datergx, '')) callback(); }); }); }; const datergx = RegExp('T00:00:00.000Z', 'g'); // ensure dates represented as dates