import {z} from 'zod' export type ComponentReferenceRequest = {component_id?: string | undefined, socket_names?: Array | undefined}; export type MergingConfigurationRequest = {deliverable_type?: string | undefined, formats?: (Array<'CSV' | 'HEADLESS_CSV' | 'HEADLESS_PSV' | 'JSON' | 'JSONL' | 'PSV' | 'XLSX'>) | undefined, sort_by?: Array | undefined, unique_by?: Array | undefined}; export type PauseInfoRequest = {description?: (string | null) | undefined, paused?: boolean | undefined}; export type ReportNamePatternInReportRunnerCreateRequestBase = string | string; export type SftpReportNamePatternInReportRunnerCreateRequestBase = string | string; export type ZonedDateTime = string; export type ReportRunnerCreateRequest = {component_ids?: Array | undefined, component_references?: Array | undefined, deliver_empty_reports_to_sftp?: (boolean | null) | undefined, enabled?: boolean | undefined, execution_policy?: ('AWAIT_DATA' | 'IMMEDIATE') | undefined, formats?: (Array<'CSV' | 'HEADLESS_CSV' | 'HEADLESS_PSV' | 'JSON' | 'JSONL' | 'PSV' | 'XLSX'>) | undefined, merging_configuration?: MergingConfigurationRequest | undefined, name: string, parameters?: {[key: string]: string} | undefined, pause_info?: PauseInfoRequest | undefined, report_name_pattern?: ReportNamePatternInReportRunnerCreateRequestBase | undefined, report_type: string, scopes?: (Array<'CLIENT_ADMIN' | 'CLIENT_SUPERUSER' | 'CONSUMER_PUBLIC'>) | undefined, sftp_report_name_pattern?: SftpReportNamePatternInReportRunnerCreateRequestBase | undefined, sftp_server_id?: (string | null) | undefined, tags?: Array | undefined, type: 'REFRESHING', expiration_ms?: number | undefined} | {component_ids?: Array | undefined, component_references?: Array | undefined, deliver_empty_reports_to_sftp?: (boolean | null) | undefined, enabled?: boolean | undefined, execution_policy?: ('AWAIT_DATA' | 'IMMEDIATE') | undefined, formats?: (Array<'CSV' | 'HEADLESS_CSV' | 'HEADLESS_PSV' | 'JSON' | 'JSONL' | 'PSV' | 'XLSX'>) | undefined, merging_configuration?: MergingConfigurationRequest | undefined, name: string, parameters?: {[key: string]: string} | undefined, pause_info?: PauseInfoRequest | undefined, report_name_pattern?: ReportNamePatternInReportRunnerCreateRequestBase | undefined, report_type: string, scopes?: (Array<'CLIENT_ADMIN' | 'CLIENT_SUPERUSER' | 'CONSUMER_PUBLIC'>) | undefined, sftp_report_name_pattern?: SftpReportNamePatternInReportRunnerCreateRequestBase | undefined, sftp_server_id?: (string | null) | undefined, tags?: Array | undefined, type: 'SCHEDULED', frequency?: ('DAILY' | 'HOURLY' | 'LOGIN' | 'MONTHLY' | 'QUARTERLY' | 'WEEKLY') | undefined, legacy_sftp_report_name_format?: boolean | undefined, schedule_start_date: ZonedDateTime}; export type CreateApiV7ReportRunnersArgs = {body: ReportRunnerCreateRequest}; export type CreateApiV7ReportRunnersOptions = {baseUrl?: string | undefined, HEADER?: string | undefined, fetch?: typeof fetch | undefined}; export const componentReferenceResponse = z.object({component_id: z.string().optional(), socket_names: z.array(z.string()).optional()}); export const zonedDateTime = z.string(); export const mergingConfigurationResponse = z.object({deliverable_type: z.enum(["ACCUMULATING", "INDIVIDUAL", "NEW_ENTRIES"]).optional(), formats: z.array(z.enum(["CSV", "HEADLESS_CSV", "HEADLESS_PSV", "JSON", "JSONL", "PSV", "XLSX"])).optional(), sort_by: z.array(z.string()).optional(), unique_by: z.array(z.string()).optional()}); 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 instant = z.string(); export const pauseInfoResponse = z.object({description: z.string().optional(), updated_date: instant.optional(), user_id: z.string().optional()}); export const reportNamePatternInReportRunnerResponseBase = z.union([z.string(), z.string()]); export const sftpReportNamePatternInReportRunnerResponseBase = z.union([z.string(), z.string()]); export const reportRunnerResponse = z.discriminatedUnion("type", [z.object({component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, deliver_empty_reports_to_sftp: z.boolean(), enabled: z.boolean(), execution_policy: z.enum(["AWAIT_DATA", "IMMEDIATE"]), formats: z.array(z.enum(["CSV", "HEADLESS_CSV", "HEADLESS_PSV", "JSON", "JSONL", "PSV", "XLSX"])), id: z.string(), merging_configuration: mergingConfigurationResponse, name: z.string(), parameters: z.record(z.string(), reportParameterResponse), pause_info: pauseInfoResponse, paused: pauseInfoResponse, report_name_pattern: reportNamePatternInReportRunnerResponseBase, report_type: z.string(), report_type_name: z.string(), scopes: z.array(z.enum(["CLIENT_ADMIN", "CLIENT_SUPERUSER", "CONSUMER_PUBLIC"])), sftp_report_name_pattern: sftpReportNamePatternInReportRunnerResponseBase, sftp_server_id: z.string().nullable(), tags: z.array(z.string()), type: z.literal("REFRESHING"), updated_date: zonedDateTime, user_id: z.string(), expiration_ms: z.number().int()}), z.object({component_ids: z.array(z.string()), component_references: z.array(componentReferenceResponse), created_date: zonedDateTime, deliver_empty_reports_to_sftp: z.boolean(), enabled: z.boolean(), execution_policy: z.enum(["AWAIT_DATA", "IMMEDIATE"]), formats: z.array(z.enum(["CSV", "HEADLESS_CSV", "HEADLESS_PSV", "JSON", "JSONL", "PSV", "XLSX"])), id: z.string(), merging_configuration: mergingConfigurationResponse, name: z.string(), parameters: z.record(z.string(), reportParameterResponse), pause_info: pauseInfoResponse, paused: pauseInfoResponse, report_name_pattern: reportNamePatternInReportRunnerResponseBase, report_type: z.string(), report_type_name: z.string(), scopes: z.array(z.enum(["CLIENT_ADMIN", "CLIENT_SUPERUSER", "CONSUMER_PUBLIC"])), sftp_report_name_pattern: sftpReportNamePatternInReportRunnerResponseBase, sftp_server_id: z.string().nullable(), tags: z.array(z.string()), type: z.literal("SCHEDULED"), updated_date: zonedDateTime, user_id: z.string(), frequency: z.enum(["DAILY", "HOURLY", "LOGIN", "MONTHLY", "QUARTERLY", "WEEKLY"]), legacy_sftp_report_name_format: z.boolean(), schedule_start_date: zonedDateTime})]); 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 }; /** * Create a report runner * * POST /v7/report-runners * * Also accepts auth: QUERY, COOKIE */ export const createApiV7ReportRunners = async (args: CreateApiV7ReportRunnersArgs, options?: CreateApiV7ReportRunnersOptions) => { const {baseUrl = "https://api.extole.io", fetch: fetchFn = fetch, HEADER} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HEADER !== undefined) headers.set("Authorization", HEADER) const url = `${baseUrl.replace(/\/+$/, '')}/v7/report-runners${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(reportRunnerResponse, await res.json()) };