Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-csv

Package Overview
Dependencies
Maintainers
2
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-csv - npm Package Compare versions

Comparing version 2.4.4 to 2.5.0

12

lib/index.js

@@ -65,3 +65,3 @@ 'use strict';

const schema = internals.routeMap.get(request.route.path);
const csv = internals.schemaToCsv(schema, request.response.source, options.separator);
const csv = internals.schemaToCsv(schema, request.response.source, options.separator, options.resultKey);

@@ -86,3 +86,3 @@ // FUTURE add header=present but wait for response on https://github.com/hapijs/hapi/issues/3243

internals.schemaToCsv = (schema, dataset, separator) => {
internals.schemaToCsv = (schema, dataset, separator, resultKey) => {

@@ -94,3 +94,9 @@ // We return the dataset if the dataset is not an array or an object, just a primitive type

const schemaDescription = Joi.compile(schema).describe();
let schemaDescription = Joi.compile(schema).describe();
if (schemaDescription.children && schemaDescription.children[resultKey]) {
schemaDescription = schemaDescription.children[resultKey];
dataset = dataset[resultKey];
}
const headerQueryArray = internals.parseSchema(schemaDescription);

@@ -97,0 +103,0 @@ const headerQueryMap = internals.arrayToMap(headerQueryArray);

{
"name": "hapi-csv",
"version": "2.4.4",
"version": "2.5.0",
"description": "Hapi plugin for converting a Joi response schema and dataset to csv",

@@ -28,3 +28,3 @@ "main": "lib/index.js",

"joi": "9.x.x",
"code": "3.x.x",
"code": "4.x.x",
"hapi": "15.x.x",

@@ -31,0 +31,0 @@ "lab": "11.x.x"

@@ -51,1 +51,28 @@ # Hapi-csv [![Build Status](https://travis-ci.org/Salesflare/hapi-csv.svg?branch=master)](https://travis-ci.org/Salesflare/hapi-csv)

Currently the `content-disposition` header is set to `attachment;` by default since this plugin is intended for exporting purposes, if this hinders you just let us know.
To handle typical pagination responses like
```json
{
"page": 1,
"items": [
{ "name": "Anton", "age": 22 },
{ "name": "Lisa", "age": 25 }
]
}
```
pass in the `resultKey` option:
```javascript
server.register({
register: require('hapi-csv'),
options: {
resultKey: 'items'
}
}, function (err) {
if (err) throw err;
...
});
```

@@ -469,2 +469,147 @@ 'use strict';

});
describe('Result key (e.g. for pagination)', () => {
it('Uses the result key', (done) => {
const result = {
page: 1,
items: [{
first_name: 'firstName1',
last_name: 'lastName1',
age: 25
}, {
first_name: 'firstName2',
last_name: 'lastName2',
age: 27
}]
};
const userCSV = `first_name,last_name,age,\n"firstName1","lastName1","25",\n"firstName2","lastName2","27",`;
const server = new Hapi.Server();
server.connection();
return server.register({
register: HapiCsv,
options: {
resultKey: 'items'
}
}, (err) => {
expect(err, 'error').to.not.exist();
server.route([{
method: 'GET',
path: '/test',
config: {
handler: function (request, reply) {
return reply(result);
},
response: {
schema: Joi.object({
page: Joi.number(),
items: Joi.array().items(
Joi.object().keys({
first_name: Joi.string(),
last_name: Joi.string(),
age: Joi.number()
})
)
})
}
}
}]);
return server.initialize((err) => {
expect(err, 'error').to.not.exist();
return server.inject({
method: 'GET',
url: '/test',
headers: {
'Accept': 'text/csv'
}
}, (res) => {
expect(res.result, 'result').to.equal(userCSV);
expect(res.headers['content-type']).to.equal('text/csv; charset=utf-8');
expect(res.headers['content-disposition']).to.equal('attachment;');
return server.stop(done);
});
});
});
});
it('Ignores the result key if not used in the response', (done) => {
const result = [{
first_name: 'firstName1',
last_name: 'lastName1',
age: 25
}, {
first_name: 'firstName2',
last_name: 'lastName2',
age: 27
}];
const userCSV = `first_name,last_name,age,\n"firstName1","lastName1","25",\n"firstName2","lastName2","27",`;
const server = new Hapi.Server();
server.connection();
return server.register({
register: HapiCsv,
options: {
resultKey: 'items'
}
}, (err) => {
expect(err, 'error').to.not.exist();
server.route([{
method: 'GET',
path: '/test',
config: {
handler: function (request, reply) {
return reply(result);
},
response: {
schema: Joi.array().items(
Joi.object().keys({
first_name: Joi.string(),
last_name: Joi.string(),
age: Joi.number()
})
)
}
}
}]);
return server.initialize((err) => {
expect(err, 'error').to.not.exist();
return server.inject({
method: 'GET',
url: '/test',
headers: {
'Accept': 'text/csv'
}
}, (res) => {
expect(res.result, 'result').to.equal(userCSV);
expect(res.headers['content-type']).to.equal('text/csv; charset=utf-8');
expect(res.headers['content-disposition']).to.equal('attachment;');
return server.stop(done);
});
});
});
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc