export type GetApiExportArgs = {project_id?: number | undefined, from_date: string, to_date: string, limit?: number | undefined, event?: string | undefined, where?: string | undefined, time_in_ms?: boolean | undefined, 'Accept-Encoding'?: 'gzip' | undefined}; export type GetApiExportOptions = {baseUrl?: string | undefined, ServiceAccount?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * Download Data * * GET /export * * Also accepts auth: ProjectSecret */ export const getApiExport = async (args: GetApiExportArgs, options?: GetApiExportOptions) => { const {baseUrl = "https://mixpanel.com/api/query", fetch: fetchFn = fetch, ServiceAccount} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "text/plain") if (args.project_id != null) query.append("project_id", String(args.project_id)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.event != null) query.append("event", String(args.event)) if (args.where != null) query.append("where", String(args.where)) if (args.time_in_ms != null) query.append("time_in_ms", String(args.time_in_ms)) if (args['Accept-Encoding'] != null) headers.set("Accept-Encoding", String(args['Accept-Encoding'])) if (ServiceAccount !== undefined) headers.set('authorization', `Basic ${btoa(`${ServiceAccount.username}:${ServiceAccount.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/export${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 res.text() };