import {z} from 'zod' export type GetApiV1BillingActiveOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const billingActiveItem = z.object({machine_id: z.string(), display_name: z.string(), status: z.enum(["running", "starting", "stopping", "restarting", "stopped", "suspended"]), os_type: z.enum(["linux", "windows"]), rate_cents_per_hour: z.number().int(), running_credits_per_hour: z.number().int(), stopped_credits_per_hour: z.number().int(), accrued_cents: z.number().int(), since: z.string().nullable().optional(), total_credits_billed: z.number().int(), suspended_for_billing: z.boolean(), auto_destroy_at: z.string().nullable().optional(), ttl_minutes: z.number().int().nullable().optional()}); export const billingActiveResponse = z.object({active: z.array(billingActiveItem), current_run_rate_cents_per_hour: z.number().int(), request_id: 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 }; /** * What is billing me right now * * GET /v1/billing/active * * Also accepts auth: bearerAuth */ export const getApiV1BillingActive = async (options?: GetApiV1BillingActiveOptions) => { const {baseUrl = "https://coasty.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("X-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/billing/active${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(billingActiveResponse, await res.json()) };