Category normalizer
The normalizeCategory function is used to map a SFCC Category into the unified SfCategory data model.
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
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 defineNormalizers function. The following example demonstrates how to extend SfCategory with an thumbnail field.
import { normalizers as normalizersSFCC, defineNormalizers } from "@vsf-enterprise/unified-api-sfcc";
const normalizers = defineNormalizers<typeof normalizersSFCC>()({
...normalizersSFCC,
normalizeCategory: (category) => ({
...normalizersSFCC.normalizeCategory(category),
thumbnail: category.thumbnail,
}),
});
Source
category.ts
import { maybe, slugify } from "@shared/utils";
import { defineNormalizer } from "../defineNormalizer";
export const normalizeCategory = defineNormalizer.normalizeCategory((category, ctx) => {
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(subCategory, ctx)),
),
parentCategoryId: maybe(parentCategoryId),
};
});