import {z} from 'zod' export type Money = {amount: number, currency: unknown}; export type RefundedTaxRequest = {amount: Money, code: string}; export type RefundRequest = {order_line_id?: string | undefined, price: Money, product_id?: string | undefined, quantity?: number | undefined, reason: 'CUSTOMER_CANCELATION' | 'OUT_OF_STOCK' | 'RETURN' | 'ITEM_NOT_RECEIVED' | 'AGREEMENT' | 'DEFECT' | 'UNABLE_TO_DELIVER', shipping_price: Money, shipping_taxes?: Array | undefined, taxes?: Array | undefined}; export type RefundOrderLinesRequest = {refunds: Array}; export type CreateApiV2OrdersOrderIdRefundArgs = {order_id: string, body: RefundOrderLinesRequest}; export type CreateApiV2OrdersOrderIdRefundOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const asynchronousActionResponse = z.object({action_id: z.string(), tracking_id: 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 }; /** * v2-refundOrderLines - Refund order lines * * POST /v2/orders/{order_id}/refund */ export const createApiV2OrdersOrderIdRefund = async (args: CreateApiV2OrdersOrderIdRefundArgs, options?: CreateApiV2OrdersOrderIdRefundOptions) => { const {baseUrl = "https://miraklconnect.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/orders/${encodeURIComponent(String(args.order_id))}/refund${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(asynchronousActionResponse, await res.json()) };