@coveo/headless 3.28.4-pre.a8d07d81db → 3.28.4-pre.f33fb697dc

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 (34) 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/insight/node.cjs.stats.json +1 -1
  6. package/dist/cjs/node.cjs.stats.json +1 -1
  7. package/dist/cjs/recommendation/headless.cjs +1 -1
  8. package/dist/cjs/ssr/headless.cjs +1 -1
  9. package/dist/cjs/ssr/node.cjs.stats.json +1 -1
  10. package/dist/cjs/ssr-commerce/headless.cjs +1 -1
  11. package/dist/cjs/ssr-commerce-next/headless.cjs +1 -1
  12. package/dist/cjs/ssr-next/headless.cjs +1 -1
  13. package/dist/cjs/ssr-next/node.cjs.stats.json +1 -1
  14. package/dist/definitions/controllers/core/sort/headless-core-sort.d.ts +0 -12
  15. package/dist/definitions/ssr-next/commerce/engine/commerce-engine.ssr.d.ts +29 -5
  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 +34 -1
  18. package/dist/definitions/ssr-next/common/types/engine.d.ts +0 -8
  19. package/dist/definitions/ssr-next/common/types/fetch-static-state.d.ts +17 -1
  20. package/dist/definitions/ssr-next/search/engine/search-engine.ssr.d.ts +18 -20
  21. package/dist/esm/ssr-next/commerce/engine/commerce-engine.ssr.js +33 -14
  22. package/dist/esm/ssr-next/commerce/factories/static-state-factory.js +22 -8
  23. package/dist/esm/ssr-next/search/engine/search-engine.ssr.js +56 -39
  24. package/dist/esm/utils/version.js +1 -1
  25. package/dist/quantic/browser.cjs.stats.json +1 -1
  26. package/dist/quantic/case-assist/headless.js +1 -1
  27. package/dist/quantic/commerce/headless.js +1 -1
  28. package/dist/quantic/headless.js +1 -1
  29. package/dist/quantic/headless.js.map +2 -2
  30. package/dist/quantic/insight/browser.cjs.stats.json +1 -1
  31. package/dist/quantic/insight/headless.js +1 -1
  32. package/dist/quantic/insight/headless.js.map +2 -2
  33. package/dist/quantic/recommendation/headless.js +1 -1
  34. package/package.json +1 -1
