import {z} from 'zod' export type IsoDatetime = string; export type BillingGranularity = 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly'; export type DimensionId = string; export type BillingCostType = 'billed_cost' | 'discounted_cost' | 'amortized_cost' | 'discounted_amortized_cost' | 'real_cost' | 'on_demand_cost' | 'invoiced_amortized_cost' | 'usage_amount'; export type GetApiV2BillingCostsArgs = {start_date: IsoDatetime, end_date?: IsoDatetime | undefined, granularity?: BillingGranularity | undefined, group_by?: Array | undefined, filters?: string | undefined, cost_type?: BillingCostType | undefined, align_to_granularity?: boolean | undefined, include_projected_row_count?: boolean | undefined, cursor?: string | undefined}; export type GetApiV2BillingCostsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const isoDatetime = z.string(); export const billingCost = z.object({usage_date: isoDatetime.optional(), cost: z.number().optional(), projected_row_count: z.number().int().optional()}); export const billingCosts = z.array(billingCost); export const nullableString = z.union([z.string(), z.unknown()]); export const pagination = z.object({page_count: z.number().int().optional(), item_count: z.number().int().optional(), total_count: z.number().int().optional(), cursor: z.object({next_cursor: nullableString.optional(), previous_cursor: nullableString.optional(), has_next: z.boolean().optional(), has_previous: z.boolean().optional()}).optional()}); export const getApiV2BillingCostsResponse = z.object({costs: billingCosts.optional(), pagination: pagination.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 }; /** * This API will return cost data according to the parameters passed in. * * GET /v2/billing/costs */ export const getApiV2BillingCosts = async (args: GetApiV2BillingCostsArgs, options?: GetApiV2BillingCostsOptions) => { const {baseUrl = "https://api.cloudzero.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.granularity != null) query.append("granularity", String(args.granularity)) for (const item of args.group_by ?? []) query.append("group_by", String(item)) if (args.filters != null) query.append("filters", String(args.filters)) if (args.cost_type != null) query.append("cost_type", String(args.cost_type)) if (args.align_to_granularity != null) query.append("align_to_granularity", String(args.align_to_granularity)) if (args.include_projected_row_count != null) query.append("include_projected_row_count", String(args.include_projected_row_count)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/billing/costs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2BillingCostsResponse, await res.json()) };