@glif/filecoin-rpc-client
Version:
a convenience library for interacting with the Lotus jsonrpc
45 lines • 2.34 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
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 const removeEmptyHeaders = headers => Object.fromEntries(Object.entries(headers).filter(([_key, value]) => !!value));
export const getHeaders = (headers, token) => _objectSpread(_objectSpread({}, headers), {}, {
Accept: '*/*',
'Content-Type': 'application/json'
}, token ? {
Authorization: `Bearer ${token}`
} : {});
export const throwIfErrors = data => {
if (data.error) {
if (data.error.message) throw new Error(data.error.message);
throw new Error(`JSON-RPC error: ${JSON.stringify(data.error)}`);
}
};
export default class LotusRpcEngine {
constructor(config) {
_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);
}
async request(method, ...params) {
const {
data
} = await axios.post(this.apiAddress, {
jsonrpc: '2.0',
method: `${this.namespace}${this.delimeter}${method}`,
params,
id: 1
}, this.axiosOpts);
throwIfErrors(data);
return data.result;
}
}
//# sourceMappingURL=index.js.map