import {z} from 'zod' export type UpdateSpendingCapsRequest = {monthly_spending_budget?: (number | null) | undefined, spending_alert_thresholds?: (Array | null) | undefined, spending_alerts_enabled?: (boolean | null) | undefined, hard_spending_cap?: (number | null) | undefined, hard_cap_enabled?: (boolean | null) | undefined}; export type CreateApiV1OrganizationsBillingSpendingCapsArgs = {body: UpdateSpendingCapsRequest}; export type CreateApiV1OrganizationsBillingSpendingCapsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const spendingCapsResponse = z.object({monthly_spending_budget: z.number().int().nullable().optional(), monthly_spending_budget_usd: z.number().nullable().optional(), spending_alert_thresholds: z.array(z.number().int()).optional(), spending_alerts_enabled: z.boolean().optional(), spending_alerts_sent: z.array(z.number().int()).optional(), hard_spending_cap: z.number().int().nullable().optional(), hard_spending_cap_usd: z.number().nullable().optional(), hard_cap_enabled: z.boolean().optional(), current_spending_cents: z.number().int(), current_spending_usd: z.number(), budget_percentage_used: z.number().nullable().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 Spending Caps * * POST /v1/organizations/billing/spending-caps */ export const createApiV1OrganizationsBillingSpendingCaps = async (args: CreateApiV1OrganizationsBillingSpendingCapsArgs, options?: CreateApiV1OrganizationsBillingSpendingCapsOptions) => { const {baseUrl = "https://api.mixpeek.com", 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/organizations/billing/spending-caps${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(spendingCapsResponse, await res.json()) };