get frontend code generating and usable
This commit is contained in:
26
cmd/cli/templates/vue/fetch-rpc.ts.tmpl
Normal file
26
cmd/cli/templates/vue/fetch-rpc.ts.tmpl
Normal file
@ -0,0 +1,26 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user