import {z} from 'zod' export type DynamicOfferUiDto = {badges?: (Array<{publisherBadgeId?: string | undefined, position?: string | undefined}>) | undefined, salePercentage?: number | undefined, salePercentageDisplayType?: string | undefined}; export type ProductsSet = {quantity: number, publisherProductId: string}; export type ProductsSequence = {index: number, priceInUsdCents: number, playerAvailability?: number | undefined, products: Array}; export type PriceDiscountDto = {priceBeforeDiscount?: number | undefined, discount?: number | undefined, type?: ('percentage' | 'multiplier' | 'fixed_amount') | undefined}; export type UpdateOfferDto = {publisherOfferId: string, name: string, displayName?: string | undefined, description?: string | undefined, type: string, offerUiId?: string | undefined, offerExternalUiId?: string | undefined, dynamicOfferUi?: DynamicOfferUiDto | undefined, active: boolean, segments: Array, productsSequence: Array, schedule?: ({permanent: boolean, timeFrames: Array<{startTime: string, endTime: string, notes?: string | undefined}>, intervals?: string | undefined}) | undefined, priceDiscount?: PriceDiscountDto | undefined}; export type UpdateApiOfferingOfferPopupOfferSKUArgs = {offerSKU: string, 'x-publisher-token': string, body: UpdateOfferDto}; export type UpdateApiOfferingOfferPopupOfferSKUOptions = {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 rewardCalendarBehavior = z.object({resetBehavior: z.enum(["ToCalendarStart", "ToCurrentWeekStart"]), popupTrigger: z.enum(["ShowAutomatically", "ShowOnBadgeClick"]), rewardCollectionBehavior: z.enum(["ForcePlayerToCollect", "PlayerCanSkipCollection"])}); export const offerDtoResponse = z.object({publisherId: z.string().optional(), offerId: z.string().optional(), publisherOfferId: z.string().optional(), name: z.string().optional(), displayName: z.string().optional(), description: z.string().optional(), type: z.string().optional(), offerUi: offerUiDto.optional(), badges: z.array(z.object({publisherBadgeId: z.string().optional()})).optional(), productSale: z.object({sale: z.number().int().optional(), type: z.enum(["percentage", "multiplier", "fixed_amount"]).optional()}).optional(), priceDiscount: z.object({discount: z.number().int().optional(), type: z.literal("percentage").optional()}).optional(), active: z.boolean().optional(), segments: z.array(z.string()).optional(), publisherTabId: z.string().optional(), productsSequence: z.array(z.object({index: z.number().int().optional(), playerAvailability: z.number().optional(), hidePlayerAvailability: z.boolean().optional(), priceInUsdCents: z.number().int().optional(), products: z.array(z.object({quantity: z.number().int().optional(), priority: z.string().optional(), product: z.object({publisherProductId: z.string().optional(), name: z.string().optional(), textFontColorHex: z.string().optional(), type: z.enum(["quantity", "time"]).optional(), prefix: z.string().optional(), suffix: z.string().optional(), priority: z.enum(["Main", "Sub"]).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()}).optional()})).optional(), progressBarPoints: z.array(z.object({publisherBarId: z.string().optional(), points: z.number().optional()})).optional()})).optional(), startOver: z.boolean().optional(), rewardCalendar: rewardCalendarBehavior.optional(), priority: z.number().optional(), showAfter: z.string().optional(), triggers: z.array(z.object({type: z.string().optional(), eventName: z.string().optional(), every: z.number().int().optional(), rules: z.array(z.unknown()).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 }; /** * PUT /offering/offer/popup/{offerSKU} */ export const updateApiOfferingOfferPopupOfferSKU = async (args: UpdateApiOfferingOfferPopupOfferSKUArgs, options: UpdateApiOfferingOfferPopupOfferSKUOptions) => { 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/${encodeURIComponent(String(args.offerSKU))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(offerDtoResponse, await res.json()) };