import {z} from 'zod' export type CreateApiApiAnalyticsV1EventsArgs = {body: {website_id: string, columns: Array<{column_id: string, transformation_id?: string | undefined}>, date_from?: (string | unknown) | undefined, date_to?: (string | unknown) | undefined, relative_date?: ('today' | 'yesterday' | 'last_week' | 'last_month' | 'last_year' | 'last_X_days') | undefined, filters?: ({operator: 'and' | 'or', conditions: Array<{column_id: string, transformation_id?: string | undefined, condition: {operator: string, value: unknown}}>}) | undefined, offset?: number | undefined, limit?: number | undefined, format?: ('json' | 'json-kv' | 'csv') | undefined, column_format?: ('id' | 'name') | undefined}}; export type CreateApiApiAnalyticsV1EventsOptions = {baseUrl?: string | undefined, OAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiAnalyticsV1EventsResponse = z.object({data: z.array(z.object({})).optional(), meta: z.object({columns: z.array(z.string()).optional(), count: z.number().int().optional(), scope: z.literal("event").optional(), blended_datasets: z.array(z.enum(["google ads map", "google ads stats", "google search console"])).optional()}).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 }; /** * Fetch events * * POST /api/analytics/v1/events/ */ export const createApiApiAnalyticsV1Events = async (args: CreateApiApiAnalyticsV1EventsArgs, options?: CreateApiApiAnalyticsV1EventsOptions) => { const {baseUrl = "https://example.piwik.pro", fetch: fetchFn = fetch, OAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${OAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/analytics/v1/events/${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(createApiApiAnalyticsV1EventsResponse, await res.json()) };