import {z} from 'zod' export type UaasVolumeNdsDto = {nds?: (boolean | null) | undefined, nds_excluded_paths?: (string | null) | undefined, nds_included_paths?: (string | null) | undefined}; export type CreateApiUaasStacksStackIdVolumesVolumeIdArgs = {stack_id: string, volume_id: string, body: UaasVolumeNdsDto}; export type CreateApiUaasStacksStackIdVolumesVolumeIdOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const taskExecutor = z.literal("edge"); export const taskInitiator = z.enum(["portal", "edge"]); export const taskState = z.enum(["in_progress", "completed", "failed"]); export const taskStepState = z.enum(["pending", "in_progress", "completed", "failed"]); export const taskStepDto = z.object({name: z.string(), state: taskStepState, reason: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const taskDto = z.object({id: z.string(), created_at: z.string(), resource: z.string(), resource_id: z.union([z.string(), z.string()]).nullable().optional(), stack_id: z.string().nullable().optional(), action: z.string(), executor: taskExecutor, executor_id: z.string().nullable().optional(), initiated_by: taskInitiator, initiator_id: z.string().nullable().optional(), activity_log_id: z.string().nullable().optional(), reason: z.string().nullable().optional(), state: taskState, last_updated_at: z.string(), user_acknowledged_at: z.string().nullable().optional(), deletion_time: z.string().nullable().optional(), steps: z.array(taskStepDto).optional(), auth_context: z.record(z.string(), z.unknown()).nullable().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 }; /** * Add a Volume to Stack * * POST /uaas/stacks/{stack_id}/volumes/{volume_id} */ export const createApiUaasStacksStackIdVolumesVolumeId = async (args: CreateApiUaasStacksStackIdVolumesVolumeIdArgs, options?: CreateApiUaasStacksStackIdVolumesVolumeIdOptions) => { const {baseUrl = "https://am1.portal.api.nasuni.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/uaas/stacks/${encodeURIComponent(String(args.stack_id))}/volumes/${encodeURIComponent(String(args.volume_id))}${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(taskDto, await res.json()) };