UNPKG

@bigmi/core

Version:

TypeScript library for Bitcoin apps.

39 lines (38 loc) 1.82 kB
import type { RpcResponse } from '../types/rpc.js'; import type { MaybePromise } from '../types/utils.js'; export type RpcRequest = { jsonrpc?: '2.0'; method: string; params?: any; id?: number; }; export type HttpRequestReturnType<body extends RpcRequest | RpcRequest[] = RpcRequest> = body extends RpcRequest[] ? RpcResponse[] : RpcResponse; export type HttpRpcClientOptions = { /** Request configuration to pass to `fetch`. */ fetchOptions?: Omit<RequestInit, 'body'> | undefined; /** A callback to handle the request. */ onRequest?: ((request: Request, init: RequestInit) => MaybePromise<void | undefined | (RequestInit & { url?: string | undefined; })>) | undefined; /** A callback to handle the response. */ onResponse?: ((response: Response) => Promise<void> | void) | undefined; /** The timeout (in ms) for the request. */ timeout?: number | undefined; }; export type HttpRequestParameters<body extends RpcRequest | RpcRequest[] = RpcRequest> = { url?: string; /** The RPC request body. */ body?: body; /** Request configuration to pass to `fetch`. */ fetchOptions?: HttpRpcClientOptions['fetchOptions'] | undefined; /** A callback to handle the response. */ onRequest?: ((request: Request) => Promise<void> | void) | undefined; /** A callback to handle the response. */ onResponse?: ((response: Response) => Promise<void> | void) | undefined; /** The timeout (in ms) for the request. */ timeout?: HttpRpcClientOptions['timeout'] | undefined; }; export type HttpRpcClient = { request<body extends RpcRequest | RpcRequest[]>(params: HttpRequestParameters<body>): Promise<HttpRequestReturnType<body>>; }; export declare function getHttpRpcClient(url: string, options?: HttpRpcClientOptions): HttpRpcClient;