@coveo/headless 3.29.1 → 3.29.2-pre.027eeece31
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.
- package/dist/cjs/case-assist/headless.cjs +1 -1
- package/dist/cjs/case-assist/node.cjs.stats.json +1 -1
- package/dist/cjs/commerce/headless.cjs +1 -1
- package/dist/cjs/commerce/node.cjs.stats.json +1 -1
- package/dist/cjs/headless.cjs +1 -1
- package/dist/cjs/insight/headless.cjs +1 -1
- package/dist/cjs/insight/node.cjs.stats.json +1 -1
- package/dist/cjs/node.cjs.stats.json +1 -1
- package/dist/cjs/recommendation/headless.cjs +1 -1
- package/dist/cjs/recommendation/node.cjs.stats.json +1 -1
- package/dist/cjs/ssr/headless.cjs +1 -1
- package/dist/cjs/ssr/node.cjs.stats.json +1 -1
- package/dist/cjs/ssr-commerce/headless.cjs +1 -1
- package/dist/cjs/ssr-commerce/node.cjs.stats.json +1 -1
- package/dist/cjs/ssr-commerce-next/headless.cjs +1 -1
- package/dist/cjs/ssr-commerce-next/node.cjs.stats.json +1 -1
- package/dist/cjs/ssr-next/headless.cjs +1 -1
- package/dist/cjs/ssr-next/node.cjs.stats.json +1 -1
- package/dist/definitions/ssr-next/commerce/controllers/recommendations/headless-recommendations.ssr.d.ts +8 -4
- package/dist/definitions/ssr-next/commerce/factories/hydrated-state-factory.d.ts +2 -1
- package/dist/definitions/ssr-next/commerce/types/build.d.ts +60 -16
- package/dist/definitions/ssr-next/commerce/types/controller-definitions.d.ts +4 -119
- package/dist/definitions/ssr-next/commerce/types/controller-inference.d.ts +3 -3
- package/dist/definitions/ssr-next/commerce/types/fetch-static-state.d.ts +1 -1
- package/dist/definitions/ssr-next/commerce/types/hydrate-static-state.d.ts +2 -1
- package/dist/definitions/ssr-next/commerce/utils/controller-wiring.d.ts +29 -2
- package/dist/definitions/ssr-next/commerce/utils/engine-wiring.d.ts +1 -1
- package/dist/definitions/ssr-next/commerce/utils/recommendation-filter.d.ts +14 -1
- package/dist/definitions/ssr-next/common/types/utilities.d.ts +0 -3
- package/dist/definitions/test/mock-ssr-controller-definitions.d.ts +12 -0
- package/dist/esm/ssr-next/commerce/controllers/recommendations/headless-recommendations.ssr.js +1 -1
- package/dist/esm/ssr-next/commerce/factories/build-factory.js +4 -9
- package/dist/esm/ssr-next/commerce/factories/hydrated-state-factory.js +1 -3
- package/dist/esm/ssr-next/commerce/factories/recommendation-hydrated-state-factory.js +1 -3
- package/dist/esm/ssr-next/commerce/factories/recommendation-static-state-factory.js +6 -10
- package/dist/esm/ssr-next/commerce/factories/static-state-factory.js +4 -1
- package/dist/esm/ssr-next/commerce/utils/controller-wiring.js +161 -3
- package/dist/esm/ssr-next/commerce/utils/engine-wiring.js +9 -3
- package/dist/esm/ssr-next/commerce/utils/recommendation-filter.js +27 -8
- package/dist/esm/test/mock-ssr-controller-definitions.js +15 -0
- package/dist/esm/utils/version.js +1 -1
- package/dist/quantic/browser.cjs.stats.json +1 -1
- package/dist/quantic/case-assist/browser.cjs.stats.json +1 -1
- package/dist/quantic/case-assist/headless.js +1 -1
- package/dist/quantic/case-assist/headless.js.map +1 -1
- package/dist/quantic/commerce/browser.cjs.stats.json +1 -1
- package/dist/quantic/commerce/headless.js +1 -1
- package/dist/quantic/commerce/headless.js.map +1 -1
- package/dist/quantic/headless.js +1 -1
- package/dist/quantic/headless.js.map +1 -1
- package/dist/quantic/insight/browser.cjs.stats.json +1 -1
- package/dist/quantic/insight/headless.js +1 -1
- package/dist/quantic/insight/headless.js.map +1 -1
- package/dist/quantic/recommendation/browser.cjs.stats.json +1 -1
- package/dist/quantic/recommendation/headless.js +1 -1
- package/dist/quantic/recommendation/headless.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { type Recommendations, type RecommendationsOptions, type
|
|
1
|
+
import { type Recommendations, type RecommendationsOptions, type RecommendationsState } from '../../../../controllers/commerce/recommendations/headless-recommendations.js';
|
|
2
2
|
import { recommendationInternalOptionKey } from '../../types/controller-constants.js';
|
|
3
3
|
import type { RecommendationOnlyControllerDefinitionWithProps } from '../../types/controller-definitions.js';
|
|
4
4
|
export type { Recommendations, RecommendationsState };
|
|
5
5
|
export type RecommendationsDefinitionMeta = {
|
|
6
|
-
[recommendationInternalOptionKey]: {} &
|
|
6
|
+
[recommendationInternalOptionKey]: {} & RecommendationsOptions;
|
|
7
7
|
};
|
|
8
|
-
export interface RecommendationsDefinition extends RecommendationOnlyControllerDefinitionWithProps<Recommendations,
|
|
8
|
+
export interface RecommendationsDefinition extends RecommendationOnlyControllerDefinitionWithProps<Recommendations, {
|
|
9
|
+
initialState: Partial<RecommendationsOptions>;
|
|
10
|
+
}> {
|
|
9
11
|
}
|
|
10
12
|
/**
|
|
11
13
|
* Defines a `Recommendations` controller instance.
|
|
@@ -14,4 +16,6 @@ export interface RecommendationsDefinition extends RecommendationOnlyControllerD
|
|
|
14
16
|
* @param props - The configurable `Recommendations` properties.
|
|
15
17
|
* @returns The `Recommendations` controller definition.
|
|
16
18
|
* */
|
|
17
|
-
export declare function defineRecommendations(props:
|
|
19
|
+
export declare function defineRecommendations(props: {
|
|
20
|
+
options: Omit<RecommendationsOptions, 'productId'>;
|
|
21
|
+
}): RecommendationsDefinition & RecommendationsDefinitionMeta;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SolutionType } from '../types/controller-constants.js';
|
|
2
|
+
import type { AugmentedControllerDefinition } from '../types/controller-definitions.js';
|
|
2
3
|
import type { CommerceControllerDefinitionsMap, HydrateStaticStateFunction } from '../types/engine.js';
|
|
3
4
|
import { type CommerceEngineDefinitionOptions } from './build-factory.js';
|
|
4
|
-
export declare function hydratedStaticStateFactory<TControllerDefinitions extends CommerceControllerDefinitionsMap>(controllerDefinitions: TControllerDefinitions
|
|
5
|
+
export declare function hydratedStaticStateFactory<TControllerDefinitions extends CommerceControllerDefinitionsMap>(controllerDefinitions: AugmentedControllerDefinition<TControllerDefinitions>, options: CommerceEngineDefinitionOptions<TControllerDefinitions>): (solutionType: SolutionType) => HydrateStaticStateFunction<TControllerDefinitions>;
|
|
@@ -1,45 +1,89 @@
|
|
|
1
|
-
import type { CurrencyCodeISO4217 } from '@coveo/relay-event-types';
|
|
2
1
|
import type { CommerceEngineOptions } from '../../../app/commerce-engine/commerce-engine.js';
|
|
3
2
|
import type { Controller } from '../../../controllers/controller/headless-controller.js';
|
|
4
3
|
import type { ControllersMap, ControllersPropsMap } from '../../common/types/controllers.js';
|
|
5
|
-
import type { OptionsTuple } from '../../common/types/utilities.js';
|
|
4
|
+
import type { HasKey, OptionsTuple } from '../../common/types/utilities.js';
|
|
6
5
|
import type { CartInitialState } from '../controllers/cart/headless-cart.ssr.js';
|
|
7
|
-
import type {
|
|
6
|
+
import type { ContextOptions } from '../controllers/context/headless-context.ssr.js';
|
|
8
7
|
import type { ParameterManagerState, Parameters } from '../controllers/parameter-manager/headless-core-parameter-manager.ssr.js';
|
|
9
8
|
import type { SolutionType } from './controller-constants.js';
|
|
10
|
-
import type { ControllerDefinitionsMap, EngineDefinitionControllersPropsOption } from './controller-definitions.js';
|
|
9
|
+
import type { ControllerDefinitionsMap, EngineDefinitionControllersPropsOption, IsRecommendationController } from './controller-definitions.js';
|
|
11
10
|
import type { CommerceEngineDefinitionBuildResult } from './engine.js';
|
|
12
11
|
export interface SearchBuildConfig extends CommonBuildConfig {
|
|
13
12
|
searchParams: ParameterManagerState<Parameters & {
|
|
14
13
|
query: string;
|
|
15
14
|
}>['parameters'];
|
|
16
15
|
}
|
|
17
|
-
export interface RecommendationBuildConfig extends CommonBuildConfig {
|
|
18
|
-
recommendations?: [];
|
|
19
|
-
}
|
|
20
16
|
export interface ListingBuildConfig extends CommonBuildConfig {
|
|
21
17
|
}
|
|
22
18
|
export interface StandaloneBuildConfig extends CommonBuildConfig {
|
|
23
19
|
}
|
|
24
20
|
export interface CommonBuildConfig {
|
|
25
|
-
context:
|
|
26
|
-
url: string;
|
|
27
|
-
language: string;
|
|
28
|
-
country: string;
|
|
29
|
-
currency: CurrencyCodeISO4217;
|
|
30
|
-
location?: UserLocation;
|
|
31
|
-
};
|
|
21
|
+
context: ContextOptions;
|
|
32
22
|
searchParams?: ParameterManagerState<Parameters>['parameters'];
|
|
33
23
|
cart?: CartInitialState;
|
|
34
24
|
}
|
|
35
|
-
export type BuildConfig<TSolutionType extends SolutionType> = TSolutionType extends SolutionType.search ? SearchBuildConfig : TSolutionType extends SolutionType.listing ? ListingBuildConfig : TSolutionType extends SolutionType.recommendation ? RecommendationBuildConfig : TSolutionType extends SolutionType.standalone ? CommonBuildConfig : never;
|
|
36
25
|
/**
|
|
37
26
|
* Commerce engine options for SSR scenarios where context is defined when fetching static state.
|
|
38
27
|
*/
|
|
39
28
|
export type SSRCommerceEngineOptions = Omit<CommerceEngineOptions, 'configuration'> & {
|
|
40
29
|
configuration: Omit<CommerceEngineOptions['configuration'], 'context'>;
|
|
41
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* The `RecommendationBuildConfig` type defines the shape of the configuration object required when fetching static state for recommendations.
|
|
33
|
+
*
|
|
34
|
+
* The `recommendations` property is a string array containing the names of the recommendation controllers
|
|
35
|
+
* that were defined in your engine definition. This allows you to specify which recommendation controllers
|
|
36
|
+
* should be included in the SSR request.
|
|
37
|
+
*
|
|
38
|
+
* For example, on a storefront homepage, it might make sense to display a carousel of popular products.
|
|
39
|
+
* However, showing recommendations based on items frequently bought together may not be relevant in that context and should not be retrieved unnecessarily.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* Given the following engine definition:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const {recommendationEngineDefinition} = defineCommerceEngine({
|
|
46
|
+
* controllers: {
|
|
47
|
+
* popularProducts: defineRecommendations({options: {slotId: '...'}}),
|
|
48
|
+
* frequentlyBought: defineRecommendations({options: {slotId: '...'}}),
|
|
49
|
+
* // ...other controllers
|
|
50
|
+
* },
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* // When fetching static state for recommendations:
|
|
54
|
+
* const staticState = await recommendationEngineDefinition.fetchStaticState({
|
|
55
|
+
* recommendations: ['popularProducts'],
|
|
56
|
+
* // ...other config
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* In this example, only the `popularProducts` recommendation controller will be included in the SSR request.
|
|
61
|
+
*/
|
|
62
|
+
export type RecommendationBuildConfig<TControllers extends ControllerDefinitionsMap<Controller>> = CommonBuildConfig & {
|
|
63
|
+
/**
|
|
64
|
+
* The unique identifier of the product to use for seeded recommendations.
|
|
65
|
+
*/
|
|
66
|
+
productId?: string;
|
|
67
|
+
/**
|
|
68
|
+
* An array of recommendation controller names from your engine definition to include in the SSR request.
|
|
69
|
+
* Each name corresponds to a key used when defining recommendation controllers in your engine definition.
|
|
70
|
+
* If not specified, no recommendation requests will be executed.
|
|
71
|
+
*/
|
|
72
|
+
recommendations: Array<Extract<keyof TControllers, {
|
|
73
|
+
[K in keyof TControllers]: HasKey<TControllers[K], SolutionType.recommendation> extends never ? never : IsRecommendationController<TControllers[K]> extends never ? never : K;
|
|
74
|
+
}[keyof TControllers]>>;
|
|
75
|
+
};
|
|
76
|
+
export interface ListingBuildConfig extends CommonBuildConfig {
|
|
77
|
+
}
|
|
78
|
+
export interface StandaloneBuildConfig extends CommonBuildConfig {
|
|
79
|
+
}
|
|
80
|
+
export interface CommonBuildConfig {
|
|
81
|
+
context: ContextOptions;
|
|
82
|
+
searchParams?: ParameterManagerState<Parameters>['parameters'];
|
|
83
|
+
cart?: CartInitialState;
|
|
84
|
+
}
|
|
85
|
+
export type BuildConfig<TControllers extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = TSolutionType extends SolutionType.search ? SearchBuildConfig : TSolutionType extends SolutionType.listing ? ListingBuildConfig : TSolutionType extends SolutionType.recommendation ? RecommendationBuildConfig<TControllers> : TSolutionType extends SolutionType.standalone ? CommonBuildConfig : never;
|
|
42
86
|
/**
|
|
43
87
|
* @internal
|
|
44
88
|
*/
|
|
45
|
-
export type Build<TControllersMap extends ControllersMap, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<BuildConfig<TSolutionType> & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<CommerceEngineDefinitionBuildResult<TControllersMap>>;
|
|
89
|
+
export type Build<TControllersMap extends ControllersMap, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<BuildConfig<TControllersDefinitionsMap, TSolutionType> & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<CommerceEngineDefinitionBuildResult<TControllersMap>>;
|
|
@@ -3,7 +3,7 @@ import type { InvalidControllerDefinition } from '../../common/errors.js';
|
|
|
3
3
|
import type { ControllersPropsMap } from '../../common/types/controllers.js';
|
|
4
4
|
import type { HydratedState } from '../../common/types/hydrate-static-state.js';
|
|
5
5
|
import type { InferControllerStaticStateFromController, InferControllerStaticStateMapFromControllers } from '../../common/types/inference.js';
|
|
6
|
-
import type { HasKey,
|
|
6
|
+
import type { HasKey, HasRequiredKeys, OptionsTuple } from '../../common/types/utilities.js';
|
|
7
7
|
import type { CartDefinition } from '../controllers/cart/headless-cart.ssr.js';
|
|
8
8
|
import type { ContextDefinition } from '../controllers/context/headless-context.ssr.js';
|
|
9
9
|
import type { ParameterManagerDefinition } from '../controllers/parameter-manager/headless-core-parameter-manager.ssr.js';
|
|
@@ -13,15 +13,6 @@ import type { ListingAndStandaloneController, ListingOnlyController, NonRecommen
|
|
|
13
13
|
import type { Kind } from './kind.js';
|
|
14
14
|
import type { HasSolutionType } from './utilities.js';
|
|
15
15
|
export type { HydratedState, OptionsTuple, InferControllerStaticStateFromController, InferControllerStaticStateMapFromControllers, };
|
|
16
|
-
export type RecommendationControllerSettings = {
|
|
17
|
-
/**
|
|
18
|
-
* Toggle to enable or disable the recommendation controller.
|
|
19
|
-
* When set to `true`, the controller will perform a recommendation request server-side.
|
|
20
|
-
*
|
|
21
|
-
* @default false
|
|
22
|
-
*/
|
|
23
|
-
enabled?: boolean;
|
|
24
|
-
};
|
|
25
16
|
export interface ControllerDefinitionWithoutProps<TController extends Controller> {
|
|
26
17
|
/**
|
|
27
18
|
* Creates an instance of the given controller.
|
|
@@ -94,120 +85,14 @@ export type AugmentedControllerDefinition<TControllerDefinitions extends Control
|
|
|
94
85
|
/**
|
|
95
86
|
* This type defines the required and optional controller props for the engine definition.
|
|
96
87
|
*/
|
|
97
|
-
export type EngineDefinitionControllersPropsOption<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, TSolutionType extends SolutionType> =
|
|
98
|
-
/**
|
|
99
|
-
* Represents an optional engine definition for controller properties.
|
|
100
|
-
*
|
|
101
|
-
* This type is used to define a map of optional controller properties based on the provided
|
|
102
|
-
* controller definitions, controller properties map, and solution type.
|
|
103
|
-
*
|
|
104
|
-
* @template TControllers - A map of controller definitions.
|
|
105
|
-
* @template TControllersPropsMap - A map of controller properties.
|
|
106
|
-
* @template TSolutionType - The type of solution.
|
|
107
|
-
*
|
|
108
|
-
* The type iterates over the controller keys (defined in the engine definition) and includes only those keys where:
|
|
109
|
-
* 1. The controller can be used for a specific solution type (HasKey<TControllers[K], TSolutionType> is not 'never').
|
|
110
|
-
* 2. The controller properties have optional options (HasOptionalKeys<ConditionalControllerProps<...>> is true).
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* Given the following controller definitions:
|
|
114
|
-
* ```
|
|
115
|
-
* const {recommendationEngineDefinition} = defineCommerceEngine({
|
|
116
|
-
* controllers: {
|
|
117
|
-
* popularViewed: defineRecommendations({
|
|
118
|
-
* options: {slotId: 'slot-id'}
|
|
119
|
-
* })
|
|
120
|
-
* }
|
|
121
|
-
* });
|
|
122
|
-
* ```
|
|
123
|
-
*
|
|
124
|
-
* The following code will not throw an error since the 'popularViewed' controller props are optional
|
|
125
|
-
* ```
|
|
126
|
-
* recommendationEngineDefinition.fetchStaticState({
|
|
127
|
-
* controllers: {
|
|
128
|
-
* popularViewed: {enabled: true, productId: 'some-product-id'} // This is optional
|
|
129
|
-
* }
|
|
130
|
-
* })
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* The following code (with no arguments) is also valid because the 'popularViewed' controller props are optional, and there are no other required controller props in the engine definition.
|
|
134
|
-
* ```
|
|
135
|
-
* recommendationEngineDefinition.fetchStaticState()
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
type OptionalEngineDefinitionControllersPropsOption<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, TSolutionType extends SolutionType> = {
|
|
139
|
-
[K in keyof TControllers as HasKey<TControllers[K], TSolutionType> extends never ? never : HasOptionalKeys<ConditionalControllerProps<TControllers, TControllersPropsMap, TSolutionType, K>> extends false ? never : 'controllers']?: ConditionalControllerProps<TControllers, TControllersPropsMap, TSolutionType, K>;
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* Represents a type that defines the required controller properties for engine definition controllers.
|
|
143
|
-
*
|
|
144
|
-
* @template TControllers - A map of controller definitions.
|
|
145
|
-
* @template TControllersPropsMap - A map of controller properties.
|
|
146
|
-
* @template TSolutionType - The type of solution being used.
|
|
147
|
-
*
|
|
148
|
-
* The type iterates over the controller keys (defined in the engine definition) and includes only those keys where:
|
|
149
|
-
* 1. The controller can be used for a specific solution type (HasKey<TControllers[K], TSolutionType> is not 'never').
|
|
150
|
-
* 2. The controller properties have required options (HasRequiredKeys<ConditionalControllerProps<...>> is true).
|
|
151
|
-
*
|
|
152
|
-
* The resulting type maps the valid keys to their corresponding conditional controller properties.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* Given the following controller definitions:
|
|
156
|
-
* ```
|
|
157
|
-
* const {standaloneEngineDefinition} = defineCommerceEngine({
|
|
158
|
-
* controllers: {cart: defineCart()},
|
|
159
|
-
* });
|
|
160
|
-
* ```
|
|
161
|
-
*
|
|
162
|
-
* The following code will not throw an error since the 'cart' controller props are required for the standalone engine definition:
|
|
163
|
-
* ```
|
|
164
|
-
* standaloneEngineDefinition.fetchStaticState({
|
|
165
|
-
* controllers: {
|
|
166
|
-
* cart: {initialState: {items: []}},
|
|
167
|
-
* },
|
|
168
|
-
* })
|
|
169
|
-
* ```
|
|
170
|
-
*
|
|
171
|
-
*/
|
|
88
|
+
export type EngineDefinitionControllersPropsOption<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, TSolutionType extends SolutionType> = RequiredEngineDefinitionControllersPropsOption<TControllers, TControllersPropsMap, TSolutionType>;
|
|
172
89
|
type RequiredEngineDefinitionControllersPropsOption<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, TSolutionType extends SolutionType> = {
|
|
173
|
-
[K in keyof TControllers as HasKey<TControllers[K], TSolutionType> extends never ? never : HasRequiredKeys<
|
|
174
|
-
};
|
|
175
|
-
type IsRecommendationController<TController extends ControllerDefinition<Controller>> = HasKey<TController, typeof recommendationInternalOptionKey>;
|
|
176
|
-
/**
|
|
177
|
-
* This type ensures that recommendation controller props are optional, while other controller props remain required.
|
|
178
|
-
*
|
|
179
|
-
* It works by checking if the controller definition includes the `recommendationInternalOptionKey`.
|
|
180
|
-
*
|
|
181
|
-
* - If the key is present, the controller props are made optional.
|
|
182
|
-
* - If the key is absent, the controller props are required.
|
|
183
|
-
*
|
|
184
|
-
* Example:
|
|
185
|
-
*
|
|
186
|
-
* ```typescript
|
|
187
|
-
* type ControllerProps = InferControllerPropsFromDefinition<{
|
|
188
|
-
* recommendation: defineRecommendation({}),
|
|
189
|
-
* cart: defineCart(),
|
|
190
|
-
* }>;
|
|
191
|
-
*
|
|
192
|
-
* // In this example:
|
|
193
|
-
* // - The `recommendation` controller props are optional.
|
|
194
|
-
* // - The `cart` controller props are required.
|
|
195
|
-
*
|
|
196
|
-
* const props: ControllerProps = {
|
|
197
|
-
* cart: {initialState: {items: []}},
|
|
198
|
-
* // recommendation props can be omitted
|
|
199
|
-
* };
|
|
200
|
-
* ```
|
|
201
|
-
*/
|
|
202
|
-
type RecommendationControllerProps<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, K extends keyof TControllers> = {
|
|
203
|
-
[I in keyof TControllersPropsMap as I extends K ? IsRecommendationController<TControllers[I]> extends never ? never : I : never]?: TControllersPropsMap[I];
|
|
204
|
-
} & {
|
|
205
|
-
[I in keyof TControllersPropsMap as I extends K ? IsRecommendationController<TControllers[I]> extends never ? I : never : never]: TControllersPropsMap[I];
|
|
90
|
+
[K in keyof TControllers as HasKey<TControllers[K], TSolutionType> extends never ? never : IsRecommendationController<TControllers[K]> extends never ? HasRequiredKeys<DefaultControllerProps<TControllers, TControllersPropsMap, K>> extends false ? never : 'controllers' : never]: DefaultControllerProps<TControllers, TControllersPropsMap, K>;
|
|
206
91
|
};
|
|
92
|
+
export type IsRecommendationController<TController extends ControllerDefinition<Controller>> = HasKey<TController, typeof recommendationInternalOptionKey>;
|
|
207
93
|
type DefaultControllerProps<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, K extends keyof TControllers> = {
|
|
208
94
|
[I in keyof TControllersPropsMap as I extends K ? I : never]: TControllersPropsMap[I];
|
|
209
95
|
};
|
|
210
|
-
type ConditionalControllerProps<TControllers extends ControllerDefinitionsMap<Controller>, TControllersPropsMap extends ControllersPropsMap, TSolutionType extends SolutionType, K extends keyof TControllers> = TSolutionType extends SolutionType.recommendation ? RecommendationControllerProps<TControllers, TControllersPropsMap, K> : DefaultControllerProps<TControllers, TControllersPropsMap, K>;
|
|
211
96
|
export interface ControllerDefinitionOption {
|
|
212
97
|
/**
|
|
213
98
|
* Whether the controller will be used in a product listing context.
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type { Controller } from '../../../controllers/controller/headless-controller.js';
|
|
2
2
|
import type { HasKey, HasKeys } from '../../common/types/utilities.js';
|
|
3
|
-
import type {
|
|
4
|
-
import type { ControllerDefinition, ControllerDefinitionsMap, ControllerDefinitionWithoutProps, ControllerDefinitionWithProps, InferControllerStaticStateFromController
|
|
3
|
+
import type { SolutionType } from './controller-constants.js';
|
|
4
|
+
import type { ControllerDefinition, ControllerDefinitionsMap, ControllerDefinitionWithoutProps, ControllerDefinitionWithProps, InferControllerStaticStateFromController } from './controller-definitions.js';
|
|
5
5
|
export type InferStaticState<T extends {
|
|
6
6
|
fetchStaticState(...args: unknown[]): Promise<unknown>;
|
|
7
7
|
}> = Awaited<ReturnType<T['fetchStaticState']>>;
|
|
8
8
|
export type InferHydratedState<T extends {
|
|
9
9
|
hydrateStaticState(...args: unknown[]): Promise<unknown>;
|
|
10
10
|
}> = Awaited<ReturnType<T['hydrateStaticState']>>;
|
|
11
|
-
type InferControllerPropsFromDefinition<TController extends ControllerDefinition<Controller>> = TController extends ControllerDefinitionWithProps<Controller, infer Props> ?
|
|
11
|
+
type InferControllerPropsFromDefinition<TController extends ControllerDefinition<Controller>> = TController extends ControllerDefinitionWithProps<Controller, infer Props> ? Props : TController extends ControllerDefinitionWithoutProps<Controller> ? {} : unknown;
|
|
12
12
|
export type InferControllerPropsMapFromDefinitions<TControllers extends ControllerDefinitionsMap<Controller>> = {
|
|
13
13
|
[K in keyof TControllers as HasKeys<InferControllerPropsFromDefinition<TControllers[K]>> extends false ? never : K]: InferControllerPropsFromDefinition<TControllers[K]>;
|
|
14
14
|
};
|
|
@@ -10,4 +10,4 @@ import type { EngineStaticState } from './engine.js';
|
|
|
10
10
|
*
|
|
11
11
|
* Useful for static generation and server-side rendering.
|
|
12
12
|
*/
|
|
13
|
-
export type FetchStaticState<TSearchAction extends UnknownAction, TControllersStaticState extends ControllerStaticStateMap, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<BuildConfig<TSolutionType> & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<EngineStaticState<TSearchAction, TControllersStaticState & FilteredBakedInControllers<TSolutionType
|
|
13
|
+
export type FetchStaticState<TSearchAction extends UnknownAction, TControllersStaticState extends ControllerStaticStateMap, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<BuildConfig<TControllersDefinitionsMap, TSolutionType> & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<EngineStaticState<TSearchAction, TControllersStaticState & FilteredBakedInControllers<TSolutionType>> & BuildConfig<TControllersDefinitionsMap, TSolutionType>>;
|
|
@@ -3,6 +3,7 @@ import type { Controller } from '../../../controllers/controller/headless-contro
|
|
|
3
3
|
import type { ControllersMap, ControllersPropsMap } from '../../common/types/controllers.js';
|
|
4
4
|
import type { HydratedState } from '../../common/types/hydrate-static-state.js';
|
|
5
5
|
import type { SSRCommerceEngine } from '../factories/build-factory.js';
|
|
6
|
+
import type { BuildConfig } from './build.js';
|
|
6
7
|
import type { SolutionType } from './controller-constants.js';
|
|
7
8
|
import type { ControllerDefinitionsMap, EngineDefinitionControllersPropsOption, OptionsTuple } from './controller-definitions.js';
|
|
8
9
|
export interface HydrateStaticStateOptions<TSearchAction> {
|
|
@@ -13,4 +14,4 @@ export interface HydrateStaticStateOptions<TSearchAction> {
|
|
|
13
14
|
*
|
|
14
15
|
* Useful when hydrating a server-side-rendered engine.
|
|
15
16
|
*/
|
|
16
|
-
export type HydrateStaticState<TControllers extends ControllersMap, TSearchAction extends UnknownAction, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<HydrateStaticStateOptions<TSearchAction> & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<HydratedState<SSRCommerceEngine, TControllers>>;
|
|
17
|
+
export type HydrateStaticState<TControllers extends ControllersMap, TSearchAction extends UnknownAction, TControllersProps extends ControllersPropsMap, TControllersDefinitionsMap extends ControllerDefinitionsMap<Controller>, TSolutionType extends SolutionType> = (...params: OptionsTuple<HydrateStaticStateOptions<TSearchAction> & BuildConfig<TControllersDefinitionsMap, TSolutionType> & EngineDefinitionControllersPropsOption<TControllersDefinitionsMap, TControllersProps, TSolutionType>>) => Promise<HydratedState<SSRCommerceEngine, TControllers>>;
|
|
@@ -1,8 +1,35 @@
|
|
|
1
|
+
import { Schema } from '@coveo/bueno';
|
|
1
2
|
import type { ControllersPropsMap } from '../../common/types/controllers.js';
|
|
2
3
|
import type { BuildConfig } from '../types/build.js';
|
|
3
|
-
import
|
|
4
|
+
import { SolutionType } from '../types/controller-constants.js';
|
|
4
5
|
import type { InferControllerPropsMapFromDefinitions } from '../types/controller-inference.js';
|
|
5
6
|
import type { CommerceControllerDefinitionsMap } from '../types/engine.js';
|
|
6
|
-
export declare
|
|
7
|
+
export declare const listingDefinitionSchema: Schema<{
|
|
8
|
+
searchParams: unknown;
|
|
9
|
+
context: unknown;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const searchDefinitionSchema: Schema<{
|
|
12
|
+
searchParams: unknown;
|
|
13
|
+
context: unknown;
|
|
14
|
+
}>;
|
|
15
|
+
export declare const standaloneDefinitionSchema: Schema<{
|
|
16
|
+
context: unknown;
|
|
17
|
+
}>;
|
|
18
|
+
export declare const recommendationsDefinitionSchema: (recommendationName: string[]) => Schema<{
|
|
19
|
+
recommendations: import("@coveo/bueno").PrimitivesValues[] | null | undefined;
|
|
20
|
+
productId: string;
|
|
21
|
+
context: unknown;
|
|
22
|
+
}>;
|
|
23
|
+
/**
|
|
24
|
+
* Converts simple user configuration (URL, language, currency, query) into the
|
|
25
|
+
* nested structure required by the internal {@link Build} method. Automatically generates
|
|
26
|
+
* appropriate controllers (context, cart, parameterManager) transparently.
|
|
27
|
+
*
|
|
28
|
+
* @param solutionType - The type of solution (search, listing, recommendation, standalone)
|
|
29
|
+
* @param controllerDefinitions - Map of controller definitions to be initialized
|
|
30
|
+
* @param buildConfig - Simple configuration object with simplified controller properties
|
|
31
|
+
* @returns Formatted and validated controller properties map defined for the controller in the definition
|
|
32
|
+
*/
|
|
33
|
+
export declare function wireControllerParams<TControllerDefinitions extends CommerceControllerDefinitionsMap>(solutionType: SolutionType, controllerDefinitions: TControllerDefinitions, buildConfig: BuildConfig<TControllerDefinitions, SolutionType> & {
|
|
7
34
|
controllers?: ControllersPropsMap;
|
|
8
35
|
}): InferControllerPropsMapFromDefinitions<TControllerDefinitions>;
|
|
@@ -2,4 +2,4 @@ import type { CommerceEngineConfiguration } from '../../../app/commerce-engine/c
|
|
|
2
2
|
import type { CommerceEngineDefinitionOptions } from '../factories/build-factory.js';
|
|
3
3
|
import type { CommonBuildConfig } from '../types/build.js';
|
|
4
4
|
import type { CommerceControllerDefinitionsMap } from '../types/engine.js';
|
|
5
|
-
export declare function extendEngineConfiguration<TControllerDefinitions extends CommerceControllerDefinitionsMap>(
|
|
5
|
+
export declare function extendEngineConfiguration<TControllerDefinitions extends CommerceControllerDefinitionsMap>(configuration: CommerceEngineDefinitionOptions<TControllerDefinitions>['configuration'], commonBuildOptions: CommonBuildConfig): CommerceEngineConfiguration;
|
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
import type { Controller } from '../../../controllers/controller/headless-controller.js';
|
|
2
|
-
import type {
|
|
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>;
|
|
3
16
|
export declare function filterRecommendationControllers<TControllerDefinitions extends ControllerDefinitionsMap<Controller>>(controllers: Record<string, Controller>, controllerDefinitions: TControllerDefinitions): {
|
|
4
17
|
/**
|
|
5
18
|
* Go through all the controllers passed in argument and only refresh recommendation controllers.
|
|
@@ -3,9 +3,6 @@ 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;
|
|
9
6
|
type ExtractRequiredOptions<TOptions> = {
|
|
10
7
|
[TKey in keyof TOptions as Pick<TOptions, TKey> extends Required<Pick<TOptions, TKey>> ? TKey : never]: TOptions[TKey];
|
|
11
8
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
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';
|
|
4
5
|
import type { ControllerDefinitionWithoutProps, ControllerDefinitionWithProps } from '../ssr-next/common/types/controllers.js';
|
|
5
6
|
interface MockController {
|
|
6
7
|
initialState?: Record<string, unknown>;
|
|
@@ -35,4 +36,15 @@ export declare function defineMockCommerceControllerWithProps(options?: Solution
|
|
|
35
36
|
standalone: boolean;
|
|
36
37
|
recommendation: boolean;
|
|
37
38
|
};
|
|
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
|
+
};
|
|
38
50
|
export {};
|
package/dist/esm/ssr-next/commerce/controllers/recommendations/headless-recommendations.ssr.js
CHANGED
|
@@ -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 },
|
|
20
|
+
options: { ...staticOptions, ...options.initialState },
|
|
21
21
|
});
|
|
22
22
|
return createControllerWithKind(controller, Kind.Recommendations);
|
|
23
23
|
},
|
|
@@ -57,21 +57,16 @@ 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
|
-
}
|
|
68
60
|
export const buildFactory = (controllerDefinitions, options) => (solutionType) => async (...[buildOptions]) => {
|
|
69
61
|
const controllerProps = wireControllerParams(solutionType, controllerDefinitions, buildOptions); // TODO: KIT-4754: remove non-null assertion operator
|
|
70
62
|
const logger = buildLogger(options.loggerOptions);
|
|
71
63
|
if (!options.navigatorContextProvider) {
|
|
72
64
|
logger.warn('[WARNING] Missing navigator context in server-side code. Make sure to set it with `setNavigatorContextProvider` before calling fetchStaticState()');
|
|
73
65
|
}
|
|
74
|
-
const enabledRecommendationControllers =
|
|
66
|
+
const enabledRecommendationControllers = buildOptions && 'recommendations' in buildOptions // TODO: KIT-4754: remove non-null assertion
|
|
67
|
+
? buildOptions
|
|
68
|
+
?.recommendations.length
|
|
69
|
+
: 0;
|
|
75
70
|
const engineOptions = {
|
|
76
71
|
...options,
|
|
77
72
|
configuration: extendEngineConfiguration(options.configuration, buildOptions), // TODO: KIT-4754: remove non-null assertion operator
|
|
@@ -2,9 +2,7 @@ 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(
|
|
6
|
-
// @ts-expect-error: TODO: KIT-4742: the wiring will fix also the type inconsistencies
|
|
7
|
-
...params);
|
|
5
|
+
const { engine, controllers } = await solutionTypeBuild(...params);
|
|
8
6
|
params[0].searchActions.forEach((action) => {
|
|
9
7
|
engine.dispatch(action);
|
|
10
8
|
});
|
|
@@ -3,9 +3,7 @@ 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(
|
|
7
|
-
// @ts-expect-error: TODO: KIT-4742: the wiring will fix also the type inconsistencies
|
|
8
|
-
...params));
|
|
6
|
+
const { engine, controllers } = (await solutionTypeBuild(...params));
|
|
9
7
|
params[0].searchActions.forEach((action) => {
|
|
10
8
|
engine.dispatch(action);
|
|
11
9
|
});
|
|
@@ -1,26 +1,22 @@
|
|
|
1
|
-
import { filterObject } from '../../../utils/utils.js';
|
|
2
1
|
import { createStaticState } from '../controller-utils.js';
|
|
3
2
|
import { SolutionType } from '../types/controller-constants.js';
|
|
4
3
|
import { filterRecommendationControllers } from '../utils/recommendation-filter.js';
|
|
5
4
|
import { buildFactory, } from './build-factory.js';
|
|
6
5
|
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
|
-
};
|
|
14
6
|
return async (...params) => {
|
|
15
7
|
const [props] = params;
|
|
16
|
-
const allowedRecommendationKeys =
|
|
8
|
+
const allowedRecommendationKeys = props.recommendations;
|
|
17
9
|
const solutionTypeBuild = await buildFactory(controllerDefinitions, options)(SolutionType.recommendation);
|
|
18
10
|
const { engine, controllers } = (await solutionTypeBuild(...params));
|
|
19
11
|
filterRecommendationControllers(controllers, controllerDefinitions ?? {}).refresh(allowedRecommendationKeys);
|
|
20
12
|
const searchActions = await Promise.all(engine.waitForRequestCompletedAction());
|
|
21
|
-
|
|
13
|
+
const staticState = createStaticState({
|
|
22
14
|
searchActions,
|
|
23
15
|
controllers,
|
|
24
16
|
});
|
|
17
|
+
return {
|
|
18
|
+
...params[0], // TODO: KIT-4754: remove index access after no longer relying on OptionTuple type
|
|
19
|
+
...staticState,
|
|
20
|
+
};
|
|
25
21
|
};
|
|
26
22
|
}
|
|
@@ -27,6 +27,9 @@ export function fetchStaticStateFactory(controllerDefinitions, options) {
|
|
|
27
27
|
searchActions,
|
|
28
28
|
controllers,
|
|
29
29
|
});
|
|
30
|
-
return
|
|
30
|
+
return {
|
|
31
|
+
...params[0], // TODO: KIT-4754: remove index access after no longer relying on OptionTuple type
|
|
32
|
+
...staticState,
|
|
33
|
+
};
|
|
31
34
|
};
|
|
32
35
|
}
|