import {z} from 'zod' export type TermConjunction = Record; export type SearchTerm = {and?: TermConjunction | undefined, not?: TermConjunction | undefined, or?: TermConjunction | undefined}; export type EventIntervalQuery = {end_time?: string | undefined, service: Array<'alerts' | 'all' | 'directory' | 'ldap' | 'mdm' | 'notifications' | 'object_storage' | 'password_manager' | 'password_vault' | 'pam' | 'radius' | 'reports' | 'saas_app_management' | 'software' | 'sso' | 'systems' | 'access_management' | 'asset_management' | 'workflows' | 'access_certification' | 'genai' | 'aigw' | 'ai_admin' | 'adbridge_user' | 'generic_di'>, start_time: string, exact_match?: string | undefined, exclude_provider_activity?: boolean | undefined, interval_unit: 's' | 'm' | 'h' | 'd' | 'w', interval_value?: string | undefined, q?: string | undefined, search_term?: SearchTerm | undefined, timezone?: string | undefined}; export type CreateApiEventsIntervalArgs = {body: EventIntervalQuery}; export type CreateApiEventsIntervalOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, xOrgId?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEventsIntervalResponse = z.object({buckets: z.array(z.object({doc_count: z.number().int().optional(), key: z.number().int().optional(), key_as_string: z.string().optional()})).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 }; /** * Query event counts by bucketed by a time interval * * POST /events/interval */ export const createApiEventsInterval = async (args: CreateApiEventsIntervalArgs, options?: CreateApiEventsIntervalOptions) => { const {baseUrl = "https://api.jumpcloud.com/insights/directory/v1", fetch: fetchFn = fetch, xApiKey, xOrgId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) if (xOrgId !== undefined) headers.set("x-org-id", xOrgId) const url = `${baseUrl.replace(/\/+$/, '')}/events/interval${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(createApiEventsIntervalResponse, await res.json()) };