import {z} from 'zod' export type UpdateS3BucketTagsRequest = {tags: {[key: string]: string}}; export type PatchApiProjectsProjectIdStorageS3BucketsBucketNameArgs = {project_id: string, bucket_name: string, body: UpdateS3BucketTagsRequest}; export type PatchApiProjectsProjectIdStorageS3BucketsBucketNameOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const s3RetentionMode = z.string(); export const s3RetentionPeriodUnit = z.string(); export const s3VersioningState = z.string(); export const s3Bucket = z.object({archived_at: z.string().optional(), bucket_size: z.number().optional(), created_at: z.string(), location: z.string(), name: z.string(), object_lock_enabled: z.boolean(), project_id: z.string(), retention_mode: s3RetentionMode.optional(), retention_period: z.number().int().optional(), retention_period_unit: s3RetentionPeriodUnit.optional(), s3_url: z.string().optional(), tags: z.record(z.string(), z.string()).optional(), updated_at: z.string(), user_id: z.string(), versioning_state: s3VersioningState}); 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 }; /** * Replaces the tags on an S3 bucket and returns the updated bucket. * * PATCH /projects/{project_id}/storage/s3/buckets/{bucket_name} */ export const patchApiProjectsProjectIdStorageS3BucketsBucketName = async (args: PatchApiProjectsProjectIdStorageS3BucketsBucketNameArgs, options?: PatchApiProjectsProjectIdStorageS3BucketsBucketNameOptions) => { const {baseUrl = "https://api.cloud.crusoe.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/projects/${encodeURIComponent(String(args.project_id))}/storage/s3/buckets/${encodeURIComponent(String(args.bucket_name))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(s3Bucket, await res.json()) };