Vue Storefront is now Alokai! Learn More
GetCategory

GetCategory

Implements GetCategory Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-sfcc";
import { getNormalizers } from "@vsf-enterprise/unified-api-sfcc/udl";
import type { Category } from "@vsf-enterprise/sfcc-types";


declare module "@vsf-enterprise/unified-api-sfcc" {
  interface GetCategoryExtendedArgs {
    /**
     * The nesting depth of subcategories to get
     * @default 0
     */
    levels?: number;
  }
}


interface RawResult {
  ancestors: Category[];
  category: Category | null;
}

export const getCategory = defineApi.getCategory(async (context, args) => {
  const categories = await context.api.getCategory({ id: "root", levels: 10 });
  const { normalizeCategory } = getNormalizers(context);

  const rawResult: RawResult = { ancestors: [], category: null };

  const searchCategory = (tree: Category[], ancestors: Category[]) => {
    for (const category of tree) {
      if (category.id === args.id) {
        rawResult.category = category;
        rawResult.ancestors = [...ancestors];
        return;
      } else if (category.categories && category.categories.length > 0) {
        ancestors.push(category);
        searchCategory(category.categories, ancestors);
        ancestors.pop(); // Remove the last ancestor for backtracking
      }
    }
  };

  searchCategory(categories.categories || [], []);

  if (!rawResult.category) {
    throw { statusCode: 404, message: "Category not found" };
  }

  return {
    ancestors: rawResult.ancestors.map((category) => normalizeCategory(category)),
    category: normalizeCategory(rawResult.category),
  };
});