Skip to content

Commit

Permalink
feat(commerce): simplify commerce facet types (#3657)
Browse files Browse the repository at this point in the history
> Since the import is coming from commerce, [...] there's no need to
specify Commerce in the controller name. `CommerceRegularFacet` ->
`RegularFacet`.

See:
coveo/barca-sports#165 (review)

[CAPI-581]

[CAPI-581]:
https://coveord.atlassian.net/browse/CAPI-581?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
  • Loading branch information
Spuffynism committed Mar 1, 2024
1 parent e4ac6ba commit 8732da4
Show file tree
Hide file tree
Showing 27 changed files with 127 additions and 133 deletions.
12 changes: 6 additions & 6 deletions packages/headless/src/commerce.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ export {
export {buildProductListingSort} from './controllers/commerce/product-listing/sort/headless-product-listing-sort';
export {buildSearchSort} from './controllers/commerce/search/sort/headless-search-sort';

export type {CommerceRegularFacet} from './controllers/commerce/core/facets/regular/headless-commerce-regular-facet';
export type {CommerceNumericFacet} from './controllers/commerce/core/facets/numeric/headless-commerce-numeric-facet';
export type {CommerceDateFacet} from './controllers/commerce/core/facets/date/headless-commerce-date-facet';
export type {RegularFacet} from './controllers/commerce/core/facets/regular/headless-commerce-regular-facet';
export type {NumericFacet} from './controllers/commerce/core/facets/numeric/headless-commerce-numeric-facet';
export type {DateFacet} from './controllers/commerce/core/facets/date/headless-commerce-date-facet';
export type {
FacetType,
FacetValueRequest,
Expand All @@ -112,10 +112,10 @@ export type {SearchFacetGenerator} from './controllers/commerce/search/facets/he
export {buildSearchFacetGenerator} from './controllers/commerce/search/facets/headless-search-facet-generator';

export type {
CommerceFacetSearch,
CommerceFacetSearchResult,
FacetSearch,
FacetSearchResult,
} from './controllers/commerce/core/facets/searchable/headless-commerce-facet-search';
export type {CommerceFacetSearchState} from './controllers/commerce/core/facets/searchable/headless-commerce-searchable-facet';
export type {FacetSearchState} from './controllers/commerce/core/facets/searchable/headless-commerce-searchable-facet';

export type {Search} from './controllers/commerce/search/headless-search';
export {buildSearch} from './controllers/commerce/search/headless-search';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import {
import {commonOptions} from '../../../product-listing/facets/headless-product-listing-facet-options';
import {DateRangeRequest} from '../headless-core-commerce-facet';
import {
CommerceDateFacet,
CommerceDateFacetOptions,
DateFacet,
DateFacetOptions,
buildCommerceDateFacet,
} from './headless-commerce-date-facet';

jest.mock(
'../../../../../features/facets/range-facets/date-facet-set/date-facet-actions'
);

describe('CommerceDateFacet', () => {
describe('DateFacet', () => {
const facetId: string = 'date_facet_id';
const type: FacetType = 'dateRange';
const start = '2023-01-01';
const end = '2024-01-01';
let options: CommerceDateFacetOptions;
let options: DateFacetOptions;
let state: CommerceAppState;
let engine: MockedCommerceEngine;
let facet: CommerceDateFacet;
let facet: DateFacet;

function initFacet() {
engine = buildMockCommerceEngine(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,33 @@ import {
buildCoreCommerceFacet,
} from '../headless-core-commerce-facet';

export type CommerceDateFacetOptions = Omit<
export type DateFacetOptions = Omit<
CoreCommerceFacetOptions,
'toggleSelectActionCreator' | 'toggleExcludeActionCreator'
>;

/**
* The `CommerceDateFacet` controller offers a high-level programming interface for implementing date commerce
* The `DateFacet` controller offers a high-level programming interface for implementing date commerce
* facet UI component.
*/
export type CommerceDateFacet = CoreCommerceFacet<
DateRangeRequest,
DateFacetValue
>;
export type DateFacet = CoreCommerceFacet<DateRangeRequest, DateFacetValue>;

/**
* @internal
*
* **Important:** This initializer is meant for internal use by headless only.
* As an implementer, you must not import or use this initializer directly in your code.
* You will instead interact with `CommerceDateFacet` controller instances through the state of a `FacetGenerator`
* You will instead interact with `DateFacet` controller instances through the state of a `FacetGenerator`
* controller.
*
* @param engine - The headless commerce engine.
* @param options - The `CommerceDateFacet` options used internally.
* @returns A `CommerceDateFacet` controller instance.
* @param options - The `DateFacet` options used internally.
* @returns A `DateFacet` controller instance.
*/
export function buildCommerceDateFacet(
engine: CommerceEngine,
options: CommerceDateFacetOptions
): CommerceDateFacet {
options: DateFacetOptions
): DateFacet {
return buildCoreCommerceFacet<DateRangeRequest, DateFacetValue>(engine, {
options: {
...options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ import {buildProductListingDateFacet} from '../../../product-listing/facets/head
import {buildProductListingNumericFacet} from '../../../product-listing/facets/headless-product-listing-numeric-facet';
import {buildProductListingRegularFacet} from '../../../product-listing/facets/headless-product-listing-regular-facet';
import {
buildCommerceFacetGenerator,
CommerceFacetGenerator,
CommerceFacetGeneratorOptions,
buildFacetGenerator,
FacetGenerator,
FacetGeneratorOptions,
} from './headless-commerce-facet-generator';

describe('CommerceFacetGenerator', () => {
describe('FacetGenerator', () => {
let engine: MockedCommerceEngine;
let state: CommerceAppState;
let options: CommerceFacetGeneratorOptions;
let facetGenerator: CommerceFacetGenerator;
let options: FacetGeneratorOptions;
let facetGenerator: FacetGenerator;

function initEngine(preloadedState = buildMockCommerceState()) {
engine = buildMockCommerceEngine(preloadedState);
}

function initCommerceFacetGenerator() {
facetGenerator = buildCommerceFacetGenerator(engine, options);
facetGenerator = buildFacetGenerator(engine, options);
}

function setFacetState(config: {facetId: string; type: FacetType}[] = []) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@ import {
buildController,
Controller,
} from '../../../../controller/headless-controller';
import {CommerceDateFacet} from '../date/headless-commerce-date-facet';
import {DateFacet} from '../date/headless-commerce-date-facet';
import {
CommerceFacetOptions,
CoreCommerceFacet,
} from '../headless-core-commerce-facet';
import {CommerceNumericFacet} from '../numeric/headless-commerce-numeric-facet';
import {CommerceRegularFacet} from '../regular/headless-commerce-regular-facet';
import {CommerceSearchableFacetOptions} from '../searchable/headless-commerce-searchable-facet';
import {NumericFacet} from '../numeric/headless-commerce-numeric-facet';
import {RegularFacet} from '../regular/headless-commerce-regular-facet';
import {SearchableFacetOptions} from '../searchable/headless-commerce-searchable-facet';

/**
* The `CommerceFacetGenerator` headless controller creates commerce facet controllers from the Commerce API search or
* The `FacetGenerator` headless controller creates commerce facet controllers from the Commerce API search or
* product listing response.
*
* Commerce facets are not requested by the implementer, but rather pre-configured through the Coveo Merchandising Hub
* (CMH). The implementer is only responsible for leveraging the facet controllers created by this controller to
* properly render facets in their application.
*/
export interface CommerceFacetGenerator extends Controller {
export interface FacetGenerator extends Controller {
/**
* The state of the facet generator controller.
*/
state: CommerceFacetGeneratorState;
state: FacetGeneratorState;
}

/**
* A scoped and simplified part of the headless state that is relevant to the facet generator controller.
*/
export interface CommerceFacetGeneratorState {
export interface FacetGeneratorState {
/**
* The generated commerce facet controllers.
*/
Expand All @@ -58,33 +58,33 @@ type CommerceSearchableFacetBuilder<
Facet extends CoreCommerceFacet<AnyFacetValueRequest, AnyFacetValueResponse>,
> = (
engine: CommerceEngine,
options: CommerceFacetOptions & CommerceSearchableFacetOptions
options: CommerceFacetOptions & SearchableFacetOptions
) => Facet;
/**
* @internal
*
* The `CommerceFacetGenerator` options used internally.
* The `FacetGenerator` options used internally.
*/
export interface CommerceFacetGeneratorOptions {
buildRegularFacet: CommerceSearchableFacetBuilder<CommerceRegularFacet>;
buildNumericFacet: CommerceFacetBuilder<CommerceNumericFacet>;
buildDateFacet: CommerceFacetBuilder<CommerceDateFacet>;
// TODO: buildCategoryFacet: CommerceFacetBuilder<CommerceCategoryFacet>;
export interface FacetGeneratorOptions {
buildRegularFacet: CommerceSearchableFacetBuilder<RegularFacet>;
buildNumericFacet: CommerceFacetBuilder<NumericFacet>;
buildDateFacet: CommerceFacetBuilder<DateFacet>;
// TODO: buildCategoryFacet: CommerceFacetBuilder<CategoryFacet>;
}

/**
* @internal
*
* Creates a `CommerceFacetGenerator` instance.
* Creates a `FacetGenerator` instance.
*
* @param engine - The headless commerce engine.
* @param options - The facet generator options used internally.
* @returns A `CommerceFacetGenerator` controller instance.
* @returns A `FacetGenerator` controller instance.
*/
export function buildCommerceFacetGenerator(
export function buildFacetGenerator(
engine: CommerceEngine,
options: CommerceFacetGeneratorOptions
): CommerceFacetGenerator {
options: FacetGeneratorOptions
): FacetGenerator {
if (!loadCommerceFacetGeneratorReducers(engine)) {
throw loadReducerError;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ import {
import {commonOptions} from '../../../product-listing/facets/headless-product-listing-facet-options';
import {NumericRangeRequest} from '../headless-core-commerce-facet';
import {
CommerceNumericFacet,
CommerceNumericFacetOptions,
NumericFacet,
NumericFacetOptions,
buildCommerceNumericFacet,
} from './headless-commerce-numeric-facet';

jest.mock(
'../../../../../features/facets/range-facets/numeric-facet-set/numeric-facet-actions'
);

describe('CommerceNumericFacet', () => {
describe('NumericFacet', () => {
const facetId: string = 'numeric_facet_id';
const type: FacetType = 'numericalRange';
const start = 0;
const end = 100;
let options: CommerceNumericFacetOptions;
let options: NumericFacetOptions;
let state: CommerceAppState;
let engine: MockedCommerceEngine;
let facet: CommerceNumericFacet;
let facet: NumericFacet;

function initFacet() {
engine = buildMockCommerceEngine(state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import {
buildCoreCommerceFacet,
} from '../headless-core-commerce-facet';

export type CommerceNumericFacetOptions = Omit<
export type NumericFacetOptions = Omit<
CoreCommerceFacetOptions,
'toggleSelectActionCreator' | 'toggleExcludeActionCreator'
>;

/**
* The `CommerceNumericFacet` controller offers a high-level programming interface for implementing numeric commerce
* The `NumericFacet` controller offers a high-level programming interface for implementing numeric commerce
* facet UI component.
*/
export type CommerceNumericFacet = CoreCommerceFacet<
export type NumericFacet = CoreCommerceFacet<
NumericRangeRequest,
NumericFacetValue
>;
Expand All @@ -30,17 +30,17 @@ export type CommerceNumericFacet = CoreCommerceFacet<
*
* **Important:** This initializer is meant for internal use by headless only.
* As an implementer, you must not import or use this initializer directly in your code.
* You will instead interact with `CommerceNumericFacet` controller instances through the state of a `FacetGenerator`
* You will instead interact with `NumericFacet` controller instances through the state of a `FacetGenerator`
* controller.
*
* @param engine - The headless commerce engine.
* @param options - The `CommerceNumericFacet` options used internally.
* @returns A `CommerceNumericFacet` controller instance.
* @param options - The `NumericFacet` options used internally.
* @returns A `NumericFacet` controller instance.
*/
export function buildCommerceNumericFacet(
engine: CommerceEngine,
options: CommerceNumericFacetOptions
): CommerceNumericFacet {
options: NumericFacetOptions
): NumericFacet {
return buildCoreCommerceFacet<NumericRangeRequest, NumericFacetValue>(
engine,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import {buildMockFacetSearch} from '../../../../../test/mock-facet-search';
import {commonOptions} from '../../../product-listing/facets/headless-product-listing-facet-options';
import {FacetValueRequest} from '../headless-core-commerce-facet';
import {
CommerceRegularFacet,
CommerceRegularFacetOptions,
RegularFacet,
RegularFacetOptions,
buildCommerceRegularFacet,
} from './headless-commerce-regular-facet';

jest.mock('../../../../../features/facets/facet-set/facet-set-actions');

describe('CommerceRegularFacet', () => {
describe('RegularFacet', () => {
const facetId: string = 'regular_facet_id';
let engine: MockedCommerceEngine;
let state: CommerceAppState;
let options: CommerceRegularFacetOptions;
let facet: CommerceRegularFacet;
let options: RegularFacetOptions;
let facet: RegularFacet;

function initEngine(preloadedState = buildMockCommerceState()) {
engine = buildMockCommerceEngine(preloadedState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import {
RegularFacetValue,
} from '../headless-core-commerce-facet';
import {
CommerceSearchableFacet,
CommerceSearchableFacetOptions,
SearchableFacet,
SearchableFacetOptions,
buildCommerceSearchableFacet,
} from '../searchable/headless-commerce-searchable-facet';

export type CommerceRegularFacetOptions = Omit<
export type RegularFacetOptions = Omit<
CoreCommerceFacetOptions,
'toggleSelectActionCreator' | 'toggleExcludeActionCreator'
> &
CommerceSearchableFacetOptions;
SearchableFacetOptions;

/**
* The `CommerceRegularFacet` controller offers a high-level programming interface for implementing a regular commerce
* The `RegularFacet` controller offers a high-level programming interface for implementing a regular commerce
* facet UI component.
*/
export type CommerceRegularFacet = CommerceSearchableFacet<
export type RegularFacet = SearchableFacet<
FacetValueRequest,
RegularFacetValue
>;
Expand All @@ -34,17 +34,17 @@ export type CommerceRegularFacet = CommerceSearchableFacet<
*
* **Important:** This initializer is meant for internal use by headless only.
* As an implementer, you must not import or use this initializer directly in your code.
* You will instead interact with `CommerceRegularFacet` controller instances through the state of a `FacetGenerator`
* You will instead interact with `RegularFacet` controller instances through the state of a `FacetGenerator`
* controller.
*
* @param engine - The headless commerce engine.
* @param options - The `CommerceRegularFacet` options used internally.
* @returns A `CommerceRegularFacet` controller instance.
* @param options - The `RegularFacet` options used internally.
* @returns A `RegularFacet` controller instance.
* */
export function buildCommerceRegularFacet(
engine: CommerceEngine,
options: CommerceRegularFacetOptions
): CommerceRegularFacet {
options: RegularFacetOptions
): RegularFacet {
return buildCommerceSearchableFacet<FacetValueRequest, RegularFacetValue>(
engine,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import {buildMockFacetSearch} from '../../../../../test/mock-facet-search';
import {FacetSearchProps} from '../../../../core/facets/facet-search/specific/headless-facet-search';
import {
CommerceFacetSearch,
FacetSearch,
buildCommerceFacetSearch,
} from './headless-commerce-facet-search';

Expand All @@ -22,7 +22,7 @@ describe('CommerceFacetSearch', () => {
const facetId: string = 'searchable_facet_id';
let engine: MockedCommerceEngine;
let props: FacetSearchProps;
let facetSearch: CommerceFacetSearch;
let facetSearch: FacetSearch;

function initEngine(preloadedState = buildMockCommerceState()) {
engine = buildMockCommerceEngine(preloadedState);
Expand Down
Loading

0 comments on commit 8732da4

Please sign in to comment.