import {z} from 'zod' export type GetApiV1UsageMachinesStorageArgs = {period_start?: string | undefined, period_end?: string | undefined, machine_id?: string | undefined}; export type GetApiV1UsageMachinesStorageOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const machineStorageUsageRowBody = z.object({bucket_end: z.string(), bucket_start: z.string(), latest_meter_emitted_at: z.string().optional(), logical_storage_bytes: z.number().int(), machine_id: z.string(), org_metering_bucket_id: z.string(), storage_mib_seconds: z.number().int()}); export const machineStorageUsageBody = z.object({period_end: z.string(), period_start: z.string(), rows: z.array(machineStorageUsageRowBody).nullable()}); 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 }; /** * List machine storage usage breakdown * * GET /v1/usage/machines/storage * * Also accepts auth: BearerAuth */ export const getApiV1UsageMachinesStorage = async (args: GetApiV1UsageMachinesStorageArgs, options?: GetApiV1UsageMachinesStorageOptions) => { const {baseUrl = "https://dcs.dedaluslabs.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period_start != null) query.append("period_start", String(args.period_start)) if (args.period_end != null) query.append("period_end", String(args.period_end)) if (args.machine_id != null) query.append("machine_id", String(args.machine_id)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/usage/machines/storage${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(machineStorageUsageBody, await res.json()) };