Vue Storefront is now Alokai! Learn More
Order normalizer

Order normalizer

  • normalizeOrder: This function is used to map BigCommerce OrderByCartResponse into SfOrder, which includes order details data.
  • normalizeOrderListItem: This function maps BigCommerce Order into Unified SfOrderListItem which includes only basic order details, used to display an data in an order list.

Parametersri:link

normalizeOrderri:link

NameTypeDefault valueDescription
inputOrderByCartResponseBigCommerce OrderByCartResponse

normalizeOrderListItemri:link

NameTypeDefault valueDescription
orderOrderBigCommerce Order

normalizeOrderLineItemri:link

NameTypeDefault valueDescription
lineItemOrderItemBigCommerce Order Item

Extendingri:link

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.

Sourceri:link

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",
  };
});