import {z} from 'zod' export type CreateApiV1EventsAggregateArgs = {'x-api-version': string, body: {customer_id?: string | undefined, entity_id?: string | undefined, feature_id: string | Array, group_by?: string | undefined, range?: ('24h' | '7d' | '30d' | '90d' | 'last_cycle' | '1bc' | '3bc') | undefined, bin_size?: ('day' | 'hour' | 'week' | 'month') | undefined, custom_range?: {start: number, end: number} | undefined, filter_by?: {[key: string]: string} | undefined, max_groups?: number | undefined}}; export type CreateApiV1EventsAggregateOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1EventsAggregateResponse = z.object({list: z.array(z.object({period: z.number(), values: z.record(z.string(), z.number()), grouped_values: z.record(z.string(), z.record(z.string(), z.number())).optional()})), total: z.record(z.string(), z.object({count: z.number(), sum: z.number()}))}); 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 usage events by time period. Returns usage totals grouped by feature and optionally by a custom property. * * POST /v1/events.aggregate */ export const createApiV1EventsAggregate = async (args: CreateApiV1EventsAggregateArgs, options?: CreateApiV1EventsAggregateOptions) => { const {baseUrl = "https://api.useautumn.com", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (secretKey !== undefined) headers.set('authorization', `Bearer ${secretKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events.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(createApiV1EventsAggregateResponse, await res.json()) };