import {z} from 'zod' export type ChecksumAlgorithm = string; export type Checksum = {checksum_algorithm: ChecksumAlgorithm, checksum_value: unknown}; export type AwsCredentials = {access_key: string, secret_key: string}; export type Credentials = {username: string, password?: string | undefined}; export type SourceAuth = {aws_auth?: AwsCredentials | undefined, basic_auth?: Credentials | undefined}; export type FileItemResources = {checksum?: Checksum | undefined, source_uri?: string | undefined, source_auth?: SourceAuth | undefined}; export type ClusterReference = {kind: string, name?: string | undefined, uuid: unknown}; export type AvailabilityZoneReference = {kind: string, name?: string | undefined, uuid: unknown}; export type FileItem = {description?: string | undefined, resources: FileItemResources, cluster_reference?: ClusterReference | undefined, name: string, availability_zone_reference?: AvailabilityZoneReference | undefined}; export type UserReference = {kind: string, name?: string | undefined, uuid: unknown}; export type FileItemMetadata = {last_update_time?: string | undefined, kind: string, uuid?: unknown | undefined, spec_version?: number | undefined, creation_time?: string | undefined, spec_hash?: string | undefined, should_force_translate?: boolean | undefined, owner_reference?: UserReference | undefined, categories?: {[key: string]: string} | undefined, name?: string | undefined}; export type FileItemIntentInput = {spec: FileItem, api_version?: string | undefined, metadata: FileItemMetadata}; export type UpdateApiFileStoreUuidArgs = {uuid: unknown, body: FileItemIntentInput}; export type UpdateApiFileStoreUuidOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const availabilityZoneReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const messageResource = z.object({message: z.string(), reason: z.string(), details: z.record(z.string(), z.string()).optional()}); export const clusterReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const checksumAlgorithm = z.string(); export const checksum = z.object({checksum_algorithm: checksumAlgorithm, checksum_value: z.string()}); export const awsCredentials = z.object({access_key: z.string(), secret_key: z.string()}); export const credentials = z.object({username: z.string(), password: z.string().optional()}); export const sourceAuth = z.object({aws_auth: awsCredentials.optional(), basic_auth: credentials.optional()}); export const fileItemResourcesDefStatus = z.object({retrieval_uri_list: z.array(z.string()).optional(), checksum: checksum.optional(), source_uri: z.string().optional(), source_auth: sourceAuth.optional(), size_bytes: z.number().int().optional()}); export const fileItemDefStatus = z.object({state: z.string().optional(), name: z.string(), availability_zone_reference: availabilityZoneReference.optional(), message_list: z.array(messageResource).optional(), cluster_reference: clusterReference.optional(), resources: fileItemResourcesDefStatus, description: z.string().optional()}); export const fileItemResources = z.object({checksum: checksum.optional(), source_uri: z.string().optional(), source_auth: sourceAuth.optional()}); export const fileItem = z.object({description: z.string().max(1000).optional(), resources: fileItemResources, cluster_reference: clusterReference.optional(), name: z.string().max(64), availability_zone_reference: availabilityZoneReference.optional()}); export const userReference = z.object({kind: z.string(), name: z.string().optional(), uuid: z.string()}); export const fileItemMetadata = z.object({last_update_time: z.string().optional(), kind: z.string(), uuid: z.string().optional(), spec_version: z.number().int().optional(), creation_time: z.string().optional(), spec_hash: z.string().optional(), should_force_translate: z.boolean().optional(), owner_reference: userReference.optional(), categories: z.record(z.string(), z.string()).optional(), name: z.string().max(64).optional()}); export const fileItemIntentResponse = z.object({status: fileItemDefStatus.optional(), spec: fileItem.optional(), api_version: z.string(), metadata: fileItemMetadata}); 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 }; /** * Update a file_item * * PUT /file_store/{uuid} */ export const updateApiFileStoreUuid = async (args: UpdateApiFileStoreUuidArgs, options: UpdateApiFileStoreUuidOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/file_store/${encodeURIComponent(String(args.uuid))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(fileItemIntentResponse, await res.json()) };