import {z} from 'zod' export type GetApiComponentsV1OfferDesignArgs = {'x-publisher-token': string}; export type GetApiComponentsV1OfferDesignOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const offerTitle = z.object({text: z.string().optional(), fontColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), direction: z.string().optional()}).and(z.record(z.string(), z.unknown())).optional(), fontSize: z.number().optional(), fontWeight: z.string().optional()}); export const colorDetails = z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), direction: z.string().optional()}); export const subRollingOfferResponse = z.object({backgroundImageUrl: z.string().optional(), backgroundExternalImageUrl: z.string().optional(), collectText: z.string().optional()}); export const rewardCalendarDesignBlockResponse = z.object({collectibleBadgeImage: z.string().optional(), lockedBadgeImage: z.string().optional(), backgroundColor: z.string().optional(), collectIconImage: z.string().optional(), lockedIconImage: z.string().optional(), dayFontColor: z.string().optional(), subRewardBackgroundImage: z.string().optional(), mainRewardBackgroundImage: z.string().optional(), headerImage: z.string().optional()}); export const offerDesignResponse = z.object({id: z.string().optional(), offerUiType: z.enum(["SpecialOffer", "Bundle", "PopUp", "RollingOffer"]).optional(), offerUiSubType: z.enum(["DailyBonus", "PostPurchase", "RewardCalendar"]).optional(), name: z.string().optional(), externalId: z.string().optional(), backgroundImageUrl: z.string().optional(), externalBackgroundImageUrl: z.string().optional(), backgroundExternalImageUrl: z.string().optional(), title: offerTitle.optional(), buttonSuffixImage: z.string().optional(), buttonSuffixAnimation: z.string().optional(), backgroundColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), gradientDirection: z.enum(["to top", "to bottom", "to left", "to right", "to top left", "to top right", "to bottom left", "to bottom right"]).optional()}).and(z.record(z.string(), z.unknown())).optional(), backgroundOpacity: z.number().optional(), headerImage: z.string().optional(), arrowColor: z.string().optional(), borderColor: colorDetails.optional(), borderWidth: z.number().optional(), subRollingOffer: subRollingOfferResponse.optional(), rewardCalendar: rewardCalendarDesignBlockResponse.optional()}); export const getAllOfferDesignsResponse = z.array(offerDesignResponse); 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 Offer Designs * * GET /components/v1/offer-design */ export const getApiComponentsV1OfferDesign = async (args: GetApiComponentsV1OfferDesignArgs, options: GetApiComponentsV1OfferDesignOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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(/\/+$/, '')}/components/v1/offer-design${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getAllOfferDesignsResponse, await res.json()) };