import {z} from 'zod' export type CreateApi30ListsListIdSignupFormsArgs = {list_id: string, body: {contents?: (Array<{section?: ('signup_message' | 'unsub_message' | 'signup_thank_you_title') | undefined, value?: string | undefined}>) | undefined, header?: ({image_align?: ('none' | 'left' | 'center' | 'right') | undefined, image_alt?: string | undefined, image_border_color?: string | undefined, image_border_style?: ('none' | 'solid' | 'dotted' | 'dashed' | 'double' | 'groove' | 'outset' | 'inset' | 'ridge') | undefined, image_border_width?: string | undefined, image_height?: string | undefined, image_link?: string | undefined, image_target?: ('_blank' | 'null') | undefined, image_url?: string | undefined, image_width?: string | undefined, text?: string | undefined}) | undefined, styles?: (Array<{options?: (Array<{property?: string | undefined, value?: string | undefined}>) | undefined, selector?: ('page_background' | 'page_header' | 'page_outer_wrapper' | 'body_background' | 'body_link_style' | 'forms_buttons' | 'forms_buttons_hovered' | 'forms_field_label' | 'forms_field_text' | 'forms_required' | 'forms_required_legend' | 'forms_help_text' | 'forms_errors' | 'monkey_rewards_badge') | undefined}>) | undefined}}; export type CreateApi30ListsListIdSignupFormsOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApi30ListsListIdSignupFormsResponse = 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(), contents: z.array(z.object({section: z.enum(["signup_message", "unsub_message", "signup_thank_you_title"]).optional(), value: z.string().optional()})).optional(), header: z.object({image_align: z.enum(["none", "left", "center", "right"]).optional(), image_alt: z.string().optional(), image_border_color: z.string().optional(), image_border_style: z.enum(["none", "solid", "dotted", "dashed", "double", "groove", "outset", "inset", "ridge"]).optional(), image_border_width: z.string().optional(), image_height: z.string().optional(), image_link: z.string().optional(), image_target: z.enum(["_blank", "null"]).optional(), image_url: z.string().optional(), image_width: z.string().optional(), text: z.string().optional()}).optional(), list_id: z.string().optional(), signup_form_url: z.string().optional(), styles: z.array(z.object({options: z.array(z.object({property: z.string().optional(), value: z.string().optional()})).optional(), selector: z.enum(["page_background", "page_header", "page_outer_wrapper", "body_background", "body_link_style", "forms_buttons", "forms_buttons_hovered", "forms_field_label", "forms_field_text", "forms_required", "forms_required_legend", "forms_help_text", "forms_errors", "monkey_rewards_badge"]).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 }; /** * Customize signup form * * POST /3.0/lists/{list_id}/signup-forms * * Also accepts auth: oauth2 */ export const createApi30ListsListIdSignupForms = async (args: CreateApi30ListsListIdSignupFormsArgs, options?: CreateApi30ListsListIdSignupFormsOptions) => { 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))}/signup-forms${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(createApi30ListsListIdSignupFormsResponse, await res.json()) };