Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ const json2csv = require('json2csv');
const register = (server, pluginOptions) => {
server.ext('onRequest', (request, h) => {
if (request.path.endsWith('.csv')) {
const query = request.query;
request.headers.accept = 'text/csv';
request.setUrl(request.path.replace('.csv', ''));
request.query = query;
}
return h.continue;
});
Expand Down
41 changes: 41 additions & 0 deletions tests/test.csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,44 @@ tap.test('will not interfere with non-200 results', async(t) => {
await server.stop();
t.end();
});

tap.test('will forward query params to the underlying route', async(t) => {
const server = await new Hapi.Server({ port: 8080 });
server.route({
method: 'get',
path: '/path1',
config: {
plugins: {
'hapi-transform-csv': {
}
}
},
handler(request, h) {
t.equal(request.query.test, '1', 'query param forwarded');
return [
{
car: 'Audi',
price: 40000,
color: 'blue'
}, {
car: 'BMW',
price: 35000,
color: 'black'
}, {
car: 'Porsche',
price: 60000,
color: 'green'
}
];
}
});
await server.register(plugin, {});
await server.start();
const csvResponse = await server.inject({
method: 'get',
url: '/path1.csv?test=1'
});
t.equal(csvResponse.statusCode, 200, 'returns HTTP OK');
await server.stop();
t.end();
});