@dioxide-js/silas
Version:
RPC utility for Silas
44 lines (41 loc) • 1.66 kB
JavaScript
import { __awaiter } from '../node_modules/tslib/tslib.es6.mjs';
import '../node_modules/axios/index.mjs';
import provider from './provider.mjs';
import { composeParams } from './rpc.mjs';
import axios from '../node_modules/axios/lib/axios.mjs';
const TIMEOUT = 30 * 1000;
class Fetcher {
get autherization() {
return 'Bearer ' + this.apiKey;
}
constructor(opts) {
this.apiKey = '';
this.prune = (url) => (url.endsWith('/') ? url.slice(0, -1) : url);
const { apiKey } = opts;
this.apiKey = apiKey !== null && apiKey !== void 0 ? apiKey : provider.apiKey;
}
postToBC(action_1, payload_1) {
return __awaiter(this, arguments, void 0, function* (action, payload, opts = {}) {
return this.post(action, payload, true, opts);
});
}
post(action_1) {
return __awaiter(this, arguments, void 0, function* (action, payload = {}, toBC = false, opts = {}) {
const { dioxide } = provider.get();
const host = this.prune(dioxide);
const data = composeParams(action, toBC, payload);
const options = Object.assign({ headers: {
'Content-Type': 'application/json',
Authorization: this.autherization,
}, timeout: TIMEOUT }, opts);
const resp = yield axios.post(host + '/api/jsonrpc/v1', data, options).then((r) => r.data);
const { error, result } = resp;
if (error) {
throw error.message;
}
return result;
});
}
}
export { Fetcher as default };
//# sourceMappingURL=request.mjs.map