import {z} from 'zod' export type JumpcloudSearchFunction = 'function_unknown' | 'count' | 'avg' | 'max' | 'min' | 'sum' | 'count_unique'; export type JumpcloudSearchSearchRequestAggregation = {field?: Array | undefined, function?: JumpcloudSearchFunction | undefined, name?: string | undefined}; export type JumpcloudSearchSearchRequestFields = {exclude?: Array | undefined, include?: Array | undefined}; export type JumpcloudSearchSearchRequestFilterOperation = 'operation_unknown' | 'and' | 'or' | 'equals' | 'not_equals' | 'greater_than' | 'less_than' | 'starts_with' | 'ends_with' | 'contains' | 'not_contains' | 'in' | 'not_in' | 'greater_than_or_equals' | 'less_than_or_equals' | 'is_null' | 'is_empty' | 'equals_case_sensitive' | 'not_equals_case_sensitive' | 'starts_with_case_sensitive' | 'ends_with_case_sensitive' | 'contains_case_sensitive' | 'not_contains_case_sensitive' | 'in_case_sensitive' | 'not_in_case_sensitive' | 'member_of' | 'not_member_of' | 'not_contains_with_null' | 'not_contains_case_sensitive_with_null' | 'not_in_with_null' | 'not_in_case_sensitive_with_null' | 'not_starts_with' | 'not_ends_with'; export type JumpcloudSearchSearchRequestFilter = {field?: (string | null) | undefined, filters?: Array | undefined, operation?: JumpcloudSearchSearchRequestFilterOperation | undefined, query?: JumpcloudSearchSearchRequest | undefined, unit?: (string | null) | undefined, value: unknown}; export type JumpcloudSearchMetadataContextEntry = {key?: string | undefined, value?: string | undefined}; export type JumpcloudSearchSearchPurpose = 'unspecified' | 'reporting' | 'health_monitoring' | 'alerting' | 'audit' | 'troubleshooting' | 'user_initiated' | 'scheduled' | 'export' | 'analytics' | 'automation' | 'data_validation'; export type JumpcloudSearchSearchRequestMetadata = {context?: Array | undefined, searchPurpose?: (JumpcloudSearchSearchPurpose | null) | undefined, source?: (string | null) | undefined}; export type JumpcloudSearchPaginationParams = {offset?: number | undefined, pageSize?: number | undefined}; export type JumpcloudSearchSearchRequestSortOrder = 'asc' | 'desc'; export type JumpcloudSearchSearchRequestSort = {field?: string | undefined, order?: JumpcloudSearchSearchRequestSortOrder | undefined}; export type JumpcloudSearchSearchRequest = {aggregations?: Array | undefined, cacheToken?: (string | null) | undefined, fields?: JumpcloudSearchSearchRequestFields | undefined, filters?: Array | undefined, limit?: number | undefined, metadata?: (JumpcloudSearchSearchRequestMetadata | null) | undefined, pagination?: (JumpcloudSearchPaginationParams | null) | undefined, requestCache?: boolean | undefined, sort?: Array | undefined}; export type CreateApiSearchQueryArgs = {'x-org-id'?: string | undefined, body: JumpcloudSearchSearchRequest}; export type CreateApiSearchQueryOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudSearchFunction = z.enum(["function_unknown", "count", "avg", "max", "min", "sum", "count_unique"]); export const jumpcloudSearchType = z.enum(["type_unknown", "datetime", "boolean", "string", "number", "array"]); export const jumpcloudSearchSchemaField = z.object({field: z.string().optional(), format: z.string().nullable().optional(), function: jumpcloudSearchFunction.nullable().optional(), type: jumpcloudSearchType.optional()}); export const jumpcloudSearchSearchMetadata = z.object({queryTime: z.number().optional(), schema: z.array(jumpcloudSearchSchemaField).optional()}); export const jumpcloudSearchSearchResponseField = z.object({field: z.string().optional(), function: jumpcloudSearchFunction.nullable().optional(), value: z.unknown()}); export const jumpcloudSearchSearchResponseResult = z.object({fields: z.array(jumpcloudSearchSearchResponseField).optional(), itemnum: z.number().int().optional()}); export const jumpcloudSearchSearchResponse = z.object({cacheToken: z.string().nullable().optional(), metadata: jumpcloudSearchSearchMetadata.optional(), results: z.array(jumpcloudSearchSearchResponseResult).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 }; /** * Run Search Query * * POST /search/query */ export const createApiSearchQuery = async (args: CreateApiSearchQueryArgs, options?: CreateApiSearchQueryOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-org-id'] != null) headers.set("x-org-id", String(args['x-org-id'])) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/search/query${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(jumpcloudSearchSearchResponse, await res.json()) };