0.1.2 • Published 6 years ago

data2csv v0.1.2

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

data2csv · GitHub license Build Status npm version

Middleware for export data list to csv file.

If you have a api for getting data lists, but if you want to export these data lists to csv file, then only one thing you can do is to using this middleware package, but on need to refactor your api code.

Getting started

$ npm install data2csv --save

Usage

First, you neend to set request headers: 'Accept' to 'text/csv', but not to 'application/json'.

and then add middleware

'use strict';

const data2csv = require('data2csv');

const double = (data, defaultValue) => {
  return data && (data+data) || defaultValue;
};

// 'method' is option attribute
const fields = [
  { label: 'Code', value: 'code', method: double, default: '-' },
  { label: 'Name', value: 'name', default: '-' }
];

const handler = async (data, req, res) => {
  // data = [{}, {}, ...];
  // for ex. => data = [{ code: '123', name: 'name123' }, { code: '', name: 'name456' } ];
  const csv = data2csv.convertJsonToCSV(data, fields);

  // csv = 'Code,Name\n123123,name123\n-,name456\n'
  // Code   Name
  // 123123 name123
  // -      name456

  const filename = 'the export csv file name you want to call';

  return { csv, filename };
};

module.exports = data2csv.mw(handler);

then add middleware to your app.js file (before the route handling middleware)

app.use(require('./middleware.js'));

Contact

If you have any questions, please contact me root@laravue.org

License

The MIT license

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago