Skip to main content

Cart Factory

The CartFactory is a class responsible for creating the server-side and client-side components for the cart feature. It extends the AbstractFeatureFactory, ensuring a consistent pattern for feature creation.

This class is generic and must be typed with a schema collection (<S extends CartSchemaCollectionType>) to maintain type safety between the client and server.

Constructor

new CartFactory(definition)

NameTypeDescriptionDefault
definitionFactoryDefinition<S>The feature definition containing the schema, as well as adapter and provider classesundefined

Methods

createModule(config: CartServerConfig)

Creates an instance of the CartModule for use on the server.

It will throw an error if it's not run in a 'server' environment (based on process.env.APP_CODE). It uses the provided config to instantiate the module and its provider.

createClient(options: CartClientOptions)

Creates an instance of the CartClient, which is used on the client application to interact with the cart API.

The api config should be passed as part of the options.

Usage example

The factory is instantiated with a cart definition (an object defining the schema as well as the adapter and provider classes for the cart), which allows the resulting CartClient to be tailored to your specific backend implementation:

import { CartFactory } from '@archibald/cart';
import { CartClientProvider, commerceDefinition } from '@archibald/commerce/cart';

// Initialize the universal CartClient
const cart = new CartFactory(commerceDefinition);
const cartClient = cart.createClient({ api });

// Wrap your app
<CartClientProvider client={cartClient}>
<App />
</CartClientProvider>;

This approach allows you to easily switch between different cart implementations by simply changing the definition that is passed to the CartFactory.