Skip to content

Commit

Permalink
Remove q dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
k-grube committed Apr 14, 2017
1 parent 3458f86 commit de47f3c
Show file tree
Hide file tree
Showing 25 changed files with 123 additions and 156 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
"homepage": "https://github.com/covenanttechnologysolutions/connectwise-rest",
"dependencies": {
"btoa": "~1.1.2",
"q": "~1.4.1",
"request": "~2.78.0"
},
"devDependencies": {
"jsdoc": "~3.4.3",
"jsdoc-to-markdown": "~2.0.1"
},
"engines": {
"node": ">=4.0.0"
}
}
3 changes: 1 addition & 2 deletions src/CompanyAPI/Companies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/CompanyAPI/CompanyTeams.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/CompanyAPI/Configurations.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/CompanyAPI/Contacts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
183 changes: 91 additions & 92 deletions src/ConnectWise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* @private
*/
var request = require('request'),
btoa = require('btoa'),
Q = require('q');
btoa = require('btoa');

/**
* @const {string} DEFAULTS.apiPath
Expand Down Expand Up @@ -82,71 +81,69 @@ function ConnectWise(options) {
* @returns {Promise<*>}
*/
ConnectWise.prototype.api = function (path, method, params) {
var deferred = Q.defer();

if (!path) {
throw new Error('path must be defined');
}
if (!method) {
throw new Error('method must be defined');
}
return new Promise((resolve, reject) => {
if (!path) {
throw new Error('path must be defined');
}
if (!method) {
throw new Error('method must be defined');
}

var options = {
url: this.config.apiUrl + path,
headers: {
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Authorization': this.config.auth
},
method: method,
timeout: this.config.timeout
};

//@TODO perform URL validation here
if (path.match(/http:\/\//i) || path.match(/https:\/\//i)) {
options.url = path;
}
var options = {
url: this.config.apiUrl + path,
headers: {
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Authorization': this.config.auth
},
method: method,
timeout: this.config.timeout
};

//@TODO perform URL validation here
if (path.match(/http:\/\//i) || path.match(/https:\/\//i)) {
options.url = path;
}

if (method === 'GET' && params) {
options.url += parameterize(params);
}
if (method === 'GET' && params) {
options.url += parameterize(params);
}

if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
options.headers['Content-Type'] = 'application/json';
options.body = JSON.stringify(params);
}
if (method === 'POST' || method === 'PUT' || method === 'PATCH') {
options.headers['Content-Type'] = 'application/json';
options.body = JSON.stringify(params);
}

request(options, function (err, res) {
if (err) {
deferred.reject({
code: err.code,
message: err.message,
errors: [err]
});
} else {
if (method === 'DELETE' && res.body === '' || method === 'POST' && res.statusCode === 204) {
/** @type DeleteResponse */
deferred.resolve({});
return request(options, function (err, res) {
if (err) {
return reject({
code: err.code,
message: err.message,
errors: [err]
});
} else {
try {
var body = JSON.parse(res.body);
if (body.code) {
deferred.reject(body);
} else {
deferred.resolve(body);
if (method === 'DELETE' && res.body === '' || method === 'POST' && res.statusCode === 204) {
/** @type DeleteResponse */
return resolve({});
} else {
try {
var body = JSON.parse(res.body);
if (body.code) {
return reject(body);
} else {
return resolve(body);
}
} catch (e) {
return reject({
code: 'EPARSE',
message: 'Error parsing response from server.',
errors: [e]
});
}
} catch (e) {
deferred.reject({
code: 'EPARSE',
message: 'Error parsing response from server.',
errors: [e]
});
}
}
}
});
});

return deferred.promise;
};

/**
Expand All @@ -159,52 +156,54 @@ ConnectWise.prototype.api = function (path, method, params) {
* @returns {Promise<*>}
*/
ConnectWise.prototype.paginate = function (fn, args, module, pageSize, startPage) {
var deferred = Q.defer(),
results = [];
return new Promise((resolve, reject) => {
var results = [];

var page = startPage;
if (startPage === undefined) {
page = 0;
}
var page = startPage;
if (startPage === undefined) {
page = 0;
}

if (pageSize === undefined) {
pageSize = 100;
}
if (pageSize === undefined) {
pageSize = 100;
}

getPage(page);
getPage(page);

function getPage(page) {
function getPage(page) {

function pageHandler(res) {
if (res.length > 0) {
results = results.concat(res);
}
function pageHandler(res) {
if (res.length > 0) {
results = results.concat(res);
}

if (res.length === pageSize) {
getPage(++page);
} else {
deferred.resolve(results);
if (res.length === pageSize) {
getPage(++page);
} else {
return resolve(results);
}
}
}

//inject page key into params arg
args.forEach(function (arg) {
for (var key in arg) {
if (arg.hasOwnProperty(key)) {
if (key === 'page' || key === 'pageSize' || key === 'conditions' || key === 'orderBy') {
arg.page = page;
arg.pageSize = pageSize;
//inject page key into params arg
args.forEach(function (arg) {
for (var key in arg) {
if (arg.hasOwnProperty(key)) {
if (key === 'page' || key === 'pageSize' || key === 'conditions' || key === 'orderBy') {
arg.page = page;
arg.pageSize = pageSize;
}
}
}
}
});
});

fn.apply(module, args).then(pageHandler).fail(function (err) {
deferred.reject(err);
});
}
fn.apply(module, args)
.then(pageHandler)
.catch(function (err) {
return reject(err);
});
}

return deferred.promise;
});
};

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/Additions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/Adjustments.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/AgreementSites.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/Agreements.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/BoardDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/WorkRoles.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/WorkTypeExclusions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/FinanceAPI/WorkTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/ProjectAPI/Projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/ScheduleAPI/ScheduleEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/ScheduleAPI/ScheduleTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
3 changes: 1 addition & 2 deletions src/ServiceDeskAPI/BoardTeams.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* @private
*/
var Q = require('q'),
inherits = require('util').inherits,
var inherits = require('util').inherits,
ConnectWise = require('../ConnectWise.js');

/**
Expand Down
Loading

0 comments on commit de47f3c

Please sign in to comment.