import {z} from 'zod' export type EventEventType = 'UNKNOWN_EVENT_TYPE' | 'SEARCH' | 'ITEM_PAGE_VIEW' | 'ADD_TO_CART' | 'PURCHASE' | 'ADD_TO_WISHLIST' | 'HOME' | 'LAND' | 'PAGE_VIEW'; export type CommonChannelType = 'UNKNOWN_CHANNEL_TYPE' | 'APP' | 'SITE' | 'DESKTOP_SITE' | 'MOBILE_SITE'; export type EventDevice = {os?: string | undefined, os_version?: string | undefined, advertising_id?: string | undefined, unique_device_id?: string | undefined, persistent_id?: string | undefined, model?: string | undefined, ua?: string | undefined, language?: string | undefined} | {[key: string]: unknown}; export type CommonCurrency = 'UNKNOWN_CURRENCY' | 'USD' | 'KRW' | 'JPY' | 'EUR' | 'GBP' | 'SEK' | 'INR' | 'THB' | 'IDR' | 'CNY' | 'CAD' | 'RUB' | 'BRL' | 'SGD' | 'HKD' | 'AUD' | 'PLN' | 'DKK' | 'VND' | 'MYR' | 'PHP' | 'TRY' | 'VES' | 'AED' | 'CHF'; export type CommonMoney = {currency: CommonCurrency, amount: number} | {[key: string]: unknown}; export type EventItem = {id: string, price?: CommonMoney | undefined, quantity?: number | undefined, item_group_id?: string | undefined, seller_id?: string | undefined, price_seller?: CommonMoney | undefined} | {[key: string]: unknown}; export type CommonPersonalizationMode = 'UNKNOWN_PERSONALIZATION_MODE' | 'STANDARD' | 'METRIC_ONLY' | 'ID_LESS'; export type V1Event = {id?: string | undefined, event_type: EventEventType, timestamp: unknown, channel_type: CommonChannelType, domain?: string | undefined, user_id?: string | undefined, device?: EventDevice | undefined, items?: Array | undefined, revenue?: CommonMoney | undefined, search_query?: string | undefined, session_id?: string | undefined, custom_id?: string | undefined, page_id?: string | undefined, referrer_page_id?: string | undefined, shipping_charge?: CommonMoney | undefined, decision_track_id?: string | undefined, personalization_mode?: CommonPersonalizationMode | undefined} | {[key: string]: unknown}; export type CreateApiRmpEventV1PlatformsPlatformUsereventsArgs = {platform: string, body: V1Event}; export type CreateApiRmpEventV1PlatformsPlatformUsereventsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v1PostUserEventResponse = z.record(z.string(), z.unknown()); 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 }; /** * PostUserEvent * * POST /rmp/event/v1/platforms/{platform}/userevents */ export const createApiRmpEventV1PlatformsPlatformUserevents = async (args: CreateApiRmpEventV1PlatformsPlatformUsereventsArgs, options?: CreateApiRmpEventV1PlatformsPlatformUsereventsOptions) => { const {baseUrl = "https://sandbox-dcsn.mcm-api.moloco.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/rmp/event/v1/platforms/${encodeURIComponent(String(args.platform))}/userevents${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(v1PostUserEventResponse, await res.json()) };