import {z} from 'zod' export type WidgetEventRequest = {event_name: 'WIDGET_LOAD' | 'WIDGET_DISPLAY' | 'WIDGET_VIEW' | 'BRAND_DISPLAY' | 'BRAND_VIEW' | 'BRAND_CLICK' | 'PRODUCT_DISPLAY' | 'PRODUCT_VIEW' | 'PRODUCT_CLICK' | 'ELEMENT_CLICK', event_datetime: string, session_id: string}; export type BrandEventProperties = {offer_id: string, advertiser_id: string, insertion_id: string}; export type ElementClickRequest = {event_name: 'WIDGET_LOAD' | 'WIDGET_DISPLAY' | 'WIDGET_VIEW' | 'BRAND_DISPLAY' | 'BRAND_VIEW' | 'BRAND_CLICK' | 'PRODUCT_DISPLAY' | 'PRODUCT_VIEW' | 'PRODUCT_CLICK' | 'ELEMENT_CLICK', event_datetime: string, session_id: string, initiating_target: 'INLINE_NAV_FORWARD' | 'INLINE_NAV_BACKWARD' | 'INLINE_FOOTER_DISCO' | 'INLINE_FOOTER_PRIVACY', brand_properties?: BrandEventProperties | undefined}; export type BrandDisplayRequest = {event_name: 'WIDGET_LOAD' | 'WIDGET_DISPLAY' | 'WIDGET_VIEW' | 'BRAND_DISPLAY' | 'BRAND_VIEW' | 'BRAND_CLICK' | 'PRODUCT_DISPLAY' | 'PRODUCT_VIEW' | 'PRODUCT_CLICK' | 'ELEMENT_CLICK', event_datetime: string, session_id: string, initiating_target: 'INLINE_SLOT', brand_properties: BrandEventProperties}; export type BrandClickRequest = {event_name: 'WIDGET_LOAD' | 'WIDGET_DISPLAY' | 'WIDGET_VIEW' | 'BRAND_DISPLAY' | 'BRAND_VIEW' | 'BRAND_CLICK' | 'PRODUCT_DISPLAY' | 'PRODUCT_VIEW' | 'PRODUCT_CLICK' | 'ELEMENT_CLICK', event_datetime: string, session_id: string, initiating_target: 'INLINE_ACTION_BUTTON' | 'INLINE_ASSET_TILE', asset_id?: string | undefined, brand_properties: BrandEventProperties}; export type EventRequest = WidgetEventRequest | ElementClickRequest | BrandDisplayRequest | BrandClickRequest; export type BatchEventRequest = {events: Array}; export type CreateApiEventsBatchArgs = {version: string, body: BatchEventRequest}; export type CreateApiEventsBatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const batchEventResult = z.object({index: z.number().int(), success: z.boolean()}); export const batchEventResponse = z.object({results: z.array(batchEventResult), accepted: z.number().int(), failed: z.number().int()}); 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 }; /** * Create a batch of events used to record user actions * * POST /events/batch */ export const createApiEventsBatch = async (args: CreateApiEventsBatchArgs, options?: CreateApiEventsBatchOptions) => { const {baseUrl = "https://partners.disconetwork.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.version != null) headers.set("version", String(args.version)) const url = `${baseUrl.replace(/\/+$/, '')}/events/batch${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(batchEventResponse, await res.json()) };