import {z} from 'zod' export type GetApiApiV1EventsGroupedStatisticsSummaryArgs = {tracker?: (Array | null) | undefined, offer?: (Array | null) | undefined, domain?: (string | null) | undefined, appNamespace?: (Array | null) | undefined, country?: (string | null) | undefined, startDate?: (string | null) | undefined, endDate?: (string | null) | undefined}; export type GetApiApiV1EventsGroupedStatisticsSummaryOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const appEventsStatisticsSummaryDTO = z.object({clickCount: z.number().int().optional(), uniqueClickCount: z.number().int().optional(), leadCount: z.number().int().optional(), installCount: z.number().int().optional(), saleCount: z.number().int().optional(), repeatedSaleCount: 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 summary app events statistics * * GET /api/v1/events/grouped-statistics/summary */ export const getApiApiV1EventsGroupedStatisticsSummary = async (args: GetApiApiV1EventsGroupedStatisticsSummaryArgs, options: GetApiApiV1EventsGroupedStatisticsSummaryOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.tracker ?? []) query.append("tracker", String(item)) for (const item of args.offer ?? []) query.append("offer", String(item)) if (args.domain != null) query.append("domain", String(args.domain)) for (const item of args.appNamespace ?? []) query.append("appNamespace", String(item)) if (args.country != null) query.append("country", String(args.country)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/events/grouped-statistics/summary${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(appEventsStatisticsSummaryDTO, await res.json()) };