Category normalizer
The normalizeCategory
function is used to map a Commercetools Category
into the unified SfCategory
data model.
Parameters
Name | Type | Default value | Description |
---|---|---|---|
category | Category | Commercetools Category |
Extending
The SfCategory
model is returned from the Unified Methods such as SearchProducts
, and GetCategories
. If the SfCategory
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 SfCategory
with an externalId
field.
import { normalizers as normalizersCT, defineNormalizers } from "@vsf-enterprise/unified-api-commercetools";
const normalizers = defineNormalizers<typeof normalizersCT>()({
...normalizersCT,
normalizeCategory: (category) => ({
...normalizersCT.normalizeCategory(category),
externalId: category.externalId,
}),
});
Source
category.ts
import { maybe, slugify } from "@shared/utils";
import { defineNormalizer } from "../defineNormalizer";
export const normalizeCategory = defineNormalizer.normalizeCategory((category, ctx) => {
const subcategories =
category.children && category.children.length > 0
? category.children.map((child) => ctx.normalizers.normalizeCategory(child))
: null;
const name = category.name ?? String(category.id);
return {
id: category.id,
name,
slug: category.slug ?? slugify(name),
subcategories,
parentCategoryId: maybe(category.parent?.id),
};
});