import {z} from 'zod' 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 VolumeTemplateSpec = {defaultSize?: number | undefined, description?: string | undefined}; export type VolumeTemplateState = {lastVersionUploadedAt?: string | undefined, latestVersion?: string | undefined, status?: ('created' | 'ready' | 'error') | undefined, versionCount?: number | undefined}; export type VolumeTemplateVersion = {bucket?: string | undefined, contentSize?: number | undefined, name?: string | undefined, region?: string | undefined, status?: ('CREATED' | 'READY' | 'FAILED') | undefined, templateName?: string | undefined, versionId?: string | undefined, workspace?: string | undefined}; export type VolumeTemplate = {metadata: Metadata, spec: VolumeTemplateSpec, state?: VolumeTemplateState | undefined, versions?: Array | undefined}; export type CreateApiVolumeTemplatesArgs = {upload?: boolean | undefined, version?: string | undefined, body: VolumeTemplate}; export type CreateApiVolumeTemplatesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; 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 volumeTemplateSpec = z.object({defaultSize: z.number().int().optional(), description: z.string().optional()}); export const volumeTemplateState = z.object({lastVersionUploadedAt: z.string().optional(), latestVersion: z.string().optional(), status: z.enum(["created", "ready", "error"]).optional(), versionCount: z.number().int().optional()}); export const volumeTemplateVersion = z.object({bucket: z.string().optional(), contentSize: z.number().int().optional(), name: z.string().optional(), region: z.string().optional(), status: z.enum(["CREATED", "READY", "FAILED"]).optional(), templateName: z.string().optional(), versionId: z.string().optional(), workspace: z.string().optional()}); export const volumeTemplate = z.object({metadata: metadata, spec: volumeTemplateSpec, state: volumeTemplateState.optional(), versions: z.array(volumeTemplateVersion).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 volume template * * POST /volume_templates * * Also accepts auth: ApiKeyAuth */ export const createApiVolumeTemplates = async (args: CreateApiVolumeTemplatesArgs, options?: CreateApiVolumeTemplatesOptions) => { 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 (args.upload != null) query.append("upload", String(args.upload)) if (args.version != null) query.append("version", String(args.version)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/volume_templates${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(volumeTemplate, await res.json()) };