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 ApiPreference = {id?: number | undefined, publicName?: (string | null) | undefined, privateName?: (string | null) | undefined, isPreference?: boolean | undefined, order?: (string | null) | undefined, isPublic?: boolean | undefined, categoryId?: number | undefined, preferenceCount?: number | undefined, created?: string | undefined, lastModified?: string | undefined, isOptedIn?: (boolean | null) | undefined, preferences?: (Array | null) | undefined}; export type ApiContactWithConsentAndPreferences = {contact?: (ApiContact | null) | undefined, consentFields?: (Array | null) | undefined, preferences?: (Array | null) | undefined}; export type UpdateApiV2ContactsIdWithConsentAndPreferencesArgs = {id: number, body: ApiContactWithConsentAndPreferences}; export type UpdateApiV2ContactsIdWithConsentAndPreferencesOptions = {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 apiPreference: z.ZodType = z.object({id: z.number().int().optional(), publicName: z.string().nullable().optional(), privateName: z.string().nullable().optional(), isPreference: z.boolean().optional(), order: z.string().nullable().optional(), isPublic: z.boolean().optional(), categoryId: z.number().int().optional(), preferenceCount: z.number().int().optional(), created: z.string().optional(), lastModified: z.string().optional(), isOptedIn: z.boolean().nullable().optional(), preferences: z.array(z.lazy(() => apiPreference)).nullable().optional()}); export const apiContactWithConsentAndPreferences = z.object({contact: apiContact.nullable().optional(), consentFields: z.array(apiConsent).nullable().optional(), preferences: z.array(apiPreference).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 }; /** * Updates a contact with consent and preference records. * * PUT /v2/contacts/{id}/with-consent-and-preferences */ export const updateApiV2ContactsIdWithConsentAndPreferences = async (args: UpdateApiV2ContactsIdWithConsentAndPreferencesArgs, options?: UpdateApiV2ContactsIdWithConsentAndPreferencesOptions) => { 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/${encodeURIComponent(String(args.id))}/with-consent-and-preferences${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(apiContactWithConsentAndPreferences, await res.json()) };