@coveo/headless 3.28.4-pre.f565c74b85 → 3.28.4-pre.fe2dbcea63

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 (48) 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-commerce-next.index.d.ts +1 -1
  11. package/dist/definitions/ssr-next/commerce/controller-utils.d.ts +6 -7
  12. package/dist/definitions/ssr-next/commerce/controllers/context/headless-context.ssr.d.ts +3 -1
  13. package/dist/definitions/ssr-next/commerce/controllers/parameter-manager/headless-core-parameter-manager.ssr.d.ts +2 -1
  14. package/dist/definitions/ssr-next/commerce/engine/commerce-engine.ssr.d.ts +28 -3
  15. package/dist/definitions/ssr-next/commerce/types/controller-inference.d.ts +3 -2
  16. package/dist/definitions/ssr-next/commerce/types/engine.d.ts +0 -8
  17. package/dist/definitions/ssr-next/commerce/types/fetch-static-state.d.ts +22 -1
  18. package/dist/definitions/ssr-next/common/builders/controller-builder.d.ts +17 -0
  19. package/dist/definitions/ssr-next/common/builders/static-controller-builder.d.ts +24 -0
  20. package/dist/definitions/ssr-next/common/controller-utils.d.ts +6 -6
  21. package/dist/definitions/ssr-next/common/navigator-context-utils.d.ts +16 -0
  22. package/dist/definitions/ssr-next/common/navigator-context-utils.spec.d.ts +1 -0
  23. package/dist/definitions/ssr-next/common/types/controllers.d.ts +6 -0
  24. package/dist/definitions/ssr-next/common/types/engine.d.ts +0 -8
  25. package/dist/definitions/ssr-next/common/types/fetch-static-state.d.ts +4 -1
  26. package/dist/definitions/ssr-next/common/types/inference.d.ts +3 -2
  27. package/dist/definitions/ssr-next/search/engine/search-engine.ssr.d.ts +18 -20
  28. package/dist/definitions/test/mock-controller.d.ts +9 -0
  29. package/dist/definitions/test/mock-ssr-controller-definitions.d.ts +38 -0
  30. package/dist/esm/ssr-next/commerce/controller-utils.js +6 -18
  31. package/dist/esm/ssr-next/commerce/controllers/cart/headless-cart.ssr.js +3 -5
  32. package/dist/esm/ssr-next/commerce/controllers/context/headless-context.ssr.js +1 -1
  33. package/dist/esm/ssr-next/commerce/engine/commerce-engine.ssr.js +32 -12
  34. package/dist/esm/ssr-next/commerce/factories/static-state-factory.js +3 -10
  35. package/dist/esm/ssr-next/common/builders/controller-builder.js +38 -0
  36. package/dist/esm/ssr-next/common/builders/static-controller-builder.js +46 -0
  37. package/dist/esm/ssr-next/common/controller-utils.js +7 -13
  38. package/dist/esm/ssr-next/common/navigator-context-utils.js +27 -0
  39. package/dist/esm/ssr-next/search/engine/search-engine.ssr.js +34 -40
  40. package/dist/esm/test/mock-controller.js +13 -0
  41. package/dist/esm/test/mock-ssr-controller-definitions.js +43 -0
  42. package/dist/esm/utils/version.js +1 -1
  43. package/dist/quantic/case-assist/headless.js +1 -1
  44. package/dist/quantic/commerce/headless.js +1 -1
  45. package/dist/quantic/headless.js +1 -1
  46. package/dist/quantic/insight/headless.js +1 -1
  47. package/dist/quantic/recommendation/headless.js +1 -1
  48. package/package.json +1 -1
@@ -17,7 +17,7 @@ export function defineContext() {
17
17
  if (props === undefined) {
18
18
  throw new MissingControllerProps(Kind.Context);
19
19
  }
20
- const controller = buildContext(engine, { options: props });
20
+ const controller = buildContext(engine, { options: props.initialState });
21
21
  return createControllerWithKind(controller, Kind.Context);
22
22
  },
23
23
  };
@@ -18,29 +18,49 @@ import { SolutionType } from '../types/controller-constants.js';
18
18
  *
19
19
  * @example
