import {z} from 'zod' export type UpdateReportRequestBody = {report: {name?: string | undefined, description?: string | undefined, space_token?: string | undefined}}; export type PatchApiAccountReportsReportArgs = {account: string, report: string, body: UpdateReportRequestBody}; export type PatchApiAccountReportsReportOptions = {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 reportLinks = z.object({self: link, web: link, web_edit: link.optional(), web_external_url: link.optional(), csv_export: link.optional(), share: link.optional(), web_report_runs: link.optional(), account: link, report_run: link, star: link.optional(), space: link.optional(), space_links: link.optional(), queries: link, report_runs: link, report_pins: link, report_filters: link.optional(), report_schedules: link, report_subscriptions: link.optional(), dataset_dependencies: link.optional(), python_visualizations: link.optional(), embed_key: link.optional(), last_run: link, last_successful_run: link, python_notebook: link.optional(), perspective_email_subscription_memberships: link, validate_email_subscriber: link.optional(), creator: link.optional(), report_theme: link.optional(), last_successful_github_sync: link, report_index_web: link.optional(), dbt_metadata: link.optional()}); export const reportEmbeds = z.unknown(); export const report = z.object({token: z.string(), id: z.number().int(), name: z.string(), description: z.string().optional(), created_at: z.string(), updated_at: z.string(), published_at: z.string(), edited_at: z.string(), theme_id: z.number().int().optional(), color_mappings: z.string().optional(), type: z.string(), last_successful_sync_at: z.string(), last_saved_at: z.string(), archived: z.boolean(), space_token: z.string().optional(), account_id: z.number().int(), account_username: z.string(), public: z.boolean(), full_width: z.boolean().optional(), manual_run_disabled: z.boolean(), drill_anywhere_enabled: z.boolean(), run_privately: z.boolean(), drilldowns_enabled: z.boolean(), layout: z.string().optional(), is_embedded: z.boolean().optional(), is_signed: z.boolean().optional(), imported_datasets: z.string().optional(), shared: z.boolean().optional(), expected_runtime: z.number(), last_successfully_run_at: z.string(), last_run_at: z.string(), web_preview_image: z.string().optional(), last_successful_run_token: z.string(), github_link: z.string().optional(), query_count: z.string(), max_query_count: z.string(), chart_count: z.string().optional(), runs_count: z.string(), schedules_count: z.string(), query_preview: z.string(), view_count: z.string(), thoughtspot_published_at: z.string(), _links: reportLinks, _embedded: reportEmbeds.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 }; /** * Update a report * * PATCH /{account}/reports/{report} */ export const patchApiAccountReportsReport = async (args: PatchApiAccountReportsReportArgs, options?: PatchApiAccountReportsReportOptions) => { 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.account))}/reports/${encodeURIComponent(String(args.report))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(report, await res.json()) };