@coveo/headless 3.29.0-pre.48ec9ee539 → 3.29.0-pre.4c98ddef9f

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/cjs/case-assist/headless.cjs +1 -1
  2. package/dist/cjs/commerce/headless.cjs +1 -1
  3. package/dist/cjs/headless.cjs +1 -1
  4. package/dist/cjs/insight/headless.cjs +1 -1
  5. package/dist/cjs/recommendation/headless.cjs +1 -1
  6. package/dist/cjs/ssr/headless.cjs +1 -1
  7. package/dist/cjs/ssr-commerce/headless.cjs +1 -1
  8. package/dist/cjs/ssr-commerce-next/headless.cjs +1 -1
  9. package/dist/cjs/ssr-next/headless.cjs +1 -1
  10. package/dist/definitions/ssr-next/commerce/controller-utils.d.ts +5 -7
  11. package/dist/definitions/ssr-next/commerce/controllers/recommendations/headless-recommendations.ssr.d.ts +4 -8
  12. package/dist/definitions/ssr-next/commerce/factories/build-factory.d.ts +4 -5
  13. package/dist/definitions/ssr-next/commerce/factories/hydrated-state-factory.d.ts +2 -6
  14. package/dist/definitions/ssr-next/commerce/factories/recommendation-hydrated-state-factory.d.ts +1 -3
  15. package/dist/definitions/ssr-next/commerce/factories/recommendation-static-state-factory.d.ts +2 -12
  16. package/dist/definitions/ssr-next/commerce/factories/static-state-factory.d.ts +2 -7
  17. package/dist/definitions/ssr-next/commerce/types/build.d.ts +16 -51
  18. package/dist/definitions/ssr-next/commerce/types/controller-definitions.d.ts +119 -4
  19. package/dist/definitions/ssr-next/commerce/types/controller-inference.d.ts +3 -3
  20. package/dist/definitions/ssr-next/commerce/types/engine.d.ts +10 -8
  21. package/dist/definitions/ssr-next/commerce/types/fetch-static-state.d.ts +1 -1
  22. package/dist/definitions/ssr-next/commerce/types/hydrate-static-state.d.ts +1 -2
  23. package/dist/definitions/ssr-next/commerce/utils/controller-wiring.d.ts +2 -29
  24. package/dist/definitions/ssr-next/commerce/utils/engine-wiring.d.ts +1 -1
  25. package/dist/definitions/ssr-next/commerce/utils/recommendation-filter.d.ts +2 -15
  26. package/dist/definitions/ssr-next/common/types/utilities.d.ts +3 -0
  27. package/dist/definitions/test/mock-ssr-controller-definitions.d.ts +0 -12
  28. package/dist/esm/ssr-next/commerce/controllers/recommendations/headless-recommendations.ssr.js +1 -1
  29. package/dist/esm/ssr-next/commerce/factories/build-factory.js +11 -12
  30. package/dist/esm/ssr-next/commerce/factories/hydrated-state-factory.js +6 -1
  31. package/dist/esm/ssr-next/commerce/factories/recommendation-hydrated-state-factory.js +6 -1
  32. package/dist/esm/ssr-next/commerce/factories/recommendation-static-state-factory.js +11 -7
  33. package/dist/esm/ssr-next/commerce/factories/static-state-factory.js +1 -4
  34. package/dist/esm/ssr-next/commerce/utils/controller-wiring.js +3 -152
  35. package/dist/esm/ssr-next/commerce/utils/engine-wiring.js +3 -9
  36. package/dist/esm/ssr-next/commerce/utils/recommendation-filter.js +8 -27
  37. package/dist/esm/test/mock-ssr-controller-definitions.js +0 -15
  38. package/dist/esm/utils/version.js +1 -1
  39. package/dist/quantic/case-assist/headless.js +1 -1
  40. package/dist/quantic/commerce/headless.js +1 -1
  41. package/dist/quantic/headless.js +1 -1
  42. package/dist/quantic/insight/headless.js +1 -1
  43. package/dist/quantic/recommendation/headless.js +1 -1
  44. package/package.json +1 -1
@@ -1,18 +1,5 @@
1
1
  import type { Controller } from '../../../controllers/controller/headless-controller.js';
