import {z} from 'zod' export type GetApiAccountDataSourcesDataSourceReportsArgs = {account: string, data_source: string, filter?: string | undefined, order?: string | undefined, order_by?: string | undefined}; export type GetApiAccountDataSourcesDataSourceReportsOptions = {baseUrl?: string | undefined, accessTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const pagination = z.object({first_page: z.string(), last_page: z.string(), total_pages: z.number().int(), total_count: z.number().int()}); export const link = z.object({href: z.string(), templated: z.boolean().optional()}); export const reportsLinks = z.object({next_page: link.optional(), prev_page: link.optional(), self: link}); export const reportsEmbeds = z.unknown(); export const reports = z.object({pagination: pagination.optional(), _links: reportsLinks, _embedded: reportsEmbeds.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 }; /** * List reports using a data source * * GET /{account}/data_sources/{data_source}/reports */ export const getApiAccountDataSourcesDataSourceReports = async (args: GetApiAccountDataSourcesDataSourceReportsArgs, options?: GetApiAccountDataSourcesDataSourceReportsOptions) => { 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 (args.filter != null) query.append("filter", String(args.filter)) if (args.order != null) query.append("order", String(args.order)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (accessTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${accessTokenAuth.username}:${accessTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.account))}/data_sources/${encodeURIComponent(String(args.data_source))}/reports${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(reports, await res.json()) };