31 lines
778 B
TypeScript

import { invoke } from "$lib/runtime/invoke";
import type { BackendCommand, BackendResponse } from "./types";
function newCorrelationId(): string {
return `ui-${Date.now()}-${Math.random().toString(16).slice(2)}`;
}
type SendCommandOptions = {
keepalive?: boolean;
};
export async function sendCommand<T>(
command: BackendCommand,
options: SendCommandOptions = {},
): Promise<T> {
const envelope: BackendCommand = {
...command,
correlationId: command.correlationId ?? newCorrelationId(),
};
const response = await invoke<BackendResponse<T>>("sidecar_command", {
command: envelope,
keepalive: options.keepalive === true,
});
if (!response.ok) {
throw new Error(response.error || "Backend command failed");
}
return response.data;
}