Files
masonry/cmd/cli/templates/vue/fetch-rpc.ts.tmpl

27 lines
817 B
Cheetah

export interface Rpc {
request(service: string, method: string, data: Uint8Array): Promise<Uint8Array>;
}
export class FetchRpc implements Rpc {
constructor(private baseUrl: string) {}
async request(service: string, method: string, data: Uint8Array): Promise<Uint8Array> {
const url = `${this.baseUrl}/${service}/${method}`;
// Optionally, convert or wrap data as needed by your grpc-gateway.
const jsonBody = JSON.stringify({ message: Array.from(data) });
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: jsonBody,
});
if (!response.ok) {
throw new Error(`HTTP error: ${response.status}`);
}
const responseJson = await response.json();
return new Uint8Array(responseJson.message);
}
}