import {z} from 'zod' export type CoreEvent = {canaryRevision?: string | undefined, message?: string | undefined, revision?: string | undefined, status?: string | undefined, time?: string | undefined, type?: string | undefined}; export type CoreEvents = Array; export type MetadataLabels = {[key: string]: string}; export type Metadata = {createdAt?: string | undefined, updatedAt?: string | undefined, createdBy?: string | undefined, updatedBy?: string | undefined, displayName?: string | undefined, externalId?: string | undefined, labels?: MetadataLabels | undefined, name: string, plan?: string | undefined, url?: string | undefined, workspace?: string | undefined}; export type DrivePermission = {labels?: Record | undefined, mode?: ('read' | 'read-write') | undefined, path?: string | undefined}; export type DriveSpec = {infrastructureId?: string | undefined, permissions?: Array | undefined, region?: string | undefined, size?: number | undefined}; export type DriveState = {s3Url?: string | undefined}; export type Drive = {events?: CoreEvents | undefined, metadata: Metadata, spec: DriveSpec, state?: DriveState | undefined, status?: string | undefined}; export type CreateApiDrivesArgs = {body: Drive}; export type CreateApiDrivesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const coreEvent = z.object({canaryRevision: z.string().optional(), message: z.string().optional(), revision: z.string().optional(), status: z.string().optional(), time: z.string().optional(), type: z.string().optional()}); export const coreEvents = z.array(coreEvent); export const metadataLabels = z.record(z.string(), z.string()); export const metadata = z.object({createdAt: z.string().optional(), updatedAt: z.string().optional(), createdBy: z.string().optional(), updatedBy: z.string().optional(), displayName: z.string().optional(), externalId: z.string().optional(), labels: metadataLabels.optional(), name: z.string(), plan: z.string().optional(), url: z.string().optional(), workspace: z.string().optional()}); export const drivePermission = z.object({labels: z.object({}).optional(), mode: z.enum(["read", "read-write"]).optional(), path: z.string().optional()}); export const driveSpec = z.object({infrastructureId: z.string().optional(), permissions: z.array(drivePermission).optional(), region: z.string().optional(), size: z.number().int().optional()}); export const driveState = z.object({s3Url: z.string().optional()}); export const drive = z.object({events: coreEvents.optional(), metadata: metadata, spec: driveSpec, state: driveState.optional(), status: 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 a drive * * POST /drives * * Also accepts auth: ApiKeyAuth */ export const createApiDrives = async (args: CreateApiDrivesArgs, options?: CreateApiDrivesOptions) => { const {baseUrl = "https://api.blaxel.ai/v0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/drives${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(drive, await res.json()) };