UNPKG

@pgchain/blockchain-libs

Version:
98 lines (97 loc) 4.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonRPCRequest = void 0; const cross_fetch_1 = __importDefault(require("cross-fetch")); const timeout_signal_1 = __importDefault(require("timeout-signal")); const exceptions_1 = require("./exceptions"); class JsonRPCRequest { constructor(url, headers, timeout = 10000) { this.url = url; this.timeout = timeout; this.headers = { 'Content-Type': 'application/json', }; if (headers) { Object.assign(this.headers, headers); } } static async parseRPCResponse(response) { if (typeof response !== 'object') { throw new exceptions_1.ResponseError('Invalid JSON RPC response, typeof response should be an object', response); } else if (response.error) { throw new exceptions_1.JsonPRCResponseError('Error JSON PRC response', response); } else if (!('result' in response)) { throw new exceptions_1.ResponseError('Invalid JSON RPC response, result not found', response); } return response.result; } static normalizePayload(method, params, id = 0) { const payload = { jsonrpc: '2.0', id, method, }; if (typeof payload !== 'undefined') { payload.params = params; } return payload; } async call(method, params, headers, timeout) { headers = this.assembleHeaders(headers); const payload = JsonRPCRequest.normalizePayload(method, params); const response = await (0, cross_fetch_1.default)(this.url, { headers, method: 'POST', body: JSON.stringify(payload), signal: (0, timeout_signal_1.default)(timeout || this.timeout), }); if (!response.ok) { throw new exceptions_1.ResponseError(`Wrong response<${response.status}>`, response); } const jsonResponse = await response.json(); return JsonRPCRequest.parseRPCResponse(jsonResponse); } async batchCall(calls, headers, timeout, ignoreSoloError = true) { console.log('calls:', calls) headers = this.assembleHeaders(headers); const payload = calls.map(([method, params], index) => JsonRPCRequest.normalizePayload(method, params, index)); console.log(payload) const response = await (0, cross_fetch_1.default)(this.url, { headers, method: 'POST', body: JSON.stringify(payload), signal: (0, timeout_signal_1.default)(timeout || this.timeout), }); console.log(response) if (!response.ok) { throw new exceptions_1.ResponseError(`Wrong response<${response.status}>`, response); } const jsonResponses = await response.json(); if (!Array.isArray(jsonResponses)) { throw new exceptions_1.ResponseError('Invalid JSON Batch RPC response, response should be an array', response); } else if (calls.length !== jsonResponses.length) { throw new exceptions_1.ResponseError(`Invalid JSON Batch RPC response, batch with ${calls.length} calls, but got ${jsonResponses.length} responses`, response); } // @ts-ignore return Promise.all(jsonResponses.map((response) => JsonRPCRequest.parseRPCResponse(response).catch((e) => { if (e instanceof exceptions_1.JsonPRCResponseError && ignoreSoloError) { return undefined; } else { throw e; } }))); } assembleHeaders(headers) { headers = headers || {}; return Object.assign(Object.assign({}, this.headers), headers); } } exports.JsonRPCRequest = JsonRPCRequest; //# sourceMappingURL=json-rpc.js.map