@@ -32,18 +32,6 @@ export interface Sort extends Controller {
32
32
  * Verifies whether the specified sort criterion is the currently active one.
33
33
  *
34
34
  * @param criterion - The sort criterion to evaluate.
35
- * Can be a single criterion or an array of criteria.
36
- * The criteria in an array will be applied sequentially. For example, if there's a tie on the 1st criterion, the API uses the 2nd criterion to break the tie. However, this only works when combining:
37
- * `SortByRelevancy` followed by one or more `SortByField` or `SortByDate` criteria.
38
- * `SortByQRE` followed by one or more `SortByField` or `SortByDate` criteria.
39
- * Two or more `SortByField` criteria.
40
- * A single `SortByDate` criterion and one or more `SortByField` criteria in any order.
41
- *
42
- * Examples:
43
- * * `SortByRelevancy`, `SortByField`
44
- * * `SortByQRE`, `SortByField`
45
- * * `SortByDate`, `SortByField`, `SortByField`
46
- *
47
35
  * @returns `true` if the specified sort criterion is the currently active one; `false` otherwise.
48
36
  */
49
37
  isSortedBy(criterion: SortCriterion | SortCriterion[]): boolean;
@@ -9,8 +9,7 @@ import type { CommerceEngineDefinition } from '../types/engine.js';
9
9
  /**
10
10
  * Initializes a Commerce engine definition in SSR with given controllers definitions and commerce engine config.
11
11
  * @param options - The commerce engine definition
12
- * @returns Three utility functions to fetch the initial state of the engine in SSR, hydrate the state in CSR,
13
- * and a build function that can be used for edge cases requiring more control.
12
+ * @returns Engine definitions for different solution types that support per-call navigator context.
14
13
  *
15
14
  * @remarks
16
15
  * You can use the {@link InferStaticState} and {@link InferHydratedState} utility types with the returned engine definitions
@@ -18,9 +17,34 @@ import type { CommerceEngineDefinition } from '../types/engine.js';
18
17
  *
19
18
  * @example
20
19
  * ```ts
21
- * const engineDefinition = defineCommerceEngine(engineConfig);
22
- * type SearchStaticState = InferStaticState<typeof engineDefinition>;
23
- * type SearchHydratedState = InferHydratedState<typeof engineDefinition>;
20
+ * const { listingEngineDefinition } = defineCommerceEngine(engineConfig);
21
+ *
22
+ * // Pass navigator context directly to fetchStaticState
23
+ * const staticState = await listingEngineDefinition.fetchStaticState({
24
+ * navigatorContext: {
25
+ * forwardedFor: req.ip,
26
+ * referrer: req.headers.referer || null,
27
+ * userAgent: req.headers['user-agent'] || null,
28
+ * clientId: 'unique-session-id'
29
+ * }
30
+ * });
31
+ *
32
+ * // Framework examples:
33
+ * // Express.js
34
+ * app.get('/api/listing', async (req, res) => {
35
+ * const state = await listingEngineDefinition.fetchStaticState({
36
+ * navigatorContext: {
37
+ * forwardedFor: req.ip,
38
+ * referrer: req.headers.referer || null,
39
+ * userAgent: req.headers['user-agent'] || null,
40
+ * clientId: req.sessionID
41
+ * }
42
+ * });
43
+ * res.json(state);
44
+ * });
45
+ *
46
+ * type SearchStaticState = InferStaticState<typeof listingEngineDefinition>;
47
+ * type SearchHydratedState = InferHydratedState<typeof listingEngineDefinition>;
24
48
  * ```
25
49
  *
26
50
  * @group Engine
@@ -1,7 +1,6 @@
1
1
  import type { UnknownAction } from '@reduxjs/toolkit';
2
2
  import type { CommerceEngineOptions } from '../../../app/commerce-engine/commerce-engine.js';
3
3
  import type { EngineConfiguration } from '../../../app/engine-configuration.js';
4
- import type { NavigatorContextProvider } from '../../../app/navigator-context-provider.js';
5
4
  import type { Controller } from '../../../controllers/controller/headless-controller.js';
6
5
  import type { ControllerStaticStateMap } from '../../common/types/controllers.js';
7
6
  import type { SSRCommerceEngine } from '../factories/build-factory.js';
@@ -33,13 +32,6 @@ export interface CommerceEngineDefinition<TControllers extends ControllerDefinit
33
32
  * Fetches the hydrated state on the client side using your engine definition and the static state.
34
33
  */
35
34
  hydrateStaticState: HydrateStaticState<InferControllersMapFromDefinition<TControllers, TSolutionType>, UnknownAction, InferControllerPropsMapFromDefinitions<TControllers>, TControllers, TSolutionType>;
36
- /**
37
- * Sets the navigator context provider.
38
- * This provider is essential for retrieving navigation-related data such as referrer, userAgent, location, and clientId, which are crucial for handling both server-side and client-side API requests effectively.
39
- *
40
- * Note: The implementation specifics of the navigator context provider depend on the Node.js framework being utilized. It is the developer's responsibility to appropriately define and implement the navigator context provider to ensure accurate navigation context is available throughout the application. If the user fails to provide a navigator context provider, a warning will be logged either on the server or the browser console.
41
- */
42
- setNavigatorContextProvider: (navigatorContextProvider: NavigatorContextProvider) => void;
43
35
  /**
44
36
  * Returns the access token.
45
37
  */
@@ -1,13 +1,46 @@
1
1
  import type { UnknownAction } from '@reduxjs/toolkit';
2
+ import type { NavigatorContext, NavigatorContextProvider } from '../../../app/navigator-context-provider.js';
2
3
  import type { Controller } from '../../../controllers/controller/headless-controller.js';
3
4
  import type { ControllerStaticStateMap, ControllersPropsMap } from '../../common/types/controllers.js';
4
5
  import type { SolutionType } from './controller-constants.js';
5
6
  import type { ControllerDefinitionsMap, EngineDefinitionControllersPropsOption, OptionsTuple } from './controller-definitions.js';
6
7
  import type { EngineStaticState } from './engine.js';
7
- export type FetchStaticStateOptions = {};
8
+ export interface FetchStaticStateOptions {
9
+ /**
10
+ * Navigator context for this request. Used to set headers like x-forwarded-for during SSR.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * // In your server route handler
15
+ * const staticState = await listingEngineDefinition.fetchStaticState({
16
+ * navigatorContext: {
17
+ * forwardedFor: req.ip,
18
+ * referrer: req.headers.referer || null,
19
+ * userAgent: req.headers['user-agent'] || null,
20
+ * clientId: req.sessionID || 'anonymous'
21
+ * }
22
+ * });
23
+ * ```
24
+ */
25
+ navigatorContext?: NavigatorContext | NavigatorContextProvider;
26
+ }
8
27
  /**
9
28
  * Executes only the initial search for a given configuration, then returns a resumable snapshot of engine state along with the state of the controllers.
10
29
  *
11
30
  * Useful for static generation and server-side rendering.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * // Pass navigator context directly in the options
35
+ * const staticState = await engineDefinition.fetchStaticState({
36
+ * navigatorContext: {
37
+ * forwardedFor: request.ip,
38
+ * referrer: request.headers.referer,
39
+ * userAgent: request.headers['user-agent'],
40
+ * location: request.url,
41
+ * clientId: 'session-123'
42
+ * }
43
+ * });
44
+ * ```
12
45
  */
13
46
  export type FetchStaticState<TSearchAction extends UnknownAction, TControllersStaticState extends ControllerStaticStateMap, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<FetchStaticStateOptions & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<EngineStaticState<TSearchAction, TControllersStaticState>>;
@@ -1,7 +1,6 @@
1
1
  import type { AnyAction, UnknownAction } from '@reduxjs/toolkit';
2
2
  import type { CoreEngine, CoreEngineNext } from '../../../app/engine.js';
3
3
  import type { EngineConfiguration } from '../../../app/engine-configuration.js';
4
- import type { NavigatorContextProvider } from '../../../app/navigator-context-provider.js';
5
4
  import type { Controller } from '../../../controllers/controller/headless-controller.js';
6
5
  import type { Build } from './build.js';
7
6
  import type { ControllerDefinitionsMap, ControllerStaticStateMap, ControllersMap, ControllersPropsMap } from './controllers.js';
@@ -27,13 +26,6 @@ export interface EngineDefinition<TEngine extends CoreEngine | CoreEngineNext, T
27
26
  * Fetches the hydrated state on the client side using your engine definition and the static state.
28
27
  */
29
28
  hydrateStaticState: HydrateStaticState<TEngine, InferControllersMapFromDefinition<TControllers>, AnyAction, InferControllerPropsMapFromDefinitions<TControllers>>;
30
- /**
31
- * Sets the navigator context provider.
32
- * This provider is essential for retrieving navigation-related data such as referrer, userAgent, location, and clientId, which are crucial for handling both server-side and client-side API requests effectively.
33
- *
34
- * Note: The implementation specifics of the navigator context provider depend on the Node.js framework being utilized. It is the developer's responsibility to appropriately define and implement the navigator context provider to ensure accurate navigation context is available throughout the application. If the user fails to provide a navigator context provider, a warning will be logged either on the server or the browser console.
35
- */
36
- setNavigatorContextProvider: (navigatorContextProvider: NavigatorContextProvider) => void;
37
29
  }
