import {z} from 'zod' export type GetApiAlertsArgs = {filter?: Array | undefined, fields?: Array | undefined, skip?: unknown | undefined, sort?: Array | undefined, limit?: unknown | undefined}; export type GetApiAlertsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudAlertsAlertCategory = z.enum(["ALERT_CATEGORY_UNSPECIFIED", "ALERT_CATEGORY_DIRECTORY", "ALERT_CATEGORY_SYSTEM", "ALERT_CATEGORY_ADMIN", "ALERT_CATEGORY_ACCESS", "ALERT_CATEGORY_GENERAL", "ALERT_CATEGORY_CUSTOM"]); export const jumpcloudAlertsAlertSeverity = z.enum(["ALERT_SEVERITY_UNSPECIFIED", "ALERT_SEVERITY_LOW", "ALERT_SEVERITY_MEDIUM", "ALERT_SEVERITY_HIGH"]); export const jumpcloudAlertsAlertSourceType = z.enum(["ALERT_SOURCE_TYPE_UNSPECIFIED", "ALERT_SOURCE_TYPE_USER", "ALERT_SOURCE_TYPE_ADMIN", "ALERT_SOURCE_TYPE_DEVICE", "ALERT_SOURCE_TYPE_USER_GROUP", "ALERT_SOURCE_TYPE_DEVICES", "ALERT_SOURCE_TYPE_ACCESS_REQUEST", "ALERT_SOURCE_TYPE_SAAS_APPLICATION", "ALERT_SOURCE_TYPE_CUSTOM", "ALERT_SOURCE_TYPE_COMMAND", "ALERT_SOURCE_TYPE_POLICY", "ALERT_SOURCE_TYPE_APPLICATION", "ALERT_SOURCE_TYPE_IPLIST", "ALERT_SOURCE_TYPE_SOFTWARE", "ALERT_SOURCE_TYPE_ACCESSORY", "ALERT_SOURCE_TYPE_LOCATION"]); export const jumpcloudAlertsAlertStatus = z.enum(["ALERT_STATUS_UNSPECIFIED", "ALERT_STATUS_OPEN", "ALERT_STATUS_ACKNOWLEDGED", "ALERT_STATUS_RESOLVED", "ALERT_STATUS_AUTO_RESOLVED", "ALERT_STATUS_DELETED"]); export const jumpcloudAlertsAlert = z.object({acknowledgedAt: z.string().optional(), category: jumpcloudAlertsAlertCategory.optional(), createdAt: z.string().optional(), description: z.string().optional(), firstOccurredAt: z.string().optional(), frequency: z.number().int().optional(), lastOccurredAt: z.string().optional(), lastOccurrenceContext: z.object({}).optional(), objectId: z.string().optional(), occurrencesCount: z.number().int().optional(), organizationObjectId: z.string().optional(), referenceId: z.string().optional(), remark: z.string().optional(), resolvedAt: z.string().optional(), severity: jumpcloudAlertsAlertSeverity.optional(), sourceId: z.string().optional(), sourceName: z.string().optional(), sourceType: jumpcloudAlertsAlertSourceType.optional(), status: jumpcloudAlertsAlertStatus.optional(), title: z.string().optional(), updatedAt: z.string().optional()}); export const jumpcloudAlertsListAlertsResponse = z.object({alerts: z.array(jumpcloudAlertsAlert).optional(), count: z.number().int().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 }; /** * This endpoint provides details of all alerts. * * GET /alerts */ export const getApiAlerts = async (args: GetApiAlertsArgs, options?: GetApiAlertsOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.filter ?? []) query.append("filter", String(item)) for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.skip != null) query.append("skip", String(args.skip)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/alerts${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(jumpcloudAlertsListAlertsResponse, await res.json()) };