import {z} from 'zod' export type ApiCampaignFromAddress = {id?: number | undefined, email?: (string | null) | undefined}; export type ApiCampaignReplyActions = 'Unset' | 'WebMailForward' | 'Webmail' | 'Delete' | 'NotAvailableInThisVersion'; export type ApiCampaignStatuses = 'Unsent' | 'Sending' | 'Sent' | 'Paused' | 'Cancelled' | 'RequiresSystemApproval' | 'RequiresSMSApproval' | 'RequiresWorkflowApproval' | 'Triggered' | 'NotAvailableInThisVersion'; export type ApiSplitTestCampaign = {id?: number | undefined, name: string, subjects?: (Array | null) | undefined, fromNames?: (Array | null) | undefined, fromAddresses?: (Array | null) | undefined, htmlContents?: (Array | null) | undefined, plainTextContents: Array, replyAction?: ApiCampaignReplyActions | undefined, replyToAddress?: (string | null) | undefined, status?: ApiCampaignStatuses | undefined, customReplyToAddress?: (string | null) | undefined}; export type CreateApiV2CampaignsSplitTestArgs = {body: ApiSplitTestCampaign}; export type CreateApiV2CampaignsSplitTestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiCampaignFromAddress = z.object({id: z.number().int().optional(), email: z.string().nullable().optional()}); export const apiCampaignReplyActions = z.enum(["Unset", "WebMailForward", "Webmail", "Delete", "NotAvailableInThisVersion"]); export const apiCampaignStatuses = z.enum(["Unsent", "Sending", "Sent", "Paused", "Cancelled", "RequiresSystemApproval", "RequiresSMSApproval", "RequiresWorkflowApproval", "Triggered", "NotAvailableInThisVersion"]); export const apiSplitTestCampaign = z.object({id: z.number().int().optional(), name: z.string().min(1).max(255), subjects: z.array(z.string()).nullable().optional(), fromNames: z.array(z.string()).nullable().optional(), fromAddresses: z.array(apiCampaignFromAddress).nullable().optional(), htmlContents: z.array(z.string()).nullable().optional(), plainTextContents: z.array(z.string()), replyAction: apiCampaignReplyActions.optional(), replyToAddress: z.string().nullable().optional(), status: apiCampaignStatuses.optional(), customReplyToAddress: z.string().nullable().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 split test campaign. * * POST /v2/campaigns/split-test */ export const createApiV2CampaignsSplitTest = async (args: CreateApiV2CampaignsSplitTestArgs, options?: CreateApiV2CampaignsSplitTestOptions) => { const {baseUrl = "https://api.dotdigital.com", 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(/\/+$/, '')}/v2/campaigns/split-test${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(apiSplitTestCampaign, await res.json()) };