backendless-console-sdk
Version:
Backendless Console SDK for Node.js and browser
256 lines (254 loc) • 10.3 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _cookie = require("./utils/cookie");
var _routes = require("./utils/routes");
var _baseService = _interopRequireDefault(require("./base/base-service"));
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _createSuper(t) { var r = _isNativeReflectConstruct(); return function () { var e, o = (0, _getPrototypeOf2["default"])(t); if (r) { var s = (0, _getPrototypeOf2["default"])(this).constructor; e = Reflect.construct(o, arguments, s); } else e = o.apply(this, arguments); return (0, _possibleConstructorReturn2["default"])(this, e); }; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); } /* eslint-disable max-len */
var routes = (0, _routes.prepareRoutes)({
getMyAccount: '/console/home/myaccount',
updateMyAccount: '/console/home/myaccount/',
login: '/console/home/login',
loginWithTOTP: '/console/home/otp-login',
cloudLogin: '/console/home/cloud/login',
cloudLoginWithTOTP: '/console/home/cloud/otp-login',
logout: '/console/home/logout',
registerDev: '/console/devreg',
deleteDev: '/console/developer-suicide',
twoFA: '/console/developer/login/2fa',
stripeConnect: '/console/developer/stripe-connect',
changePassword: '/console/passwordchange',
restorePassword: '/console/restorepassword',
resendConfirmEmail: '/console/resend',
discourseSSO: '/console/discourse/sso',
socialLogin: '/console/social/oauth/:type/request_url',
devToAutomationWorkspace: '/console/automation/management/:workspaceId/activatedev',
stripeConnectionAuth: '/console/community/marketplace/stripe-connect/auth',
stripeConnectionToken: '/console/community/marketplace/stripe-connect/token',
devToAppTeam: '/:appId/console/activatedev',
devPermissions: '/:appId/console/my-permissions'
});
var User = /*#__PURE__*/function (_BaseService) {
(0, _inherits2["default"])(User, _BaseService);
var _super = _createSuper(User);
function User(req, context) {
var _this;
(0, _classCallCheck2["default"])(this, User);
_this = _super.call(this, req);
_this.context = context;
_this.serviceName = 'user';
return _this;
}
/**
* @aiToolName Get Account Info
* @category User
* @description Get account information for the current user
* @paramDef {"type":"string","name":"authKey","label":"Auth Key","description":"Optional authentication key","required":false}
* @sampleResult {"name":"John Developer","email":"john@example.com","authKey":"auth123"}
*/
(0, _createClass2["default"])(User, [{
key: "getAccountInfo",
value: function getAccountInfo(authKey) {
var _this2 = this;
var request = this.req.get(routes.getMyAccount());
if (authKey) {
request.set('auth-key', authKey);
}
return request.send().then(function (account) {
_this2.context.setAuthKey(account.authKey);
return account;
});
}
}, {
key: "register",
value: function register(userData) {
return this.req.post(routes.registerDev()).send(userData);
}
}, {
key: "suicide",
value: function suicide() {
return this.req["delete"](routes.deleteDev()).send();
}
}, {
key: "login",
value: function login(_login, password) {
var _this3 = this;
return this.req.post(routes.login()).unwrapBody(false).send({
login: _login,
password: password
}).then(function (res) {
if (res.body.otpCreated) {
return res.body;
}
return contextifyWithAuthToken(res, _this3.context);
});
}
}, {
key: "loginWithTOTP",
value: function loginWithTOTP(authData) {
var _this4 = this;
return this.req.post(routes.loginWithTOTP()).unwrapBody(false).send(authData).then(function (res) {
return contextifyWithAuthToken(res, _this4.context);
});
}
}, {
key: "cloudLogin",
value: function cloudLogin(login, password) {
var _this5 = this;
return this.req.post(routes.cloudLogin()).unwrapBody(false).send({
login: login,
password: password
}).then(function (res) {
if (res.body.otpCreated) {
return res.body;
}
return contextifyWithAuthToken(res, _this5.context);
});
}
}, {
key: "cloudLoginWithTOTP",
value: function cloudLoginWithTOTP(authData) {
var _this6 = this;
return this.req.post(routes.cloudLoginWithTOTP()).unwrapBody(false).send(authData).then(function (res) {
return contextifyWithAuthToken(res, _this6.context);
});
}
}, {
key: "loginSocial",
value: function loginSocial(type) {
return this.req.get(routes.socialLogin(type));
}
}, {
key: "logout",
value: function logout() {
var _this7 = this;
return this.req["delete"](routes.logout()).then(function () {
_this7.context.setAuthKey(null);
if ((0, _cookie.cookieEnabled)()) {
(0, _cookie.deleteCookie)('dev-auth-key');
}
});
}
}, {
key: "restorePassword",
value: function restorePassword(email) {
return this.req.post(routes.restorePassword()).type('application/x-www-form-urlencoded; charset=UTF-8').send('email=' + encodeURIComponent(email));
}
}, {
key: "resendConfirmEmail",
value: function resendConfirmEmail(email) {
return this.req.get(routes.resendConfirmEmail()).query({
email: email
});
}
}, {
key: "updateProfile",
value: function updateProfile(profile) {
return this.req.put(routes.updateMyAccount(), profile);
}
}, {
key: "registerAndJoinAppTeam",
value: function registerAndJoinAppTeam(appId, confirmationCode, userData) {
var _this8 = this;
return this.req.post(routes.devToAppTeam(appId)).query({
'confirmation-code': confirmationCode
}).unwrapBody(false).send(userData).then(function (res) {
return contextifyWithAuthToken(res, _this8.context);
});
}
}, {
key: "registerAndJoinWorkspace",
value: function registerAndJoinWorkspace(workspaceId, confirmationCode, userData) {
var _this9 = this;
return this.req.automation.post(routes.devToAutomationWorkspace(workspaceId)).query({
'confirmation-code': confirmationCode
}).unwrapBody(false).send(userData).then(function (res) {
return contextifyWithAuthToken(res, _this9.context);
});
}
}, {
key: "loginToDiscourse",
value: function loginToDiscourse(user, sig, sso) {
return this.req.post(routes.discourseSSO(), {
user: user,
sig: sig,
sso: sso
});
}
/**
* @aiToolName Get Permissions
* @category User
* @description Get user permissions for an application
* @paramDef {"type":"string","name":"appId","label":"Application ID","description":"The identifier of the application","required":true}
* @sampleResult {"canRead":true,"canWrite":false,"canDelete":false,"roles":["viewer"]}
*/
}, {
key: "getPermissions",
value: function getPermissions(appId) {
return this.req.get(routes.devPermissions(appId));
}
}, {
key: "completeStripeConnection",
value: function completeStripeConnection(data) {
return this.req.post(routes.stripeConnectionAuth(), data);
}
}, {
key: "getStripeConnectToken",
value: function getStripeConnectToken() {
return this.req.get(routes.stripeConnectionToken());
}
}, {
key: "getStripeConnectAccountId",
value: function getStripeConnectAccountId() {
return this.req.get(routes.stripeConnect());
}
}, {
key: "setStripeConnectAccountId",
value: function setStripeConnectAccountId(data) {
return this.req.put(routes.stripeConnect(), data);
}
}, {
key: "get2FAState",
value: function get2FAState() {
return this.req.get(routes.twoFA());
}
}, {
key: "update2FAState",
value: function update2FAState(enabled) {
return this.req.put(routes.twoFA(), {
enabled: enabled
});
}
}, {
key: "changePassword",
value: function changePassword(id, password) {
return this.req.post(routes.changePassword(), {
id: id,
password: password
});
}
}]);
return User;
}(_baseService["default"]);
var _default = exports["default"] = function _default(req, context) {
return User.create(req, context);
};
function contextifyWithAuthToken(res, context) {
var authKey = (0, _cookie.cookieEnabled)() && (0, _cookie.getCookie)('dev-auth-key') || res.headers['auth-key'];
context.setAuthKey(authKey);
return _objectSpread(_objectSpread({}, res.body), {}, {
authKey: authKey
});
}