import {z} from 'zod' export type GetApiWorkspaceReportsReportSchedulesScheduleArgs = {workspace: string, report: string, schedule: string}; export type GetApiWorkspaceReportsReportSchedulesScheduleOptions = {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 }; /** * Get a report schedule * * GET /{workspace}/reports/{report}/schedules/{schedule} */ export const getApiWorkspaceReportsReportSchedulesSchedule = async (args: GetApiWorkspaceReportsReportSchedulesScheduleArgs, options?: GetApiWorkspaceReportsReportSchedulesScheduleOptions) => { const {baseUrl = "https://modeanalytics.com/api", fetch: fetchFn = fetch, accessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/${encodeURIComponent(String(args.schedule))}${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(reportSchedule, await res.json()) };