import {z} from 'zod' export type CampaignPresentationData = {name: string, subtitle?: string | undefined, note?: string | undefined, description?: string | undefined, image_url?: string | undefined}; export type PropertyRecord = {property_record_id?: string | undefined, property_id: string, property_name?: string | undefined, property_value?: (string | number | number | boolean | Array | Record) | undefined, last_change?: string | undefined}; export type CampaignProduct = {campaign_product_id?: string | undefined, product_id: string, currency_id: string, campaign_id: string, value: number, value_type_id: 1 | 2, valid_from: string, valid_to: string, display_in?: Array | undefined, type_id: string, presentation_data: CampaignPresentationData, campaign_product_property_records?: Array | undefined, last_change?: string | undefined, state?: (0 | 1) | undefined}; export type CreateApiCampaignProductsArgs = {'Accept-Language'?: string | undefined, body: {campaign_product: CampaignProduct}}; export type CreateApiCampaignProductsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCampaignProductsResponse = z.object({data: z.object({campaign_product_id: z.string().optional()}).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 }; /** * Create a campaign product * * POST /campaign-products * * Also accepts auth: basicAuth */ export const createApiCampaignProducts = async (args: CreateApiCampaignProductsArgs, options?: CreateApiCampaignProductsOptions) => { const {baseUrl = "https://sandbox.crmcarecloud.com/webservice/rest-api/enterprise-interface/v1.0", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/campaign-products${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(createApiCampaignProductsResponse, await res.json()) };