import {z} from 'zod' export type Currency = string; export type BudgetPlannedLimit = {[key: string]: {amount: Currency}}; export type BudgetPlannedLimits = {monthly: BudgetPlannedLimit}; export type WithId = {id: string}; export type BudgetAlertsValue = 'Y' | 'N'; export type BudgetAlerts = {[key: string]: BudgetAlertsValue}; export type BudgetUpdate = {name?: string | undefined, planned_limits?: BudgetPlannedLimits | undefined, view?: WithId | undefined, alerts?: BudgetAlerts | undefined}; export type PatchApiV2BudgetsBudgetIdArgs = {'cloudzero-idempotency-key'?: string | undefined, 'cloudzero-test-key'?: string | undefined, budget_id: string, body: BudgetUpdate}; export type PatchApiV2BudgetsBudgetIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.string(); export const budgetPlannedLimit = z.record(z.string(), z.object({amount: currency})); export const budgetPlannedLimits = z.object({monthly: budgetPlannedLimit}); export const withId = z.object({id: z.string()}); export const budgetAlertsValue = z.enum(["Y", "N"]); export const budgetAlerts = z.record(z.string(), budgetAlertsValue); export const epoch = z.string(); export const budgetsCostType = z.literal("real_cost"); export const budgetsGranularity = z.literal("monthly"); export const budget = z.object({name: z.string(), planned_limits: budgetPlannedLimits, view: withId, alerts: budgetAlerts.optional(), id: z.string(), created: epoch, last_updated: epoch, cost_type: budgetsCostType, granularity: budgetsGranularity}); export const patchApiV2BudgetsBudgetIdResponse = z.object({budget: budget.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 }; /** * Update a single Budget * * PATCH /v2/budgets/{budget_id} */ export const patchApiV2BudgetsBudgetId = async (args: PatchApiV2BudgetsBudgetIdArgs, options?: PatchApiV2BudgetsBudgetIdOptions) => { 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/budgets/${encodeURIComponent(String(args.budget_id))}${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(patchApiV2BudgetsBudgetIdResponse, await res.json()) };