import {z} from 'zod' export type ApiContactOptInTypes = 'Unknown' | 'Single' | 'Double' | 'VerifiedDouble' | 'NotAvailableInThisVersion'; export type ApiContactEmailTypes = 'PlainText' | 'Html' | 'NotAvailableInThisVersion'; export type ApiContactData = {key?: (string | null) | undefined, value: unknown}; export type ApiContactStatuses = 'Subscribed' | 'Unsubscribed' | 'SoftBounced' | 'HardBounced' | 'IspComplained' | 'MailBlocked' | 'PendingOptIn' | 'DirectComplaint' | 'Deleted' | 'SharedSuppression' | 'Suppressed' | 'NotAllowed' | 'DomainSuppression' | 'NoMxRecord' | 'NotAvailableInThisVersion'; export type ApiContact = {id?: number | undefined, email: string, optInType?: (ApiContactOptInTypes | null) | undefined, emailType?: (ApiContactEmailTypes | null) | undefined, dataFields?: (Array | null) | undefined, status?: ApiContactStatuses | undefined}; export type ApiConsent = {id?: (string | null) | undefined, fields?: (Array | null) | undefined}; export type ApiContactWithConsent = {contact?: (ApiContact | null) | undefined, consentFields?: (Array | null) | undefined}; export type CreateApiV2ContactsWithConsentArgs = {body: ApiContactWithConsent}; export type CreateApiV2ContactsWithConsentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiContactOptInTypes = z.enum(["Unknown", "Single", "Double", "VerifiedDouble", "NotAvailableInThisVersion"]); export const apiContactEmailTypes = z.enum(["PlainText", "Html", "NotAvailableInThisVersion"]); export const apiContactData = z.object({key: z.string().nullable().optional(), value: z.unknown()}); export const apiContactStatuses = z.enum(["Subscribed", "Unsubscribed", "SoftBounced", "HardBounced", "IspComplained", "MailBlocked", "PendingOptIn", "DirectComplaint", "Deleted", "SharedSuppression", "Suppressed", "NotAllowed", "DomainSuppression", "NoMxRecord", "NotAvailableInThisVersion"]); export const apiContact = z.object({id: z.number().int().optional(), email: z.string().min(1), optInType: apiContactOptInTypes.nullable().optional(), emailType: apiContactEmailTypes.nullable().optional(), dataFields: z.array(apiContactData).nullable().optional(), status: apiContactStatuses.optional()}); export const apiConsent = z.object({id: z.string().nullable().optional(), fields: z.array(apiContactData).nullable().optional()}); export const apiContactWithConsent = z.object({contact: apiContact.nullable().optional(), consentFields: z.array(apiConsent).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 contact with consent records. * * POST /v2/contacts/with-consent */ export const createApiV2ContactsWithConsent = async (args: CreateApiV2ContactsWithConsentArgs, options?: CreateApiV2ContactsWithConsentOptions) => { 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/contacts/with-consent${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(apiContactWithConsent, await res.json()) };