import {z} from 'zod' export type S3Destination = {type: 's3', bucket: string, region: string, prefix?: string | undefined}; export type AzureBlobDestination = {type: 'azure_blob', account: string, container: string, prefix?: string | undefined}; export type StorageDestination = S3Destination | AzureBlobDestination; export type CreateApiV1StorageConnectionsArgs = {'Idempotency-Key'?: string | undefined, body: {name: string, destination: StorageDestination}}; export type CreateApiV1StorageConnectionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const s3Destination = z.object({type: z.literal("s3"), bucket: z.string(), region: z.string(), prefix: z.string().max(512).optional()}); export const azureBlobDestination = z.object({type: z.literal("azure_blob"), account: z.string(), container: z.string(), prefix: z.string().max(512).optional()}); export const storageDestination = z.discriminatedUnion("type", [s3Destination, azureBlobDestination]); export const storageConnection = z.object({id: z.string(), name: z.string(), destination: storageDestination, status: z.enum(["draft", "ready", "degraded", "revoked"]), can_manage: z.boolean(), external_id: z.string(), aws_trusted_principal_arn: z.string().nullable().optional(), aws_trusted_principal_arns: z.array(z.string()).optional(), credentials_pending: z.boolean().optional(), validation_running: z.boolean().optional(), credential_generation: z.number().int(), validated_at: z.string().nullable().optional(), reactivation_requires_approval: z.boolean().optional(), revoked_reason: z.enum(["owner", "administrator", "member_removed", "user_disabled", "null"]).nullable().optional(), revoked_at: z.string().nullable().optional(), created_at: z.string()}).and(z.record(z.string(), z.unknown())); 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 storage connection * * POST /v1/storage-connections */ export const createApiV1StorageConnections = async (args: CreateApiV1StorageConnectionsArgs, options?: CreateApiV1StorageConnectionsOptions) => { const {baseUrl = "https://console.nace.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("Idempotency-Key", String(args['Idempotency-Key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/storage-connections${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(storageConnection, await res.json()) };