2
- import type { RecommendationsDefinitionMeta } from '../controllers/recommendations/headless-recommendations.ssr.js';
3
- import type { ControllerDefinition, ControllerDefinitionsMap } from '../types/controller-definitions.js';
4
- /**
5
- * Type guard to check if a controller definition is a recommendation controller.
6
- * A recommendation controller must have both the recommendation flag and a slotId.
7
- */
8
- export declare function isRecommendationDefinition<C extends ControllerDefinition<Controller>>(controllerDefinition: C): controllerDefinition is C & RecommendationsDefinitionMeta;
9
- /**
10
- * Extracts recommendation controller definitions from a controller definitions map.
11
- *
12
- * @param controllerDefinitions - Map of controller definitions
13
- * @returns Object containing only the recommendation controller definitions
14
- */
15
- export declare function getRecommendationDefinitions(controllerDefinitions: ControllerDefinitionsMap<Controller>): Record<string, ControllerDefinition<Controller> & RecommendationsDefinitionMeta>;
2
+ import type { ControllerDefinitionsMap } from '../types/controller-definitions.js';
16
3
  export declare function filterRecommendationControllers<TControllerDefinitions extends ControllerDefinitionsMap<Controller>>(controllers: Record<string, Controller>, controllerDefinitions: TControllerDefinitions): {
17
4
  /**
18
5
  * Go through all the controllers passed in argument and only refresh recommendation controllers.
@@ -20,5 +7,5 @@ export declare function filterRecommendationControllers<TControllerDefinitions e
20
7
  * @param controllers - A record of all controllers where the key is the controller name and the value is the controller instance.
21
8
  * @param controllerNames - A list of all recommendation controllers to refresh
22
9
  */
23
- refresh(whitelist?: Array<keyof TControllerDefinitions>): void;
10
+ refresh(whitelist?: string[]): void;
24
11
  };
@@ -3,6 +3,9 @@ export type HasKeys<TObject> = TObject extends {} ? keyof TObject extends never
3
3
  export type HasRequiredKeys<TObject> = TObject extends {} ? {
4
4
  [K in keyof TObject]-?: {} extends Pick<TObject, K> ? never : K;
5
5
  }[keyof TObject] extends never ? false : true : boolean;
6
+ export type HasOptionalKeys<TObject> = TObject extends {} ? {
7
+ [K in keyof TObject]-?: {} extends Pick<TObject, K> ? K : never;
8
+ }[keyof TObject] extends never ? false : true : boolean;
6
9
  type ExtractRequiredOptions<TOptions> = {
7
10
  [TKey in keyof TOptions as Pick<TOptions, TKey> extends Required<Pick<TOptions, TKey>> ? TKey : never]: TOptions[TKey];
8
11
  };
@@ -1,7 +1,6 @@
1
1
  import type { SearchEngine } from '../app/search-engine/search-engine.js';
2
2
  import type { Controller } from '../controllers/controller/headless-controller.js';
3
3
  import type { Kind } from '../ssr/commerce/types/kind.js';
4
- import { recommendationInternalOptionKey } from '../ssr-next/commerce/types/controller-constants.js';
5
4
  import type { ControllerDefinitionWithoutProps, ControllerDefinitionWithProps } from '../ssr-next/common/types/controllers.js';
6
5
  interface MockController {
7
6
  initialState?: Record<string, unknown>;
@@ -36,15 +35,4 @@ export declare function defineMockCommerceControllerWithProps(options?: Solution
36
35
  standalone: boolean;
37
36
  recommendation: boolean;
38
37
  };
39
- export declare function defineMockRecommendationDefinition(slotId: string): {
40
- recommendation: boolean;
41
- [recommendationInternalOptionKey]: {
42
- slotId: string;
43
- };
44
- buildWithProps: import("vitest").Mock<(engine: any, props: any) => {
45
- state: {};
46
- subscribe(listener: () => void): import("redux").Unsubscribe;
47
- _kind: Kind;
48
- }>;
49
- };
50
38
  export {};
@@ -17,7 +17,7 @@ export function defineRecommendations(props) {
17
17
  buildWithProps: (engine, options) => {
18
18
  const staticOptions = props.options;
19
19
  const controller = buildRecommendations(engine, {
20
- options: { ...staticOptions, ...options.initialState },
20
+ options: { ...staticOptions, ...options },
21
21
  });
22
22
  return createControllerWithKind(controller, Kind.Recommendations);
23
23
  },
@@ -57,35 +57,34 @@ function buildSSRCommerceEngine(solutionType, options, recommendationCount) {
57
57
  },
58
58
  };
59
59
  }
