import {z} from 'zod' export type CreateorUpdateReportSubscriptionRequestBody = {csv_attachments?: boolean | undefined, pdf_attachments?: boolean | undefined, data_previews?: boolean | undefined, data_tables?: boolean | undefined, report_links?: boolean | undefined}; export type PatchApiAccountReportsReportSubscriptionsSubscriptionArgs = {account: string, report: string, subscription: string, body: CreateorUpdateReportSubscriptionRequestBody}; export type PatchApiAccountReportsReportSubscriptionsSubscriptionOptions = {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 reportSubscriptionLinks = z.object({self: link, report_schedule: link, email_subscription_memberships: link, slack_subscription_memberships: link}); export const reportSubscriptionEmbeds = z.unknown(); export const reportSubscription = z.object({data_previews_enabled: z.boolean(), data_tables_enabled: z.boolean(), csv_attachments_enabled: z.boolean(), pdf_attachments_enabled: z.boolean(), report_links_enabled: z.boolean(), email_subscriber_count: z.number().int(), _links: reportSubscriptionLinks, _embedded: reportSubscriptionEmbeds.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 subscription * * PATCH /{account}/reports/{report}/subscriptions/{subscription} */ export const patchApiAccountReportsReportSubscriptionsSubscription = async (args: PatchApiAccountReportsReportSubscriptionsSubscriptionArgs, options?: PatchApiAccountReportsReportSubscriptionsSubscriptionOptions) => { 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))}/subscriptions/${encodeURIComponent(String(args.subscription))}${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(reportSubscription, await res.json()) };