import {z} from 'zod' export type ProductsSet = {quantity: number, publisherProductId: string}; export type ProductsSequence = {index: number, priceInUsdCents: number, playerAvailability?: number | undefined, products: Array}; export type CreatePopupDto = {publisherOfferId: string, name: string, displayName?: string | undefined, description?: string | undefined, type: string, subType: string, priority?: number | undefined, offerUiId: string, offerExternalUiId?: string | undefined, active: boolean, startOver?: boolean | undefined, triggers: Array<{type?: 'event' | undefined, eventName?: 'purchase' | undefined, every?: number | undefined, rules?: (Array<{fieldName?: 'publisherOfferId' | undefined, operator?: 'in' | undefined, value?: Array | undefined}>) | undefined}>, segments: Array, productsSequence: Array}; export type CreateApiOfferingOfferPopupArgs = {'x-publisher-token': string, body: CreatePopupDto}; export type CreateApiOfferingOfferPopupOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const offerUiDto = z.object({offerUiId: z.string().optional(), externalId: z.string().optional(), active: z.boolean().optional(), offerUiType: z.string().optional(), name: z.string().optional(), description: z.string().optional(), backgroundImage: z.string().optional(), specialOffer: z.object({templateType: z.string().optional(), title: z.string().optional(), fontSize: z.number().optional(), fontWeight: z.string().optional(), fontColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), direction: z.string().optional()}).optional(), backgroundColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), direction: z.string().optional()}).optional()}).optional()}); export const productDto = z.object({publisherProductId: z.string().optional(), name: z.string().optional(), textFontColorHex: z.string().optional(), type: z.string().optional(), prefix: z.string().optional(), suffix: z.string().optional(), priority: z.string().optional(), images: z.array(z.object({type: z.string().optional(), url: z.string().optional()})).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), productId: z.string().optional()}); export const productsSetResponse = z.object({quantity: z.number().optional(), product: productDto.optional()}); export const productsSequenceResponse = z.object({index: z.number().optional(), playerAvailability: z.number().optional(), priceInUsdCents: z.number().optional(), products: z.array(productsSetResponse).optional()}); export const popupDtoResponse = z.object({offerId: z.string().optional(), publisherOfferId: z.string().optional(), name: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), type: z.string().optional(), subType: z.string().optional(), createdBy: z.string().optional(), offerUi: offerUiDto.optional(), active: z.boolean().optional(), coolDownInHours: z.number().optional(), segments: z.array(z.string()).optional(), productsSequence: z.array(productsSequenceResponse).optional(), startOver: z.boolean().optional(), priority: z.number().optional(), showAfter: z.string().optional(), triggers: z.array(z.object({type: z.literal("event").optional(), eventName: z.literal("purchase").optional(), every: z.number().int().optional(), rules: z.array(z.object({fieldName: z.literal("publisherOfferId").optional(), operator: z.literal("in").optional(), value: z.array(z.string()).optional()})).optional()})).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); 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 }; /** * POST /offering/offer/popup */ export const createApiOfferingOfferPopup = async (args: CreateApiOfferingOfferPopupArgs, options: CreateApiOfferingOfferPopupOptions) => { const {baseUrl, 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['x-publisher-token'] != null) headers.set("x-publisher-token", String(args['x-publisher-token'])) const url = `${baseUrl.replace(/\/+$/, '')}/offering/offer/popup${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(popupDtoResponse, await res.json()) };