import {z} from 'zod' export type PlacementDetailsObject = {display_mode: 'OVERLAY' | 'FULLSCREEN' | 'PULLUP' | 'INLINE', layout_id?: string | undefined, widget_id?: string | undefined, view: 'ORDER_STATUS' | 'CHECKOUT' | 'THANK_YOU' | 'POST_ORDER' | 'ORDER_TRACKING' | 'SUPPORT_CENTER' | 'REWARDED' | 'EMAIL', sub_view?: ('DEFAULT' | 'POST_CLAIM' | 'BOOKING' | 'PURCHASE' | 'BOOKING_AND_PURCHASE') | undefined}; export type CostDetailsSummary = {amount?: number | undefined, currency_code?: string | undefined}; export type PurchaseAttributes = {is_clearance?: boolean | undefined, is_gift?: boolean | undefined}; export type ProductSummaryDto = {name?: string | undefined, type?: string | undefined, variant?: {[key: string]: string} | undefined, quantity?: number | undefined, description?: string | undefined, cost_details?: CostDetailsSummary | undefined, product_id?: string | undefined, attributes?: PurchaseAttributes | undefined}; export type AddressRequestDto = {address_line_1?: string | undefined, address_line_2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, phone?: string | undefined}; export type ExperimentSummaryDTO = {name: string, version: number}; export type RecommendationsRequest = {user_details: {email: string, title?: ('MR' | 'MRS' | 'MS' | 'MISS') | undefined, first_name?: string | undefined, last_name?: string | undefined, phone?: string | undefined, gender?: ('MALE' | 'FEMALE' | 'OTHER' | 'UNSPECIFIED') | undefined, dob?: string | undefined, age?: number | undefined} | {email_hash: string, title?: ('MR' | 'MRS' | 'MS' | 'MISS') | undefined, first_name?: string | undefined, last_name?: string | undefined, phone?: string | undefined, gender?: ('MALE' | 'FEMALE' | 'OTHER' | 'UNSPECIFIED') | undefined, dob?: string | undefined, age?: number | undefined} | {phone: string, title?: ('MR' | 'MRS' | 'MS' | 'MISS') | undefined, first_name?: string | undefined, last_name?: string | undefined, gender?: ('MALE' | 'FEMALE' | 'OTHER' | 'UNSPECIFIED') | undefined, dob?: string | undefined, age?: number | undefined} | {external_guid: string, title?: ('MR' | 'MRS' | 'MS' | 'MISS') | undefined, first_name?: string | undefined, last_name?: string | undefined, phone?: string | undefined, gender?: ('MALE' | 'FEMALE' | 'OTHER' | 'UNSPECIFIED') | undefined, dob?: string | undefined, age?: number | undefined}, placement_details: PlacementDetailsObject, order_details?: ({cost_details: CostDetailsSummary, order_id: string, payment_type?: string | undefined}) | undefined, purchases?: Array | undefined, attributes?: ({user_agent?: string | undefined, device_type?: ('MOBILE' | 'TABLET' | 'DESKTOP') | undefined, operating_system?: string | undefined, operating_system_version?: string | undefined, ip_address?: string | undefined, ip_address_hash?: string | undefined, platform_type?: ('MOBILE' | 'WEB') | undefined, language?: string | undefined}) | undefined, custom_metadata?: ({is_sandbox?: boolean | undefined, is_add_to_order_customer?: boolean | undefined, is_sports_betting_eligible?: boolean | undefined, is_fam?: boolean | undefined, is_student?: boolean | undefined}) | undefined, shipping?: AddressRequestDto | undefined, billing?: AddressRequestDto | undefined, running_experiments?: Array | undefined}; export type CreateApiRecommendationsArgs = {version: string, body: RecommendationsRequest}; export type CreateApiRecommendationsOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const resolvedWidget = z.object({widget_id: z.string().max(64), layout_id: z.string().max(64).nullable().optional(), widget_mode: z.enum(["OVERLAY", "INLINE", "FULLSCREEN", "PULLUP"]), widget_type: z.enum(["LEAD_GEN", "ESSENTIAL", "APP_NATIVE_ESSENTIAL", "APP_NATIVE_SCROLLABLE", "APP_NATIVE_HYBRID", "APP_NATIVE_BANNER_VISUAL", "APP_NATIVE_BRAND_VISUAL", "APP_NATIVE_GALLERY", "SHOPIFY_NATIVE_ESSENTIAL", "SHOPIFY_NATIVE_LEAD_GEN"]), widget_version: z.number(), content: z.record(z.string(), z.string().nullable())}).nullable(); export const experimentSummaryDTO = z.object({name: z.string(), version: z.number().int().gte(0).lte(999)}); export const recommendationsResponse = z.object({session_id: z.string(), resolved_widget: resolvedWidget.optional(), recommendations: z.record(z.string(), z.object({advertiser: z.object({id: z.string(), name: z.string(), logo_url: z.string(), description: z.string().optional()}), offer: z.object({id: z.string(), heading: z.string(), subheading: z.string().optional(), json_content: z.record(z.string(), z.unknown()).nullable().optional(), target_url: z.string(), cta_details: z.object({accept_copy: z.string(), decline_copy: z.string(), transition_copy: z.string().optional(), claimed_accept_copy: z.string().optional(), claimed_decline_copy: z.string().optional()}), assets: z.array(z.object({id: z.string(), asset_type: z.enum(["PRODUCT", "IMAGE"]), image_url: z.string(), insertion_order: z.number().int().gte(0), target_url: z.string().optional(), should_trigger_claim_flow: z.boolean()})).optional(), metadata: z.union([z.object({code: z.string(), type: z.literal("PROMO_CODE"), layout_type: z.enum(["TEXT_RICH", "BRAND_VISUAL", "FEATURED_IMAGE", "BANNER_VISUAL", "STACKED_TILE", "LAYERED_TILE", "PRODUCT_VISUAL"])}), z.object({type: z.literal("NO_PROMO_CODE"), layout_type: z.enum(["TEXT_RICH", "BRAND_VISUAL", "FEATURED_IMAGE", "BANNER_VISUAL", "STACKED_TILE", "LAYERED_TILE", "PRODUCT_VISUAL"])})]), disclosure_details: z.object({copy: z.string(), target_url: z.string().optional(), content: z.string().nullable().optional()}).optional()})})), recommendation_groups: z.record(z.string(), z.array(z.object({insertion_order: z.number().int().gte(0), insertion_id: z.string(), offer_id: z.string(), advertiser_id: z.string()}))), recommendation_group_properties: z.record(z.string(), z.object({heading: z.string().optional(), slot_context: z.enum(["HERO", "SCROLLABLE"])})).optional(), running_experiments: z.array(experimentSummaryDTO).optional(), footer: z.object({attribution_details: z.object({copy: z.string(), disco_logo_url: z.string(), target_url: z.string()}), privacy_policy: z.object({copy: z.string(), target_url: z.string()})})}); 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 }; /** * Get Disco Advertiser Recommendations * * POST /recommendations */ export const createApiRecommendations = async (args: CreateApiRecommendationsArgs, options?: CreateApiRecommendationsOptions) => { const {baseUrl = "https://partners.disconetwork.com", fetch: fetchFn = fetch, ApiKey} = 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)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/recommendations${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(recommendationsResponse, await res.json()) };