aspire_core
Version:
Core api
222 lines (196 loc) • 8.65 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
Object.defineProperty(exports, "__esModule", {
value: true
});
var _Errors = require("./Errors.js");
var _bluebird = require("bluebird");
var _bluebird2 = _interopRequireDefault(_bluebird);
var _request = require("request");
var _request2 = _interopRequireDefault(_request);
var _nforce = require("nforce");
var _nforce2 = _interopRequireDefault(_nforce);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var AspireSF = function () {
function AspireSF(securityToken) {
_classCallCheck(this, AspireSF);
this.securityToken = securityToken.slice(0, 112);
this.authObj = {
auth: {
bearer: this.securityToken
}
};
this.hostname = process.env.HOST_NAME;
this.nforce = _nforce2.default;
this.org = _nforce2.default.createConnection({
clientId: 'SOME_OAUTH_CLIENT_ID',
clientSecret: 'SOME_OAUTH_CLIENT_SECRET',
redirectUri: 'http://localhost:3000/oauth/_callback',
oauth: {
instance_url: "https://" + this.hostname,
access_token: this.securityToken
}
});
}
_createClass(AspireSF, [{
key: "getIdentity",
value: function getIdentity() {
var _this2 = this;
return new _bluebird2.default(function (resolve, reject) {
var _this = _this2;
var url = "https://" + _this.hostname + "/services/data/v29.0/";
_request2.default.get(url, _this.authObj, function (error, resp, body) {
if (error) {
console.log("error in getIdentitiy", error);
reject("error msg", new _Errors.SalesforceAuthError(error));
}
if (body && JSON.parse(body) && JSON.parse(body).identity && JSON.parse(body).identity.slice(-18)) {
resolve(JSON.parse(body).identity.slice(-18));
} else {
reject(new Error("User cannot be found"));
}
});
});
}
}, {
key: "getIdentityUpTo3Times",
value: function getIdentityUpTo3Times(attempts) {
var _this3 = this;
attempts = attempts || 0;
var url = "https://" + this.hostname + "/services/data/v29.0/";
return new _bluebird2.default(function (resolve, reject) {
_request2.default.get(url, _this3.authObj, function (error, resp, body) {
if (error) {
if (attempts > 2) {
reject(reject("getIdentitiy() error", new _Errors.SalesforceAuthError(error)));
} else {
_bluebird2.default.delay(500).then(function () {
return _this3.getIdentityUpTo3Times(++attempts);
}).then(function (response) {
return resolve(response);
}).catch(function (err) {
return reject("getIdentitiy() error", new _Errors.SalesforceAuthError(err));
});
}
}
if (body && JSON.parse(body) && JSON.parse(body).identity && JSON.parse(body).identity.slice(-18)) {
resolve(JSON.parse(body).identity.slice(-18));
} else {
if (attempts > 2) {
reject(reject("getIdentitiy() error", new _Errors.SalesforceAuthError(error)));
} else {
_bluebird2.default.delay(500).then(function () {
return _this3.getIdentityUpTo3Times(++attempts);
}).then(function (response) {
return resolve(response);
}).catch(function (err) {
return reject("getIdentitiy() error", new _Errors.SalesforceAuthError(err));
});
}
}
});
});
}
}, {
key: "attemptToGetObjectUpto3Times",
value: function attemptToGetObjectUpto3Times(objectType, objectId, attempts) {
var _this4 = this;
var _this = this;
return new _bluebird2.default(function (resolve, reject) {
_request2.default.get("https://" + _this.hostname + "/services/data/v29.0/sobjects/" + objectType + "/" + objectId, _this4.authObj, function (error, resp, body) {
if (error) {
if (attempts > 2) {
reject(new _Errors.SalesforceAuthError(error));
} else {
_bluebird2.default.delay(500).then(function () {
return _this.attemptToGetObjectUpto3Times(objectType, objectId, ++attempts);
}).then(function (response) {
return resolve(response);
}).catch(function (err) {
return reject(new _Errors.SalesforceAuthError(err));
});
}
} else {
resolve(body);
}
});
});
}
}, {
key: "attemptToUpdateObjectUpto3Times",
value: function attemptToUpdateObjectUpto3Times(objectType, objectId, objectProperties, attempts) {
var _this5 = this;
var _this = this;
return new _bluebird2.default(function (resolve, reject) {
var options = {
method: 'PATCH',
url: "https://" + _this.hostname + "/services/data/v29.0/sobjects/" + objectType + "/" + objectId,
headers: {
'content-type': 'application/json',
authorization: "Bearer " + _this5.securityToken
},
body: objectProperties,
json: true
};
(0, _request2.default)(options, function (error, resp, body) {
if (error) {
console.log("error in the attemptToUpdateObjectUpto3Times", error);
console.log("error in the attemptToUpdateObjectUpto3Times stack: ", error.stack);
if (attempts > 2) {
reject(new _Errors.SalesforceAuthError(error));
} else {
_bluebird2.default.delay(500).then(function () {
return _this.attemptToUpdateObjectUpto3Times(objectType, objectId, objectProperties, ++attempts);
}).then(function (response) {
return resolve(response);
}).catch(function (err) {
return reject(new _Errors.SalesforceAuthError(err));
});
}
} else {
resolve(body);
}
});
});
}
}, {
key: "attemptToGetQueryUpto3Times",
value: function attemptToGetQueryUpto3Times(query, attempts, results) {
var _this6 = this;
results = results || [];
var _this = this;
return new _bluebird2.default(function (resolve, reject) {
_request2.default.get("https://" + _this.hostname + "/services/data/v29.0/query/?q=" + query.replace(/ /g, '+'), _this6.authObj, function (error, resp, body) {
if (error) {
if (attempts > 2) {
reject(new _Errors.SalesforceAuthError(error));
} else {
_bluebird2.default.delay(500).then(function () {
return _this.attemptToGetQueryUpto3Times(query, attempts);
}).then(function (response) {
return resolve(response);
}).catch(function (err) {
return reject(new _Errors.SalesforceAuthError(err));
});
}
} else {
if (body.nextRecordsUrl) {
results.push(JSON.parse(body));
var queryIndex = body.nextRecordsUrl.indexOf("query/");
var nextQuery = body.nextRecordsUrl.slice(queryIndex + 6);
_this.attemptToGetQueryUpto3Times(nextQuery, ++attempts, results);
} else {
var _Object;
results.push(JSON.parse(body));
resolve((_Object = Object).assign.apply(_Object, _toConsumableArray(results)));
}
}
});
});
}
}]);
return AspireSF;
}();
exports.default = AspireSF;