RPC Services

WebHare supports setting up RPC services for easy front- and backend integration. RPC Services are fully type checked and support all types supported by @webhare/std's typed stringify (eg. Money, Date, many Temporals) and provide cross-call stack tracing while still providing readable JSON request/responses for when you can't use WebHare libraries.

Starting with WebHare 5.7 we recommend using RPC services over JSON/RPC for building 'private' communication APIs between frontend and backend if the service side is implemented in TypeScript.

Server side

Your moduledefinition.yml shall declare

rpcServices:
  myapi:
    api: js/rpcservice.ts#myAPI

The server side is implemented as a class:

import type { RPCContext } from "@webhare/router";

export const myAPI = {
  async validateEmail(context: RPCContext, emailAddress: string): Promise<boolean> {
    return emailAddress.match(/webhare.dev$/));
  }
};

Client side

import { rpc } from "@webhare/rpc";

const emailValid = await rpc("mymodule:myapi").validateEmail("user@webhare.dev");

Use the GetRPCClientInterface type if you need to convert any object to its RPC interface type.