import {z} from 'zod' export type CreateAnomalyConfigRequest = {alert_budget_per_day?: (number | null) | undefined, alert_destinations?: Array | undefined, alert_direction?: (string | null) | undefined, alert_enabled?: (boolean | null) | undefined, alert_window_buckets?: (number | null) | undefined, alert_window_fire_pct?: (number | null) | undefined, alert_window_recover_pct?: (number | null) | undefined, band_width?: (number | null) | undefined, custom_sql?: (string | null) | undefined, description?: (string | null) | undefined, detection_function: string, detection_function_field?: (string | null) | undefined, detection_window_seconds: number, enabled?: (boolean | null) | undefined, filters: unknown, folder_id?: (string | null) | undefined, histogram_interval: string, level_half_width_seconds?: (number | null) | undefined, name: string, owner?: (string | null) | undefined, percentile?: (number | null) | undefined, priority?: (number | null) | undefined, query_mode: string, rcf_num_trees?: (number | null) | undefined, rcf_shingle_size?: (number | null) | undefined, rcf_tree_size?: (number | null) | undefined, retrain_interval_days?: (number | null) | undefined, schedule_interval: string, stream_name: string, stream_type: string, tags?: Array | undefined, template?: (string | null) | undefined, training_window_days?: (number | null) | undefined}; export type CreateApiApiOrgIdAnomalyDetectionArgs = {org_id: string, body: CreateAnomalyConfigRequest}; export type CreateApiApiOrgIdAnomalyDetectionOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const anomalyConfigResponse = z.object({}); 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 }; /** * Create a new anomaly detection configuration * * POST /api/{org_id}/anomaly_detection */ export const createApiApiOrgIdAnomalyDetection = async (args: CreateApiApiOrgIdAnomalyDetectionArgs, options: CreateApiApiOrgIdAnomalyDetectionOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/anomaly_detection${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(anomalyConfigResponse, await res.json()) };