Customer normalizer
The normalizeCustomer function is used to map a Magento Customer into the unified SfCustomer data model.
Parameters
| Name | Type | Default value | Description |
|---|---|---|---|
context | NormalizerContext | context needed for the normalizer | |
customer | Customer | Magento Customer |
Extending
The SfCustomer model is returned from Unified Methods such as RegisterCustomer, LoginCustomer and GetCustomer. If the SfCustomer 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 SfCustomer with a isSubscribed field.
export const unifiedApiExtension = createUnifiedExtension({
normalizers: {
addCustomFields: [
{
normalizeCustomer: (context, customer) => ({
isSubscribed: customer.is_subscribed,
}),
},
],
},
config: {
...
},
});
Source
customer.ts
import { ValidationError } from "@alokai/connect/middleware";
import { z } from "zod";
import { defineNormalizer } from "../defineNormalizer";
const customerSchema = z.looseObject({
email: z.string(),
firstname: z.string(),
lastname: z.string(),
});
export const normalizeCustomer = defineNormalizer.normalizeCustomer((_context, customer) => {
const result = customerSchema.safeParse(customer);
if (!result.success) {
throw ValidationError.fromStandardSchemaError(
result.error,
"Customer must have a firstName, lastName and email",
);
}
const validated = result.data;
return {
email: validated.email,
firstName: validated.firstname,
id: validated.email,
lastName: validated.lastname,
};
});