import {z} from 'zod' export type GroupsAggregationFunction = string; export type GroupsRequestedAttribute = {attribute: string, operation?: GroupsAggregationFunction | undefined, ancestor_entity_type?: string | undefined}; export type GroupsAttributeType = string; export type GroupsSortDownsamplingFunction = string; export type GroupsSortOrder = string; export type GroupsGetEntitiesRequest = {number_of_intervals_for_latest_data?: number | undefined, filter_criteria?: string | undefined, group_sort_attribute?: string | undefined, group_attributes?: Array | undefined, number_of_buckets?: number | undefined, entity_ids?: Array | undefined, group_count?: number | undefined, group_member_attributes?: Array | undefined, group_member_sort_attribute?: string | undefined, bucket_boundary?: number | undefined, group_offset?: number | undefined, interval_end_ms?: number | undefined, downsampling_interval?: number | undefined, availability_zone_scope?: string | undefined, interval_start_ms?: number | undefined, entity_type: string, grouping_attribute_type?: GroupsAttributeType | undefined, group_member_offset?: number | undefined, group_member_count?: number | undefined, grouping_attribute?: string | undefined, group_member_sort_downsampling_function?: GroupsSortDownsamplingFunction | undefined, group_member_sort_order?: GroupsSortOrder | undefined, query_name?: string | undefined, group_sort_order?: GroupsSortOrder | undefined, group_sort_downsample_function?: GroupsSortDownsamplingFunction | undefined}; export type CreateApiGroupsArgs = {body: GroupsGetEntitiesRequest}; export type CreateApiGroupsOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const groupsBucketSummaryMap = z.record(z.string(), z.number().int()); export const groupsTimevaluePair = z.object({values: z.array(z.string()).optional(), time: z.number().int().optional()}); export const groupsFieldData = z.object({buckets: groupsBucketSummaryMap.optional(), values: z.array(groupsTimevaluePair).optional(), name: z.string().max(64).optional()}); export const groupsEntity = z.object({entity_id: z.string().optional(), data: z.array(groupsFieldData).optional()}); export const groupsGroupSummaryMap = z.record(z.string(), groupsFieldData); export const groupsGroupResult = z.object({entity_results: z.array(groupsEntity).optional(), group_by_column_value: z.string().optional(), total_entity_count: z.number().int().optional(), group_summaries: groupsGroupSummaryMap.optional()}); export const groupsGetEntitiesResponse = z.object({filtered_entity_count: z.number().int().optional(), group_results: z.array(groupsGroupResult).optional(), entity_type: z.string().optional(), filtered_group_count: z.number().int().optional(), total_entity_count: z.number().int().optional(), total_group_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 }; /** * Get Entities. * * POST /groups */ export const createApiGroups = async (args: CreateApiGroupsArgs, options: CreateApiGroupsOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/groups${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(groupsGetEntitiesResponse, await res.json()) };