Order normalizer
normalizeOrder
: This function is used to map BigCommerceOrderByCartResponse
intoSfOrder
, which includes order details data.normalizeOrderListItem
: This function maps BigCommerceOrder
into UnifiedSfOrderListItem
which includes only basic order details, used to display an data in an order list.
Parameters
normalizeOrder
Name | Type | Default value | Description |
---|---|---|---|
input | OrderByCartResponse | BigCommerce OrderByCartResponse |
normalizeOrderListItem
Name | Type | Default value | Description |
---|---|---|---|
order | Order | BigCommerce Order |
normalizeOrderLineItem
Name | Type | Default value | Description |
---|---|---|---|
lineItem | OrderItem | BigCommerce Order Item |
Extending
The SfOrder
is returned from the GetOrders
Method. If the SfOrder
structure doesn't contain the information you need for your Storefront, you can extend its logic using the defineNormalizers
function. The following example demonstrates how to extend SfOrder
with a staffNotes
field.
import { normalizers as normalizersBC, defineNormalizers } from "@vsf-enterprise/unified-api-bigcommerce";
const normalizers = defineNormalizers<typeof normalizersBC>()({
...normalizersBC,
normalizeOrder: (order, context) => ({
...normalizersBC.normalizeOrder(order, context),
staffNotes: order.staff_notes,
}),
});
You can override the normalizeOrder
, but it's also available to override the smaller normalizers such as normalizeAddress
, normalizeShippingMethod
.
Source
order.ts
/* eslint-disable complexity */
import { defineNormalizer } from "../defineNormalizer";
export const normalizeOrder = defineNormalizer.normalizeOrder((input, ctx) => {
const {
id,
date_created,
status,
products,
subtotal_ex_tax,
shipping_cost_inc_tax,
total_tax,
total_inc_tax,
billing_address,
shipping_addresses: { 0: shipping_address },
} = input;
const { shipping_method } = shipping_address ?? {};
if (
!date_created ||
!subtotal_ex_tax ||
!shipping_cost_inc_tax ||
!total_tax ||
!total_inc_tax ||
!billing_address ||
!products ||
products.length === 0 ||
!shipping_method ||
!shipping_address
) {
throw new Error("Missing required order fields");
}
const lineItems = products.map((entry) => ctx.normalizers.normalizeOrderLineItem(entry));
return {
id: id.toString(),
orderDate: new Date(date_created).toISOString(),
status: status ?? "UNKNOWN",
lineItems,
subtotalPrice: ctx.normalizers.normalizeMoney(Number.parseFloat(subtotal_ex_tax)),
totalShippingPrice: ctx.normalizers.normalizeMoney(Number.parseFloat(shipping_cost_inc_tax)),
totalTax: ctx.normalizers.normalizeMoney(Number.parseFloat(total_tax)),
totalPrice: ctx.normalizers.normalizeMoney(Number.parseFloat(total_inc_tax)),
shippingAddress: ctx.normalizers.normalizeAddress(shipping_address),
billingAddress: ctx.normalizers.normalizeAddress(billing_address),
shippingMethod: ctx.normalizers.normalizeShippingMethod({
name: shipping_method,
price: shipping_cost_inc_tax,
}),
paymentMethod: "CARD",
};
});