Files

12 lines
928 B
TypeScript

import { containersStateSchema, containerMutationAckSchema } from "@mmapp/contracts/containers"
import { requestJson } from "./http-client"
export async function readContainersState(baseUrl: string, options?: RequestInit) {
return containersStateSchema.parse(await requestJson<unknown>(baseUrl, "/api/containers/state", options))
}
export type ContainerAction = "start" | "stop" | "restart" | "remove"
export async function mutateContainer(baseUrl: string, serverId: string, rosId: string, action: ContainerAction, options?: RequestInit) {
if (!/^[1-9]\d*$/.test(serverId) || !Number.isSafeInteger(Number(serverId)) || !rosId.trim()) throw new Error("Неверный контекст контейнера")
return containerMutationAckSchema.parse(await requestJson<unknown>(baseUrl, `/api/servers/${encodeURIComponent(serverId)}/containers/${action}`, { ...options, method: "POST", body: JSON.stringify({ rosId }) }))
}