import {z} from 'zod' export type GetApiApiV2OrgIdReportsHistoryArgs = {org_id: string, report_id?: string | undefined, run_report_id?: string | undefined, start_time?: number | undefined, end_time?: number | undefined, status?: Array | undefined, trigger_source?: string | undefined, is_cached?: boolean | undefined, from?: number | undefined, size?: number | undefined, retries?: number | undefined, sort_by?: string | undefined, sort_order?: string | undefined}; export type GetApiApiV2OrgIdReportsHistoryOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const reportRunCounts = z.object({cached: z.number().int(), error: z.number().int(), skipped: z.number().int(), succeeded: z.number().int(), total: z.number().int()}); export const reportRunHit = z.object({attachment_type: z.string().nullable().optional(), dashboard_count: z.number().int().nullable().optional(), delay_in_secs: z.number().int().nullable().optional(), delivery_attempted: z.boolean().nullable().optional(), duration_ms: z.number().int().nullable().optional(), error: z.string().nullable().optional(), finished_at: z.number().int().nullable().optional(), is_cached: z.boolean(), is_partial: z.boolean(), next_run_at: z.number().int().nullable().optional(), recipient_count: z.number().int().nullable().optional(), report_id: z.string(), report_name: z.string(), retries: z.number().int(), scheduled_for: z.number().int().nullable().optional(), scheduler_trace_id: z.string().nullable().optional(), source_node: z.string().nullable().optional(), status: z.string(), time_in_queue_ms: z.number().int().nullable().optional(), timestamp: z.number().int(), trigger_source: z.string(), triggered_by: z.string().nullable().optional()}); export const getApiApiV2OrgIdReportsHistoryResponse = z.object({counts: reportRunCounts, disabled_reason: z.string().nullable().optional(), from: z.number().int(), hits: z.array(reportRunHit), size: z.number().int(), total: z.number().int().gte(0)}); 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 report run history * * GET /api/v2/{org_id}/reports/history */ export const getApiApiV2OrgIdReportsHistory = async (args: GetApiApiV2OrgIdReportsHistoryArgs, options: GetApiApiV2OrgIdReportsHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.report_id != null) query.append("report_id", String(args.report_id)) if (args.run_report_id != null) query.append("run_report_id", String(args.run_report_id)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) for (const item of args.status ?? []) query.append("status", String(item)) if (args.trigger_source != null) query.append("trigger_source", String(args.trigger_source)) if (args.is_cached != null) query.append("is_cached", String(args.is_cached)) if (args.from != null) query.append("from", String(args.from)) if (args.size != null) query.append("size", String(args.size)) if (args.retries != null) query.append("retries", String(args.retries)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/reports/history${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(getApiApiV2OrgIdReportsHistoryResponse, await res.json()) };