import {z} from 'zod' export type GetApiOrderUuidDetailsArgs = {Authorization: string, uuid: string, 'X-ShopBack-Idempotent-Id'?: string | undefined}; export type GetApiOrderUuidDetailsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressResponse = z.object({id: z.number().int().optional(), uuid: z.string().optional(), createdAt: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), line3: z.string().optional(), suburb: z.string().optional(), postcode: z.string().optional(), countryCode: z.string().optional(), district: z.string().optional(), generalDistrictLocation: z.string().optional(), city: z.string().optional(), state: z.string().optional()}); export const orderItemImageResponse = z.object({imageLocation: z.string()}); export const merchantOrderItemResponse = z.object({name: z.string(), sku: z.string().optional(), ean: z.string().optional(), description: z.string().optional(), detailedDescription: z.string().optional(), quantity: z.number().int(), amount: z.number().optional(), originalAmount: z.number().optional(), taxAmount: z.number().optional(), images: z.array(orderItemImageResponse).optional()}); export const detailedMerchantOrderResponse = z.object({id: z.number().int(), uuid: z.string(), createdAt: z.string(), status: z.enum(["INITIATED", "APPROVED", "CANCELLED", "REJECTED", "REFUNDED"]), orderType: z.enum(["INSTORE", "ONLINE"]), initialAmount: z.number(), totalAmount: z.number(), originalAmount: z.number(), taxAmount: z.number().optional(), voucherCode: z.string().optional(), currency: z.string(), merchantRef: z.string(), consumerEmail: z.string(), shippingAddress: addressResponse, shippingMethod: z.enum(["NORMAL", "EXPRESS", "PICKUP", "FREE", "MERCHANT_SPECIFIED"]).optional(), orderItems: z.array(merchantOrderItemResponse), shippingAmount: z.number().optional(), paymentType: z.enum(["PAYLATER", "PAY"]).optional(), failureReason: z.string().nullable().optional(), ccdOrderReference: 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 }; /** * Get order details * * GET /order/{uuid}/details */ export const getApiOrderUuidDetails = async (args: GetApiOrderUuidDetailsArgs, options?: GetApiOrderUuidDetailsOptions) => { const {baseUrl = "https://integrations-sandbox.shopback.com/demo/merchant", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['X-ShopBack-Idempotent-Id'] != null) headers.set("X-ShopBack-Idempotent-Id", String(args['X-ShopBack-Idempotent-Id'])) const url = `${baseUrl.replace(/\/+$/, '')}/order/${encodeURIComponent(String(args.uuid))}/details${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(detailedMerchantOrderResponse, await res.json()) };