Vue Storefront is now Alokai! Learn More
GetCategory

GetCategory

Implements GetCategory Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-commercetools";
import type { CategoryWhereSearch, GetCategoryResponse } from "@vsf-enterprise/commercetools-api";
import type { Category } from "@vsf-enterprise/commercetools-types";
import { getNormalizers } from "@vue-storefront/unified-data-model";
import { validate as validateUuid } from "uuid";

export const getCategory = defineApi.getCategory(async (context, args) => {
  const search: CategoryWhereSearch = validateUuid(args.id)
    ? { catId: args.id }
    : { slug: args.id };
  const response = await context.api.getCategory(search);
  const category = unwrapResponse(response);

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

  const { normalizeCategory } = getNormalizers(context);
  const ancestors = getCategoryAncestors(category);

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

function unwrapResponse(response: GetCategoryResponse) {
  return response.data?.categories.results?.at(0);
}

function getCategoryAncestors(category: Category): Category[] {
  // Although category.ancestors is declared on the type, it is not always present
  return category?.parent ? [...getCategoryAncestors(category.parent), category.parent] : [];
}