import {z} from 'zod' export type ChannelMetric = 'impressions' | 'clicks' | 'conversions' | 'channel_payout' | 'revenue_with_email' | 'revenue_without_email' | 'feed_loads' | 'widget_displays' | 'viewable_widget_displays' | 'order_revenue_amount' | 'attributed_ad_spend_amount' | 'billable_ad_spend_amount' | 'ctr' | 'cvr' | 'rpl' | 'attributed_cpa'; export type GetApiDiscobeatReportingV2ReportArgs = {from?: string | undefined, to?: string | undefined, metrics?: Array | undefined, time_grain?: ('total' | 'day' | 'week' | 'month' | 'hour') | undefined, group_by?: (Array<'publisher' | 'page_type' | 'widget_type' | 'widget_id'>) | undefined, sort_order?: ('desc' | 'asc') | undefined, sort_by?: string | undefined, sort_direction?: ('asc' | 'desc') | undefined, publisher_ids?: Array | undefined, page_types?: Array | undefined, widget_types?: Array | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiDiscobeatReportingV2ReportOptions = {baseUrl?: string | undefined, ChannelSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const channelReportRow = z.object({period_start: z.string(), period_end: z.string(), publisher_id: z.string().nullable().optional(), publisher_name: z.string().nullable().optional(), page_type: z.string().nullable().optional(), widget_type: z.string().nullable().optional(), widget_id: z.string().nullable().optional(), impressions: z.number().int().gte(0).optional(), clicks: z.number().int().gte(0).optional(), conversions: z.number().int().gte(0).optional(), channel_payout: z.number().optional(), revenue_with_email: z.number().optional(), revenue_without_email: z.number().optional(), feed_loads: z.number().int().gte(0).optional(), widget_displays: z.number().int().gte(0).optional(), viewable_widget_displays: z.number().int().gte(0).optional(), order_revenue_amount: z.number().optional(), attributed_ad_spend_amount: z.number().optional(), billable_ad_spend_amount: z.number().optional(), ctr: z.number().gte(0).optional(), cvr: z.number().gte(0).optional(), rpl: z.number().gte(0).optional(), attributed_cpa: z.number().gte(0).optional()}); export const channelMetricValues = z.object({impressions: z.number().int().gte(0).optional(), clicks: z.number().int().gte(0).optional(), conversions: z.number().int().gte(0).optional(), channel_payout: z.number().optional(), revenue_with_email: z.number().optional(), revenue_without_email: z.number().optional(), feed_loads: z.number().int().gte(0).optional(), widget_displays: z.number().int().gte(0).optional(), viewable_widget_displays: z.number().int().gte(0).optional(), order_revenue_amount: z.number().optional(), attributed_ad_spend_amount: z.number().optional(), billable_ad_spend_amount: z.number().optional(), ctr: z.number().gte(0).optional(), cvr: z.number().gte(0).optional(), rpl: z.number().gte(0).optional(), attributed_cpa: z.number().gte(0).optional()}); export const pagination = z.object({offset: z.number().int().gte(0), limit: z.number().int().gte(1).lte(250), total: z.number().int().gte(0), has_more: z.boolean()}); export const reportMeta = z.object({timezone: z.literal("UTC"), data_through: z.string().nullable(), request_id: z.string().nullable()}); export const channelReportResponse = z.object({data: z.array(channelReportRow), summary: channelMetricValues, pagination: pagination, meta: reportMeta}); 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 }; /** * Build a Channel Reporting V2 report * * GET /discobeat/reporting/v2/report/ */ export const getApiDiscobeatReportingV2Report = async (args: GetApiDiscobeatReportingV2ReportArgs, options?: GetApiDiscobeatReportingV2ReportOptions) => { const {baseUrl = "https://api.disconetwork.com", fetch: fetchFn = fetch, ChannelSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.metrics != null) query.append("metrics", args.metrics.join(',')) if (args.time_grain != null) query.append("time_grain", String(args.time_grain)) if (args.group_by != null) query.append("group_by", args.group_by.join(',')) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.publisher_ids != null) query.append("publisher_ids", args.publisher_ids.join(',')) if (args.page_types != null) query.append("page_types", args.page_types.join(',')) if (args.widget_types != null) query.append("widget_types", args.widget_types.join(',')) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (ChannelSecretKey !== undefined) headers.set("X-API-Key", ChannelSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/discobeat/reporting/v2/report/${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(channelReportResponse, await res.json()) };