import {z} from 'zod' export type PatchApi30ListsListIdSegmentsSegmentIdArgs = {list_id: string, segment_id: string, body: {name?: string | undefined, options?: ({conditions?: (Array<{condition_type?: 'Aim' | undefined, field?: 'aim' | undefined, op?: ('open' | 'click' | 'sent' | 'noopen' | 'noclick' | 'nosent') | undefined, value?: string | undefined} | {condition_type?: 'Automation' | undefined, field: 'automation', op: 'started' | 'completed' | 'not_started' | 'not_completed', value: string} | {condition_type?: 'CampaignPoll' | undefined, field: 'poll', op: 'member' | 'notmember', value: number} | {condition_type?: 'Conversation' | undefined, field: 'conversation', op: 'member' | 'notmember', value: string} | {condition_type?: 'Date' | undefined, extra?: string | undefined, field: 'timestamp_opt' | 'info_changed' | 'ecomm_date', op: 'greater' | 'less' | 'is' | 'not' | 'blank' | 'blank_not' | 'within' | 'notwithin', value: string} | {condition_type?: 'EmailClient' | undefined, field: 'email_client', op: 'client_is' | 'client_not', value: string} | {condition_type?: 'Language' | undefined, field: 'language', op: 'is' | 'not', value: string} | {condition_type?: 'MemberRating' | undefined, field: 'rating', op: 'is' | 'not' | 'greater' | 'less', value: number | string} | {condition_type: 'SignupSource', field: 'source', op: 'source_is' | 'source_not', value?: string | undefined} | {condition_type?: 'SurveyMonkey' | undefined, field: 'survey_monkey', op: 'started' | 'completed' | 'not_started' | 'not_completed', value: string} | {condition_type?: 'VIP' | undefined, field: 'gmonkey', op: 'member' | 'notmember'} | {condition_type?: 'Interests' | undefined, field?: string | undefined, op?: ('interestcontains' | 'interestcontainsall' | 'interestnotcontains') | undefined, value?: Array | undefined} | {condition_type?: 'EcommCategory' | undefined, field?: ('ecomm_cat' | 'ecomm_prod') | undefined, op?: ('is' | 'not' | 'contains' | 'notcontain' | 'starts' | 'ends') | undefined, value?: string | undefined} | {condition_type?: 'EcommNumber' | undefined, field: 'ecomm_spent_avg' | 'ecomm_orders' | 'ecomm_prod_all' | 'ecomm_avg_ord', op: 'is' | 'not' | 'greater' | 'less', value: number | string} | {condition_type?: 'EcommPurchased' | undefined, field?: 'ecomm_purchased' | undefined, op?: ('member' | 'notmember') | undefined} | {condition_type?: 'EcommSpent' | undefined, field?: ('ecomm_spent_one' | 'ecomm_spent_all') | undefined, op?: ('greater' | 'less') | undefined, value?: (number | string) | undefined} | {condition_type?: 'EcommStore' | undefined, field?: 'ecomm_store' | undefined, op?: ('is' | 'not') | undefined, value?: string | undefined} | {condition_type?: 'GoalActivity' | undefined, field: 'goal', op: 'is' | 'goal_not' | 'contains' | 'goal_notcontain' | 'starts' | 'ends', value: string} | {condition_type?: 'GoalTimestamp' | undefined, field: 'goal_last_visited', op: 'greater' | 'less' | 'is', value: string} | {condition_type?: 'FuzzySegment' | undefined, field: 'fuzzy_segment', op: 'fuzzy_is' | 'fuzzy_not', value: number | string} | {condition_type?: 'StaticSegment' | undefined, field: 'static_segment', op: 'static_is' | 'static_not', value: number | string} | {condition_type?: 'IPGeoCountryState' | undefined, field: 'ipgeo', op: 'ipgeocountry' | 'ipgeonotcountry' | 'ipgeostate' | 'ipgeonotstate', value: string} | {addr: string, condition_type?: 'IPGeoIn' | undefined, field: 'ipgeo', lat: string, lng: string, op: 'ipgeoin' | 'ipgeonotin', value: number} | {condition_type?: 'IPGeoInZip' | undefined, extra: number, field: 'ipgeo', op: 'ipgeoinzip', value: number} | {condition_type?: 'IPGeoUnknown' | undefined, field: 'ipgeo', op: 'ipgeounknown'} | {condition_type?: 'IPGeoZip' | undefined, field: 'ipgeo', op: 'ipgeoiszip' | 'ipgeonotzip', value: number} | {condition_type?: 'SocialAge' | undefined, field: 'social_age', op: 'is' | 'not', value: '18-24' | '25-34' | '35-54' | '55+'} | {condition_type?: 'SocialGender' | undefined, field: 'social_gender', op: 'is' | 'not', value: 'male' | 'female'} | {condition_type?: 'SocialInfluence' | undefined, field: 'social_influence', op: 'is' | 'not' | 'greater' | 'less', value: number} | {condition_type?: 'SocialNetworkMember' | undefined, field: 'social_network', op: 'member' | 'notmember', value: 'twitter' | 'facebook' | 'linkedin' | 'flickr' | 'foursquare' | 'lastfm' | 'myspace' | 'quora' | 'vimeo' | 'yelp' | 'youtube'} | {condition_type?: 'SocialNetworkFollow' | undefined, field: 'social_network', op: 'follow' | 'notfollow', value: 'twitter_follow'} | {condition_type?: 'AddressMerge' | undefined, field: string, op: 'contains' | 'notcontain' | 'blank' | 'blank_not', value?: string | undefined} | {condition_type?: 'ZipMerge' | undefined, extra: string, field: string, op: 'geoin', value: string} | {condition_type?: 'BirthdayMerge' | undefined, field: string, op: 'is' | 'not' | 'blank' | 'blank_not', value?: string | undefined} | {condition_type?: 'DateMerge' | undefined, field: string, op: 'is' | 'not' | 'less' | 'blank' | 'blank_not' | 'greater', value?: string | undefined} | {condition_type?: 'SelectMerge' | undefined, field: string, op: 'is' | 'not' | 'blank' | 'blank_not' | 'notcontain' | 'contains', value?: string | undefined} | {condition_type?: 'TextMerge' | undefined, field: string, op: 'is' | 'not' | 'contains' | 'notcontain' | 'starts' | 'ends' | 'greater' | 'less' | 'blank' | 'blank_not', value?: string | undefined} | {condition_type?: 'EmailAddress' | undefined, field: 'merge0' | 'EMAIL', op: 'is' | 'not' | 'contains' | 'notcontain' | 'starts' | 'ends' | 'greater' | 'less', value?: string | undefined} | {condition_type?: 'PredictedGender' | undefined, field: 'predicted_gender', op: 'is' | 'not', value: 'male' | 'female'} | {condition_type?: 'PredictedAge' | undefined, field: 'predicted_age_range', op: 'is', value: '18-24' | '25-34' | '35-44' | '45-54' | '55-64' | '65+'} | {condition_type?: 'NewSubscribers' | undefined, field?: 'timestamp_opt' | undefined, op?: 'date_within' | undefined, value?: string | undefined}>) | undefined, match?: ('any' | 'all') | undefined}) | undefined, static_segment?: Array | undefined}}; export type PatchApi30ListsListIdSegmentsSegmentIdOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const patchApi30ListsListIdSegmentsSegmentIdResponse = 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(), created_at: z.string().optional(), id: z.number().int().optional(), list_id: z.string().optional(), member_count: z.number().int().optional(), name: z.string().optional(), options: z.object({conditions: z.array(z.union([z.object({condition_type: z.literal("Aim").optional(), field: z.literal("aim").optional(), op: z.enum(["open", "click", "sent", "noopen", "noclick", "nosent"]).optional(), value: z.string().optional()}), z.object({condition_type: z.literal("Automation").optional(), field: z.literal("automation"), op: z.enum(["started", "completed", "not_started", "not_completed"]), value: z.string()}), z.object({condition_type: z.literal("CampaignPoll").optional(), field: z.literal("poll"), op: z.enum(["member", "notmember"]), value: z.number()}), z.object({condition_type: z.literal("Conversation").optional(), field: z.literal("conversation"), op: z.enum(["member", "notmember"]), value: z.string()}), z.object({condition_type: z.literal("Date").optional(), extra: z.string().optional(), field: z.enum(["timestamp_opt", "info_changed", "ecomm_date"]), op: z.enum(["greater", "less", "is", "not", "blank", "blank_not", "within", "notwithin"]), value: z.string()}), z.object({condition_type: z.literal("EmailClient").optional(), field: z.literal("email_client"), op: z.enum(["client_is", "client_not"]), value: z.string()}), z.object({condition_type: z.literal("Language").optional(), field: z.literal("language"), op: z.enum(["is", "not"]), value: z.string()}), z.object({condition_type: z.literal("MemberRating").optional(), field: z.literal("rating"), op: z.enum(["is", "not", "greater", "less"]), value: z.union([z.number(), z.string()])}), z.object({condition_type: z.literal("SignupSource"), field: z.literal("source"), op: z.enum(["source_is", "source_not"]), value: z.string().optional()}), z.object({condition_type: z.literal("SurveyMonkey").optional(), field: z.literal("survey_monkey"), op: z.enum(["started", "completed", "not_started", "not_completed"]), value: z.string()}), z.object({condition_type: z.literal("VIP").optional(), field: z.literal("gmonkey"), op: z.enum(["member", "notmember"])}), z.object({condition_type: z.literal("Interests").optional(), field: z.string().optional(), op: z.enum(["interestcontains", "interestcontainsall", "interestnotcontains"]).optional(), value: z.array(z.string()).optional()}), z.object({condition_type: z.literal("EcommCategory").optional(), field: z.enum(["ecomm_cat", "ecomm_prod"]).optional(), op: z.enum(["is", "not", "contains", "notcontain", "starts", "ends"]).optional(), value: z.string().optional()}), z.object({condition_type: z.literal("EcommNumber").optional(), field: z.enum(["ecomm_spent_avg", "ecomm_orders", "ecomm_prod_all", "ecomm_avg_ord"]), op: z.enum(["is", "not", "greater", "less"]), value: z.union([z.number(), z.string()])}), z.object({condition_type: z.literal("EcommPurchased").optional(), field: z.literal("ecomm_purchased").optional(), op: z.enum(["member", "notmember"]).optional()}), z.object({condition_type: z.literal("EcommSpent").optional(), field: z.enum(["ecomm_spent_one", "ecomm_spent_all"]).optional(), op: z.enum(["greater", "less"]).optional(), value: z.union([z.number(), z.string()]).optional()}), z.object({condition_type: z.literal("EcommStore").optional(), field: z.literal("ecomm_store").optional(), op: z.enum(["is", "not"]).optional(), value: z.string().optional()}), z.object({condition_type: z.literal("GoalActivity").optional(), field: z.literal("goal"), op: z.enum(["is", "goal_not", "contains", "goal_notcontain", "starts", "ends"]), value: z.string()}), z.object({condition_type: z.literal("GoalTimestamp").optional(), field: z.literal("goal_last_visited"), op: z.enum(["greater", "less", "is"]), value: z.string()}), z.object({condition_type: z.literal("FuzzySegment").optional(), field: z.literal("fuzzy_segment"), op: z.enum(["fuzzy_is", "fuzzy_not"]), value: z.union([z.number(), z.string()])}), z.object({condition_type: z.literal("StaticSegment").optional(), field: z.literal("static_segment"), op: z.enum(["static_is", "static_not"]), value: z.union([z.number(), z.string()])}), z.object({condition_type: z.literal("IPGeoCountryState").optional(), field: z.literal("ipgeo"), op: z.enum(["ipgeocountry", "ipgeonotcountry", "ipgeostate", "ipgeonotstate"]), value: z.string()}), z.object({addr: z.string(), condition_type: z.literal("IPGeoIn").optional(), field: z.literal("ipgeo"), lat: z.string(), lng: z.string(), op: z.enum(["ipgeoin", "ipgeonotin"]), value: z.number().int()}), z.object({condition_type: z.literal("IPGeoInZip").optional(), extra: z.number().int(), field: z.literal("ipgeo"), op: z.literal("ipgeoinzip"), value: z.number().int()}), z.object({condition_type: z.literal("IPGeoUnknown").optional(), field: z.literal("ipgeo"), op: z.literal("ipgeounknown")}), z.object({condition_type: z.literal("IPGeoZip").optional(), field: z.literal("ipgeo"), op: z.enum(["ipgeoiszip", "ipgeonotzip"]), value: z.number().int()}), z.object({condition_type: z.literal("SocialAge").optional(), field: z.literal("social_age"), op: z.enum(["is", "not"]), value: z.enum(["18-24", "25-34", "35-54", "55+"])}), z.object({condition_type: z.literal("SocialGender").optional(), field: z.literal("social_gender"), op: z.enum(["is", "not"]), value: z.enum(["male", "female"])}), z.object({condition_type: z.literal("SocialInfluence").optional(), field: z.literal("social_influence"), op: z.enum(["is", "not", "greater", "less"]), value: z.number()}), z.object({condition_type: z.literal("SocialNetworkMember").optional(), field: z.literal("social_network"), op: z.enum(["member", "notmember"]), value: z.enum(["twitter", "facebook", "linkedin", "flickr", "foursquare", "lastfm", "myspace", "quora", "vimeo", "yelp", "youtube"])}), z.object({condition_type: z.literal("SocialNetworkFollow").optional(), field: z.literal("social_network"), op: z.enum(["follow", "notfollow"]), value: z.literal("twitter_follow")}), z.object({condition_type: z.literal("AddressMerge").optional(), field: z.string(), op: z.enum(["contains", "notcontain", "blank", "blank_not"]), value: z.string().optional()}), z.object({condition_type: z.literal("ZipMerge").optional(), extra: z.string(), field: z.string(), op: z.literal("geoin"), value: z.string()}), z.object({condition_type: z.literal("BirthdayMerge").optional(), field: z.string(), op: z.enum(["is", "not", "blank", "blank_not"]), value: z.string().optional()}), z.object({condition_type: z.literal("DateMerge").optional(), field: z.string(), op: z.enum(["is", "not", "less", "blank", "blank_not", "greater"]), value: z.string().optional()}), z.object({condition_type: z.literal("SelectMerge").optional(), field: z.string(), op: z.enum(["is", "not", "blank", "blank_not", "notcontain", "contains"]), value: z.string().optional()}), z.object({condition_type: z.literal("TextMerge").optional(), field: z.string(), op: z.enum(["is", "not", "contains", "notcontain", "starts", "ends", "greater", "less", "blank", "blank_not"]), value: z.string().optional()}), z.object({condition_type: z.literal("EmailAddress").optional(), field: z.enum(["merge0", "EMAIL"]), op: z.enum(["is", "not", "contains", "notcontain", "starts", "ends", "greater", "less"]), value: z.string().optional()}), z.object({condition_type: z.literal("PredictedGender").optional(), field: z.literal("predicted_gender"), op: z.enum(["is", "not"]), value: z.enum(["male", "female"])}), z.object({condition_type: z.literal("PredictedAge").optional(), field: z.literal("predicted_age_range"), op: z.literal("is"), value: z.enum(["18-24", "25-34", "35-44", "45-54", "55-64", "65+"])}), z.object({condition_type: z.literal("NewSubscribers").optional(), field: z.literal("timestamp_opt").optional(), op: z.literal("date_within").optional(), value: z.string().optional()})])).optional(), match: z.enum(["any", "all"]).optional()}).optional(), type: z.enum(["saved", "static", "fuzzy"]).optional(), updated_at: 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 }; /** * Update segment * * PATCH /3.0/lists/{list_id}/segments/{segment_id} * * Also accepts auth: oauth2 */ export const patchApi30ListsListIdSegmentsSegmentId = async (args: PatchApi30ListsListIdSegmentsSegmentIdArgs, options?: PatchApi30ListsListIdSegmentsSegmentIdOptions) => { 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: "PATCH", 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(patchApi30ListsListIdSegmentsSegmentIdResponse, await res.json()) };