import {z} from 'zod' export type GetApi30AudiencesAudienceIdContactsArgs = {fields?: Array | undefined, exclude_fields?: Array | undefined, count?: number | undefined, cursor?: string | undefined, created_before?: string | undefined, created_since?: string | undefined, updated_before?: string | undefined, updated_since?: string | undefined, audience_id: string, sort_field?: ('created_at' | 'updated_at') | undefined, sort_dir?: ('ASC' | 'DESC') | undefined}; export type GetApi30AudiencesAudienceIdContactsOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApi30AudiencesAudienceIdContactsResponse = z.object({contacts: z.array(z.object({audience_id: z.string().optional(), created_at: z.string().optional(), email_channel: z.object({effective_subscription_status: z.object({value: z.enum(["subscribed", "unsubscribed", "nonsubscribed", "pending"]).optional()}).optional(), email: z.string().optional(), hashed_email: z.string().optional(), marketing_consent: z.object({source: z.object({name: z.string().optional()}).optional(), status: z.enum(["consented", "denied", "confirmed", "unknown"]).optional(), captured_at: z.string().optional()}).optional(), source: z.object({name: z.string().optional()}).optional()}).optional(), id: z.string().optional(), language: z.enum(["", "en", "ar", "af", "be", "bg", "ca", "zh", "zh_CN", "hr", "cs", "da", "nl", "et", "fa", "fi", "fr", "fr_CA", "de", "el", "he", "hi", "hu", "is", "id", "ga", "it", "ja", "km", "ko", "lv", "lt", "mt", "ms", "mk", "no", "pl", "pt", "pt_PT", "ro", "ru", "sr", "sk", "sl", "es", "es_ES", "sw", "sv", "ta", "th", "tr", "uk", "vi"]).optional(), last_updated_at: z.string().optional(), merge_fields: z.record(z.string(), z.union([z.object({addr1: z.string(), addr2: z.string().optional(), city: z.string(), state: z.string(), zip: z.string(), country: z.string().optional()}), z.string(), z.number()])).nullable().optional(), sms_channel: z.object({effective_subscription_status: z.object({value: z.enum(["subscribed", "unsubscribed", "nonsubscribed", "pending"]).optional()}).optional(), marketing_consent: z.object({source: z.object({name: z.string().optional()}).optional(), status: z.enum(["consented", "confirmed", "denied", "unknown"]).optional(), captured_at: z.string().optional()}).optional(), sms_phone: z.string().optional(), source: z.object({name: z.string().optional()}).optional(), hashed_sms_phone: z.string().optional()}).optional(), source: z.object({name: z.string().optional()}).optional(), status: z.enum(["active", "archived"]).optional(), tags: z.array(z.string()).optional()})).optional(), next_cursor: z.string().optional(), _links: z.array(z.object({href: z.string().optional(), method: z.enum(["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]).optional(), rel: z.string().optional(), schema: z.string().optional(), targetSchema: z.string().optional()})).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 }; /** * A collection of Audience Contacts * * GET /3.0/audiences/{audience_id}/contacts * * Also accepts auth: oauth2 */ export const getApi30AudiencesAudienceIdContacts = async (args: GetApi30AudiencesAudienceIdContactsArgs, options?: GetApi30AudiencesAudienceIdContactsOptions) => { const {baseUrl = "https://api.mailchimp.com", fetch: fetchFn = fetch, bearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fields != null) query.append("fields", args.fields.join(',')) if (args.exclude_fields != null) query.append("exclude_fields", args.exclude_fields.join(',')) if (args.count != null) query.append("count", String(args.count)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.created_since != null) query.append("created_since", String(args.created_since)) if (args.updated_before != null) query.append("updated_before", String(args.updated_before)) if (args.updated_since != null) query.append("updated_since", String(args.updated_since)) if (args.sort_field != null) query.append("sort_field", String(args.sort_field)) if (args.sort_dir != null) query.append("sort_dir", String(args.sort_dir)) if (bearerToken !== undefined) headers.set('authorization', `Bearer ${bearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/3.0/audiences/${encodeURIComponent(String(args.audience_id))}/contacts${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApi30AudiencesAudienceIdContactsResponse, await res.json()) };