import {z} from 'zod' export type ReportScheduleRequestBodyCronProperty = {freq?: ('every_15_minutes' | 'every_30_minutes' | 'hourly' | 'daily' | 'weekly' | 'monthly') | undefined, day_of_month: unknown, day_of_week: unknown, hour: unknown, minute: unknown, time_zone?: ('International Date Line West' | 'Midway Island' | 'American Samoa' | 'Hawaii' | 'Alaska' | 'Pacific Time (US & Canada)' | 'Tijuana' | 'Mountain Time (US & Canada)' | 'Arizona' | 'Chihuahua' | 'Mazatlan' | 'Central Time (US & Canada)' | 'Saskatchewan' | 'Guadalajara' | 'Mexico City' | 'Monterrey' | 'Central America' | 'Eastern Time (US & Canada)' | 'Indiana (East)' | 'Bogota' | 'Lima' | 'Quito' | 'Atlantic Time (Canada)' | 'Caracas' | 'La Paz' | 'Santiago' | 'Newfoundland' | 'Brasilia' | 'Buenos Aires' | 'Montevideo' | 'Georgetown' | 'Puerto Rico' | 'Greenland' | 'Mid-Atlantic' | 'Azores' | 'Cape Verde Is.' | 'Dublin' | 'Edinburgh' | 'Lisbon' | 'London' | 'Casablanca' | 'Monrovia' | 'UTC' | 'Belgrade' | 'Bratislava' | 'Budapest' | 'Ljubljana' | 'Prague' | 'Sarajevo' | 'Skopje' | 'Warsaw' | 'Zagreb' | 'Brussels' | 'Copenhagen' | 'Madrid' | 'Paris' | 'Amsterdam' | 'Berlin' | 'Bern' | 'Zurich' | 'Rome' | 'Stockholm' | 'Vienna' | 'West Central Africa' | 'Bucharest' | 'Cairo' | 'Helsinki' | 'Kyiv' | 'Riga' | 'Sofia' | 'Tallinn' | 'Vilnius' | 'Athens' | 'Istanbul' | 'Minsk' | 'Jerusalem' | 'Harare' | 'Pretoria' | 'Kaliningrad' | 'Moscow' | 'St. Petersburg' | 'Volgograd' | 'Samara' | 'Kuwait' | 'Riyadh' | 'Nairobi' | 'Baghdad' | 'Tehran' | 'Abu Dhabi' | 'Muscat' | 'Baku' | 'Tbilisi' | 'Yerevan' | 'Kabul' | 'Ekaterinburg' | 'Islamabad' | 'Karachi' | 'Tashkent' | 'Chennai' | 'Kolkata' | 'Mumbai' | 'New Delhi' | 'Kathmandu' | 'Astana' | 'Dhaka' | 'Sri Jayawardenepura' | 'Almaty' | 'Novosibirsk' | 'Rangoon' | 'Bangkok' | 'Hanoi' | 'Jakarta' | 'Krasnoyarsk' | 'Beijing' | 'Chongqing' | 'Hong Kong' | 'Urumqi' | 'Kuala Lumpur' | 'Singapore' | 'Taipei' | 'Perth' | 'Irkutsk' | 'Ulaanbaatar' | 'Seoul' | 'Osaka' | 'Sapporo' | 'Tokyo' | 'Yakutsk' | 'Darwin' | 'Adelaide' | 'Canberra' | 'Melbourne' | 'Sydney' | 'Brisbane' | 'Hobart' | 'Vladivostok' | 'Guam' | 'Port Moresby' | 'Magadan' | 'Srednekolymsk' | 'Solomon Is.' | 'New Caledonia' | 'Fiji' | 'Kamchatka' | 'Marshall Is.' | 'Auckland' | 'Wellington' | 'Nuku'alofa' | 'Tokelau Is.' | 'Chatham Is.' | 'Samoa') | undefined}; export type ReportScheduleRequestBodyNameProperty = string; export type ReportScheduleRequestBodyTimeoutProperty = number; export type ReportScheduleRequestBodyParamsProperty = Record; export type CreateReportScheduleRequestBody = {cron: ReportScheduleRequestBodyCronProperty, name?: ReportScheduleRequestBodyNameProperty | undefined, timeout?: ReportScheduleRequestBodyTimeoutProperty | undefined, params?: ReportScheduleRequestBodyParamsProperty | undefined}; export type CreateApiWorkspaceReportsReportSchedulesArgs = {workspace: string, report: string, body: CreateReportScheduleRequestBody}; export type CreateApiWorkspaceReportsReportSchedulesOptions = {baseUrl?: string | undefined, accessTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({href: z.string(), templated: z.boolean().optional()}); export const reportScheduleLinks = z.object({account: link, creator: link.optional(), self: link, report: link, report_web: link, report_runs: link, report_subscriptions: link}); export const reportScheduleEmbeds = z.unknown(); export const reportSchedule = z.object({name: z.string(), token: z.string(), parameters: z.string(), created_at: z.string(), updated_at: z.string(), retry_state: z.string(), retry_count: z.string(), retry_delay: z.string(), timeout: z.string(), last_run_at: z.string(), last_succeeded_at: z.string(), last_scheduled_run: z.string(), next_scheduled_run: z.string(), report_name: z.string(), delivery: z.string(), report_token: z.string(), report_type: z.string(), subscribed: z.string(), subscriber_count: z.string(), _links: reportScheduleLinks, _embedded: reportScheduleEmbeds.optional()}); 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 schedule * * POST /{workspace}/reports/{report}/schedules */ export const createApiWorkspaceReportsReportSchedules = async (args: CreateApiWorkspaceReportsReportSchedulesArgs, options?: CreateApiWorkspaceReportsReportSchedulesOptions) => { const {baseUrl = "https://modeanalytics.com/api", fetch: fetchFn = fetch, accessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/hal+json") if (accessTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${accessTokenAuth.username}:${accessTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.workspace))}/reports/${encodeURIComponent(String(args.report))}/schedules${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(reportSchedule, await res.json()) };