import {z} from 'zod' export type GetApi30ListsListIdSignupFormsArgs = {list_id: string}; export type GetApi30ListsListIdSignupFormsOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApi30ListsListIdSignupFormsResponse = 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(), list_id: z.string().optional(), signup_forms: 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(), 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()})).optional(), total_items: 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 }; /** * List signup forms * * GET /3.0/lists/{list_id}/signup-forms * * Also accepts auth: oauth2 */ export const getApi30ListsListIdSignupForms = async (args: GetApi30ListsListIdSignupFormsArgs, options?: GetApi30ListsListIdSignupFormsOptions) => { 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 (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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApi30ListsListIdSignupFormsResponse, await res.json()) };