import {z} from 'zod' export type OrderLineItem = {name?: string | undefined, quantity?: number | undefined, unit_price?: number | undefined, sku?: string | undefined, total_price?: number | undefined, type?: string | undefined, tax_amount?: number | undefined} | {[key: string]: unknown}; export type OrderUpdateRequest = {amount?: number | undefined, order_number?: string | undefined, po_number?: string | undefined, line_items?: Array | undefined}; export type UpdateApiOrdersOrderIdArgs = {order_id: string, body: OrderUpdateRequest}; export type UpdateApiOrdersOrderIdOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const orderLineItem = z.object({name: z.string().optional(), quantity: z.number().optional(), unit_price: z.number().optional(), sku: z.string().optional(), total_price: z.number().optional(), type: z.string().optional(), tax_amount: z.number().optional()}).and(z.record(z.string(), z.unknown())); export const orderCaptureEvent = z.object({amount: z.number().optional(), idempotency_key: z.string().optional(), processed_at: z.string().optional(), invoice_id: z.string().optional()}); export const orderCustomer = z.object({id: z.string().optional(), name: z.string().optional(), remaining_credit: z.number().optional()}); export const order = z.object({id: z.string().optional(), checkout_id: z.string().optional(), number: z.string().optional(), po_number: z.string().optional(), advance_requested: z.boolean().optional(), source: z.string().optional(), status: z.string().optional(), amount_authorized: z.number().optional(), amount_captured: z.number().optional(), amount_canceled: z.number().optional(), amount_remaining: z.number().optional(), first_captured_at: z.string().nullable().optional(), line_items: z.array(orderLineItem).optional(), authorization_expires_at: z.string().optional(), authorization_expiry_source: z.string().optional(), capture_events: z.array(orderCaptureEvent).optional(), authorized_at: z.string().optional(), updated_at: z.string().optional(), customer: orderCustomer.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 }; /** * Update an order * * PUT /orders/{order_id} * * Also accepts auth: basicAuth */ export const updateApiOrdersOrderId = async (args: UpdateApiOrdersOrderIdArgs, options?: UpdateApiOrdersOrderIdOptions) => { const {baseUrl = "https://app-sandbox.resolvepay.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/orders/${encodeURIComponent(String(args.order_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(order, await res.json()) };