@bigmi/core
Version:
TypeScript library for Bitcoin apps.
75 lines • 2.82 kB
JavaScript
import { RpcRequestError } from '../errors/request.js';
import { UrlRequiredError, } from '../errors/transport.js';
import { createTransport } from '../factories/createTransport.js';
import { createBatchScheduler } from '../utils/createBatchScheduler.js';
import { getHttpRpcClient, } from './getHttpRpcClient.js';
import { UTXOAPISchemaMethods } from './types.js';
/**
* @description Creates a HTTP transport that connects to a JSON-RPC API.
*/
export function http(
/** URL of the JSON-RPC API. Defaults to the chain's public RPC URL. */
url, config = {}) {
const { batch, fetchOptions, key = 'http', methods = { exclude: UTXOAPISchemaMethods }, name = 'HTTP JSON-RPC', onFetchRequest, onFetchResponse, retryDelay, raw, } = config;
return ({ chain, retryCount: retryCount_, timeout: timeout_ }) => {
const { batchSize = 1000, wait = 0 } = typeof batch === 'object' ? batch : {};
const retryCount = config.retryCount ?? retryCount_;
const timeout = timeout_ ?? config.timeout ?? 10000;
const url_ = url || chain?.rpcUrls.default.http[0];
if (!url_) {
throw new UrlRequiredError();
}
const rpcClient = getHttpRpcClient(url_, {
fetchOptions,
onRequest: onFetchRequest,
onResponse: onFetchResponse,
timeout,
});
return createTransport({
key,
methods,
name,
async request({ method, params }) {
const body = { method, params };
const { schedule } = createBatchScheduler({
id: url_,
wait,
shouldSplitBatch(requests) {
return requests.length > batchSize;
},
fn: (body) => rpcClient.request({
body,
}),
sort: (a, b) => a.id - b.id,
});
const fn = async (body) => batch
? schedule(body)
: [
await rpcClient.request({
body,
}),
];
const [{ error, result }] = await fn(body);
if (raw) {
return { error, result };
}
if (error) {
throw new RpcRequestError({
body,
error,
url: url_,
});
}
return result;
},
retryCount,
retryDelay,
timeout,
type: 'http',
}, {
fetchOptions,
url: url_,
});
};
}
//# sourceMappingURL=http.js.map