UNPKG

@bigmi/core

Version:

TypeScript library for Bitcoin apps.

59 lines 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.utxo = utxo; const request_js_1 = require("../errors/request.js"); const transport_js_1 = require("../errors/transport.js"); const createTransport_js_1 = require("../factories/createTransport.js"); const getHttpRpcClient_js_1 = require("./getHttpRpcClient.js"); const getRpcProviderMethods_js_1 = require("./getRpcProviderMethods.js"); function utxo(url, config = {}) { const { fetchOptions, key = 'utxo', name = 'UTXO HTTP API', onFetchRequest, onFetchResponse, retryDelay, apiKey, methods, } = config; return ({ chain, retryCount: retryCount_, timeout: timeout_ }) => { const retryCount = config.retryCount ?? retryCount_; const timeout = timeout_ ?? config.timeout ?? 10000; let url_ = url; if (config.includeChainToURL) { const chainName = chain?.name.replace(' ', '-').toLowerCase(); url_ = `${url}${chainName ? `/${chainName}` : ''}`; } if (!url_) { throw new transport_js_1.UrlRequiredError(); } const client = (0, getHttpRpcClient_js_1.getHttpRpcClient)(url_, { fetchOptions, onRequest: onFetchRequest, onResponse: onFetchResponse, timeout, }); const rpcMethods = (0, getRpcProviderMethods_js_1.getRpcProviderMethods)(key); return (0, createTransport_js_1.createTransport)({ key, name, methods, async request({ method, params }) { const body = { method, params }; const methodHandler = rpcMethods?.[method]; const { error, result } = await (methodHandler?.(client, { baseUrl: url_, apiKey }, params) ?? client.request({ body, })); if (error) { throw new request_js_1.RpcRequestError({ body, error, url: url_, }); } return result; }, retryCount, retryDelay, timeout, type: 'http', }, { fetchOptions, url: url_, }); }; } //# sourceMappingURL=utxo.js.map