-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.ts
122 lines (106 loc) · 3.61 KB
/
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
import {
loadFacetSetActions,
buildSearchEngine,
SearchEngine,
getOrganizationEndpoints,
RegisterFacetActionCreatorPayload,
ContextPayload,
CoreEngine,
buildContext,
loadSearchAnalyticsActions,
loadSearchActions,
} from "@coveo/headless";
import {
RecommendationEngine,
RecommendationEngineConfiguration,
buildRecommendationEngine,
} from '@coveo/headless/recommendation';
const searchKey = process.env.NEXT_PUBLIC_API_KEY ?? '';
const orgId = process.env.NEXT_PUBLIC_ORGANIZATION_ID ?? '';
const DEFAULT_CONFIG = {
accessToken: searchKey,
organizationEndpoints: getOrganizationEndpoints(orgId),
organizationId: orgId,
};
const buildConfig = <T>(customConfig: any = {}) => {
const config = {
configuration: {
preprocessRequest: async (request: any) => {
//Do we want to set custom contexts into the search request?
// try {
// const loggedInUser = sessionStorage.getItem('barca-user');
// const requestBody = JSON.parse(request.body as string);
// if (!requestBody.context) {
// requestBody.context = {};
// }
// requestBody.context.log_in = false;
// if (loggedInUser) {
// requestBody.context.log_in = true;
// requestBody.context.purchased_products = 'Skipper';
// }
// request.body = JSON.stringify(requestBody);
// } catch (e) {
// // no-op - window is not defined
// }
return request;
},
...DEFAULT_CONFIG,
...customConfig,
analytics: {
analyticsMode: 'next',
trackingId: 'support',
...(customConfig.analytics || {}),
},
},
} as T;
return config;
};
const setCustomContext = (customContext?: Partial<ContextPayload>) => (engine: CoreEngine) => {
const ctx = buildContext(engine);
ctx.set({ website: 'support', ...(customContext || {}) });
return engine;
};
const searchEngineGenerator = compose<SearchEngine>(buildConfig, buildSearchEngine, setCustomContext());
export const searchEngine = searchEngineGenerator({
name: 'searchEngine',
search: { searchHub: 'wimssearch', pipeline: 'Search' },
});
export const articleEngine = searchEngineGenerator({
analytics: { enabled: false },
name: 'articleEngine',
search: { searchHub: 'wimssearch' },
});
export const recsEngineGenerator = (
engConfig: Partial<RecommendationEngineConfiguration>,
customContext?: Partial<ContextPayload>,
) => {
const { searchHub = 'Recs', name = 'recommendationsEngine', ...remainingProps } = engConfig || {};
return compose<RecommendationEngine>(
buildConfig,
buildRecommendationEngine,
setCustomContext(customContext),
)({ searchHub, name, ...remainingProps });
};
export const selectFacet = (facetId: string, facetValue: string, engine: SearchEngine = searchEngine) => {
const facetActions = loadFacetSetActions(engine);
engine.dispatch(
facetActions.toggleSelectFacetValue({
facetId,
selection: { value: facetValue, state: 'selected', numberOfResults: 1 },
}),
);
const analyticsActions = loadSearchAnalyticsActions(engine);
const searchActions = loadSearchActions(engine);
engine.dispatch(searchActions.executeSearch(analyticsActions.logFacetSelect({ facetId, facetValue })));
};
type AnyFunction = (..._args: any[]) => any;
function compose<R>(...funcs: AnyFunction[]) {
return function (this: any, ...args: any[]): R {
const [currFunc, ...reaminingFuncs] = funcs;
let result = currFunc.apply(this, args);
for (let i = 0; i < reaminingFuncs.length; i++) {
result = reaminingFuncs[i].call(this, result);
}
return result;
};
}