import {z} from 'zod' export type CreateApi30ListsListIdSegmentsSegmentIdArgs = {list_id: string, segment_id: string, body: {members_to_add?: Array | undefined, members_to_remove?: Array | undefined}}; export type CreateApi30ListsListIdSegmentsSegmentIdOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApi30ListsListIdSegmentsSegmentIdResponse = z.object({_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(), error_count: z.number().int().optional(), errors: z.array(z.object({email_addresses: z.array(z.string()).optional(), error: z.string().optional()})).optional(), members_added: z.array(z.object({_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(), contact_id: z.string().optional(), email_address: z.string().optional(), email_client: z.string().optional(), email_type: z.string().optional(), id: z.string().optional(), interests: z.record(z.string(), z.boolean()).optional(), ip_opt: z.string().optional(), ip_signup: z.string().optional(), language: z.string().optional(), last_changed: z.string().optional(), last_note: z.object({created_at: z.string().optional(), created_by: z.string().optional(), note: z.string().optional(), note_id: z.number().int().optional()}).optional(), list_id: z.string().optional(), location: z.object({country_code: z.string().optional(), dstoff: z.number().int().optional(), gmtoff: z.number().int().optional(), latitude: z.number().optional(), longitude: z.number().optional(), timezone: z.string().optional()}).optional(), member_rating: z.number().int().optional(), merge_fields: z.record(z.string(), z.unknown()).optional(), stats: z.object({avg_click_rate: z.number().optional(), avg_open_rate: z.number().optional()}).optional(), status: z.enum(["subscribed", "unsubscribed", "cleaned", "pending", "transactional"]).optional(), tags: z.array(z.object({id: z.number().int().optional(), name: z.string().optional()})).optional(), tags_count: z.number().int().optional(), timestamp_opt: z.string().optional(), timestamp_signup: z.string().optional(), unique_email_id: z.string().optional(), vip: z.boolean().optional()})).optional(), members_removed: z.array(z.object({_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(), contact_id: z.string().optional(), email_address: z.string().optional(), email_client: z.string().optional(), email_type: z.string().optional(), id: z.string().optional(), interests: z.record(z.string(), z.boolean()).optional(), ip_opt: z.string().optional(), ip_signup: z.string().optional(), language: z.string().optional(), last_changed: z.string().optional(), last_note: z.object({created_at: z.string().optional(), created_by: z.string().optional(), note: z.string().optional(), note_id: z.number().int().optional()}).optional(), list_id: z.string().optional(), location: z.object({country_code: z.string().optional(), dstoff: z.number().int().optional(), gmtoff: z.number().int().optional(), latitude: z.number().optional(), longitude: z.number().optional(), timezone: z.string().optional()}).optional(), member_rating: z.number().int().optional(), merge_fields: z.record(z.string(), z.unknown()).optional(), stats: z.object({avg_click_rate: z.number().optional(), avg_open_rate: z.number().optional()}).optional(), status: z.enum(["subscribed", "unsubscribed", "cleaned", "pending", "transactional"]).optional(), tags: z.array(z.object({id: z.number().int().optional(), name: z.string().optional()})).optional(), tags_count: z.number().int().optional(), timestamp_opt: z.string().optional(), timestamp_signup: z.string().optional(), unique_email_id: z.string().optional(), vip: z.boolean().optional()})).optional(), total_added: z.number().int().optional(), total_removed: z.number().int().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 }; /** * Batch add or remove members * * POST /3.0/lists/{list_id}/segments/{segment_id} * * Also accepts auth: oauth2 */ export const createApi30ListsListIdSegmentsSegmentId = async (args: CreateApi30ListsListIdSegmentsSegmentIdArgs, options?: CreateApi30ListsListIdSegmentsSegmentIdOptions) => { const {baseUrl = "https://api.mailchimp.com", fetch: fetchFn = fetch, bearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerToken !== undefined) headers.set('authorization', `Bearer ${bearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/3.0/lists/${encodeURIComponent(String(args.list_id))}/segments/${encodeURIComponent(String(args.segment_id))}${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(createApi30ListsListIdSegmentsSegmentIdResponse, await res.json()) };