UNPKG

cosmic-lib

Version:

A JavaScript implementation of the CosmicLink protocol for Stellar

132 lines (101 loc) 4.71 kB
"use strict"; var _classCallCheck = require("@babel/runtime/helpers/classCallCheck"); var _inherits = require("@babel/runtime/helpers/inherits"); var _possibleConstructorReturn = require("@babel/runtime/helpers/possibleConstructorReturn"); var _getPrototypeOf = require("@babel/runtime/helpers/getPrototypeOf"); 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } var env = require("@cosmic-plus/jsutils/es5/env"); var misc = require("@cosmic-plus/jsutils/es5/misc"); if (env.isBrowser) require("@cosmic-plus/domutils/es5/polyfill"); /** * Automatically pass `config` to `module` functions as first argument. * * @param {Object} module A cosmic-lib module whose functions takes config as * the first argument. * @param {Object} config A configuration object similar than config.js module. * @private */ function exposeModule(module) { var config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var layer = Object.assign({}, module); misc.setHiddenProperty(layer, "_config", config); var _loop = function _loop(name) { if (typeof module[name] === "function") { layer[name] = function () { for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) { params[_key] = arguments[_key]; } return module[name].apply(module, [this._config].concat(params)); }; } else { layer[name] = module[name]; } }; for (var name in module) { _loop(name); } return layer; } /** * Create a clone of the library with an alternative config object derived from * **params**. Fields which are not defined in **params** stay in sync with parent * configuration. * * @example * const testnet = cosmicLib.withConfig({ network: 'test' }) * const account = await testnet.resolve.account('tips*cosmic.link') * * @params {Object} params Alternative configuration * @return {Object} A clone of cosmicLib */ exports.withConfig = function (params) { var library = Object.create(this); library.config = Object.assign({}, this.config, params); for (var module in this) { if (this[module].prototype && this[module].prototype.__proto__ === this.config) { library[module] = /*#__PURE__*/function (_this$module) { _inherits(_class, _this$module); var _super = _createSuper(_class); function _class() { _classCallCheck(this, _class); return _super.apply(this, arguments); } return _class; }(this[module]); Object.assign(library[module].prototype, this[module].prototype); library[module].prototype.__proto__ = library.config; } else if (this[module]._config) { library[module] = Object.create(this[module]); misc.setHiddenProperty(library[module], "_config", library.config); } } return library; }; /** * Export modules **names** after linking them to `config`. * * @param {...String} names Module names */ function exportModule(name, module) { exports[name] = exposeModule(module, config); } // Export modules. var config = exports.config = require("./config"); exports.CosmicLink = require("./cosmiclink"); if (env.isBrowser) exports.load = require("./load"); exportModule("resolve", require("./resolve")); exportModule("signersUtils", require("./signers-utils")); exports.specs = require("./specs"); // sep7Utils var _require = require("./sep7-utils"), isWebHandlerSupported = _require.isWebHandlerSupported, registerWebHandler = _require.registerWebHandler; exports.sep7Utils = { isWebHandlerSupported: isWebHandlerSupported, registerWebHandler: registerWebHandler }; // Export helpers (not documented in the manual - please check the code). exportModule("check", require("./check")); exportModule("expand", require("./expand")); exportModule("construct", require("./construct")); exportModule("destruct", require("./destruct")); exportModule("encode", require("./encode")); exportModule("decode", require("./decode"));