Setting up webservices
WebHare supports setting up JSON/RPC 1.0 services and having the call signatures validated using TypeScript.
We generally recommend using JSON/RPC over OpenAPI for building 'private' communication APIs between frontend and backend especially if both are hosted on the same WebHare server as OpenAPI requires you to explicitly define the API whereas JSON/RPC makes this implicit
Server side
A moduledefinition shall declare
<services>
<webservice name="servicename" transports="jsonrpc" service="myservice.ts#ServiceObject">
<accesscheck />
</webservice>
</services>
The server side is implemented as a class:
import { WebRequest } from "@webhare/router";
export class ServiceObject {
constructor(private req: WebRequest) {
}
async myFunction(param1: string, param2: number): Promise<boolean> {
}
}
Client side
Assuming you're using TypeScript and can reach the implementation using an import, you can use this to automatically derive the JSON/RPC service:
import { createClient } from "@webhare/jsonrpc-client";
import { type ServiceObject } from "@mod-mymodule/myservice";
const client = createClient<ServiceObject>("moduleservice:servicename");
console.log(await client.myFunction(param1, param2));
Use the GetClientInterface
type if you need to convert any class/interfacee to an async JSON/RPC interface.
See also the @webhare/jsonrpc
documentation for more details