Comparing version 0.1.4 to 0.1.5
@@ -97,2 +97,4 @@ 'use strict'; | ||
* @param {string} options.schema The name of the saved schema. Use this this to generate data based on schema you've built using the website. Use the fields array to generate data using an ad-doc schema. | ||
* @param {string} [options.format='json'] 'json' by default, set to 'csv' to generate csv data. | ||
* @param {boolean} [options.header=true] when using format: 'csv', set to false to remove the header row | ||
* @param {Object[]} options.fields An array of fields | ||
@@ -169,7 +171,9 @@ * @param {string} options.fields.type The type of field. Many field types such as "Number" and "Custom List" take additional parameters, which are documented here: http://mockaroo.com/api/docs#types. | ||
value: function getUrl(options) { | ||
options = Object.assign({ count: 1 }, options); | ||
options = Object.assign({ count: 1, format: 'json' }, options); | ||
var protocol = this.secure ? 'https' : 'http'; | ||
var port = this.port ? ':' + this.port : ''; | ||
var schema = options.schema ? '&schema=' + options.schema : ''; | ||
return protocol + '://' + this.host + port + '/api/generate.json?client=node&key=' + encodeURIComponent(this.apiKey) + '&count=' + options.count + schema; | ||
var header = options.header !== undefined ? '&header=' + options.header : ''; | ||
if (options.format !== 'json' && options.format !== 'csv') throw new Error('format must be json or csv'); | ||
return protocol + '://' + this.host + port + '/api/generate.' + options.format + '?client=node&key=' + encodeURIComponent(this.apiKey) + '&count=' + options.count + schema + header; | ||
} | ||
@@ -176,0 +180,0 @@ }, { |
{ | ||
"name": "mockaroo", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Generate data using the mockaroo.com API", | ||
@@ -5,0 +5,0 @@ "main": "lib/mockaroo.js", |
@@ -54,2 +54,15 @@ # mockaroo-node | ||
You can also download data in csv format: | ||
```js | ||
client.generate({ | ||
count: 10, | ||
format: 'csv', | ||
header: true, // this is the default, set to false to remove the header row | ||
fields: [...] | ||
}).then(function(records) { | ||
// handle response | ||
}); | ||
``` | ||
# Handling Responses | ||
@@ -111,2 +124,1 @@ | ||
npm test | ||
@@ -34,3 +34,3 @@ var nock = require('nock'); | ||
client.getUrl().should.equal('https://mockaroo.com/api/generate.json?key=xxx&count=1') | ||
client.getUrl().should.equal('https://mockaroo.com/api/generate.json?client=node&key=xxx&count=1') | ||
}); | ||
@@ -45,3 +45,3 @@ | ||
client.getUrl().should.equal('http://mockaroo.com:3000/api/generate.json?key=xxx&count=1'); | ||
client.getUrl().should.equal('http://mockaroo.com:3000/api/generate.json?client=node&key=xxx&count=1'); | ||
}); | ||
@@ -55,3 +55,3 @@ | ||
client.getUrl().should.equal('http://mockaroo.com/api/generate.json?key=xxx&count=1'); | ||
client.getUrl().should.equal('http://mockaroo.com/api/generate.json?client=node&key=xxx&count=1'); | ||
}); | ||
@@ -64,3 +64,3 @@ | ||
client.getUrl({count: 10}).should.equal('https://mockaroo.com/api/generate.json?key=xxx&count=10'); | ||
client.getUrl({count: 10}).should.equal('https://mockaroo.com/api/generate.json?client=node&key=xxx&count=10'); | ||
}); | ||
@@ -74,3 +74,3 @@ | ||
client.getUrl().should.equal('https://foo/api/generate.json?key=xxx&count=1'); | ||
client.getUrl().should.equal('https://foo/api/generate.json?client=node&key=xxx&count=1'); | ||
}); | ||
@@ -83,4 +83,20 @@ | ||
client.getUrl({schema: 'MySchema'}).should.equal('https://mockaroo.com/api/generate.json?key=xxx&count=1&schema=MySchema'); | ||
client.getUrl({schema: 'MySchema'}).should.equal('https://mockaroo.com/api/generate.json?client=node&key=xxx&count=1&schema=MySchema'); | ||
}); | ||
it('should allow you to generate csv', function() { | ||
var client = new Mockaroo.Client({ | ||
apiKey: 'xxx' | ||
}); | ||
client.getUrl({format: 'csv'}).should.equal('https://mockaroo.com/api/generate.csv?client=node&key=xxx&count=1'); | ||
}); | ||
it('should allow you to remove the header from csv', function() { | ||
var client = new Mockaroo.Client({ | ||
apiKey: 'xxx' | ||
}); | ||
client.getUrl({format: 'csv', header: false}).should.equal('https://mockaroo.com/api/generate.csv?client=node&key=xxx&count=1&header=false'); | ||
}) | ||
}); | ||
@@ -102,3 +118,3 @@ | ||
var api = nock('http://mockaroo.com') | ||
.post('/api/generate.json?key=xxx&count=1') | ||
.post('/api/generate.json?client=node&key=xxx&count=1') | ||
.reply(200, JSON.stringify([{ foo: 'bar' }])) | ||
@@ -121,3 +137,3 @@ | ||
var api = nock('http://mockaroo.com') | ||
.post('/api/generate.json?key=xxx&count=1') | ||
.post('/api/generate.json?client=node&key=xxx&count=1') | ||
.reply(500, JSON.stringify({ error: 'Invalid API Key' })) | ||
@@ -124,0 +140,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27988
382
123