import {z} from 'zod' export type MachineIDPathSegment = string; export type CreatePortRequest = {port: number, protocol?: ('http' | 'https') | undefined}; export type CreateApiV1MachinesMachineIdPortsArgs = {'X-Dedalus-Org-Id'?: string | undefined, machine_id: MachineIDPathSegment, 'Idempotency-Key': string, body: CreatePortRequest}; export type CreateApiV1MachinesMachineIdPortsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const portResponse = z.object({created_at: z.string(), error_code: z.string().optional(), error_message: z.string().optional(), expires_at: z.string().optional(), machine_id: z.string(), port: z.number().int(), port_id: z.string(), protocol: z.enum(["http", "https"]).optional(), ready_at: z.string().optional(), retry_after_ms: z.number().int().optional(), status: z.enum(["wake_in_progress", "ready", "closed", "expired", "failed"]), url: z.string().optional()}); 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 port * * POST /v1/machines/{machine_id}/ports * * Also accepts auth: BearerAuth */ export const createApiV1MachinesMachineIdPorts = async (args: CreateApiV1MachinesMachineIdPortsArgs, options?: CreateApiV1MachinesMachineIdPortsOptions) => { const {baseUrl = "https://dcs.dedaluslabs.ai", 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 (args['X-Dedalus-Org-Id'] != null) headers.set("X-Dedalus-Org-Id", String(args['X-Dedalus-Org-Id'])) if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/machines/${encodeURIComponent(String(args.machine_id))}/ports${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(portResponse, await res.json()) };