import {z} from 'zod' export type GetApiV4ReportsReportIdViewsPublicArgs = {reportId: string}; export type GetApiV4ReportsReportIdViewsPublicOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const zonedDateTime = z.string(); export const reportParameterTypeResponse = z.object({name: z.enum(["AUDIENCE_ID", "AUDIENCE_IDS", "AUDIENCE_MEMBERSHIPS_METRIC_MAPPING", "AUDIENCE_MEMBERSHIPS_ROW_MAPPINGS", "AUDIENCE_MEMBERS_ROW_MAPPINGS", "BOOLEAN", "CAMPAIGN_ID", "CAMPAIGN_IDS", "CHANNEL_MAPPING", "CLIENT_EVENT_METRIC_MAPPINGS", "CLIENT_ID", "CLIENT_IDS", "DATE", "DATE_RANGE", "DECIMAL", "EMAIL_NAMES", "ENUM", "ENUM_LIST", "EQUALITY", "FILTER_MAPPINGS", "FLOWS", "FLOW_FILTER", "GRANULAR_TIME_RANGE", "ID", "IDS", "INPUT_RECORD_METRIC_MAPPING", "INPUT_RECORD_ROW_MAPPINGS", "INTEGER", "LONG", "MESSAGE_SUMMARY_METRIC_MAPPINGS", "METRIC_MAPPINGS", "MODEL_CONFIGURATION_METRIC_MAPPING", "NORMALIZED_EMAIL", "NOT_ARCHIVED_CAMPAIGN_ID", "PERSON_DIMENSION_MAPPINGS", "PERSON_MAPPINGS", "PROGRAM_LABEL", "PROGRAM_LABELS", "REGEX", "REWARD_EVENT_METRIC_MAPPINGS", "REWARD_ROW_MAPPINGS", "REWARD_SUPPLIER_DISPLAY_TYPES", "REWARD_SUPPLIER_IDS", "REWARD_SUPPLIER_TYPES", "ROW_MAPPINGS", "SORT_ORDER_MAPPINGS", "SORT_ORDER_MAPPINGS_ON_STEPS", "STEP", "STEPS", "STEP_COLUMN_LIST", "STRING", "STRING_LIST", "TIERS", "TIME", "TIME_RANGE", "WEBHOOK_DISPATCH_METRIC_MAPPING", "WEBHOOK_DISPATCH_ROW_MAPPINGS", "WEBHOOK_METRIC_MAPPING", "WEBHOOK_ROW_MAPPINGS"]).optional(), value_type: z.enum(["DYNAMIC", "STATIC"]).nullable().optional(), values: z.array(z.string()).nullable().optional()}); export const reportParameterDetailsResponse = z.object({category: z.string().nullable().optional(), display_name: z.string().optional(), is_required: z.boolean().optional(), name: z.string().optional(), order: z.number().int().optional(), type: reportParameterTypeResponse.optional()}); export const reportParameterResponse = z.object({details: reportParameterDetailsResponse.optional(), value: z.string().optional()}); export const formatReportInfoResponse = z.object({content_length: z.number().int(), total_rows: z.number().int()}); export const publicReportResultResponse = z.object({completed_date: zonedDateTime.optional(), formats_info: z.record(z.string(), formatReportInfoResponse).optional(), started_date: zonedDateTime.optional(), status: z.enum(["CANCELED", "DONE", "EXPIRED", "FAILED", "IN_PROGRESS", "PENDING", "PENDING_DATA", "SFTP_DELIVERY_FAILED"]).optional(), total_rows: z.number().int().optional()}); export const publicReportResponse = z.object({created_date: zonedDateTime, display_name: z.string(), download_uri: z.string(), executor_type: z.enum(["DASHBOARD", "SPARK", "SQL"]), formats: z.array(z.enum(["CSV", "HEADLESS_CSV", "HEADLESS_PSV", "JSON", "JSONL", "PSV", "XLSX"])), id: z.string(), parameters: z.record(z.string(), reportParameterResponse), report_type: z.string(), result: publicReportResultResponse, scopes: z.array(z.enum(["CLIENT_ADMIN", "CLIENT_SUPERUSER", "CONSUMER_PUBLIC"])), tags: z.array(z.string()), user_id: z.string()}); 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 public view of a report * * GET /v4/reports/{reportId}/views/public * * Also accepts auth: QUERY, COOKIE */ export const getApiV4ReportsReportIdViewsPublic = async (args: GetApiV4ReportsReportIdViewsPublicArgs, options?: GetApiV4ReportsReportIdViewsPublicOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v4/reports/${encodeURIComponent(String(args.reportId))}/views/public${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(publicReportResponse, await res.json()) };