import {z} from 'zod' export type CreateApi30EcommerceStoresStoreIdCustomersArgs = {store_id: string, body: {address?: ({address1?: string | undefined, address2?: string | undefined, city?: string | undefined, country?: string | undefined, country_code?: string | undefined, postal_code?: string | undefined, province?: string | undefined, province_code?: string | undefined}) | undefined, company?: string | undefined, email_address?: string | undefined, first_name?: string | undefined, id: string, last_name?: string | undefined, opt_in_status: boolean, sms_phone_number?: string | undefined, total_spent?: (number | string) | undefined}}; export type CreateApi30EcommerceStoresStoreIdCustomersOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApi30EcommerceStoresStoreIdCustomersResponse = 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(), address: z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), country: z.string().optional(), country_code: z.string().optional(), postal_code: z.string().optional(), province: z.string().optional(), province_code: z.string().optional()}).optional(), company: z.string().optional(), created_at: z.string().optional(), email_address: z.string().optional(), first_name: z.string().optional(), id: z.string().optional(), last_name: z.string().optional(), opt_in_status: z.boolean().optional(), orders_count: z.number().int().optional(), sms_phone_number: z.string().optional(), total_spent: z.union([z.number(), z.string()]).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 }; /** * Add customer * * POST /3.0/ecommerce/stores/{store_id}/customers * * Also accepts auth: oauth2 */ export const createApi30EcommerceStoresStoreIdCustomers = async (args: CreateApi30EcommerceStoresStoreIdCustomersArgs, options?: CreateApi30EcommerceStoresStoreIdCustomersOptions) => { 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/ecommerce/stores/${encodeURIComponent(String(args.store_id))}/customers${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(createApi30EcommerceStoresStoreIdCustomersResponse, await res.json()) };