lib/jsonrpc.js

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.post = post;
exports.postObject = postObject;
var _axios = _interopRequireDefault(require("./axios"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
 * Sends the jsonrpc 'params' as an array.
 */
async function post(url, method, ...params) {
  if (params && params.length < 1) {
    params = null;
  }
  const response = await _axios.default.post(url, {
    jsonrpc: '2.0',
    // TODO: Generate a unique request id
    id: 1,
    method,
    params
  });
  if (hasOwnProperty(response.data, 'error')) {
    throw response.data.error;
  } else {
    var _response$data;
    return (_response$data = response.data) === null || _response$data === void 0 ? void 0 : _response$data.result;
  }
}

/**
 * Sends the jsonrpc 'params' as the single 'param' obj, no array wrapper is applied.
 */
async function postObject(url, method, param) {
  const response = await _axios.default.post(url, {
    jsonrpc: '2.0',
    // TODO: Generate a unique request id
    id: 1,
    method,
    params: param
  });
  if (hasOwnProperty(response.data, 'error')) {
    throw response.data.error;
  } else {
    var _response$data2;
    return (_response$data2 = response.data) === null || _response$data2 === void 0 ? void 0 : _response$data2.result;
  }
}

// Check if the given object X has a field Y, and make that available to
// typescript typing.
function hasOwnProperty(obj, prop) {
  return obj.hasOwnProperty(prop);
}