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 ApiCampaignTypes = 'Standard' | 'Triggered' | 'NotAvailableInThisVersion'; export type ApiCampaignWithType = {id?: number | undefined, name: string, subject: string, fromName: string, fromAddress?: (ApiCampaignFromAddress | null) | undefined, htmlContent: string, plainTextContent: string, replyAction?: ApiCampaignReplyActions | undefined, replyToAddress?: (string | null) | undefined, isSplitTest?: boolean | undefined, status?: ApiCampaignStatuses | undefined, customReplyToAddress?: (string | null) | undefined, type?: ApiCampaignTypes | undefined}; export type CreateApiV2CampaignsArgs = {body: ApiCampaignWithType}; export type CreateApiV2CampaignsOptions = {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 apiCampaignTypes = z.enum(["Standard", "Triggered", "NotAvailableInThisVersion"]); export const apiCampaignWithType = z.object({id: z.number().int().optional(), name: z.string().min(1).max(255), subject: z.string().min(1), fromName: z.string().min(1), fromAddress: apiCampaignFromAddress.nullable().optional(), htmlContent: z.string().min(1), plainTextContent: z.string().min(1), replyAction: apiCampaignReplyActions.optional(), replyToAddress: z.string().nullable().optional(), isSplitTest: z.boolean().optional(), status: apiCampaignStatuses.optional(), customReplyToAddress: z.string().nullable().optional(), type: apiCampaignTypes.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 campaign. * * POST /v2/campaigns */ export const createApiV2Campaigns = async (args: CreateApiV2CampaignsArgs, options?: CreateApiV2CampaignsOptions) => { 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${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(apiCampaignWithType, await res.json()) };