Skip to content

Commit 404f37c

Browse files
committed
don't assume fields/data
1 parent feec6e8 commit 404f37c

File tree

4 files changed

+32
-26
lines changed

4 files changed

+32
-26
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ const register = (server, pluginOptions) => {
1313
return h.continue;
1414
}
1515
if (request.headers.accept === 'text/csv') {
16-
const routeOptions = request.route.settings.plugins['hapi-transform-csv'] || {};
16+
const input = Object.assign({}, pluginOptions, request.route.settings.plugins['hapi-transform-csv'] || {});
17+
// get the fields:
18+
input.fields = Array.from(new Set(response.source.reduce((memo, item) => memo.concat(Object.keys(item)), [])));
19+
input.data = response.source;
1720
// json2csv may throw an error if not formatted correctly:
18-
return h.response(json2csv(Object.assign({}, pluginOptions, routeOptions, response.source)));
21+
return h.response(json2csv(input));
1922
}
2023
return h.continue;
2124
});

tests/output1.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"car","price","color"
2+
"Audi",40000,"blue"
3+
"BMW",35000,"black"
4+
"Porsche",60000,"green"

tests/output2.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$car$_$price$_$color$
2+
$Audi$_40000_$blue$
3+
$BMW$_35000_$black$
4+
$Porsche$_60000_$green$

tests/test.csv.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
const Hapi = require('hapi');
22
const tap = require('tap');
33
const plugin = require('../index');
4+
const fs = require('fs');
5+
const path = require('path');
46

57
tap.test('can configure a route to return csv instead of json', async(t) => {
68
const server = await new Hapi.Server({ port: 8080 });
79
server.route({
810
method: 'get',
911
path: '/normal',
1012
handler(request, h) {
11-
return {
12-
fields: ['car', 'price', 'color'],
13-
data: [
14-
{
15-
car: 'Audi',
16-
price: 40000,
17-
color: 'blue'
18-
}, {
19-
car: 'BMW',
20-
price: 35000,
21-
color: 'black'
22-
}, {
23-
car: 'Porsche',
24-
price: 60000,
25-
color: 'green'
26-
}
27-
]
28-
};
13+
return [
14+
{
15+
car: 'Audi',
16+
price: 40000,
17+
color: 'blue'
18+
}, {
19+
car: 'BMW',
20+
price: 35000,
21+
color: 'black'
22+
}, {
23+
car: 'Porsche',
24+
price: 60000,
25+
color: 'green'
26+
}
27+
];
2928
}
3029
});
3130

@@ -64,10 +63,7 @@ tap.test('can configure a route to return csv instead of json', async(t) => {
6463
});
6564
t.equal(csvResponse.statusCode, 200, 'returns HTTP OK');
6665
t.equal(typeof csvResponse.result, 'string', 'returns a string value');
67-
const rows = csvResponse.result.split('\n');
68-
t.notEqual(rows[0].indexOf('car'), -1, 'top row of csv is the headers');
69-
t.notEqual(rows[0].indexOf('price'), -1, 'top row of csv is the headers');
70-
t.notEqual(rows[0].indexOf('color'), -1, 'top row of csv is the headers');
66+
t.equal(csvResponse.result, fs.readFileSync(path.join(__dirname, 'output1.txt'), 'utf-8'), 'returns correct output');
7167
const jsonResponse = await server.inject({
7268
method: 'get',
7369
url: '/normal'
@@ -129,8 +125,7 @@ tap.test('will pass config options to json2csv', async(t) => {
129125
url: '/path1.csv'
130126
});
131127
t.equal(csvResponse.statusCode, 200, 'returns HTTP OK');
132-
t.equal(csvResponse.result.indexOf('$car$'), 0, 'applied the json2csv route options');
133-
t.equal(csvResponse.result.indexOf('$car$_$price$_$color$'), 0, 'applied the json2csv plugin options');
128+
t.equal(csvResponse.result, fs.readFileSync(path.join(__dirname, 'output2.txt'), 'utf-8'), 'returns correct output');
134129
await server.stop();
135130
t.end();
136131
});

0 commit comments

Comments
 (0)