import {z} from 'zod' export type ApiSplitTestMetrics = 'Opens' | 'Clicks' | 'ROI' | 'NotAvailableInThisVersion'; export type ApiSplitTestSendOptions = {testMetric: ApiSplitTestMetrics, testPercentage: number, testPeriodHours: number}; export type ApiCampaignSendStatuses = 'NotSent' | 'Scheduled' | 'Sending' | 'Sent' | 'Cancelled' | 'NotAvailableInThisVersion'; export type ApiCampaignSend = {id?: unknown | undefined, campaignId: number, addressBookIds?: (Array | null) | undefined, contactIds?: (Array | null) | undefined, sendDate?: string | undefined, splitTestOptions?: (ApiSplitTestSendOptions | null) | undefined, status?: ApiCampaignSendStatuses | undefined}; export type CreateApiV2CampaignsSendArgs = {body: ApiCampaignSend}; export type CreateApiV2CampaignsSendOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiSplitTestMetrics = z.enum(["Opens", "Clicks", "ROI", "NotAvailableInThisVersion"]); export const apiSplitTestSendOptions = z.object({testMetric: apiSplitTestMetrics, testPercentage: z.number().int().gte(1).lte(100), testPeriodHours: z.number().int().gte(1).lte(2147483647)}); export const apiCampaignSendStatuses = z.enum(["NotSent", "Scheduled", "Sending", "Sent", "Cancelled", "NotAvailableInThisVersion"]); export const apiCampaignSend = z.object({id: z.string().optional(), campaignId: z.number().int(), addressBookIds: z.array(z.number().int()).nullable().optional(), contactIds: z.array(z.number().int()).nullable().optional(), sendDate: z.string().optional(), splitTestOptions: apiSplitTestSendOptions.nullable().optional(), status: apiCampaignSendStatuses.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 }; /** * Sends a specified campaign to one or more lists, segments or contacts at a specified time. Leave the list array empty to send to All Contacts. * * POST /v2/campaigns/send */ export const createApiV2CampaignsSend = async (args: CreateApiV2CampaignsSendArgs, options?: CreateApiV2CampaignsSendOptions) => { 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/send${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(apiCampaignSend, await res.json()) };