import {z} from 'zod' export type TermConjunction = Record; export type SearchTerm = {and?: TermConjunction | undefined, not?: TermConjunction | undefined, or?: TermConjunction | undefined}; export type EventDistinctQuery = {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, exclude_provider_activity?: boolean | undefined, field: string, search_term?: SearchTerm | undefined}; export type CreateApiEventsDistinctArgs = {body: EventDistinctQuery}; export type CreateApiEventsDistinctOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, xOrgId?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEventsDistinctResponse = z.object({buckets: z.array(z.object({doc_count: z.number().int().optional(), key: z.string().optional()})).optional(), doc_count_error_upper_bound: z.number().int().optional(), sum_other_doc_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 }; /** * Query event distinct field values * * POST /events/distinct */ export const createApiEventsDistinct = async (args: CreateApiEventsDistinctArgs, options?: CreateApiEventsDistinctOptions) => { 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/distinct${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(createApiEventsDistinctResponse, await res.json()) };