import {z} from 'zod' export type SvstorePublicapiEntityAggregationDimension = 'ENTITY_AGGREGATION_DIMENSION_UNSPECIFIED' | 'ENTITY_AGGREGATION_DIMENSION_ENTITY_TYPE' | 'ENTITY_AGGREGATION_DIMENSION_CREATED_AT' | 'ENTITY_AGGREGATION_DIMENSION_FIELD_VALUE'; export type SvstorePublicapiEntityAggregationTimeGranularity = 'ENTITY_AGGREGATION_TIME_GRANULARITY_UNSPECIFIED' | 'ENTITY_AGGREGATION_TIME_GRANULARITY_DAY' | 'ENTITY_AGGREGATION_TIME_GRANULARITY_MONTH'; export type GoogleProtobufTimestamp = string; export type SvstorePublicapiAggregateEntitiesRequest = {teamId?: string | undefined, entitySchemaId?: string | undefined, groupBy?: SvstorePublicapiEntityAggregationDimension | undefined, granularity?: SvstorePublicapiEntityAggregationTimeGranularity | undefined, entityTypeIds?: Array | undefined, createdAfter?: GoogleProtobufTimestamp | undefined, createdBefore?: GoogleProtobufTimestamp | undefined, fieldKey?: (string | null) | undefined, sumFieldKey?: (string | null) | undefined}; export type CreateApiV2EntitiesAggregateArgs = {body: SvstorePublicapiAggregateEntitiesRequest}; export type CreateApiV2EntitiesAggregateOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const googleProtobufTimestamp = z.string(); export const svstorePublicapiEntityTypeAggregationKey = z.object({entityTypeId: z.string().optional(), entityTypeName: z.string().optional()}); export const svstorePublicapiEntityFieldValueAggregationKey = z.object({value: z.string().optional(), displayName: z.string().optional()}); export const svstorePublicapiEntityAggregationBucket = z.union([z.object({createdAtBucketStart: googleProtobufTimestamp, count: z.number().int().optional(), sum: z.number().nullable().optional()}), z.object({entityType: svstorePublicapiEntityTypeAggregationKey, count: z.number().int().optional(), sum: z.number().nullable().optional()}), z.object({fieldValue: svstorePublicapiEntityFieldValueAggregationKey, count: z.number().int().optional(), sum: z.number().nullable().optional()})]); export const svstorePublicapiAggregateEntitiesResponse = z.object({buckets: z.array(svstorePublicapiEntityAggregationBucket).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 }; /** * Aggregate Entities * * POST /v2/entities/aggregate */ export const createApiV2EntitiesAggregate = async (args: CreateApiV2EntitiesAggregateArgs, options?: CreateApiV2EntitiesAggregateOptions) => { const {baseUrl = "https://public.api.serval.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/entities/aggregate${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(svstorePublicapiAggregateEntitiesResponse, await res.json()) };