UNPKG

belvo

Version:
283 lines (236 loc) 11.8 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _resources = _interopRequireDefault(require("./resources")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; } var id = 0; function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; } var _endpoint = /*#__PURE__*/_classPrivateFieldLooseKey("endpoint"); /** * A Link is a set of credentials associated to a end-user access to an Institution. * @extends Resource * */ var Link = /*#__PURE__*/function (_Resource) { _inherits(Link, _Resource); var _super = _createSuper(Link); function Link() { var _this; _classCallCheck(this, Link); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); Object.defineProperty(_assertThisInitialized(_this), _endpoint, { writable: true, value: 'api/links/' }); return _this; } _createClass(Link, [{ key: "register", value: /** * Register a new link. * @async * @param {string} institution - Institution's code. * @param {string} username - Username used to sign in online by the end-user. * @param {string} password - Password used to sign in online by the end-user. * @param {object} options - Optional parameters * (token, usernameType, username2, username3, password2, accessMode, externalId). * @returns {object} Newly created link. * @throws {RequestError} */ function () { var _register = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(institution, username, password) { var options, token, usernameType, username2, username3, password2, accessMode, externalId, result, _args = arguments; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: options = _args.length > 3 && _args[3] !== undefined ? _args[3] : {}; token = options.token, usernameType = options.usernameType, username2 = options.username2, username3 = options.username3, password2 = options.password2, accessMode = options.accessMode, externalId = options.externalId; _context.next = 4; return this.session.post(_classPrivateFieldLooseBase(this, _endpoint)[_endpoint], { institution: institution, username: username, username2: username2, username3: username3, password: password, password2: password2, token: token, access_mode: accessMode, username_type: usernameType, external_id: externalId }); case 4: result = _context.sent; return _context.abrupt("return", result); case 6: case "end": return _context.stop(); } } }, _callee, this); })); function register(_x, _x2, _x3) { return _register.apply(this, arguments); } return register; }() /** * Update link's password and password2. * Use this function when you want to update the link credentials. * Only updating passwords is available. * @async * @param {string} id - UUID4 representation of the link Id. * @param {string} password - New password. * @param {object} options - Optional parameters * (token, password2, usernameType). * @returns {object} Response * @throws {RequestError} */ }, { key: "update", value: function () { var _update = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(id) { var options, token, password, password2, usernameType, result, _args2 = arguments; return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: options = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {}; token = options.token, password = options.password, password2 = options.password2, usernameType = options.usernameType; _context2.next = 4; return this.session.put(_classPrivateFieldLooseBase(this, _endpoint)[_endpoint], id, { password: password, password2: password2, token: token, username_type: usernameType }); case 4: result = _context2.sent; return _context2.abrupt("return", result); case 6: case "end": return _context2.stop(); } } }, _callee2, this); })); function update(_x4) { return _update.apply(this, arguments); } return update; }() /** * Request scoped tokens to operate with a single link * @async * @param {string} id - UUID4 representation of the link Id. * @param {string} scopes - List of comma separated scopes * @returns {object} Response * @throws {RequestError} */ }, { key: "token", value: function () { var _token = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(id, scopes) { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: return _context3.abrupt("return", this.session.post('api/token/', { link_id: id, scopes: scopes })); case 1: case "end": return _context3.stop(); } } }, _callee3, this); })); function token(_x5, _x6) { return _token.apply(this, arguments); } return token; }() /** * Patch an existing link. * @async * @param {string} id - UUID4 representation of the link Id. * @param {object} options - Optional parameters * (accessMode). * @returns {object} Link data updated. * @throws {RequestError} */ }, { key: "patch", value: function () { var _patch = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(id) { var options, accessMode, result, _args4 = arguments; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: options = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {}; accessMode = options.accessMode; _context4.next = 4; return this.session.patch("".concat(_classPrivateFieldLooseBase(this, _endpoint)[_endpoint]).concat(id, "/"), { access_mode: accessMode }); case 4: result = _context4.sent; return _context4.abrupt("return", result); case 6: case "end": return _context4.stop(); } } }, _callee4, this); })); function patch(_x7) { return _patch.apply(this, arguments); } return patch; }() }]); return Link; }(_resources["default"]); Link.SINGLE = 'single'; Link.RECURRENT = 'recurrent'; var _default = Link; exports["default"] = _default;