import {z} from 'zod' export type CreateApiOfferingOfferGetOffersArgs = {offerType?: string | undefined, recordLimit?: number | undefined, offset?: number | undefined, publisherOfferId?: string | undefined, 'x-publisher-token': string}; export type CreateApiOfferingOfferGetOffersOptions = {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 getOffersResponse = z.object({totalCount: z.number().optional(), offers: z.array(offerDtoResponse).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/get-offers */ export const createApiOfferingOfferGetOffers = async (args: CreateApiOfferingOfferGetOffersArgs, options: CreateApiOfferingOfferGetOffersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.offerType != null) query.append("offerType", String(args.offerType)) if (args.recordLimit != null) query.append("recordLimit", String(args.recordLimit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.publisherOfferId != null) query.append("publisherOfferId", String(args.publisherOfferId)) if (args['x-publisher-token'] != null) headers.set("x-publisher-token", String(args['x-publisher-token'])) const url = `${baseUrl.replace(/\/+$/, '')}/offering/offer/get-offers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getOffersResponse, await res.json()) };