import {z} from 'zod' export type EnvVar = {key: string, value: string}; export type HealthCheckType = 'http' | 'tcp' | 'exec'; export type HealthCheck = {intervalSecs?: number | undefined, path?: (string | null) | undefined, port?: (number | null) | undefined, retries?: number | undefined, timeoutSecs?: number | undefined, type: HealthCheckType}; export type DeployStrategy = {batchSize?: number | undefined, type: 'rolling', useHealthCheck?: boolean | undefined, waitSeconds?: number | undefined} | {autoPromote?: boolean | undefined, canaryCount?: number | undefined, type: 'canary'} | {type: 'blueGreen'}; export type VolumeMount = {mountPath: string, name: string, sizeGb?: number | undefined}; export type DeployRequest = {coLocateWith?: (string | null) | undefined, cpus?: number | undefined, domain?: (string | null) | undefined, env?: Array | undefined, health?: (HealthCheck | null) | undefined, image: string, memoryMb?: number | undefined, name: string, network?: boolean | undefined, port?: number | undefined, publicIngress?: boolean | undefined, stopGraceSecs?: number | undefined, strategy?: DeployStrategy | undefined, volume?: (VolumeMount | null) | undefined}; export type CreateApiV1GroupsArgs = {body: DeployRequest}; export type CreateApiV1GroupsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const instanceInfo = z.object({generation: z.number().int().gte(0), hostPort: z.number().int().gte(0).nullable().optional(), id: z.string(), nodeId: z.string(), status: z.string()}); export const groupInfo = z.object({createdAt: z.string(), domain: z.string().nullable().optional(), id: z.string(), image: z.string(), instances: z.array(instanceInfo), name: z.string(), port: z.number().int().gte(0), replicas: z.number().int().gte(0), status: z.string(), updatedAt: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create or update a group deployment. * * POST /v1/groups */ export const createApiV1Groups = async (args: CreateApiV1GroupsArgs, options?: CreateApiV1GroupsOptions) => { const {baseUrl = "https://api.smolmachines.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set('authorization', `Bearer ${ApiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/groups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(groupInfo, await res.json()) };