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

Remove assert module from source #101

Merged
merged 1 commit into from
Apr 30, 2019
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
6 changes: 3 additions & 3 deletions lib/airtable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

var assert = require('assert');

var Class = require('./class');
var Base = require('./base');
var Record = require('./record');
Expand Down Expand Up @@ -39,7 +37,9 @@ var Airtable = Class.extend({

this.requestTimeout = opts.requestTimeout || defaultConfig.requestTimeout;

assert(this._apiKey, 'API key is required to connect to Airtable');
if (!this._apiKey) {
throw new Error('API is required to connect to Airtable');
}
},

base: function(baseId) {
Expand Down
34 changes: 22 additions & 12 deletions lib/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var assert = require('assert');
var isPlainObject = require('lodash/isPlainObject');
var isFunction = require('lodash/isFunction');
var isString = require('lodash/isString');
Expand All @@ -22,10 +21,15 @@ var Query = Class.extend({
* or `eachPage` is called.
*/
init: function(table, params) {
assert(isPlainObject(params));
if (!isPlainObject(params)) {
throw new Error('Expected query options to be an object');
}

forEach(keys(params), function(key) {
var value = params[key];
assert(Query.paramValidators[key] && Query.paramValidators[key](value).pass, 'Invalid parameter for Query: ' + key);
if (!Query.paramValidators[key] || !Query.paramValidators[key](value).pass) {
throw new Error('Invalid parameter for Query: ' + key);
}
});

this._table = table;
Expand All @@ -41,8 +45,9 @@ var Query = Class.extend({
* then calls `done(error, records)`.
*/
firstPage: function(done) {
assert(isFunction(done),
'The first parameter to `firstPage` must be a function');
if (!isFunction(done)) {
throw new Error('The first parameter to `firstPage` must be a function');
}

this.eachPage(function(records) {
done(null, records);
Expand All @@ -62,11 +67,13 @@ var Query = Class.extend({
* `done(error)`.
*/
eachPage: function(pageCallback, done) {
assert(isFunction(pageCallback),
'The first parameter to `eachPage` must be a function');
if (!isFunction(done)) {
throw new Error('The first parameter to `eachPage` must be a function');
}

assert(isFunction(done) || (done === void 0),
'The second parameter to `eachPage` must be a function or undefined');
if (!isFunction(done) && (done !== void 0)) {
throw new Error('The second parameter to `eachPage` must be a function or undefined');
}

var that = this;
var path = '/' + this._table._urlEncodedNameOrId();
Expand Down Expand Up @@ -104,8 +111,9 @@ var Query = Class.extend({
* Fetches all pages of results asynchronously. May take a long time.
*/
all: function(done) {
assert(isFunction(done),
'The first parameter to `all` must be a function');
if (!isFunction(done)) {
throw new Error('The first parameter to `all` must be a function');
}

var allRecords = [];
this.eachPage(function(pageRecords, fetchNextPage) {
Expand Down Expand Up @@ -173,7 +181,9 @@ Query.paramValidators = {
* errors: a list of error messages.
*/
Query.validateParams = function validateParams(params) {
assert(isPlainObject(params));
if (!isPlainObject(params)) {
throw new Error('Expected query params to be an object');
}

var validParams = {};
var ignoredKeys = [];
Expand Down
11 changes: 6 additions & 5 deletions lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ var assign = require('lodash/assign');
var forEach = require('lodash/forEach');
var map = require('lodash/map');

var assert = require('assert');

var Class = require('./class');
var deprecate = require('./deprecate');
var Query = require('./query');
Expand All @@ -16,8 +14,11 @@ var callbackToPromise = require('./callback_to_promise');

var Table = Class.extend({
init: function(base, tableId, tableName) {
if (!tableId && !tableName) {
throw new Error('Table name or table ID is required');
}

this._base = base;
assert(tableId || tableName, 'Table name or table ID is required');
this.id = tableId;
this.name = tableName;

Expand Down Expand Up @@ -60,7 +61,7 @@ var Table = Class.extend({
return ' * ' + error;
});

assert(false, 'Airtable: invalid parameters for `select`:\n' +
throw new Error('Airtable: invalid parameters for `select`:\n' +
formattedErrors.join('\n'));
}

Expand All @@ -71,7 +72,7 @@ var Table = Class.extend({

return new Query(this, validationResults.validParams);
} else {
assert(false, 'Airtable: the parameter for `select` should be a plain object or undefined.');
throw new Error('Airtable: the parameter for `select` should be a plain object or undefined.');
}
},
_urlEncodedNameOrId: function(){
Expand Down