Category normalizer
The normalizeCategory function is used to map a SFCC Category into the unified SfCategory data model.
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
context | NormalizerContext | Context needed for the normalizer. | |
category | Category. | SFCC 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 addCustomFields API. The following example demonstrates how to extend SfCategory with an thumbnail field.
export const unifiedApiExtension = createUnifiedExtension({
normalizers: {
addCustomFields: [
{
normalizeCategory: (context, category) => ({
thumbnail: category.thumbnail,
}),
},
],
},
config: {
...
},
});
Source
category.ts
import { maybe, slugify } from "@/helpers";
import { defineNormalizer } from "../defineNormalizer";
export const normalizeCategory = defineNormalizer.normalizeCategory((context, category) => {
const parentCategoryId = category.parentCategoryId === "root" ? null : category.parentCategoryId;
return {
id: category.id,
name: category.name ?? category.id,
slug: slugify(category.id),
subcategories: maybe(
category.categories?.map((subCategory) => normalizeCategory(context, subCategory)),
),
parentCategoryId: maybe(parentCategoryId),
};
});