20
20
  * ```ts
21
- * const engineDefinition = defineCommerceEngine(engineConfig);
22
- * type SearchStaticState = InferStaticState<typeof engineDefinition>;
23
- * type SearchHydratedState = InferHydratedState<typeof engineDefinition>;
21
+ * const { listingEngineDefinition } = defineCommerceEngine(engineConfig);
22
+ *
23
+ * // Pass navigator context directly to fetchStaticState
24
+ * const staticState = await listingEngineDefinition.fetchStaticState({
25
+ * navigatorContext: {
26
+ * forwardedFor: req.ip,
27
+ * referrer: req.headers.referer || null,
28
+ * userAgent: req.headers['user-agent'] || null,
29
+ * clientId: 'unique-session-id'
30
+ * }
31
+ * });
32
+ *
33
+ * // Framework examples:
34
+ * // Express.js
35
+ * app.get('/api/listing', async (req, res) => {
36
+ * const state = await listingEngineDefinition.fetchStaticState({
37
+ * navigatorContext: {
38
+ * forwardedFor: req.ip,
39
+ * referrer: req.headers.referer || null,
40
+ * userAgent: req.headers['user-agent'] || null,
41
+ * clientId: req.sessionID
42
+ * }
43
+ * });
44
+ * res.json(state);
45
+ * });
46
+ *
47
+ * type SearchStaticState = InferStaticState<typeof listingEngineDefinition>;
48
+ * type SearchHydratedState = InferHydratedState<typeof listingEngineDefinition>;
24
49
  * ```
25
50
  *
26
51
  * @group Engine
27
52
  */
