import {z} from 'zod' export type OfferTitleCreate = {text: string, fontColor: string, fontSize: number, fontWeight: string}; export type PostPurchasePopupDesignCreate = {offerUiType: 'PopUp', name: string, externalId: string, backgroundImageUrl: string, title?: OfferTitleCreate | undefined, offerUiSubType: 'PostPurchase'}; export type CreateApiComponentsV1OfferDesignArgs = {body: PostPurchasePopupDesignCreate}; export type CreateApiComponentsV1OfferDesignOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const offerTitleResponse = z.object({text: z.string().optional(), fontColor: z.string().optional(), fontSize: z.number().optional(), fontWeight: z.string().optional()}); export const postPurchasePopupDesignResponse = z.object({id: z.string().optional(), offerUiType: z.literal("PopUp").optional(), name: z.string().optional(), externalId: z.string().optional(), backgroundImageUrl: z.string().optional(), title: offerTitleResponse.optional(), externalBackgroundImageUrl: z.string().optional(), offerUiSubType: z.literal("PostPurchase").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 }; /** * Creates a post purchase popup design. * * POST /components/v1/offer-design */ export const createApiComponentsV1OfferDesign = async (args: CreateApiComponentsV1OfferDesignArgs, options: CreateApiComponentsV1OfferDesignOptions) => { 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") const url = `${baseUrl.replace(/\/+$/, '')}/components/v1/offer-design${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(postPurchasePopupDesignResponse, await res.json()) };