1.0.4 • Published 5 months ago

@budarin/json-rpc-interface v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago