import {z} from 'zod' export type StoreEnum = 'IOS' | 'ANDROID' | 'PWA' | 'APK'; export type GetApiApiV1EventsCsvArgs = {trackerId: Array, createdAt?: (string | null) | undefined, limit?: (number | null) | undefined, deviceIp?: (string | null) | undefined, deviceBrand?: (string | null) | undefined, deviceModel?: (string | null) | undefined, deviceType?: (string | null) | undefined, osName?: (string | null) | undefined, osVersion?: (string | null) | undefined, browserName?: (string | null) | undefined, browserVersion?: (string | null) | undefined, store?: (StoreEnum | null) | undefined, domain?: (string | null) | undefined, appNamespace?: (string | null) | undefined, country?: (string | null) | undefined, city?: (string | null) | undefined, click?: (boolean | null) | undefined, install?: (boolean | null) | undefined, lead?: (boolean | null) | undefined, firstSale?: (boolean | null) | undefined, repeatedSale?: (boolean | null) | undefined, reasonBlock?: (boolean | null) | undefined, isBlack?: (boolean | null) | undefined, uniqueClick?: (boolean | null) | undefined, userClickId?: (string | null) | undefined, preLandingId?: (string | null) | undefined, postLandingId?: (string | null) | undefined}; export type GetApiApiV1EventsCsvOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiApiV1EventsCsvResponse = z.unknown(); 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 trackers statistics in CSV format and download * * GET /api/v1/events/csv */ export const getApiApiV1EventsCsv = async (args: GetApiApiV1EventsCsvArgs, options: GetApiApiV1EventsCsvOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.trackerId ?? []) query.append("trackerId", String(item)) if (args.createdAt != null) query.append("createdAt", String(args.createdAt)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.deviceIp != null) query.append("deviceIp", String(args.deviceIp)) if (args.deviceBrand != null) query.append("deviceBrand", String(args.deviceBrand)) if (args.deviceModel != null) query.append("deviceModel", String(args.deviceModel)) if (args.deviceType != null) query.append("deviceType", String(args.deviceType)) if (args.osName != null) query.append("osName", String(args.osName)) if (args.osVersion != null) query.append("osVersion", String(args.osVersion)) if (args.browserName != null) query.append("browserName", String(args.browserName)) if (args.browserVersion != null) query.append("browserVersion", String(args.browserVersion)) if (args.store != null) query.append("store", String(args.store)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.appNamespace != null) query.append("appNamespace", String(args.appNamespace)) if (args.country != null) query.append("country", String(args.country)) if (args.city != null) query.append("city", String(args.city)) if (args.click != null) query.append("click", String(args.click)) if (args.install != null) query.append("install", String(args.install)) if (args.lead != null) query.append("lead", String(args.lead)) if (args.firstSale != null) query.append("firstSale", String(args.firstSale)) if (args.repeatedSale != null) query.append("repeatedSale", String(args.repeatedSale)) if (args.reasonBlock != null) query.append("reasonBlock", String(args.reasonBlock)) if (args.isBlack != null) query.append("isBlack", String(args.isBlack)) if (args.uniqueClick != null) query.append("uniqueClick", String(args.uniqueClick)) if (args.userClickId != null) query.append("userClickId", String(args.userClickId)) if (args.preLandingId != null) query.append("preLandingId", String(args.preLandingId)) if (args.postLandingId != null) query.append("postLandingId", String(args.postLandingId)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/events/csv${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(getApiApiV1EventsCsvResponse, await res.json()) };