@constructor-io/constructorio-node 3.8.8 → 3.10.0
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/package.json
CHANGED
package/src/.DS_Store
ADDED
|
Binary file
|
|
@@ -47,7 +47,7 @@ function createAutocompleteUrl(query, parameters, userParameters, options) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
if (parameters) {
|
|
50
|
-
const { numResults, resultsPerSection, filters, hiddenFields } = parameters;
|
|
50
|
+
const { numResults, resultsPerSection, filters, hiddenFields, variationsMap } = parameters;
|
|
51
51
|
|
|
52
52
|
// Pull results number from parameters
|
|
53
53
|
if (numResults) {
|
|
@@ -74,14 +74,20 @@ function createAutocompleteUrl(query, parameters, userParameters, options) {
|
|
|
74
74
|
queryParams.fmt_options = { hidden_fields: hiddenFields };
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
+
|
|
78
|
+
// Pull variations map from parameters
|
|
79
|
+
if (variationsMap) {
|
|
80
|
+
queryParams.variations_map = JSON.stringify(variationsMap);
|
|
81
|
+
}
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
queryParams._dt = Date.now();
|
|
80
85
|
queryParams = helpers.cleanParams(queryParams);
|
|
81
86
|
|
|
82
87
|
const queryString = qs.stringify(queryParams, { indices: false });
|
|
88
|
+
const cleanedQuery = query.replace(/^\//, '|'); // For compatibility with backend API
|
|
83
89
|
|
|
84
|
-
return `${serviceUrl}/autocomplete/${
|
|
90
|
+
return `${serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(cleanedQuery))}?${queryString}`;
|
|
85
91
|
}
|
|
86
92
|
|
|
87
93
|
/**
|
|
@@ -106,6 +112,7 @@ class Autocomplete {
|
|
|
106
112
|
* @param {object} [parameters.filters] - Filters used to refine search
|
|
107
113
|
* @param {object} [parameters.resultsPerSection] - Number of results to return (value) per section (key)
|
|
108
114
|
* @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
|
|
115
|
+
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
|
|
109
116
|
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
110
117
|
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
111
118
|
* @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
|
package/src/modules/browse.js
CHANGED
|
@@ -34,6 +34,7 @@ function createQueryParams(parameters, userParameters, options) {
|
|
|
34
34
|
fmtOptions,
|
|
35
35
|
hiddenFields,
|
|
36
36
|
hiddenFacets,
|
|
37
|
+
variationsMap,
|
|
37
38
|
} = parameters;
|
|
38
39
|
|
|
39
40
|
// Pull page from parameters
|
|
@@ -87,6 +88,11 @@ function createQueryParams(parameters, userParameters, options) {
|
|
|
87
88
|
queryParams.fmt_options = { hidden_facets: hiddenFacets };
|
|
88
89
|
}
|
|
89
90
|
}
|
|
91
|
+
|
|
92
|
+
// Pull variations map from parameters
|
|
93
|
+
if (variationsMap) {
|
|
94
|
+
queryParams.variations_map = JSON.stringify(variationsMap);
|
|
95
|
+
}
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
// Pull test cells from options
|
|
@@ -129,7 +135,7 @@ function createBrowseUrlFromFilter(filterName, filterValue, parameters, userPara
|
|
|
129
135
|
const queryParams = createQueryParams(parameters, userParameters, options);
|
|
130
136
|
const queryString = qs.stringify(queryParams, { indices: false });
|
|
131
137
|
|
|
132
|
-
return `${serviceUrl}/browse/${
|
|
138
|
+
return `${serviceUrl}/browse/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(filterName))}/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(filterValue))}?${queryString}`;
|
|
133
139
|
}
|
|
134
140
|
|
|
135
141
|
// Create URL from supplied IDs and parameters
|
|
@@ -170,11 +176,13 @@ function createBrowseUrlForFacetOptions(facetName, parameters, userParameters, o
|
|
|
170
176
|
|
|
171
177
|
const queryParams = { ...createQueryParams(parameters, userParameters, options) };
|
|
172
178
|
|
|
179
|
+
queryParams.facet_name = facetName;
|
|
180
|
+
|
|
173
181
|
delete queryParams._dt;
|
|
174
182
|
|
|
175
183
|
const queryString = qs.stringify(queryParams, { indices: false });
|
|
176
184
|
|
|
177
|
-
return `${serviceUrl}/browse/facet_options
|
|
185
|
+
return `${serviceUrl}/browse/facet_options?${queryString}`;
|
|
178
186
|
}
|
|
179
187
|
|
|
180
188
|
// Create request headers using supplied options and user parameters
|
|
@@ -223,9 +231,11 @@ class Browse {
|
|
|
223
231
|
* @param {object} [parameters.filters] - Filters used to refine results
|
|
224
232
|
* @param {string} [parameters.sortBy='relevance'] - The sort method for results
|
|
225
233
|
* @param {string} [parameters.sortOrder='descending'] - The sort order for results
|
|
234
|
+
* @param {string} [parameters.section='Products'] - The section name for results
|
|
226
235
|
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
227
236
|
* @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
|
|
228
237
|
* @param {string[]} [parameters.hiddenFacets] - Hidden facet fields to return
|
|
238
|
+
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
|
|
229
239
|
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
230
240
|
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
231
241
|
* @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
|
|
@@ -305,9 +315,11 @@ class Browse {
|
|
|
305
315
|
* @param {object} [parameters.filters] - Filters used to refine results
|
|
306
316
|
* @param {string} [parameters.sortBy='relevance'] - The sort method for results
|
|
307
317
|
* @param {string} [parameters.sortOrder='descending'] - The sort order for results
|
|
318
|
+
* @param {string} [parameters.section='Products'] - The section name for results
|
|
308
319
|
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
309
320
|
* @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
|
|
310
321
|
* @param {string[]} [parameters.hiddenFacets] - Hidden facet fields to return
|
|
322
|
+
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
|
|
311
323
|
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
312
324
|
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
313
325
|
* @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
|
|
@@ -375,6 +387,7 @@ class Browse {
|
|
|
375
387
|
*
|
|
376
388
|
* @function getBrowseGroups
|
|
377
389
|
* @param {object} [parameters.filters] - Filters used to refine results
|
|
390
|
+
* @param {string} [parameters.section='Products'] - The section name for results
|
|
378
391
|
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
379
392
|
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
380
393
|
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
@@ -433,6 +446,7 @@ class Browse {
|
|
|
433
446
|
* @function getBrowseFacets
|
|
434
447
|
* @param {object} [parameters] - Additional parameters to refine result set
|
|
435
448
|
* @param {number} [parameters.page] - The page number of the results
|
|
449
|
+
* @param {string} [parameters.section='Products'] - The section name for results
|
|
436
450
|
* @param {number} [parameters.resultsPerPage] - The number of results per page to return
|
|
437
451
|
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
438
452
|
* @param {boolean} [parameters.fmtOptions.show_hidden_facets] - Include facets configured as hidden
|
|
@@ -492,6 +506,7 @@ class Browse {
|
|
|
492
506
|
* @function getBrowseFacetOptions
|
|
493
507
|
* @param {string} facetName - Name of the facet whose options to return
|
|
494
508
|
* @param {object} [parameters] - Additional parameters to refine result set
|
|
509
|
+
* @param {string} [parameters.section='Products'] - The section name for results
|
|
495
510
|
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
496
511
|
* @param {boolean} [parameters.fmtOptions.show_hidden_facets] - Include facets configured as hidden
|
|
497
512
|
* @param {boolean} [parameters.fmtOptions.show_protected_facets] - Include facets configured as protected
|
|
@@ -39,7 +39,7 @@ function createRecommendationsUrl(podId, parameters, userParameters, options) {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
if (parameters) {
|
|
42
|
-
const { numResults, itemIds, section, term, filters } = parameters;
|
|
42
|
+
const { numResults, itemIds, section, term, filters, variationsMap } = parameters;
|
|
43
43
|
|
|
44
44
|
// Pull num results number from parameters
|
|
45
45
|
if (!helpers.isNil(numResults)) {
|
|
@@ -65,13 +65,18 @@ function createRecommendationsUrl(podId, parameters, userParameters, options) {
|
|
|
65
65
|
if (filters) {
|
|
66
66
|
queryParams.filters = filters;
|
|
67
67
|
}
|
|
68
|
+
|
|
69
|
+
// Pull variations map from parameters
|
|
70
|
+
if (variationsMap) {
|
|
71
|
+
queryParams.variations_map = JSON.stringify(variationsMap);
|
|
72
|
+
}
|
|
68
73
|
}
|
|
69
74
|
|
|
70
75
|
queryParams = helpers.cleanParams(queryParams);
|
|
71
76
|
|
|
72
77
|
const queryString = qs.stringify(queryParams, { indices: false });
|
|
73
78
|
|
|
74
|
-
return `${serviceUrl}/recommendations/v1/pods/${podId}?${queryString}`;
|
|
79
|
+
return `${serviceUrl}/recommendations/v1/pods/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(podId))}?${queryString}`;
|
|
75
80
|
}
|
|
76
81
|
|
|
77
82
|
/**
|
|
@@ -97,6 +102,7 @@ class Recommendations {
|
|
|
97
102
|
* @param {string} [parameters.section] - The section to return results from
|
|
98
103
|
* @param {string} [parameters.term] - The term to use to refine results (strategy specific)
|
|
99
104
|
* @param {object} [parameters.filters] - Filters used to refine results (strategy specific)
|
|
105
|
+
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
|
|
100
106
|
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
101
107
|
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
102
108
|
* @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
|
package/src/modules/search.js
CHANGED
|
@@ -57,6 +57,7 @@ function createSearchUrl(query, parameters, userParameters, options) {
|
|
|
57
57
|
fmtOptions,
|
|
58
58
|
hiddenFields,
|
|
59
59
|
hiddenFacets,
|
|
60
|
+
variationsMap,
|
|
60
61
|
} = parameters;
|
|
61
62
|
|
|
62
63
|
// Pull page from parameters
|
|
@@ -111,6 +112,11 @@ function createSearchUrl(query, parameters, userParameters, options) {
|
|
|
111
112
|
queryParams.fmt_options = { hidden_facets: hiddenFacets };
|
|
112
113
|
}
|
|
113
114
|
}
|
|
115
|
+
|
|
116
|
+
// Pull variations map from parameters
|
|
117
|
+
if (variationsMap) {
|
|
118
|
+
queryParams.variations_map = JSON.stringify(variationsMap);
|
|
119
|
+
}
|
|
114
120
|
}
|
|
115
121
|
|
|
116
122
|
queryParams._dt = Date.now();
|
|
@@ -118,7 +124,7 @@ function createSearchUrl(query, parameters, userParameters, options) {
|
|
|
118
124
|
|
|
119
125
|
const queryString = qs.stringify(queryParams, { indices: false });
|
|
120
126
|
|
|
121
|
-
return `${serviceUrl}/search/${
|
|
127
|
+
return `${serviceUrl}/search/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(query))}?${queryString}`;
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
/**
|
|
@@ -148,6 +154,7 @@ class Search {
|
|
|
148
154
|
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
149
155
|
* @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
|
|
150
156
|
* @param {string[]} [parameters.hiddenFacets] - Hidden facet fields to return
|
|
157
|
+
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
|
|
151
158
|
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
152
159
|
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
153
160
|
* @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
|
package/src/modules/tracker.js
CHANGED
|
@@ -385,7 +385,7 @@ class Tracker {
|
|
|
385
385
|
if (term && typeof term === 'string') {
|
|
386
386
|
// Ensure parameters are provided (required)
|
|
387
387
|
if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) {
|
|
388
|
-
const url = `${this.options.serviceUrl}/autocomplete/${helpers.
|
|
388
|
+
const url = `${this.options.serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term))}/select?`;
|
|
389
389
|
const queryParams = {};
|
|
390
390
|
const {
|
|
391
391
|
original_query,
|
|
@@ -479,7 +479,7 @@ class Tracker {
|
|
|
479
479
|
if (term && typeof term === 'string') {
|
|
480
480
|
// Ensure parameters are provided (required)
|
|
481
481
|
if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) {
|
|
482
|
-
const url = `${this.options.serviceUrl}/autocomplete/${helpers.
|
|
482
|
+
const url = `${this.options.serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term))}/search?`;
|
|
483
483
|
const queryParams = {};
|
|
484
484
|
const { original_query, group_id, display_name } = parameters;
|
|
485
485
|
|
|
@@ -641,7 +641,7 @@ class Tracker {
|
|
|
641
641
|
if (term && typeof term === 'string') {
|
|
642
642
|
// Ensure parameters are provided (required)
|
|
643
643
|
if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) {
|
|
644
|
-
const url = `${this.options.serviceUrl}/autocomplete/${helpers.
|
|
644
|
+
const url = `${this.options.serviceUrl}/autocomplete/${helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term))}/click_through?`;
|
|
645
645
|
const queryParams = {};
|
|
646
646
|
const { item_name, name, item_id, customer_id, variation_id, result_id } = parameters;
|
|
647
647
|
|
|
@@ -740,7 +740,7 @@ class Tracker {
|
|
|
740
740
|
trackConversion(term, parameters, userParameters, networkParameters = {}) {
|
|
741
741
|
// Ensure parameters are provided (required)
|
|
742
742
|
if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) {
|
|
743
|
-
const searchTerm =
|
|
743
|
+
const searchTerm = term || 'TERM_UNKNOWN';
|
|
744
744
|
const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/conversion?`;
|
|
745
745
|
const queryParams = {};
|
|
746
746
|
const bodyParams = {};
|
package/src/utils/helpers.js
CHANGED
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
/* eslint-disable no-param-reassign */
|
|
2
|
-
const qs = require('qs');
|
|
3
|
-
|
|
4
2
|
const utils = {
|
|
5
|
-
|
|
6
|
-
if (str && typeof str === 'string') {
|
|
7
|
-
const cleanedString = str
|
|
8
|
-
.replace(/\[/g, '%5B') // Replace [
|
|
9
|
-
.replace(/\]/g, '%5D') // Replace ]
|
|
10
|
-
.replace(/&/g, '%26'); // Replace &
|
|
11
|
-
const trimmedCleanedString = cleanedString.trim();
|
|
12
|
-
const parsedStrObj = qs.parse(`s=${trimmedCleanedString}`);
|
|
13
|
-
|
|
14
|
-
parsedStrObj.s = parsedStrObj.s.replace(/\s/g, ' ');
|
|
3
|
+
trimNonBreakingSpaces: (string) => string.replace(/\s/g, ' ').trim(),
|
|
15
4
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return null;
|
|
19
|
-
},
|
|
5
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
|
|
6
|
+
encodeURIComponentRFC3986: (string) => encodeURIComponent(string).replace(/[!'()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`),
|
|
20
7
|
|
|
21
8
|
cleanParams: (paramsObj) => {
|
|
22
9
|
const cleanedParams = {};
|
|
@@ -27,7 +14,7 @@ const utils = {
|
|
|
27
14
|
if (typeof paramValue === 'string') {
|
|
28
15
|
// Replace non-breaking spaces (or any other type of spaces caught by the regex)
|
|
29
16
|
// - with a regular white space
|
|
30
|
-
cleanedParams[paramKey] =
|
|
17
|
+
cleanedParams[paramKey] = utils.trimNonBreakingSpaces(paramValue);
|
|
31
18
|
} else {
|
|
32
19
|
cleanedParams[paramKey] = paramValue;
|
|
33
20
|
}
|