60
+ function fetchActiveRecommendationControllers(controllerProps, solutionType) {
61
+ return solutionType === SolutionType.recommendation
62
+ ? Object.values(controllerProps).filter((controller) => controller &&
63
+ typeof controller === 'object' &&
64
+ 'enabled' in controller &&
65
+ controller.enabled).length
66
+ : 0;
67
+ }
60
68
  export const buildFactory = (controllerDefinitions, options) => (solutionType) => async (...[buildOptions]) => {
61
69
  const controllerProps = wireControllerParams(solutionType, controllerDefinitions, buildOptions); // TODO: KIT-4754: remove non-null assertion operator
62
70
  const logger = buildLogger(options.loggerOptions);
63
71
  if (!options.navigatorContextProvider) {
64
72
  logger.warn('[WARNING] Missing navigator context in server-side code. Make sure to set it with `setNavigatorContextProvider` before calling fetchStaticState()');
65
73
  }
66
- const enabledRecommendationControllers = buildOptions && 'recommendations' in buildOptions // TODO: KIT-4754: remove non-null assertion
67
- ? buildOptions
68
- ?.recommendations.length
69
- : 0;
74
+ const enabledRecommendationControllers = fetchActiveRecommendationControllers(controllerProps, solutionType);
70
75
  const engineOptions = {
71
76
  ...options,
72
77
  configuration: extendEngineConfiguration(options.configuration, buildOptions), // TODO: KIT-4754: remove non-null assertion operator
73
78
  };
74
79
  const engine = buildSSRCommerceEngine(solutionType, engineOptions, enabledRecommendationControllers);
75
80
  const controllers = buildControllerDefinitions({
76
- definitionsMap: controllerDefinitions ?? {},
81
+ definitionsMap: (controllerDefinitions ?? {}),
77
82
  engine,
78
83
  solutionType,
79
84
  propsMap: controllerProps,
80
85
  });
81
- // If searchActions are provided (hydrate scenario), dispatch them
82
- if (buildOptions && 'searchActions' in buildOptions) {
83
- buildOptions.searchActions.forEach((action) => {
84
- engine.dispatch(action);
85
- });
86
- }
87
86
  return {
88
87
  engine,
89
- controllers: controllers,
88
+ controllers,
90
89
  };
91
90
  };
