import {z} from 'zod' export type CreateApiV3CustomerArgs = {echo?: boolean | undefined, body: {merchantId: number, name: string, firstName: string, lastName?: string | undefined, address1?: string | undefined, address2?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, addressName?: string | undefined, customerType?: string | undefined, email?: string | undefined, mobile?: string | undefined, phone?: string | undefined, number?: string | undefined, isTaxExempt?: boolean | undefined, taxId?: string | undefined, spendProfile?: string | undefined, customFields?: (Array<{fieldDataType?: string | undefined, fieldDefinitionId?: number | undefined, isRequired?: boolean | undefined, name?: string | undefined, value?: string | undefined}>) | undefined, activeStatus?: boolean | undefined, selectedSpendProfile?: string | undefined, spendProfileOverride?: boolean | undefined}}; export type CreateApiV3CustomerOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiV3CustomerResponse = z.object({id: z.number().int().optional(), created: z.string().optional(), name: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), number: z.string().optional(), taxId: z.string().optional(), visits: z.number().int().optional(), transactionCount: z.number().int().optional(), spend: z.string().optional(), sales: z.string().optional(), refunds: z.string().optional(), lastActivity: z.string().optional(), allowPromotions: z.boolean().optional(), loyaltyStatus: z.string().optional(), loyaltyEnrolled: z.string().optional(), isVip: z.boolean().optional(), spendProfile: z.string().optional(), spendProfileIcon: z.string().optional(), addressName: z.string().optional(), address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.string().optional(), customerType: z.string().optional(), spendProfileOverride: z.boolean().optional(), isTaxExempt: z.boolean().optional(), hasPayments: z.boolean().optional(), hasContracts: z.boolean().optional(), hasInvoices: z.boolean().optional(), displayColor: z.string().optional(), activeStatus: z.boolean().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 }; /** * Create customer * * POST /v3/customer */ export const createApiV3Customer = async (args: CreateApiV3CustomerArgs, options?: CreateApiV3CustomerOptions) => { const {baseUrl = "https://sandbox.api.mxmerchant.com/checkout", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.echo != null) query.append("echo", String(args.echo)) if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/customer${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(createApiV3CustomerResponse, await res.json()) };