Vue Storefront is now Alokai! Learn More
GetOrders

GetOrders

Implements GetOrders Unified Method.

Source

import { assertAuthorized, defineApi } from "@vsf-enterprise/unified-api-sfcc";
import type { Order } from "@vsf-enterprise/sfcc-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";
import "./extended.d";

export const getOrders = defineApi.getOrders(async (context, args) => {
  await assertAuthorized(context);

  const { normalizeOrderListItem, normalizePagination } = getNormalizers(context);

  const { currentPage = 1, pageSize = 20 } = args ?? {};
  const {
    data: orders = [],
    limit,
    offset,
    total,
  } = await context.api.getCustomerOrders({
    offset: (currentPage - 1) * pageSize,
    limit: pageSize,
  });

  return {
    orders: orders.map((order: Order) => normalizeOrderListItem(order)).filter(Boolean),
    pagination: normalizePagination({
      limit,
      total,
      offset,
    }),
  };
});