import {z} from 'zod' export type PublisherMetric = 'publisher_payout' | 'widget_loads' | 'widget_displays' | 'viewable_widget_displays' | 'impressions' | 'clicks' | 'conversions' | 'ctr' | 'cvr'; export type GetApiAnalysisPublisherReportingV2ReportArgs = {from?: string | undefined, to?: string | undefined, metrics?: Array | undefined, time_grain?: ('total' | 'day' | 'week' | 'month' | 'hour') | undefined, group_by?: (Array<'page_type' | 'widget_type' | 'widget_id'>) | undefined, sort_order?: ('desc' | 'asc') | undefined, page_types?: Array | undefined, widget_types?: Array | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiAnalysisPublisherReportingV2ReportOptions = {baseUrl?: string | undefined, PublisherSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const publisherReportRow = z.object({period_start: z.string(), period_end: z.string(), page_type: z.string().nullable().optional(), widget_type: z.string().nullable().optional(), widget_id: z.string().nullable().optional(), publisher_payout: z.number().optional(), widget_loads: z.number().int().gte(0).optional(), widget_displays: z.number().int().gte(0).optional(), viewable_widget_displays: z.number().int().gte(0).optional(), impressions: z.number().int().gte(0).optional(), clicks: z.number().int().gte(0).optional(), conversions: z.number().int().gte(0).optional(), ctr: z.number().gte(0).optional(), cvr: z.number().gte(0).optional()}); export const publisherMetricValues = z.object({publisher_payout: z.number().optional(), widget_loads: z.number().int().gte(0).optional(), widget_displays: z.number().int().gte(0).optional(), viewable_widget_displays: z.number().int().gte(0).optional(), impressions: z.number().int().gte(0).optional(), clicks: z.number().int().gte(0).optional(), conversions: z.number().int().gte(0).optional(), ctr: z.number().gte(0).optional(), cvr: z.number().gte(0).optional()}); export const pagination = z.object({offset: z.number().int().gte(0), limit: z.number().int().gte(1).lte(10000), 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 publisherReportResponse = z.object({data: z.array(publisherReportRow), summary: publisherMetricValues, 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 Publisher Reporting V2 report * * GET /analysis/publisher-reporting/v2/report/ */ export const getApiAnalysisPublisherReportingV2Report = async (args: GetApiAnalysisPublisherReportingV2ReportArgs, options?: GetApiAnalysisPublisherReportingV2ReportOptions) => { const {baseUrl = "https://merchant.disconetwork.com/api", fetch: fetchFn = fetch, PublisherSecretKey} = 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.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 (PublisherSecretKey !== undefined) headers.set("X-API-Key", PublisherSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/analysis/publisher-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(publisherReportResponse, await res.json()) };