@glif/filecoin-rpc-client
Version:
a convenience library for interacting with the Lotus jsonrpc
90 lines • 4.13 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _regeneratorRuntime from "@babel/runtime/regenerator";
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) { _defineProperty(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; }
import axios from 'axios';
export var removeEmptyHeaders = function removeEmptyHeaders(headers) {
return Object.fromEntries(Object.entries(headers).filter(function (_ref) {
var _ref2 = _slicedToArray(_ref, 2),
_key = _ref2[0],
value = _ref2[1];
return !!value;
}));
};
export var getHeaders = function getHeaders(headers, token) {
return _objectSpread(_objectSpread({}, headers), {}, {
Accept: '*/*',
'Content-Type': 'application/json'
}, token ? {
Authorization: "Bearer ".concat(token)
} : {});
};
export var throwIfErrors = function throwIfErrors(data) {
if (data.error) {
if (data.error.message) throw new Error(data.error.message);
throw new Error("JSON-RPC error: ".concat(JSON.stringify(data.error)));
}
};
var LotusRpcEngine = /*#__PURE__*/function () {
function LotusRpcEngine(config) {
_classCallCheck(this, LotusRpcEngine);
_defineProperty(this, "apiAddress", void 0);
_defineProperty(this, "namespace", void 0);
_defineProperty(this, "delimeter", void 0);
_defineProperty(this, "axiosOpts", void 0);
if (!config) throw new Error('Must pass a config object to the LotusRpcEngine constructor.');
this.apiAddress = config.apiAddress;
this.namespace = config.namespace || 'Filecoin';
this.delimeter = config.delimeter || '.';
this.axiosOpts = config.axiosOpts || {};
this.axiosOpts.headers = getHeaders(this.axiosOpts.headers, config.token);
this.axiosOpts.headers = removeEmptyHeaders(this.axiosOpts.headers);
}
_createClass(LotusRpcEngine, [{
key: "request",
value: function () {
var _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(method) {
var _len,
params,
_key2,
_yield$axios$post,
data,
_args = arguments;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
for (_len = _args.length, params = new Array(_len > 1 ? _len - 1 : 0), _key2 = 1; _key2 < _len; _key2++) {
params[_key2 - 1] = _args[_key2];
}
_context.next = 3;
return axios.post(this.apiAddress, {
jsonrpc: '2.0',
method: "".concat(this.namespace).concat(this.delimeter).concat(method),
params: params,
id: 1
}, this.axiosOpts);
case 3:
_yield$axios$post = _context.sent;
data = _yield$axios$post.data;
throwIfErrors(data);
return _context.abrupt("return", data.result);
case 7:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function request(_x) {
return _request.apply(this, arguments);
}
return request;
}()
}]);
return LotusRpcEngine;
}();
export { LotusRpcEngine as default };
//# sourceMappingURL=index.js.map