@@ -2,7 +2,12 @@ import { buildFactory, } from './build-factory.js';
2
2
  export function hydratedStaticStateFactory(controllerDefinitions, options) {
3
3
  return (solutionType) => async (...params) => {
4
4
  const solutionTypeBuild = await buildFactory(controllerDefinitions, options)(solutionType);
5
- const { engine, controllers } = await solutionTypeBuild(...params);
5
+ const { engine, controllers } = await solutionTypeBuild(
6
+ // @ts-expect-error: TODO: KIT-4742: the wiring will fix also the type inconsistencies
7
+ ...params);
8
+ params[0].searchActions.forEach((action) => {
9
+ engine.dispatch(action);
10
+ });
6
11
  await engine.waitForRequestCompletedAction();
7
12
  return { engine, controllers };
8
13
  };
@@ -3,7 +3,12 @@ import { buildFactory, } from './build-factory.js';
3
3
  export function hydratedRecommendationStaticStateFactory(controllerDefinitions, options) {
4
4
  return async (...params) => {
5
5
  const solutionTypeBuild = await buildFactory(controllerDefinitions, options)(SolutionType.recommendation);
6
- const { engine, controllers } = await solutionTypeBuild(...params);
6
+ const { engine, controllers } = (await solutionTypeBuild(
7
+ // @ts-expect-error: TODO: KIT-4742: the wiring will fix also the type inconsistencies
8
+ ...params));
9
+ params[0].searchActions.forEach((action) => {
10
+ engine.dispatch(action);
11
+ });
7
12
  await engine.waitForRequestCompletedAction();
8
13
  return { engine, controllers };
9
14
  };
@@ -1,22 +1,26 @@
1
+ import { filterObject } from '../../../utils/utils.js';
1
2
  import { createStaticState } from '../controller-utils.js';
2
3
  import { SolutionType } from '../types/controller-constants.js';
3
4
  import { filterRecommendationControllers } from '../utils/recommendation-filter.js';
4
5
  import { buildFactory, } from './build-factory.js';
5
6
  export function fetchRecommendationStaticStateFactory(controllerDefinitions, options) {
7
+ const getAllowedRecommendationKeys = (props) => {
8
+ if (props && 'controllers' in props) {
9
+ const enabledRecommendationControllers = filterObject(props.controllers, (value) => Boolean(value.enabled));
10
+ return Object.keys(enabledRecommendationControllers);
11
+ }
12
+ return [];
13
+ };
6
14
  return async (...params) => {
7
15
  const [props] = params;
8
- const allowedRecommendationKeys = props?.recommendations;
16
+ const allowedRecommendationKeys = getAllowedRecommendationKeys(props);
9
17
  const solutionTypeBuild = await buildFactory(controllerDefinitions, options)(SolutionType.recommendation);
10
- const { engine, controllers } = await solutionTypeBuild(...params);
18
+ const { engine, controllers } = (await solutionTypeBuild(...params));
11
19
  filterRecommendationControllers(controllers, controllerDefinitions ?? {}).refresh(allowedRecommendationKeys);
12
20
  const searchActions = await Promise.all(engine.waitForRequestCompletedAction());
13
- const staticState = createStaticState({
21
+ return createStaticState({
14
22
  searchActions,
15
23
  controllers,
16
24
  });
17
- return {
18
- ...params[0], // TODO: KIT-4754: remove index access after no longer relying on OptionTuple type
19
- ...staticState,
20
- };
21
25
  };
22
26
  }
@@ -27,9 +27,6 @@ export function fetchStaticStateFactory(controllerDefinitions, options) {
27
27
  searchActions,
28
28
  controllers,
29
29
  });
30
- return {
31
- ...params[0], // TODO: KIT-4754: remove index access after no longer relying on OptionTuple type
32
- ...staticState,
33
- };
30
+ return staticState;
34
31
  };
35
32
  }
@@ -1,153 +1,4 @@
1
- import { ArrayValue, RecordValue, Schema, StringValue } from '@coveo/bueno';
2
- import { contextDefinition } from '../../../features/commerce/context/context-validation.js';
3
- import { parametersDefinition } from '../../../features/commerce/parameters/parameters-schema.js';
4
- import { nonEmptyString, requiredEmptyAllowedString } from '../../../utils/validate-payload.js';
5
- import { SolutionType } from '../types/controller-constants.js';
6
- import { getRecommendationDefinitions, isRecommendationDefinition, } from './recommendation-filter.js';
7
- const requiredDefinition = {
8
- context: new RecordValue({
9
- options: { required: true },
10
- values: contextDefinition,
11
- }),
12
- };
13
- const listingDefinition = {
14
- ...requiredDefinition,
15
- searchParams: new RecordValue({
16
- values: parametersDefinition,
17
- }),
18
- };
19
- const standaloneDefinition = {
20
- ...requiredDefinition,
21
- };
22
- const searchDefinition = {
23
- ...requiredDefinition,
24
- searchParams: new RecordValue({
25
- options: { required: true },
26
- values: { query: requiredEmptyAllowedString, ...parametersDefinition },
27
- }),
28
- };
29
- const recommendationsDefinition = (recommendationName) => ({
30
- ...requiredDefinition,
31
- recommendations: new ArrayValue({
32
- each: new StringValue({
33
- required: true,
34
- constrainTo: recommendationName,
35
- emptyAllowed: false,
36
- }),
37
- required: true,
38
- }),
39
- productId: nonEmptyString,
40
- });
41
- export const listingDefinitionSchema = new Schema(listingDefinition);
42
- export const searchDefinitionSchema = new Schema(searchDefinition);
43
- export const standaloneDefinitionSchema = new Schema(standaloneDefinition);
44
- export const recommendationsDefinitionSchema = (recommendationName) => new Schema(recommendationsDefinition(recommendationName));
45
- /**
46
- * Validates the build configuration based on the solution type.
47
- */
48
- function validateBuildConfig(solutionType, controllerDefinitions, buildConfig) {
49
- const validationMap = {
50
- [SolutionType.listing]: listingDefinitionSchema,
51
- [SolutionType.search]: searchDefinitionSchema,
52
- [SolutionType.standalone]: standaloneDefinitionSchema,
53
- [SolutionType.recommendation]: recommendationsDefinitionSchema(Object.keys(getRecommendationDefinitions(controllerDefinitions))),
54
- };
55
- const schema = validationMap[solutionType];
56
- schema.validate(buildConfig);
57
- }
58
- function createControllerWirer(buildConfig, controllerDefinitions, controllerProps) {
59
- return {
60
- wireParameterManager: () => {
61
- if (!controllerDefinitions?.parameterManager)
62
- return;
63
- const { searchParams } = buildConfig;
64
- const { query, ...rest } = searchParams || {};
65
- const parameters = {
66
- ...(query && { q: query }),
67
- ...(rest && typeof rest === 'object' ? rest : {}),
68
- };
69
- controllerProps.parameterManager = {
70
- initialState: { parameters },
71
- };
72
- },
73
- wireContext: () => {
74
- if (!controllerDefinitions?.context)
75
- return;
76
- const { context } = buildConfig;
77
- controllerProps.context = {
78
- initialState: {
79
- ...context,
80
- },
81
- };
82
- },
83
- wireCart: () => {
84
- if (!controllerDefinitions?.cart || !buildConfig.cart)
85
- return;
86
- controllerProps.cart = {
87
- initialState: buildConfig.cart,
88
- };
89
- },
90
- wireRecommendations: () => {
91
- if (!('recommendations' in buildConfig)) {
92
- return;
93
- }
94
- for (const recController in controllerDefinitions) {
95
- if (isRecommendationDefinition(controllerDefinitions[recController])) {
96
- controllerProps[recController] = {
97
- initialState: {
98
- ...('productId' in buildConfig && {
99
- productId: buildConfig.productId,
100
- }),
101
- },
102
- };
103
- }
104
- }
105
- },
106
- };
107
- }
108
- function wireCommonControllers(wirer) {
109
- wirer.wireCart();
110
- wirer.wireContext();
111
- }
112
- function wireSolutionSpecificControllers(solutionType, wirer) {
113
- switch (solutionType) {
114
- case SolutionType.search: {
115
- wirer.wireParameterManager();
116
- break;
117
- }
118
- case SolutionType.listing: {
119
- wirer.wireParameterManager();
120
- break;
121
- }
122
- case SolutionType.recommendation: {
123
- wirer.wireRecommendations();
124
- break;
125
- }
126
- case SolutionType.standalone:
127
- // No additional wiring needed for standalone
128
- break;
129
- default: {
130
- // Exhaustive check - TypeScript will error if we miss a case
131
- const _exhaustiveCheck = solutionType;
132
- throw new Error(`Unsupported solution type: ${_exhaustiveCheck}`);
133
- }
134
- }
135
- }
136
- /**
137
- * Converts simple user configuration (URL, language, currency, query) into the
138
- * nested structure required by the internal {@link Build} method. Automatically generates
139
- * appropriate controllers (context, cart, parameterManager) transparently.
140
- *
141
- * @param solutionType - The type of solution (search, listing, recommendation, standalone)
142
- * @param controllerDefinitions - Map of controller definitions to be initialized
143
- * @param buildConfig - Simple configuration object with simplified controller properties
144
- * @returns Formatted and validated controller properties map defined for the controller in the definition
145
- */
146
- export function wireControllerParams(solutionType, controllerDefinitions, buildConfig) {
147
- validateBuildConfig(solutionType, controllerDefinitions, buildConfig);
148
- const controllerProps = buildConfig.controllers ?? {};
149
- const wirer = createControllerWirer(buildConfig, controllerDefinitions, controllerProps);
150
- wireCommonControllers(wirer);
151
- wireSolutionSpecificControllers(solutionType, wirer);
152
- return controllerProps;
1
+ export function wireControllerParams(_solutionType, _controllerDefinitions, _paramsObject) {
2
+ // TODO: KIT-4742: Implement
3
+ return {};
153
4
  }
@@ -1,10 +1,4 @@
1
- export function extendEngineConfiguration(configuration, commonBuildOptions) {
2
- const { cart, context } = commonBuildOptions;
3
- return {
4
- ...configuration,
5
- context: {
6
- ...context,
7
- },
8
- cart,
9
- };
1
+ export function extendEngineConfiguration(_configuration, _commonBuildOptions) {
2
+ // TODO: KIT-4742: Implement: wire engine configuration with validation
3
+ return {};
10
4
  }
@@ -1,34 +1,15 @@
1
1
  import { MultipleRecommendationError } from '../../common/errors.js';
2
2
  import { recommendationInternalOptionKey } from '../types/controller-constants.js';
3
- /**
4
- * Type guard to check if a controller definition is a recommendation controller.
5
- * A recommendation controller must have both the recommendation flag and a slotId.
6
- */
7
- export function isRecommendationDefinition(controllerDefinition) {
8
- const hasRecommendationFlag = 'recommendation' in controllerDefinition &&
9
- controllerDefinition.recommendation === true;
10
- const hasValidSlotId = recommendationInternalOptionKey in controllerDefinition &&
11
- 'slotId' in
12
- controllerDefinition[recommendationInternalOptionKey];
13
- return hasRecommendationFlag && hasValidSlotId;
14
- }
15
- /**
16
- * Extracts recommendation controller definitions from a controller definitions map.
17
- *
18
- * @param controllerDefinitions - Map of controller definitions
19
- * @returns Object containing only the recommendation controller definitions
20
- */
21
- export function getRecommendationDefinitions(controllerDefinitions) {
22
- const recommendationMap = {};
23
- for (const [name, definition] of Object.entries(controllerDefinitions)) {
24
- if (isRecommendationDefinition(definition)) {
25
- recommendationMap[name] = definition;
26
- }
27
- }
28
- return recommendationMap;
29
- }
30
3
  export function filterRecommendationControllers(controllers, controllerDefinitions) {
31
4
  const slotIdSet = new Set();
5
+ const isRecommendationDefinition = (controllerDefinition) => {
6
+ const isControllerRecommendation = 'recommendation' in controllerDefinition &&
7
+ controllerDefinition.recommendation === true;
8
+ const hasSlotId = recommendationInternalOptionKey in controllerDefinition &&
9
+ 'slotId' in
10
+ controllerDefinition[recommendationInternalOptionKey];
11
+ return isControllerRecommendation && hasSlotId;
12
+ };
32
13
  const ensureSingleRecommendationPerSlot = (slotId) => {
33
14
  throw new MultipleRecommendationError(slotId);
34
15
  };
@@ -1,5 +1,4 @@
1
1
  import { vi } from 'vitest';
2
- import { recommendationInternalOptionKey } from '../ssr-next/commerce/types/controller-constants.js';
3
2
  import { buildMockController, buildMockControllerWithInitialState, } from './mock-controller.js';
4
3
  export function defineMockController() {
5
4
  return {
@@ -42,17 +41,3 @@ export function defineMockCommerceControllerWithProps(options) {
42
41
  recommendation: options?.recommendation ?? true,
43
42
  };
44
43
  }
45
- export function defineMockRecommendationDefinition(slotId) {
46
- return {
47
- recommendation: true,
48
- [recommendationInternalOptionKey]: {
49
- slotId,
50
- },
51
- buildWithProps: vi.fn((engine, props) => ({
52
- _kind: 'some-kind',
53
- ...buildMockControllerWithInitialState(engine, {
54
- initialState: props?.initialState,
55
- }),
56
- })),
57
- };
58
- }
@@ -1,2 +1,2 @@
1
- export const VERSION = "3.29.0-pre.48ec9ee539" || 'Test version';
1
+ export const VERSION = "3.29.0-pre.4c98ddef9f" || 'Test version';
2
2
  export const COVEO_FRAMEWORK = ['@coveo/atomic', '@coveo/quantic'];