import {z} from 'zod' export type MailingInput = {subject: string, language: string, groups: string, sendAt: string, replyTo?: string | undefined, fromName?: string | undefined, mailFrom?: string | undefined, bodyHtml?: string | undefined, bodyText?: string | undefined, googleAnalytics?: boolean | undefined, googleAnalyticsCampaign?: string | undefined, mailingStatistics?: ('detailed' | 'anonymized') | undefined, updateProfileForm?: number | undefined}; export type CreateApiMailingsArgs = {body: MailingInput}; export type CreateApiMailingsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const mailingDetail = z.object({id: z.number().int().optional(), subject: z.string().optional(), language: z.string().optional(), startedOn: z.string().nullable().optional(), scheduledOn: z.string().nullable().optional(), replyTo: z.string().optional(), bodyHtml: z.string().optional(), bodyText: z.string().optional(), recipientsCount: z.number().int().optional(), emailsSent: z.number().int().optional(), emailsDelivered: z.number().int().optional(), emailsStopped: z.number().int().optional(), views: z.number().int().optional(), clicks: z.number().int().optional(), sentToFriend: z.number().int().optional(), incorrectAddresses: z.number().int().optional(), technicalProblems: z.number().int().optional(), deliveryRate: z.number().optional(), clickRate: z.number().optional(), viewRate: z.number().optional(), status: z.string().optional(), mailFrom: z.string().optional(), fromName: z.string().optional(), mailingStatistics: z.string().optional(), links: z.array(z.object({id: z.number().int().optional(), url: z.string().optional(), clicksUnique: z.number().int().optional(), clicksTotal: z.number().int().optional()})).optional(), recipientGroups: z.array(z.object({id: z.number().int().optional(), title: z.string().optional()})).optional(), viewOnlineURL: z.string().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 new mailing * * POST /mailings */ export const createApiMailings = async (args: CreateApiMailingsArgs, options?: CreateApiMailingsOptions) => { const {baseUrl = "https://api.cyberimpact.com", 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/mailings${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(mailingDetail, await res.json()) };