@budarin/json-rpc-interface

1.0.4 • Public • Published

json-rpc-interface

Package with JSON RPC interface in typescript.

Installation

yarn add @budarin/json-rpc-interface

Usage

import { ErrorOrResult } from '@budarin/json-rpc-interface';

function tryToDivide(a: number, b: number): ResultOrError<number> {
    try {
        return { result: a / b };
    } catch (e) {
        return {
            error: e instanceof Error ? e.message : 'Unexpected error',
            stack: e.stack,
        };
    }
}

somwhere in code

const divided = tryToDivide(1, 0);

if (divided.error) {
    console.error(divided.error);
    return;
}

console.log(divided.result);

Type definition

type JsonRpcError<U> = {
    message: string;
    data?: U;
    stack?: string;
};

type ResultOrError<T, E = any> =
    | {
          result: DeepReadonly<T>;
          error?: never;
      }
    | {
          result?: never;
          error: DeepReadonly<JsonRpcError<E>>;
      };

License

MIT

Package Sidebar

Install

npm i @budarin/json-rpc-interface

Weekly Downloads

0

Version

1.0.4

License

MIT

Unpacked Size

4.06 kB

Total Files

4

Last publish

Collaborators

  • budarin