28
53
  export function defineCommerceEngine(options) {
29
54
  const { controllers: controllerDefinitions, ...engineOptions } = options;
30
- const getOptions = () => engineOptions;
31
- const setNavigatorContextProvider = (navigatorContextProvider) => {
32
- engineOptions.navigatorContextProvider = navigatorContextProvider;
33
- };
34
55
  const getAccessToken = () => engineOptions.configuration.accessToken;
35
56
  const setAccessToken = (accessToken) => {
36
57
  engineOptions.configuration.accessToken = accessToken;
37
58
  };
38
- const fetchStaticState = fetchStaticStateFactory(controllerDefinitions, getOptions());
39
- const hydrateStaticState = hydratedStaticStateFactory(controllerDefinitions, getOptions());
40
- const fetchRecommendationStaticState = fetchRecommendationStaticStateFactory(controllerDefinitions, getOptions());
41
- const hydrateRecommendationStaticState = hydratedRecommendationStaticStateFactory(controllerDefinitions, getOptions());
59
+ const fetchStaticState = fetchStaticStateFactory(controllerDefinitions, engineOptions);
60
+ const hydrateStaticState = hydratedStaticStateFactory(controllerDefinitions, engineOptions);
61
+ const fetchRecommendationStaticState = fetchRecommendationStaticStateFactory(controllerDefinitions, engineOptions);
62
+ const hydrateRecommendationStaticState = hydratedRecommendationStaticStateFactory(controllerDefinitions, engineOptions);
42
63
  const commonMethods = {
43
- setNavigatorContextProvider,
44
64
  getAccessToken,
45
65
  setAccessToken,
46
66
  };
@@ -1,21 +1,14 @@
1
1
  import { buildProductListing } from '../../../controllers/commerce/product-listing/headless-product-listing.js';
2
2
  import { buildSearch } from '../../../controllers/commerce/search/headless-search.js';
3
- import { augmentPreprocessRequestWithForwardedFor } from '../../common/augment-preprocess-request.js';
3
+ import { processNavigatorContext } from '../../common/navigator-context-utils.js';
4
4
  import { createStaticState } from '../controller-utils.js';
5
5
  import { SolutionType } from '../types/controller-constants.js';
6
6
  import { buildFactory, } from './build-factory.js';
7
7
  export function fetchStaticStateFactory(controllerDefinitions, options) {
8
8
  return (solutionType) => async (...params) => {
9
- const solutionTypeBuild = await buildFactory(controllerDefinitions, {
10
- ...options,
11
- })(solutionType);
9
+ const { engineOptions } = processNavigatorContext(params, options);
10
+ const solutionTypeBuild = await buildFactory(controllerDefinitions, engineOptions)(solutionType);
12
11
  const { engine, controllers } = await solutionTypeBuild(...params);
13
- options.configuration.preprocessRequest =
14
- augmentPreprocessRequestWithForwardedFor({
15
- preprocessRequest: options.configuration.preprocessRequest,
16
- navigatorContextProvider: options.navigatorContextProvider,
17
- loggerOptions: options.loggerOptions,
18
- });
19
12
  switch (solutionType) {
20
13
  case SolutionType.listing:
21
14
  buildProductListing(engine).executeFirstRequest();
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Builder class for creating controllers from definitions
3
+ */
4
+ export class ControllerBuilder {
5
+ _definition;
6
+ _engine;
7
+ _props;
8
+ _additionalArgs = [];
9
+ constructor(definition, engine, props) {
10
+ this._definition = definition;
11
+ this._engine = engine;
12
+ this._props = props;
13
+ }
14
+ withAdditionalArgs(additionalArgs = []) {
15
+ this._additionalArgs = additionalArgs;
16
+ return this;
17
+ }
18
+ build() {
19
+ if ('build' in this._definition &&
20
+ typeof this._definition.build === 'function') {
21
+ return this.buildWithoutProps();
22
+ }
23
+ if ('buildWithProps' in this._definition &&
24
+ typeof this._definition.buildWithProps === 'function') {
25
+ return this.buildWithProps();
26
+ }
27
+ throw new Error('Controller definition must have a build or buildWithProps method.');
28
+ }
29
+ buildWithoutProps() {
30
+ const buildFn = this._definition.build;
31
+ return buildFn(this._engine, ...this._additionalArgs);
32
+ }
33
+ buildWithProps() {
34
+ const buildWithPropsFn = this._definition.buildWithProps;
35
+ const controller = buildWithPropsFn(this._engine, this._props, ...this._additionalArgs);
36
+ return { ...controller, initialState: controller.state };
37
+ }
38
+ }
@@ -0,0 +1,46 @@
1
+ import { clone } from '../../../utils/utils.js';
2
+ /**
3
+ * Builder class for creating static controller
4
+ */
5
+ class StaticControllerBuilder {
6
+ _state;
7
+ _kind;
8
+ _initialState;
9
+ withState(state) {
10
+ this._state = clone(state);
11
+ return this;
12
+ }
13
+ withKind(kind) {
14
+ this._kind = kind;
15
+ return this;
16
+ }
17
+ withInitialState(initialState) {
18
+ this._initialState = clone(initialState);
19
+ return this;
20
+ }
21
+ /**
22
+ * Builds and returns the static controller state object
23
+ */
24
+ build() {
25
+ const result = {
26
+ state: this._state,
27
+ };
28
+ if (this._kind) {
29
+ result._kind = this._kind;
30
+ }
31
+ if (this._initialState) {
32
+ result.initialState = this._initialState;
33
+ }
34
+ return result;
35
+ }
36
+ }
37
+ export function createStaticControllerBuilder(controller) {
38
+ const builder = new StaticControllerBuilder().withState(controller.state);
39
+ if ('_kind' in controller) {
40
+ builder.withKind(controller._kind);
41
+ }
42
+ if ('initialState' in controller) {
43
+ builder.withInitialState(controller.initialState);
44
+ }
45
+ return builder;
46
+ }
@@ -1,21 +1,15 @@
1
1
  import { clone, mapObject } from '../../utils/utils.js';
2
- function buildControllerFromDefinition({ definition, engine, props, }) {
3
- return ('build' in definition
4
- ? definition.build(engine)
5
- : definition.buildWithProps(engine, props));
6
- }
2
+ import { ControllerBuilder } from './builders/controller-builder.js';
3
+ import { createStaticControllerBuilder } from './builders/static-controller-builder.js';
7
4
  export function buildControllerDefinitions({ definitionsMap, engine, propsMap, }) {
8
- return mapObject(definitionsMap, (definition, key) => buildControllerFromDefinition({
9
- definition,
10
- engine,
11
- props: propsMap?.[key],
12
- }));
5
+ return mapObject(definitionsMap, (definition, key) => {
6
+ const props = propsMap?.[key];
7
+ return new ControllerBuilder(definition, engine, props).build();
8
+ });
13
9
  }
14
10
  export function createStaticState({ searchAction, controllers, }) {
15
11
  return {
16
- controllers: mapObject(controllers, (controller) => ({
17
- state: clone(controller.state),
18
- })),
12
+ controllers: mapObject(controllers, (controller) => createStaticControllerBuilder(controller).build()),
19
13
  searchAction: clone(searchAction),
20
14
  };
21
15
  }
@@ -0,0 +1,27 @@
1
+ import { augmentPreprocessRequestWithForwardedFor } from './augment-preprocess-request.js';
2
+ /**
3
+ * Processes fetchStaticState parameters and returns enhanced engine options with navigator context.
4
+ * This is the single function that handles all navigator context logic for both commerce and search engines.
5
+ */
6
+ export function processNavigatorContext(params, baseOptions) {
7
+ const [callOptions] = params;
8
+ // If no navigator context, return original options
9
+ if (!callOptions?.navigatorContext) {
10
+ return { engineOptions: baseOptions, callOptions };
11
+ }
12
+ // Create provider function and enhance options
13
+ const navigatorContextProvider = () => callOptions.navigatorContext;
14
+ const engineOptions = {
15
+ ...baseOptions,
16
+ navigatorContextProvider,
17
+ configuration: {
18
+ ...baseOptions.configuration,
19
+ preprocessRequest: augmentPreprocessRequestWithForwardedFor({
20
+ preprocessRequest: baseOptions.configuration?.preprocessRequest,
21
+ navigatorContextProvider,
22
+ loggerOptions: baseOptions.loggerOptions,
23
+ }),
24
+ },
25
+ };
26
+ return { engineOptions, callOptions };
27
+ }
@@ -1,8 +1,7 @@
1
- import { buildLogger } from '../../../app/logger.js';
2
1
  import { buildSearchEngine, } from '../../../app/search-engine/search-engine.js';
3
2
  import { createWaitForActionMiddleware } from '../../../utils/utils.js';
4
- import { augmentPreprocessRequestWithForwardedFor } from '../../common/augment-preprocess-request.js';
5
3
  import { buildControllerDefinitions, createStaticState, } from '../../common/controller-utils.js';
4
+ import { processNavigatorContext } from '../../common/navigator-context-utils.js';
6
5
  function isSearchCompletedAction(action) {
7
6
  return /^search\/executeSearch\/(fulfilled|rejected)$/.test(action.type);
8
7
  }
@@ -23,50 +22,52 @@ function buildSSRSearchEngine(options) {
23
22
  };
24
23
  }
25
24
  /**
26
- * Initializes a Search engine definition in SSR with given controllers definitions and search engine config.
25
+ * Initializes a Search engine definition in SSR with per-call navigator context support.
27
26
  *
28
27
  * @param options - The search engine definition
29
- * @returns Three utility functions to fetch the initial state of the engine in SSR, hydrate the state in CSR,
30
- * and a build function that can be used for edge cases requiring more control.
28
+ * @returns Engine definition with support for per-call navigator context.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const searchEngine = defineSearchEngine(config);
33
+ *
34
+ * // Pass navigator context directly to fetchStaticState
35
+ * const staticState = await searchEngine.fetchStaticState({
36
+ * navigatorContext: {
37
+ * forwardedFor: req.ip,
38
+ * referrer: req.headers.referer || null,
39
+ * userAgent: req.headers['user-agent'] || null,
40
+ * clientId: 'abc123'
41
+ * }
42
+ * });
43
+ * ```
31
44
  *
32
45
  * @group Engine
33
46
  */
34
47
  export function defineSearchEngine(options) {
35
48
  const { controllers: controllerDefinitions, ...engineOptions } = options;
36
- const getOptions = () => {
37
- return engineOptions;
38
- };
39
- const setNavigatorContextProvider = (navigatorContextProvider) => {
40
- engineOptions.navigatorContextProvider = navigatorContextProvider;
41
- };
42
- const build = async (...[buildOptions]) => {
43
- const logger = buildLogger(options.loggerOptions);
44
- if (!getOptions().navigatorContextProvider) {
45
- logger.warn('[WARNING] Missing navigator context in server-side code. Make sure to set it with `setNavigatorContextProvider` before calling fetchStaticState()');
46
- }
47
- const engine = buildSSRSearchEngine(buildOptions?.extend
48
- ? await buildOptions.extend(getOptions())
49
- : getOptions());
49
+ const hydrateStaticState = async (...params) => {
50
+ const engine = buildSSRSearchEngine(engineOptions);
50
51
  const controllers = buildControllerDefinitions({
51
52
  definitionsMap: (controllerDefinitions ?? {}),
52
53
  engine,
53
- propsMap: (buildOptions && 'controllers' in buildOptions
54
- ? buildOptions.controllers
55
- : {}),
54
+ propsMap: {},
56
55
  });
57
- return {
58
- engine,
59
- controllers,
60
- };
56
+ const [staticState] = params;
57
+ engine.dispatch(staticState.searchAction);
58
+ await engine.waitForSearchCompletedAction();
59
+ return { engine, controllers };
61
60
  };
62
61
  const fetchStaticState = async (...params) => {
63
- const { engine, controllers } = await build(...params);
64
- options.configuration.preprocessRequest =
65
- augmentPreprocessRequestWithForwardedFor({
66
- preprocessRequest: options.configuration.preprocessRequest,
67
- navigatorContextProvider: options.navigatorContextProvider,
68
- loggerOptions: options.loggerOptions,
69
- });
62
+ const { engineOptions, callOptions } = processNavigatorContext(params, options);
63
+ const engine = buildSSRSearchEngine(engineOptions);
64
+ const controllers = buildControllerDefinitions({
65
+ definitionsMap: (controllerDefinitions ?? {}),
66
+ engine,
67
+ propsMap: (callOptions && 'controllers' in callOptions
68
+ ? callOptions.controllers
69
+ : {}),
70
+ });
70
71
  engine.executeFirstSearch();
71
72
  const staticState = createStaticState({
72
73
  searchAction: await engine.waitForSearchCompletedAction(),
@@ -74,15 +75,8 @@ export function defineSearchEngine(options) {
74
75
  });
75
76
  return staticState;
76
77
  };
77
- const hydrateStaticState = async (...params) => {
78
- const { engine, controllers } = await build(...params);
79
- engine.dispatch(params[0].searchAction);
80
- await engine.waitForSearchCompletedAction();
81
- return { engine, controllers };
82
- };
83
78
  return {
84
79
  fetchStaticState,
85
80
  hydrateStaticState,
86
- setNavigatorContextProvider,
87
81
  };
88
82
  }
@@ -0,0 +1,13 @@
1
+ import { vi } from 'vitest';
2
+ export function buildMockController() {
3
+ return {
4
+ subscribe: vi.fn(),
5
+ state: {},
6
+ };
7
+ }
8
+ export function buildMockControllerWithInitialState(_engine, props) {
9
+ return {
10
+ state: props.initialState,
11
+ subscribe: vi.fn(),
12
+ };
13
+ }
@@ -0,0 +1,43 @@
1
+ import { vi } from 'vitest';
2
+ import { buildMockController, buildMockControllerWithInitialState, } from './mock-controller.js';
3
+ export function defineMockController() {
4
+ return {
5
+ build: vi.fn((_engine) => {
6
+ return buildMockController();
7
+ }),
8
+ };
9
+ }
10
+ export function defineMockControllerWithProps() {
11
+ return {
12
+ buildWithProps: vi.fn((engine, props) => {
13
+ return buildMockControllerWithInitialState(engine, {
14
+ initialState: props?.initialState,
15
+ });
16
+ }),
17
+ };
18
+ }
19
+ export function defineMockCommerceController(options) {
20
+ return {
21
+ build: vi.fn((_engine) => {
22
+ return buildMockController();
23
+ }),
24
+ listing: options?.listing ?? true,
25
+ search: options?.search ?? true,
26
+ standalone: options?.standalone ?? true,
27
+ recommendation: options?.recommendation ?? true,
28
+ };
29
+ }
30
+ export function defineMockCommerceControllerWithProps(options) {
31
+ return {
32
+ buildWithProps: vi.fn((engine, props) => ({
33
+ _kind: 'some-kind',
34
+ ...buildMockControllerWithInitialState(engine, {
35
+ initialState: props?.initialState,
36
+ }),
37
+ })),
38
+ listing: options?.listing ?? true,
39
+ search: options?.search ?? true,
40
+ standalone: options?.standalone ?? true,
41
+ recommendation: options?.recommendation ?? true,
42
+ };
43
+ }
@@ -1,2 +1,2 @@
1
- export const VERSION = "3.28.4-pre.f565c74b85" || 'Test version';
1
+ export const VERSION = "3.28.4-pre.fe2dbcea63" || 'Test version';
2
2
  export const COVEO_FRAMEWORK = ['@coveo/atomic', '@coveo/quantic'];