UNPKG

dpayjs

Version:

dPay.js the JavaScript API for dPay blockchain

109 lines (82 loc) 4.05 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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; }; }(); exports.jsonRpc = jsonRpc; var _crossFetch = require('cross-fetch'); var _crossFetch2 = _interopRequireDefault(_crossFetch); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var _base = require('./base'); var _base2 = _interopRequireDefault(_base); 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"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var debug = (0, _debug2.default)('dpay:http'); var RPCError = function (_Error) { _inherits(RPCError, _Error); function RPCError(rpcError) { _classCallCheck(this, RPCError); var _this = _possibleConstructorReturn(this, (RPCError.__proto__ || Object.getPrototypeOf(RPCError)).call(this, rpcError.message)); _this.name = 'RPCError'; _this.code = rpcError.code; _this.data = rpcError.data; return _this; } return RPCError; }(Error); function jsonRpc(uri, _ref) { var method = _ref.method, id = _ref.id, params = _ref.params; var payload = { id: id, jsonrpc: '2.0', method: method, params: params }; return (0, _crossFetch2.default)(uri, { body: JSON.stringify(payload), method: 'post', mode: 'cors', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' } }).then(function (res) { if (!res.ok) { throw new Error('HTTP ' + res.status + ': ' + res.statusText); } return res.json(); }).then(function (rpcRes) { if (rpcRes.id !== id) { throw new Error('Invalid response id: ' + rpcRes.id); } if (rpcRes.error) { throw new RPCError(rpcRes.error); } return rpcRes.result; }); } var HttpTransport = function (_Transport) { _inherits(HttpTransport, _Transport); function HttpTransport() { _classCallCheck(this, HttpTransport); return _possibleConstructorReturn(this, (HttpTransport.__proto__ || Object.getPrototypeOf(HttpTransport)).apply(this, arguments)); } _createClass(HttpTransport, [{ key: 'send', value: function send(api, data, callback) { if (this.options.useAppbaseApi) { api = 'condenser_api'; } debug('DPay::send', api, data); var id = data.id || this.id++; var params = [api, data.method, data.params]; jsonRpc(this.options.uri, { method: 'call', id: id, params: params }).then(function (res) { callback(null, res); }, function (err) { callback(err); }); } }]); return HttpTransport; }(_base2.default); exports.default = HttpTransport;