import {z} from 'zod' export type XiqAnomalyType = 'POE_STABILITY' | 'DFS_RECURRENCE' | 'ADVERSE_TRAFFIC' | 'PORT_EFFICIENCY' | 'WIFI_CAPACITY' | 'WIFI_EFFICIENCY' | 'HARDWARE_HEALTH' | 'MISSING_VLAN'; export type XiqAnomalySeverity = 'NONE' | 'LOW' | 'MEDIUM' | 'HIGH'; export type GetApiCopilotAnomaliesAnomaliesByCategoryArgs = {anomalyType?: XiqAnomalyType | undefined, startTime: number, endTime: number, buildingId?: number | undefined, severity?: XiqAnomalySeverity | undefined, excludeMuted?: boolean | undefined}; export type GetApiCopilotAnomaliesAnomaliesByCategoryOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const xiqAnomaliesSiteEntity = z.object({building_id: z.number().int().optional(), location_name: z.string().optional(), anomalies_count_by_location: z.number().int().optional()}); export const xiqAnomaliesSeverityEntity = z.object({low_severity_anomalies_count: z.number().int().optional(), medium_severity_anomalies_count: z.number().int().optional(), high_severity_anomalies_count: z.number().int().optional(), anomalies_count_by_severity: z.number().int().optional()}); export const xiqAnomalyType = z.enum(["POE_STABILITY", "DFS_RECURRENCE", "ADVERSE_TRAFFIC", "PORT_EFFICIENCY", "WIFI_CAPACITY", "WIFI_EFFICIENCY", "HARDWARE_HEALTH", "MISSING_VLAN"]); export const xiqAnomaliesTypeEntity = z.object({anomaly_type: xiqAnomalyType.optional(), anomalies_count_by_type: z.number().int().optional()}); export const xiqCopilotAnomaliesByCategory = z.object({anomalies_by_location: z.array(xiqAnomaliesSiteEntity).optional(), anomalies_by_severity: xiqAnomaliesSeverityEntity.optional(), anomalies_by_type: z.array(xiqAnomaliesTypeEntity).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 }; /** * GET /copilot/anomalies/anomalies-by-category */ export const getApiCopilotAnomaliesAnomaliesByCategory = async (args: GetApiCopilotAnomaliesAnomaliesByCategoryArgs, options?: GetApiCopilotAnomaliesAnomaliesByCategoryOptions) => { const {baseUrl = "https://api.extremecloudiq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.anomalyType != null) query.append("anomalyType", String(args.anomalyType)) if (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.buildingId != null) query.append("buildingId", String(args.buildingId)) if (args.severity != null) query.append("severity", String(args.severity)) if (args.excludeMuted != null) query.append("excludeMuted", String(args.excludeMuted)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/copilot/anomalies/anomalies-by-category${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(xiqCopilotAnomaliesByCategory, await res.json()) };