import {z} from 'zod' export type NullableNumber = number | unknown; export type S3BucketInfoNew = {bucket_name: string, bucket_path: string, cloud_account_id?: string | undefined}; export type BillingConnectionNew = {name: string, cloud_provider: string, expected_ingest_rate?: NullableNumber | undefined, is_enabled?: boolean | undefined, s3_bucket_info?: (S3BucketInfoNew | null) | undefined}; export type CreateApiV2ConnectionsBillingArgs = {'cloudzero-idempotency-key'?: string | undefined, 'cloudzero-test-key'?: string | undefined, body: BillingConnectionNew}; export type CreateApiV2ConnectionsBillingOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const s3BucketInfoNew = z.object({bucket_name: z.string(), bucket_path: z.string(), cloud_account_id: z.string().optional()}); export const epoch = z.string(); export const billingConnection = z.object({id: z.string(), name: z.string(), connection_type: z.string(), cloud_provider: z.string(), s3_bucket_info: s3BucketInfoNew, expected_ingest_rate: z.number(), is_enabled: z.boolean(), capabilities: z.object({}), is_draft: z.boolean(), connection_id: z.string(), created: epoch, last_updated: epoch}); export const createApiV2ConnectionsBillingResponse = z.object({connection: billingConnection.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 a new Billing Connection * * POST /v2/connections/billing */ export const createApiV2ConnectionsBilling = async (args: CreateApiV2ConnectionsBillingArgs, options?: CreateApiV2ConnectionsBillingOptions) => { const {baseUrl = "https://api.cloudzero.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['cloudzero-idempotency-key'] != null) headers.set("cloudzero-idempotency-key", String(args['cloudzero-idempotency-key'])) if (args['cloudzero-test-key'] != null) headers.set("cloudzero-test-key", String(args['cloudzero-test-key'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/connections/billing${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(createApiV2ConnectionsBillingResponse, await res.json()) };