-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathheadless-engine.ts
238 lines (214 loc) · 8.38 KB
/
headless-engine.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import {ReducersMapObject, StateFromReducersMapObject} from '@reduxjs/toolkit';
import {
updateSearchConfiguration,
localeValidation,
} from '../features/configuration/configuration-actions';
import {SearchAPIClient} from '../api/search/search-api-client';
import {Logger} from 'pino';
import {NoopPreprocessRequestMiddleware} from '../api/platform-client';
import {NoopPreprocessRequest} from '../api/preprocess-request';
import {RecordValue, Schema, StringValue} from '@coveo/bueno';
import {
NoopPostprocessFacetSearchResponseMiddleware,
NoopPostprocessQuerySuggestResponseMiddleware,
NoopPostprocessSearchResponseMiddleware,
} from '../api/search/search-api-client-middleware';
import {buildEngine, CoreEngine, EngineOptions} from './engine';
import {
engineConfigurationDefinitions,
EngineConfiguration,
} from './engine-configuration';
import {buildLogger} from './logger';
import {
buildThunkExtraArguments,
ThunkExtraArguments,
} from './thunk-extra-arguments';
import {SearchAppState} from '../state/search-app-state';
import {debug, pipeline, searchHub} from './reducers';
import {SearchConfigurationOptions} from './search-engine/search-engine-configuration';
const headlessReducers = {debug, pipeline, searchHub};
type HeadlessReducers = typeof headlessReducers;
type HeadlessState = StateFromReducersMapObject<HeadlessReducers>;
/**
* The global headless engine options.
*
* @deprecated - For a search app, use `SearchEngineOptions`.
* For a recommendation, use `RecommendationEngineOptions` from "@coveo/headless/recommendation".
* For a product recommendation, use `ProductRecommendationEngineOptions` from "@coveo/headless/product-recommendation".
*/
export interface HeadlessOptions<Reducers extends ReducersMapObject>
extends EngineOptions<Reducers> {
/**
* The global headless engine configuration options.
*/
configuration: HeadlessConfigurationOptions;
}
/**
* The global headless engine configuration options.
*
* @deprecated - For a search app, use `SearchEngineConfiguration`.
* For a recommendation, use `RecommendationEngineConfiguration` from "@coveo/headless/recommendation".
* For a product recommendation, use `ProductRecommendationEngineConfiguration` from "@coveo/headless/product-recommendation".
*/
export interface HeadlessConfigurationOptions extends EngineConfiguration {
/**
* The global headless engine configuration options specific to the SearchAPI.
*/
search?: SearchConfigurationOptions;
}
/**
* The engine for powering search experiences.
*
* @deprecated - For a search app, use `SearchEngine`.
* For a recommendation, use `RecommendationEngine` from "@coveo/headless/recommendation".
* For a product recommendation, use `ProductRecommendationEngine` from "@coveo/headless/product-recommendation".
*/
export interface Engine<State = SearchAppState>
extends CoreEngine<State & HeadlessState, SearchThunkExtraArguments> {}
export interface SearchThunkExtraArguments extends ThunkExtraArguments {
searchAPIClient: SearchAPIClient;
}
/**
* The global headless engine.
* You should instantiate one `HeadlessEngine` class per application and share it.
* Every headless controller requires an instance of `Engine` as a parameter.
*
* @deprecated - For a search app, use `buildSearchEngine`.
* For a recommendation, use `buildRecommendationEngine` from "@coveo/headless/recommendation".
* For a product recommendation, use `buildProductRecommendationEngine` from "@coveo/headless/product-recommendation".
*/
export class HeadlessEngine<Reducers extends ReducersMapObject>
implements Engine<StateFromReducersMapObject<Reducers>> {
public logger!: Logger;
private engine: Engine<StateFromReducersMapObject<Reducers>>;
constructor(private options: HeadlessOptions<Reducers>) {
console.warn(
'The HeadlessEngine class is deprecated and will be removed in the next major version. Please use either:\n',
'import {buildSearchEngine} from "@coveo/headless" or,\n',
'import {buildRecommendationEngine} from "@coveo/headless/recommendation" or,\n',
'import {buildProductRecommendationEngine} from "@coveo/headless/product-recommendation".'
);
this.logger = buildLogger(options.loggerOptions);
this.validateConfiguration(options);
const thunkArguments = {
...buildThunkExtraArguments(options.configuration, this.logger),
searchAPIClient: this.createSearchAPIClient(),
};
const augmentedOptions: HeadlessOptions<Reducers & HeadlessReducers> = {
...options,
reducers: {...headlessReducers, ...options.reducers},
};
this.engine = buildEngine(augmentedOptions, thunkArguments);
if (options.configuration.search) {
this.engine.dispatch(
updateSearchConfiguration(options.configuration.search)
);
}
}
public addReducers(reducers: ReducersMapObject) {
this.engine.addReducers(reducers);
}
private validateConfiguration(options: HeadlessOptions<Reducers>) {
if (options.configuration.search?.preprocessRequestMiddleware) {
this.logger
.warn(`The "search.preprocessRequestMiddleware" configuration option is now deprecated and will be removed in the upcoming @coveo/headless major version.
Please use the "preprocessRequest" option instead, which works for both the Search and Analytics API requests.`);
}
const configurationSchema = new Schema<HeadlessConfigurationOptions>({
...engineConfigurationDefinitions,
search: new RecordValue({
options: {
required: false,
},
values: {
pipeline: new StringValue({
required: false,
emptyAllowed: false,
}),
searchHub: new StringValue({
required: false,
emptyAllowed: false,
}),
locale: localeValidation,
},
}),
});
try {
configurationSchema.validate(options.configuration);
} catch (error) {
this.logger.error(error, 'Headless engine configuration error');
throw error;
}
}
private createSearchAPIClient() {
const {search} = this.options.configuration;
const preprocessRequest =
this.options.configuration.preprocessRequest || NoopPreprocessRequest;
return new SearchAPIClient({
logger: this.logger,
renewAccessToken: () => this.renewAccessToken(),
preprocessRequest,
deprecatedPreprocessRequest:
search?.preprocessRequestMiddleware || NoopPreprocessRequestMiddleware,
postprocessSearchResponseMiddleware:
search?.preprocessSearchResponseMiddleware ||
NoopPostprocessSearchResponseMiddleware,
postprocessFacetSearchResponseMiddleware:
search?.preprocessFacetSearchResponseMiddleware ||
NoopPostprocessFacetSearchResponseMiddleware,
postprocessQuerySuggestResponseMiddleware:
search?.preprocessQuerySuggestResponseMiddleware ||
NoopPostprocessQuerySuggestResponseMiddleware,
});
}
/**
* @returns A configuration with sample data for testing purposes.
*/
static getSampleConfiguration(): HeadlessConfigurationOptions {
console.warn(
'The HeadlessEngine.getSampleConfiguration static method is deprecated and will be removed in the next major version. Please use either:\n',
'import {getSampleSearchEngineConfiguration} from "@coveo/headless" or,\n',
'import {getSampleRecommendationEngineConfiguration} from "@coveo/headless/recommendation" or,\n',
'import {getSampleProductRecommendationEngineConfiguration} from "@coveo/headless/product-recommendation".'
);
return {
organizationId: 'searchuisamples',
accessToken: 'xx564559b1-0045-48e1-953c-3addd1ee4457',
name: 'sampleName',
search: {
pipeline: 'default',
searchHub: 'default',
},
};
}
/**
* Enable analytics tracking
*/
public enableAnalytics() {
this.engine.enableAnalytics();
}
/**
* Disable analytics tracking
*/
public disableAnalytics() {
this.engine.disableAnalytics();
}
get store() {
return this.engine.store;
}
get dispatch() {
return this.engine.dispatch;
}
get subscribe() {
return this.engine.subscribe;
}
get state() {
return this.engine.state;
}
/**
* @deprecated - Calling this function directly is not needed because Headless handles token renewal internally. The function will be removed in the next major version.
*/
public async renewAccessToken() {
return await this.engine.renewAccessToken();
}
}