import {z} from 'zod' export type UpdateRollingOfferDto = {name?: string | undefined, type: 'RollingOffer', active?: boolean | undefined, offerUiId?: string | undefined, offerExternalUiId?: string | undefined, priority?: number | undefined, segments?: Array | undefined, publisherTabId?: string | undefined, productsSequence?: (Array<{index: number, displayName?: string | undefined, products: Array<{publisherProductId: string, quantity: number, priority: 'Main' | 'Sub'}>, priceInUsdCents: number, progressBarPoints?: (Array<{publisherBarId?: string | undefined, points?: number | undefined}>) | undefined, priceDiscount?: ({discount?: number | undefined, type?: 'percentage' | undefined}) | undefined, productSale?: ({sale?: number | undefined, type?: ('percentage' | 'multiplier' | 'fixed_amount') | undefined}) | undefined, badges?: (Array<{publisherBadgeId?: string | undefined}>) | undefined}>) | undefined, schedule?: ({permanent: boolean, timeFrames: Array<{startTime: string, endTime: string, notes?: string | undefined}>}) | undefined, publisherSectionId?: string | undefined, displayName?: string | undefined, description?: string | undefined}; export type UpdateApiOfferingRollingOfferRollingOfferSKUArgs = {publisherOfferId: string, 'x-publisher-token': string, body: UpdateRollingOfferDto}; export type UpdateApiOfferingRollingOfferRollingOfferSKUOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const rollingOfferDtoResponse = z.object({publisherId: z.string().optional(), offerId: z.string().optional(), publisherOfferId: z.string().optional(), name: z.string().optional(), type: z.string().optional(), active: z.boolean().optional(), priority: z.number().int().optional(), segments: z.array(z.string()).optional(), publisherTabId: z.string().optional(), offerUi: z.object({offerUiId: z.string().optional(), active: z.boolean().optional(), offerUiType: z.string().optional(), name: z.string().optional(), description: z.string().optional(), backgroundImage: z.string().optional(), borderColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), direction: z.string().optional()}).optional(), borderWidth: z.number().int().optional(), externalId: z.string().optional(), rollingOffer: z.object({arrowColor: z.string().optional(), backgroundColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), gradientDirection: z.string().optional()}).optional(), headerImage: z.string().optional(), backgroundOpacity: z.number().int().optional(), subRollingOffer: z.object({backgroundColor: z.object({colorOne: z.string().optional(), colorTwo: z.string().optional(), direction: z.string().optional()}).optional(), backgroundImage: z.string().optional(), lockImage: z.string().optional(), collectText: z.string().optional()}).optional()}).optional()}).optional(), productsSequence: z.array(z.object({index: z.number().int().optional(), displayName: z.string().optional(), products: z.array(z.object({publisherProductId: z.string().optional(), quantity: z.number().int().optional(), priority: z.enum(["Main", "Sub"]).optional()})).optional(), priceInUsdCents: z.number().int().optional(), progressBarPoints: z.array(z.object({publisherBarId: z.string().optional(), points: z.number().optional()})).optional(), badges: z.array(z.object({publisherBadgeId: z.string().optional()})).optional()})).optional(), schedule: z.object({permanent: z.boolean().optional(), timeFrames: z.array(z.object({startTime: z.string().optional(), endTime: z.string().optional(), notes: 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 }; /** * Update an existing rolling offer * * PUT /offering/rolling-offer/{rollingOfferSKU} */ export const updateApiOfferingRollingOfferRollingOfferSKU = async (args: UpdateApiOfferingRollingOfferRollingOfferSKUArgs, options: UpdateApiOfferingRollingOfferRollingOfferSKUOptions) => { 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/rolling-offer/${encodeURIComponent(String(args.rollingOfferSKU))}${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(rollingOfferDtoResponse, await res.json()) };