import {z} from 'zod' export type BillingFilters = {categories?: (Array<'operationalComputeAndStorage' | 'operationalBucketBackup' | 'operationalClusterBackup' | 'analyticsCompute' | 'analyticsStorage' | 'analyticsClusterBackup' | 'appServicesComputeAndStorage' | 'dataTransferStandard' | 'privateEndpointsStandard' | 'aiServicesLLM' | 'aiServicesAiGateway' | 'aiServicesUdsPager' | 'aiServicesSdsPager' | 'dataApiStandard'>) | undefined, projectIds?: Array | undefined, instanceIds?: Array | undefined}; export type CategorizedBillingRequest = {startDate: string, endDate: string, filters?: BillingFilters | undefined}; export type CreateApiV4OrganizationsOrganizationIdBillingArgs = {body: CategorizedBillingRequest}; export type CreateApiV4OrganizationsOrganizationIdBillingOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const billingCategoryBreakdown = z.object({category: z.string(), creditSpend: z.number().nullable().optional(), currencySpend: z.number().nullable().optional(), contributionPercent: z.number()}); export const billingPeriod = z.object({startDate: z.string(), endDate: z.string(), categories: z.array(billingCategoryBreakdown), totalCreditSpend: z.number().nullable().optional(), totalCurrencySpend: z.number().nullable().optional()}); export const billingPeriodTotal = z.object({startDate: z.string(), endDate: z.string(), categories: z.array(billingCategoryBreakdown), totalCreditSpend: z.number().nullable().optional(), totalCurrencySpend: z.number().nullable().optional()}); export const categorizedBillingResponse = z.object({data: z.object({periods: z.array(billingPeriod), total: billingPeriodTotal, billingCurrency: z.string()})}); 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 }; /** * Get Categorized Billing * * POST /v4/organizations/{organizationId}/billing */ export const createApiV4OrganizationsOrganizationIdBilling = async (args: CreateApiV4OrganizationsOrganizationIdBillingArgs, options?: CreateApiV4OrganizationsOrganizationIdBillingOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/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(categorizedBillingResponse, await res.json()) };