import {z} from 'zod' export type CreateApi30EcommerceStoresStoreIdCartsArgs = {store_id: string, body: {campaign_id?: string | undefined, checkout_url?: string | undefined, currency_code: string, customer: {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 | undefined}, lines: Array<{id: string, price: number | string, product_id: string, product_variant_id: string, quantity: number}>, order_total: number | string, tax_total?: (number | string) | undefined}}; export type CreateApi30EcommerceStoresStoreIdCartsOptions = {baseUrl?: string | undefined, bearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApi30EcommerceStoresStoreIdCartsResponse = 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(), campaign_id: z.string().optional(), checkout_url: z.string().optional(), created_at: z.string().optional(), currency_code: z.string().optional(), customer: 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()}).optional(), id: z.string().optional(), lines: 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(), id: z.string().optional(), price: z.number().optional(), product_id: z.string().optional(), product_title: z.string().optional(), product_variant_id: z.string().optional(), product_variant_title: z.string().optional(), quantity: z.number().int().optional()})).optional(), order_total: z.number().optional(), tax_total: z.number().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 cart * * POST /3.0/ecommerce/stores/{store_id}/carts * * Also accepts auth: oauth2 */ export const createApi30EcommerceStoresStoreIdCarts = async (args: CreateApi30EcommerceStoresStoreIdCartsArgs, options?: CreateApi30EcommerceStoresStoreIdCartsOptions) => { 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))}/carts${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(createApi30EcommerceStoresStoreIdCartsResponse, await res.json()) };