import {z} from 'zod' export type BudgetAlertUpdate = {name?: (string | null) | undefined, threshold_amount?: (number | null) | undefined, active?: (boolean | null) | undefined}; export type PatchApiAlertsAlertIdArgs = {alert_id: number, cookie_name?: (string | null) | undefined, tenant_id?: number | undefined, body: BudgetAlertUpdate}; export type PatchApiAlertsAlertIdOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const budgetAlert = z.object({id: z.number().int().nullable().optional(), tenant_id: z.number().int(), name: z.string().optional(), threshold_amount: z.number().optional(), current_spend: z.string().optional(), triggered: z.boolean().optional(), active: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), created_user_id: z.number().int()}); 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 budget alert * * PATCH /alerts/{alert_id} */ export const patchApiAlertsAlertId = async (args: PatchApiAlertsAlertIdArgs, options?: PatchApiAlertsAlertIdOptions) => { const {baseUrl = "https://api.your-domain.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.cookie_name != null) query.append("cookie_name", String(args.cookie_name)) if (args.tenant_id != null) query.append("tenant_id", String(args.tenant_id)) if (APIKeyHeader !== undefined) headers.set("X-API-KEY", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/alerts/${encodeURIComponent(String(args.alert_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(budgetAlert, await res.json()) };