38
30
  export interface EngineDefinitionBuildResult<TEngine extends CoreEngine | CoreEngineNext, TControllers extends ControllersMap> {
39
31
  engine: TEngine;
@@ -1,12 +1,28 @@
1
1
  import type { UnknownAction } from '@reduxjs/toolkit';
2
+ import type { NavigatorContext, NavigatorContextProvider } from '../../../app/navigator-context-provider.js';
2
3
  import type { ControllerStaticStateMap, ControllersPropsMap } from './controllers.js';
3
4
  import type { EngineDefinitionControllersPropsOption, EngineStaticState } from './engine.js';
4
5
  import type { OptionsTuple } from './utilities.js';
5
- type FetchStaticStateOptions = {};
6
+ type FetchStaticStateOptions = {
7
+ navigatorContext?: NavigatorContext | NavigatorContextProvider;
8
+ };
6
9
  /**
7
10
  * Executes only the initial search for a given configuration, then returns a resumable snapshot of engine state along with the state of the controllers.
8
11
  *
9
12
  * Useful for static generation and server-side rendering.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // Pass navigator context directly in the options
17
+ * const staticState = await engineDefinition.fetchStaticState({
18
+ * navigatorContext: {
19
+ * forwardedFor: request.ip,
20
+ * referrer: request.headers.referer || null,
21
+ * userAgent: request.headers['user-agent'] || null,
22
+ * clientId: 'session-123'
23
+ * }
24
+ * });
25
+ * ```
10
26
  */
11
27
  export type FetchStaticState<TSearchAction extends UnknownAction, TControllersStaticState extends ControllerStaticStateMap, TControllersProps extends ControllersPropsMap> = (...params: OptionsTuple<FetchStaticStateOptions & EngineDefinitionControllersPropsOption<TControllersProps>>) => Promise<EngineStaticState<TSearchAction, TControllersStaticState>>;
12
28
  export {};
@@ -1,40 +1,38 @@
1
- import type { NavigatorContextProvider } from '../../../app/navigator-context-provider.js';
2
1
  import { type SearchEngine, type SearchEngineOptions } from '../../../app/search-engine/search-engine.js';
3
2
  import type { Controller } from '../../../controllers/controller/headless-controller.js';
4
3
  import type { LegacySearchAction } from '../../../features/analytics/analytics-utils.js';
5
4
  import type { ControllerDefinitionsMap } from '../../common/types/controllers.js';
6
5
  import type { EngineDefinition, EngineDefinitionOptions } from '../../common/types/engine.js';
7
- import type { InferControllerPropsMapFromDefinitions, InferControllerStaticStateMapFromDefinitions } from '../../common/types/inference.js';
8
6
  /**
9
7
  * The SSR search engine.
10
- *
11
- * @group Engine
12
8
  */
13
9
  export interface SSRSearchEngine extends SearchEngine {
14
- /**
15
- * Waits for the search to be completed and returns a promise that resolves to a `SearchCompletedAction`.
16
- */
17
10
  waitForSearchCompletedAction(): Promise<SearchCompletedAction>;
18
11
  }
19
- /**
20
- * The options to create a search engine definition in SSR.
21
- *
22
- * @group Engine
23
- */
24
12
  export type SearchEngineDefinitionOptions<TControllers extends ControllerDefinitionsMap<SSRSearchEngine, Controller>> = EngineDefinitionOptions<SearchEngineOptions, TControllers>;
25
13
  export type SearchCompletedAction = ReturnType<LegacySearchAction['fulfilled' | 'rejected']>;
26
14
  export type SearchEngineDefinition<TControllers extends ControllerDefinitionsMap<SSRSearchEngine, Controller>> = EngineDefinition<SSRSearchEngine, TControllers>;
27
15
  /**
28
- * Initializes a Search engine definition in SSR with given controllers definitions and search engine config.
16
+ * Initializes a Search engine definition in SSR with per-call navigator context support.
29
17
  *
30
18
  * @param options - The search engine definition
31
- * @returns Three utility functions to fetch the initial state of the engine in SSR, hydrate the state in CSR,
32
- * and a build function that can be used for edge cases requiring more control.
19
+ * @returns Engine definition with support for per-call navigator context.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const searchEngine = defineSearchEngine(config);
24
+ *
25
+ * // Pass navigator context directly to fetchStaticState
26
+ * const staticState = await searchEngine.fetchStaticState({
27
+ * navigatorContext: {
28
+ * forwardedFor: req.ip,
29
+ * referrer: req.headers.referer || null,
30
+ * userAgent: req.headers['user-agent'] || null,
31
+ * clientId: 'abc123'
32
+ * }
33
+ * });
34
+ * ```
33
35
  *
34
36
  * @group Engine
35
37
  */
36
- export declare function defineSearchEngine<TControllerDefinitions extends ControllerDefinitionsMap<SearchEngine, Controller>>(options: SearchEngineDefinitionOptions<TControllerDefinitions>): {
37
- fetchStaticState: import("../../common/types/fetch-static-state.js").FetchStaticState<import("redux").AnyAction, InferControllerStaticStateMapFromDefinitions<TControllerDefinitions>, InferControllerPropsMapFromDefinitions<TControllerDefinitions>>;
38
- hydrateStaticState: import("../../common/types/hydrate-static-state.js").HydrateStaticState<SSRSearchEngine, import("../../common/types/inference.js").InferControllersMapFromDefinition<TControllerDefinitions>, import("redux").AnyAction, InferControllerPropsMapFromDefinitions<TControllerDefinitions>>;
39
- setNavigatorContextProvider: (navigatorContextProvider: NavigatorContextProvider) => void;
40
- };
38
+ export declare function defineSearchEngine<TControllerDefinitions extends ControllerDefinitionsMap<SearchEngine, Controller>>(options: SearchEngineDefinitionOptions<TControllerDefinitions>): EngineDefinition<SSRSearchEngine, TControllerDefinitions>;
@@ -9,8 +9,7 @@ import { SolutionType } from '../types/controller-constants.js';
9
9
  /**
10
10
  * Initializes a Commerce engine definition in SSR with given controllers definitions and commerce engine config.
11
11
  * @param options - The commerce engine definition
12
- * @returns Three utility functions to fetch the initial state of the engine in SSR, hydrate the state in CSR,
13
- * and a build function that can be used for edge cases requiring more control.
12
+ * @returns Engine definitions for different solution types that support per-call navigator context.
14
13
  *
15
14
  * @remarks
16
15
  * You can use the {@link InferStaticState} and {@link InferHydratedState} utility types with the returned engine definitions
@@ -18,29 +17,49 @@ import { SolutionType } from '../types/controller-constants.js';
18
17
  *
19
18
  * @example
20
19
  * ```ts
21
- * const engineDefinition = defineCommerceEngine(engineConfig);
22
- * type SearchStaticState = InferStaticState<typeof engineDefinition>;
23
- * type SearchHydratedState = InferHydratedState<typeof engineDefinition>;
20
+ * const { listingEngineDefinition } = defineCommerceEngine(engineConfig);
21
+ *
22
+ * // Pass navigator context directly to fetchStaticState
23
+ * const staticState = await listingEngineDefinition.fetchStaticState({
24
+ * navigatorContext: {
25
+ * forwardedFor: req.ip,
26
+ * referrer: req.headers.referer || null,
27
+ * userAgent: req.headers['user-agent'] || null,
28
+ * clientId: 'unique-session-id'
29
+ * }
30
+ * });
31
+ *
32
+ * // Framework examples:
33
+ * // Express.js
34
+ * app.get('/api/listing', async (req, res) => {
35
+ * const state = await listingEngineDefinition.fetchStaticState({
36
+ * navigatorContext: {
37
+ * forwardedFor: req.ip,
38
+ * referrer: req.headers.referer || null,
39
+ * userAgent: req.headers['user-agent'] || null,
40
+ * clientId: req.sessionID
41
+ * }
42
+ * });
43
+ * res.json(state);
44
+ * });
45
+ *
46
+ * type SearchStaticState = InferStaticState<typeof listingEngineDefinition>;
47
+ * type SearchHydratedState = InferHydratedState<typeof listingEngineDefinition>;
24
48
  * ```
25
49
  *
26
50
  * @group Engine
27
51
  */
28
52
  export function defineCommerceEngine(options) {
29
53
  const { controllers: controllerDefinitions, ...engineOptions } = options;
30
- const getOptions = () => engineOptions;
31
- const setNavigatorContextProvider = (navigatorContextProvider) => {
32
- engineOptions.navigatorContextProvider = navigatorContextProvider;
33
- };
34
54
  const getAccessToken = () => engineOptions.configuration.accessToken;
35
55
  const setAccessToken = (accessToken) => {
36
56
  engineOptions.configuration.accessToken = accessToken;
37
57
  };
38
- const fetchStaticState = fetchStaticStateFactory(controllerDefinitions, getOptions());
39
- const hydrateStaticState = hydratedStaticStateFactory(controllerDefinitions, getOptions());
40
- const fetchRecommendationStaticState = fetchRecommendationStaticStateFactory(controllerDefinitions, getOptions());
41
- const hydrateRecommendationStaticState = hydratedRecommendationStaticStateFactory(controllerDefinitions, getOptions());
58
+ const fetchStaticState = fetchStaticStateFactory(controllerDefinitions, engineOptions);
59
+ const hydrateStaticState = hydratedStaticStateFactory(controllerDefinitions, engineOptions);
60
+ const fetchRecommendationStaticState = fetchRecommendationStaticStateFactory(controllerDefinitions, engineOptions);
61
+ const hydrateRecommendationStaticState = hydratedRecommendationStaticStateFactory(controllerDefinitions, engineOptions);
42
62
  const commonMethods = {
43
- setNavigatorContextProvider,
44
63
  getAccessToken,
45
64
  setAccessToken,
46
65
  };
@@ -6,16 +6,30 @@ 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, {
9
+ const [callOptions] = params;
10
+ // Convert per-call navigator context to provider function
11
+ const navigatorContextProvider = callOptions?.navigatorContext
12
+ ? typeof callOptions.navigatorContext === 'function'
13
+ ? callOptions.navigatorContext
14
+ : () => callOptions.navigatorContext
15
+ : undefined;
16
+ // Create options for this call with navigator context
17
+ const callSpecificOptions = {
10
18
  ...options,
11
- })(solutionType);
19
+ navigatorContextProvider,
20
+ configuration: {
21
+ ...options.configuration,
22
+ preprocessRequest: navigatorContextProvider
23
+ ? augmentPreprocessRequestWithForwardedFor({
24
+ preprocessRequest: options.configuration.preprocessRequest,
25
+ navigatorContextProvider,
26
+ loggerOptions: options.loggerOptions,
27
+ })
28
+ : options.configuration.preprocessRequest,
29
+ },
30
+ };
31
+ const solutionTypeBuild = await buildFactory(controllerDefinitions, callSpecificOptions)(solutionType);
12
32
  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
33
  switch (solutionType) {
20
34
  case SolutionType.listing:
21
35
  buildProductListing(engine).executeFirstRequest();
@@ -1,4 +1,3 @@
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
3
  import { augmentPreprocessRequestWithForwardedFor } from '../../common/augment-preprocess-request.js';
@@ -23,50 +22,75 @@ 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
+ // biome-ignore lint/suspicious/noExplicitAny: Complex parameter typing would require extensive refactoring
50
+ const hydrateStaticState = async (...params) => {
51
+ const engine = buildSSRSearchEngine(engineOptions);
50
52
  const controllers = buildControllerDefinitions({
51
53
  definitionsMap: (controllerDefinitions ?? {}),
52
54
  engine,
53
- propsMap: (buildOptions && 'controllers' in buildOptions
54
- ? buildOptions.controllers
55
- : {}),
55
+ propsMap: {},
56
56
  });
57
- return {
58
- engine,
59
- controllers,
60
- };
57
+ const [staticState] = params;
58
+ engine.dispatch(staticState.searchAction);
59
+ await engine.waitForSearchCompletedAction();
60
+ return { engine, controllers };
61
61
  };
62
+ // biome-ignore lint/suspicious/noExplicitAny: Parameter typing matches the interface contract
62
63
  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
- });
64
+ const [callOptions] = params;
65
+ // Convert per-call navigator context to provider function
66
+ const navigatorContextProvider = callOptions?.navigatorContext
67
+ ? typeof callOptions.navigatorContext === 'function'
68
+ ? callOptions.navigatorContext
69
+ : () => callOptions.navigatorContext
70
+ : undefined;
71
+ // Create options for this call with navigator context
72
+ const callSpecificOptions = {
73
+ ...options,
74
+ navigatorContextProvider,
75
+ configuration: {
76
+ ...options.configuration,
77
+ preprocessRequest: navigatorContextProvider
78
+ ? augmentPreprocessRequestWithForwardedFor({
79
+ preprocessRequest: options.configuration.preprocessRequest,
80
+ navigatorContextProvider,
81
+ loggerOptions: options.loggerOptions,
82
+ })
83
+ : options.configuration.preprocessRequest,
84
+ },
85
+ };
86
+ const engine = buildSSRSearchEngine(callSpecificOptions);
87
+ const controllers = buildControllerDefinitions({
88
+ definitionsMap: (controllerDefinitions ?? {}),
89
+ engine,
90
+ propsMap: (callOptions && 'controllers' in callOptions
91
+ ? callOptions.controllers
92
+ : {}),
93
+ });
70
94
  engine.executeFirstSearch();
71
95
  const staticState = createStaticState({
72
96
  searchAction: await engine.waitForSearchCompletedAction(),
@@ -74,15 +98,8 @@ export function defineSearchEngine(options) {
74
98
  });
75
99
  return staticState;
76
100
  };
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
101
  return {
84
102
  fetchStaticState,
85
103
  hydrateStaticState,
86
- setNavigatorContextProvider,
87
104
  };
88
105
  }
@@ -1,2 +1,2 @@
1
- export const VERSION = "3.28.4-pre.a8d07d81db" || 'Test version';
1
+ export const VERSION = "3.28.4-pre.f33fb697dc" || 'Test version';
2
2
  export const COVEO_FRAMEWORK = ['@coveo/atomic', '@coveo/quantic'];