UNPKG

@arkts/headless-jsonrpc

Version:

Simple and Fast headless JSON-RPC communication libraries support advanced features such as retry and timeout.

122 lines 4.38 kB
//#region src/typings/jsonrpc.d.ts declare namespace JSONRPC { interface Dictionary<T = unknown> { [key: string]: T; } interface Request<TParams extends unknown[] | Dictionary<unknown> = unknown[] | Dictionary<unknown>> { jsonrpc: "2.0" | (string & {}); id: string | number | null; method: string; params?: TParams; } interface SuccessResponse<R = unknown> { jsonrpc: "2.0" | (string & {}); id: string | number | null; result: R; } interface ErrorObject<D = unknown> { /** * Error code. * * - `-32700`: Parse error * - `-32600`: Invalid Request * - `-32601`: Method not found * - `-32602`: Invalid params * - `-32603`: Internal error * - `Other`: Custom error codes */ code: -32700 | -32600 | -32601 | -32602 | -32603 | (number & {}); message: string; data?: D; } interface ErrorResponse<E = unknown> { jsonrpc: "2.0" | (string & {}); id: string | number | null; error: ErrorObject<E>; } type Response<R = unknown, E = unknown> = SuccessResponse<R> | ErrorResponse<E>; type Event = Request | Response; } //#endregion //#region src/typings/utils.d.ts type Arrayable<T> = T | T[]; type Awaitable<T> = T | Promise<T>; //#endregion //#region src/connection.d.ts interface NotificationOptions<TParams extends unknown[] | Record<string, unknown> = unknown[] | Record<string, unknown>> extends Pick<JSONRPC.Request<TParams>, "method" | "params"> { jsonrpc?: JSONRPC.Request["jsonrpc"]; id?: JSONRPC.Request["id"]; } interface SendRequestOptions<TParams extends unknown[] | Record<string, unknown> = unknown[] | Record<string, unknown>> extends Omit<NotificationOptions<TParams>, "id"> { id: Exclude<JSONRPC.Request["id"], null>; } interface ConnectionAdapter { /** * How to send a event. * * This method is expected to return a promise that resolves when the event is sent. * * @param request */ sendEvent(request: Arrayable<JSONRPC.Event>): Awaitable<void>; /** * Callback to handle responses. * * This method is expected to be called when a response is received. * * @param callback */ onEvent(callback: (response: Arrayable<JSONRPC.Event>) => void): Awaitable<void>; } interface TimeoutOptions { /** * Timeout in milliseconds for the request. * If not provided, defaults to 5000ms (5 seconds). */ timeout?: number; } interface ConnectionOptions<TFunctions extends JSONRPC.Dictionary<(...args: unknown[]) => unknown> = JSONRPC.Dictionary<(...args: unknown[]) => unknown>> extends TimeoutOptions { /** * The adapter to use for sending requests and handling responses. */ adapter: ConnectionAdapter; /** * Call functions when the request is sent. */ functions?: TFunctions; } interface Connection { /** * Sends a notification. * * @param notification - The notification to send. */ sendNotification<TParams extends unknown[] | JSONRPC.Dictionary<unknown>>(notification: Arrayable<NotificationOptions<TParams>>, options?: TimeoutOptions | number): Promise<void>; /** * Sends a request and waits for a response. * * @param request - The request to send. * @returns A promise that resolves with the response. */ sendRequest<TParams extends unknown[] | JSONRPC.Dictionary<unknown>, R = unknown, E = unknown, TRequest extends Arrayable<SendRequestOptions<TParams>> = Arrayable<SendRequestOptions<TParams>>>(request: TRequest, options?: TimeoutOptions | number): Promise<TRequest extends any[] ? JSONRPC.Response<R, E>[] : JSONRPC.Response<R, E>>; /** * Callback to handle responses. * * This method is expected to be called when a response is received. * * @param callback */ onResponse(callback: (response: Arrayable<JSONRPC.Response>) => Awaitable<unknown>): Awaitable<void>; /** * Starts listening for requests and handles them. */ listen(): Promise<void>; /** * Get current underlying adapter. */ getAdapter(): ConnectionAdapter; } declare function createConnection<TFunctions extends JSONRPC.Dictionary<(...args: unknown[]) => unknown> = JSONRPC.Dictionary<(...args: unknown[]) => unknown>>(options: ConnectionOptions): Connection; //#endregion export { Arrayable, Connection, ConnectionAdapter, ConnectionOptions, JSONRPC, NotificationOptions, SendRequestOptions, TimeoutOptions, createConnection }; //# sourceMappingURL=connection-DMjPZGJA.d.ts.map