aspire_core
Version:
Core api
124 lines (101 loc) • 4.23 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);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var SalesforceAuth = function () {
function SalesforceAuth(nforce, credentials) {
_classCallCheck(this, SalesforceAuth);
this.nforce = nforce;
this.credentials = credentials;
}
_createClass(SalesforceAuth, [{
key: "authenticate",
value: function authenticate() {
var _this = this;
_this.org = _this.nforce.createConnection({
clientId: this.credentials.clientID || "key",
clientSecret: this.credentials.clientSecret || "secret",
redirectUri: 'http://localhost:3000/oauth/_callback',
mode: 'single'
});
return new _bluebird2.default(function (resolve, reject) {
_this.org.authenticate({
username: _this.credentials.userName || "username",
password: _this.credentials.password || "password",
securityToken: _this.credentials.token || "token"
}, function (err, oauth) {
if (err) {
reject(new _Errors.SalesforceAuthError(err));
} else {
_this.oauth = oauth;
_this.org.oauth = oauth;
resolve();
}
});
});
}
}, {
key: "attemptToGetObjectUpto3Times",
value: function attemptToGetObjectUpto3Times(objectType, objectId, attempts) {
var _this2 = this;
var _this = this;
attempts = attempts ? ++attempts : 0;
return new _bluebird2.default(function (resolve, reject) {
_this2.org.getRecord({
type: objectType,
id: objectId,
oauth: _this2.oauth
}, function (err, sfObject) {
if (err) {
if (attempts >= 3) {
reject(err);
} else {
_bluebird2.default.delay(500).then(function () {
return _this.attemptToGetObjectUpto3Times(objectType, objectId, attempts);
}).then(function (resp) {
return resolve(resp);
}).catch(function (error) {
return reject(error);
});
}
} else {
resolve(sfObject);
}
});
});
}
// Recursively attempt (up to 3 times) to insert, pausing 500 ms between attempts
}, {
key: "attemptToInsertUpto3Times",
value: function attemptToInsertUpto3Times(object, sobject, retName, attempts) {
var _this3 = this;
var _this = this;
attempts = attempts ? ++attempts : 0;
return new _bluebird2.default(function (resolve, reject) {
_this3.org.insert({ sobject: sobject, oauth: _this.oauth }, function (err, sfResp) {
if (!err) {
var resp = { sfResp: sfResp };
resp[retName] = object;
resolve(resp);
} else {
if (attempts >= 3) reject(err);else _bluebird2.default.delay(500).then(function () {
return _this.attemptToInsertUpto3Times(object, sobject, retName, attempts);
}).then(function (response) {
return resolve(response);
}).catch(function (error) {
return reject(error);
});
}
});
});
}
}]);
return SalesforceAuth;
}();
exports.default = SalesforceAuth;
;