import {z} from 'zod' export type CreateApiOrderArgs = {echo?: boolean | undefined, body: {merchantId?: number | undefined, lineItems?: (Array<{id?: number | undefined, name?: string | undefined, price?: number | undefined, quantity?: string | undefined, discountAmount?: number | undefined, totalAmount?: number | undefined, taxCategory?: (Array<{id?: string | undefined, name?: string | undefined, code?: string | undefined, taxIds?: Array | undefined, totalTax?: string | undefined, isPrimary?: boolean | undefined, primaryExists?: boolean | undefined, display?: string | undefined, taxRate?: number | undefined}>) | undefined, initials?: string | undefined, taxes?: (Array<{id?: number | undefined, name?: string | undefined, description?: string | undefined, rate?: string | undefined, display?: string | undefined, taxCode?: string | undefined, taxCategoryId?: number | undefined}>) | undefined, amount?: string | undefined, taxAmount?: string | undefined, priceDiscountAmount?: string | undefined, taxRate?: number | undefined}>) | undefined, shippingLineItems?: (Array<{id?: number | undefined, name?: string | undefined, price?: number | undefined, quantity?: string | undefined, discountAmount?: number | undefined, totalAmount?: number | undefined, taxCategory?: (Array<{id?: string | undefined, name?: string | undefined, code?: string | undefined, taxIds?: Array | undefined, totalTax?: string | undefined, isPrimary?: boolean | undefined, primaryExists?: boolean | undefined, display?: string | undefined, taxRate?: number | undefined}>) | undefined, initials?: string | undefined, taxes?: (Array<{id?: number | undefined, name?: string | undefined, description?: string | undefined, rate?: string | undefined, display?: string | undefined, taxCode?: string | undefined, taxCategoryId?: number | undefined}>) | undefined, amount?: string | undefined, taxAmount?: string | undefined, priceDiscountAmount?: string | undefined, taxRate?: number | undefined}>) | undefined, orderStatus?: string | undefined, sourceType?: string | undefined, fulfillmentStatus?: string | undefined, paymentStatus?: string | undefined, orderedDate?: string | undefined, discounts?: (Array<{amount?: string | undefined, code?: string | undefined, collections?: Array | undefined, discountId?: number | undefined, discountRateType?: string | undefined, discountType?: string | undefined, merchantId?: number | undefined, name?: string | undefined, products?: Array | undefined, rate?: string | undefined}>) | undefined, totalAmount?: string | undefined, taxAmount?: string | undefined, balance?: string | undefined, subtotalAmount?: string | undefined, customerName?: string | undefined, customer?: ({id?: number | undefined, name?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined}) | undefined, customerId?: string | undefined, contactEmail?: string | undefined, notes?: string | undefined, shippedDate?: string | undefined, billingAddress?: ({address1?: string | undefined, address2?: string | undefined, isDefault?: boolean | undefined, state?: string | undefined, zip?: string | undefined}) | undefined, shippingAddress?: ({address1?: string | undefined, address2?: string | undefined, isDefault?: boolean | undefined, state?: string | undefined, zip?: string | undefined}) | undefined, lineItemCount?: number | undefined}}; export type CreateApiOrderOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiOrderResponse = z.object({id: z.number().int().optional(), merchantId: z.number().int().optional(), created: z.string().optional(), modified: z.string().optional(), sourceType: z.string().optional(), shippedDate: z.string().optional(), reference: z.string().optional(), accessCode: z.string().optional(), orderStatus: z.string().optional(), invoiceStatus: z.string().optional(), paymentStatus: z.string().optional(), packingStatus: z.string().optional(), fulfillmentStatus: z.string().optional(), returnStatus: z.string().optional(), salesOrder: z.boolean().optional(), deleted: z.boolean().optional(), subtotalAmount: z.string().optional(), discountAmount: z.string().optional(), taxAmount: z.string().optional(), totalAmount: z.string().optional(), balance: z.string().optional(), invoicedAmount: z.string().optional(), paidAmount: z.string().optional(), lineItemCount: z.number().int().optional(), saleQuantity: z.number().int().optional(), returnQuantity: z.number().int().optional(), netQuantity: z.number().int().optional(), invoiceQuantity: z.number().int().optional(), customerId: z.number().int().optional(), taxExempt: z.boolean().optional(), customerName: z.string().optional(), contactEmail: z.string().optional(), billingAddress: z.object({}).optional(), shippingAddress: z.object({}).optional(), shippingIsSameAsBilling: z.boolean().optional(), lineItems: z.array(z.object({quantityReturned: z.string().optional(), quantityInvoiced: z.string().optional(), created: z.string().optional(), modified: z.string().optional(), id: z.number().int().optional(), position: z.number().int().optional(), name: z.string().optional(), price: z.string().optional(), discountRate: z.string().optional(), discountAmount: z.string().optional(), priceDiscountAmount: z.string().optional(), taxRate: z.string().optional(), taxAmount: z.string().optional(), quantity: z.string().optional(), amount: z.string().optional(), discounts: z.array(z.object({})).optional(), taxes: z.array(z.object({})).optional()})).optional(), shippingLineItems: z.array(z.object({})).optional(), discounts: z.array(z.object({id: z.number().int().optional(), discountRateType: z.string().optional(), name: z.string().optional(), rate: z.string().optional(), amount: z.string().optional(), code: z.string().optional(), tags: z.array(z.object({})).optional(), products: z.array(z.object({})).optional(), collections: z.array(z.object({})).optional(), discountId: z.number().int().optional()})).optional(), orderNumber: z.number().int().optional(), barCode: z.string().optional(), taxes: z.array(z.object({})).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 Order * * POST /order */ export const createApiOrder = async (args: CreateApiOrderArgs, options?: CreateApiOrderOptions) => { 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(/\/+$/, '')}/order${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(createApiOrderResponse, await res.json()) };