Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dependecy Updates] Update Packages #8

Merged
merged 2 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
lib/*
routes.js
29 changes: 29 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = {
env: {
browser: true,
es2021: true,
mocha: true
},
extends: 'standard',
overrides: [
{
env: {
node: true
},
files: [
'.eslintrc.{js,cjs}'
],
parserOptions: {
sourceType: 'script'
}
}
],
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module'
},
rules: {
"semi": ["error", "always"],
"quotes": ["error", "double"],
}
}
File renamed without changes.
Binary file removed images/Mockaroo Quote 20230525-194.pdf
Binary file not shown.
29 changes: 20 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "Generate data using the mockaroo.com API",
"main": "index.js",
"type": "module",
"author": "Mark Brocato",
"license": "MIT",
"homepage": "https://github.com/mockaroo/mockaroo-node",
Expand All @@ -18,17 +19,27 @@
"publish-to-npm": "cd dist && npm publish --access public",
"release": "yarn build && yarn docs && yarn publish-to-npm && yarn deploy"
},
"targets": {
"default": {
"distDir": "dist"
}
},
"dependencies": {
"axios": "^0.5.4"
"axios": "1.6.8"
},
"devDependencies": {
"@edgio/cli": "^7.0.17",
"@edgio/core": "^7.0.17",
"chai": "^3.0.0",
"esbuild": "^0.17.19",
"esdoc": "^1.1.0",
"esdoc-standard-plugin": "^1.0.0",
"mocha": "^2.2.5",
"nock": "^2.6.0"
"@edgio/cli": "7.11.3",
"@edgio/core": "7.11.3",
"chai": "5.1.0",
"esbuild": "0.20.2",
"esdoc": "1.1.0",
"esdoc-standard-plugin": "1.0.0",
"eslint": "8.0.1",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.25.2",
"eslint-plugin-n": "15.0.0 || 16.0.0 ",
"eslint-plugin-promise": "6.0.0",
"mocha": "10.4.0",
"nock": "13.5.4"
}
}
198 changes: 98 additions & 100 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import "core-js/shim";
import axios from "axios";
import * as errors from "./errors";

axios.defaults.adapter = "http";

/**
* A client for generating data using mockaroo.
*/
export default class Client {
/**
* Creates a new instance
* @param {Object} options
* @param {string} options.apiKey Your mockaroo api key. See http://mockaroo.com/api/docs under "Gaining Access".
* @param {string} [options.host='mockaroo.com'] The hostname of the mockaroo server.
* @param {int} [options.port=80] The port to use.
* @param {boolean} [options.secure=true] Set to false to use http instead of https
*/
constructor(options) {
var defaults = {
host: "api.mockaroo.com",
* Creates a new instance
* @param {Object} options
* @param {string} options.apiKey Your mockaroo api key. See http://mockaroo.com/api/docs under "Gaining Access".
* @param {string} [options.host='mockaroo.com'] The hostname of the mockaroo server.
* @param {int} [options.port=80] The port to use.
* @param {boolean} [options.secure=true] Set to false to use http instead of https
*/
constructor (options) {
const defaults = {
host: "mockaroo.com",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
host: "mockaroo.com",
host: "api.mockaroo.com",

This should be api.mockaroo.com, not the apex domain.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Thank you!

secure: true,
port: null,
apiKey: null
};

Object.assign(this, defaults, options);
Expand All @@ -26,79 +29,80 @@ export default class Client {
}

/**
* Generates data based on the specified options. You can use either a saved schema using the "schema" option:
*
* @example
*
* // generate data from a saved schema
* client.generate({
* schema: 'My Saved Schema'
* }).then(function(data) {
* // keys are the names of the columns in your saved schema.
* })
*
* @example
*
* //specify fields using the api
* client.generate({
* count: 10,
* fields: [{
* name: 'id'
* type: 'Row Number',
* }, {
* name: 'transactionType'
* type: 'Custom List',
* values: ['debit', 'credit']
* }, {
* name: 'amount',
* type: 'Number'
* min: 1
* max: 10000,
* decimals: 2
* }]
* }).then(function(data) {
* for (var i=0; i<data.length; i++) {
* var record = data[i];
* console.log('id', record.id, 'transactionType', record.transactionType, 'amount', record.amount);
* }
* })
*
* @param {Object} options
* @param {int} [options.count=1] The number of records to generate. See usage limits here: http://mockaroo.com/api/docs
* @param {string} options.schema The name of the saved schema. Use 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 {boolean} [options.array=false] set to true to always return an array of objects, even if count == 1
* @param {Object[]} options.fields An array of fields
* @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.
* @param {string} options.field.name The value will be returned under this key in the resulting data objects.
* @return {Promise<Object[]>} The promise resolves to an object if count == 1 or array of objects if count > 1. Each object represents a record. Keys are the field names.
*/
generate(options) {
if (!options || (!options.schema && !options.fields))
throw new Error("Either fields or schema option must be specified");
* Generates data based on the specified options. You can use either a saved schema using the "schema" option:
*
* @example
*
* // generate data from a saved schema
* client.generate({
* schema: 'My Saved Schema'
* }).then(function(data) {
* // keys are the names of the columns in your saved schema.
* })
*
* @example
*
* //specify fields using the api
* client.generate({
* count: 10,
* fields: [{
* name: 'id'
* type: 'Row Number',
* }, {
* name: 'transactionType'
* type: 'Custom List',
* values: ['debit', 'credit']
* }, {
* name: 'amount',
* type: 'Number'
* min: 1
* max: 10000,
* decimals: 2
* }]
* }).then(function(data) {
* for (var i=0; i<data.length; i++) {
* var record = data[i];
* console.log('id', record.id, 'transactionType', record.transactionType, 'amount', record.amount);
* }
* })
*
*
* @typedef {Object} Field
* @property {string} 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.
* @property {string} name The value will be returned under this key in the resulting data objects.
*
* @param {Object} options
* @param {int} [options.count=1] The number of records to generate. See usage limits here: http://mockaroo.com/api/docs
* @param {string} options.schema The name of the saved schema. Use 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 {boolean} [options.array=false] set to true to always return an array of objects, even if count == 1
* @param {Field[]} options.fields
* @return {Promise<Object[]>} The promise resolves to an object if count == 1 or array of objects if count > 1. Each object represents a record. Keys are the field names.
*/
generate (options) {
if (!options || (!options.schema && !options.fields)) throw new Error("Either fields or schema option must be specified");

this.validateFields(options.fields);

var url = this.getUrl(options);
const url = this.getUrl(options);

return new Promise((resolve, reject) => {
axios
.post(url, options.fields)
.then((resp) => resolve(resp.data))
.catch((resp) => reject(this.convertError(resp)));
axios.post(url, options.fields)
.then(resp => resolve(resp.data))
.catch(resp => reject(this.convertError(resp)));
});
}

/**
* @private
* @param {Object[]} fields
*/
validateFields(fields) {
* @private
* @param {Object[]} fields
*/
validateFields (fields) {
if (!fields) return;

if (fields instanceof Array) {
for (var field of fields) {
for (const field of fields) {
if (!field.name) throw new Error("each field must have a name");
if (!field.type) throw new Error("each field must have a type");
}
Expand All @@ -108,35 +112,29 @@ export default class Client {
}

/**
* Generates the rest api url
* @private
* @return {String}
*/
getUrl(options) {
* Generates the rest api url
* @private
* @return {String}
*/
getUrl (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}` : "";
var header =
options.header !== undefined ? `&header=${options.header}` : "";
var array = options.array !== undefined ? `&array=${options.array}` : "";
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
}${array}${schema}${header}`;
const protocol = this.secure ? "https" : "http";
const port = this.port ? `:${this.port}` : "";
const schema = options.schema ? `&schema=${options.schema}` : "";
const header = options.header !== undefined ? `&header=${options.header}` : "";
const array = options.array !== undefined ? `&array=${options.array}` : "";
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}${array}${schema}${header}`;
}

/**
* Converts error messages from the mockaroo server to error classes
* @private
*/
convertError(response) {
var error = response.data.error;
* Converts error messages from the mockaroo server to error classes
* @private
*/
convertError (response) {
const error = response.data.error;

if (error == "Invalid API Key") {
if (error === "Invalid API Key") {
return new errors.InvalidApiKeyError(response.error);
} else if (error.match(/limited/)) {
return new errors.UsageLimitExceededError(response.error);
Expand All @@ -146,10 +144,10 @@ export default class Client {
}

/**
* Validates that all required options have be specified.
* @private
*/
validate() {
* Validates that all required options have be specified.
* @private
*/
validate () {
if (!this.apiKey) throw new Error("apiKey is required");
}
}
14 changes: 1 addition & 13 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class ApiError extends Error {
/**
* @param {String} message
*/
constructor(message) {
constructor (message) {
super();
this.message = message;
}
Expand All @@ -16,22 +16,10 @@ export class ApiError extends Error {
* in a 24 hour period given their plan. See usage limits here: http://mockaroo.com/api/docs
*/
export class UsageLimitExceededError extends ApiError {
/**
* @param {String} message
*/
constructor(message) {
super(message);
}
}

/**
* Thrown when an invalid api key is used.
*/
export class InvalidApiKeyError extends ApiError {
/**
* @param {String} message
*/
constructor(message) {
super(message);
}
}
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as errors from "./errors";
import Client from "./client";

/**
* @module mockaroo
*/
module.exports = {
import Client from "./client.js";
import * as errors from "./errors";

export default {
errors,
Client,
Client
};