@constructor-io/constructorio-client-javascript 2.27.13 → 2.29.2

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.
@@ -18,7 +18,9 @@ var EventDispatcher = require('../utils/event-dispatcher');
18
18
  var _require = require('../utils/helpers'),
19
19
  throwHttpErrorFromResponse = _require.throwHttpErrorFromResponse,
20
20
  cleanParams = _require.cleanParams,
21
- applyNetworkTimeout = _require.applyNetworkTimeout; // Create URL from supplied query (term) and parameters
21
+ applyNetworkTimeout = _require.applyNetworkTimeout,
22
+ trimNonBreakingSpaces = _require.trimNonBreakingSpaces,
23
+ encodeURIComponentRFC3986 = _require.encodeURIComponentRFC3986; // Create URL from supplied query (term) and parameters
22
24
 
23
25
 
24
26
  function createAutocompleteUrl(query, parameters, options) {
@@ -62,7 +64,8 @@ function createAutocompleteUrl(query, parameters, options) {
62
64
  var numResults = parameters.numResults,
63
65
  resultsPerSection = parameters.resultsPerSection,
64
66
  filters = parameters.filters,
65
- hiddenFields = parameters.hiddenFields; // Pull results number from parameters
67
+ hiddenFields = parameters.hiddenFields,
68
+ variationsMap = parameters.variationsMap; // Pull results number from parameters
66
69
 
67
70
  if (numResults) {
68
71
  queryParams.num_results = numResults;
@@ -89,6 +92,11 @@ function createAutocompleteUrl(query, parameters, options) {
89
92
  hidden_fields: hiddenFields
90
93
  };
91
94
  }
95
+ } // Pull variations map from parameters
96
+
97
+
98
+ if (variationsMap) {
99
+ queryParams.variations_map = JSON.stringify(variationsMap);
92
100
  }
93
101
  }
94
102
 
@@ -99,7 +107,7 @@ function createAutocompleteUrl(query, parameters, options) {
99
107
  });
100
108
  var cleanedQuery = query.replace(/^\//, '|'); // For compatibility with backend API
101
109
 
102
- return "".concat(serviceUrl, "/autocomplete/").concat(encodeURIComponent(cleanedQuery), "?").concat(queryString);
110
+ return "".concat(serviceUrl, "/autocomplete/").concat(encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery)), "?").concat(queryString);
103
111
  }
104
112
  /**
105
113
  * Interface to autocomplete related API calls.
@@ -127,6 +135,7 @@ var Autocomplete = /*#__PURE__*/function () {
127
135
  * @param {object} [parameters.filters] - Key / value mapping (dictionary) of filters used to refine results
128
136
  * @param {object} [parameters.resultsPerSection] - Number of results to return (value) per section (key)
129
137
  * @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
138
+ * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
130
139
  * @param {object} [networkParameters] - Parameters relevant to the network request
131
140
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
132
141
  * @returns {Promise}
@@ -66,7 +66,8 @@ function createQueryParams(parameters, options) {
66
66
  section = parameters.section,
67
67
  fmtOptions = parameters.fmtOptions,
68
68
  hiddenFields = parameters.hiddenFields,
69
- hiddenFacets = parameters.hiddenFacets; // Pull page from parameters
69
+ hiddenFacets = parameters.hiddenFacets,
70
+ variationsMap = parameters.variationsMap; // Pull page from parameters
70
71
 
71
72
  if (!helpers.isNil(page)) {
72
73
  queryParams.page = page;
@@ -121,6 +122,11 @@ function createQueryParams(parameters, options) {
121
122
  hidden_facets: hiddenFacets
122
123
  };
123
124
  }
125
+ } // Pull variations map from parameters
126
+
127
+
128
+ if (variationsMap) {
129
+ queryParams.variations_map = JSON.stringify(variationsMap);
124
130
  }
125
131
  }
126
132
 
@@ -146,7 +152,7 @@ function createBrowseUrlFromFilter(filterName, filterValue, parameters, options)
146
152
  var queryString = qs.stringify(queryParams, {
147
153
  indices: false
148
154
  });
149
- return "".concat(serviceUrl, "/browse/").concat(encodeURIComponent(filterName), "/").concat(encodeURIComponent(filterValue), "?").concat(queryString);
155
+ return "".concat(serviceUrl, "/browse/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(filterName)), "/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(filterValue)), "?").concat(queryString);
150
156
  } // Create URL from supplied id's
151
157
 
152
158
 
@@ -189,14 +195,15 @@ function createBrowseUrlForFacetOptions(facetName, parameters, options) {
189
195
  throw new Error('facetName is a required parameter of type string');
190
196
  }
191
197
 
192
- var queryParams = _objectSpread({}, createQueryParams(parameters, options)); // Endpoint does not accept _dt
198
+ var queryParams = _objectSpread({}, createQueryParams(parameters, options));
193
199
 
200
+ queryParams.facet_name = facetName; // Endpoint does not accept _dt
194
201
 
195
202
  delete queryParams._dt;
196
203
  var queryString = qs.stringify(queryParams, {
197
204
  indices: false
198
205
  });
199
- return "".concat(serviceUrl, "/browse/facet_options?facet_name=").concat(facetName, "&").concat(queryString);
206
+ return "".concat(serviceUrl, "/browse/facet_options?").concat(queryString);
200
207
  }
201
208
  /**
202
209
  * Interface to browse related API calls
@@ -230,6 +237,7 @@ var Browse = /*#__PURE__*/function () {
230
237
  * @param {object} [parameters.fmtOptions] - The format options used to refine result groups
231
238
  * @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
232
239
  * @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
240
+ * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
233
241
  * @param {object} [networkParameters] - Parameters relevant to the network request
234
242
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
235
243
  * @returns {Promise}
@@ -310,6 +318,7 @@ var Browse = /*#__PURE__*/function () {
310
318
  * @param {object} [parameters.fmtOptions] - The format options used to refine result groups
311
319
  * @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
312
320
  * @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
321
+ * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
313
322
  * @param {object} [networkParameters] - Parameters relevant to the network request
314
323
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
315
324
  * @returns {Promise}
@@ -52,7 +52,8 @@ function createRecommendationsUrl(podId, parameters, options) {
52
52
  itemIds = parameters.itemIds,
53
53
  section = parameters.section,
54
54
  term = parameters.term,
55
- filters = parameters.filters; // Pull num results number from parameters
55
+ filters = parameters.filters,
56
+ variationsMap = parameters.variationsMap; // Pull num results number from parameters
56
57
 
57
58
  if (!helpers.isNil(numResults)) {
58
59
  queryParams.num_results = numResults;
@@ -76,6 +77,11 @@ function createRecommendationsUrl(podId, parameters, options) {
76
77
 
77
78
  if (filters) {
78
79
  queryParams.filters = filters;
80
+ } // Pull variations map from parameters
81
+
82
+
83
+ if (variationsMap) {
84
+ queryParams.variations_map = JSON.stringify(variationsMap);
79
85
  }
80
86
  }
81
87
 
@@ -83,7 +89,7 @@ function createRecommendationsUrl(podId, parameters, options) {
83
89
  var queryString = qs.stringify(queryParams, {
84
90
  indices: false
85
91
  });
86
- return "".concat(serviceUrl, "/recommendations/v1/pods/").concat(encodeURIComponent(podId), "?").concat(queryString);
92
+ return "".concat(serviceUrl, "/recommendations/v1/pods/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(podId)), "?").concat(queryString);
87
93
  }
88
94
  /**
89
95
  * Interface to recommendations related API calls
@@ -112,6 +118,7 @@ var Recommendations = /*#__PURE__*/function () {
112
118
  * @param {string} [parameters.section] - The section to return results from
113
119
  * @param {string} [parameters.term] - The term to use to refine results (strategy specific)
114
120
  * @param {object} [parameters.filters] - Key / value mapping of filters used to refine results (strategy specific)
121
+ * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
115
122
  * @param {object} [networkParameters] - Parameters relevant to the network request
116
123
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
117
124
  * @returns {Promise}
@@ -66,7 +66,8 @@ function createSearchUrl(query, parameters, options) {
66
66
  section = parameters.section,
67
67
  fmtOptions = parameters.fmtOptions,
68
68
  hiddenFields = parameters.hiddenFields,
69
- hiddenFacets = parameters.hiddenFacets; // Pull page from parameters
69
+ hiddenFacets = parameters.hiddenFacets,
70
+ variationsMap = parameters.variationsMap; // Pull page from parameters
70
71
 
71
72
  if (!helpers.isNil(page)) {
72
73
  queryParams.page = page;
@@ -122,6 +123,11 @@ function createSearchUrl(query, parameters, options) {
122
123
  hidden_facets: hiddenFacets
123
124
  };
124
125
  }
126
+ } // Pull variations map from parameters
127
+
128
+
129
+ if (variationsMap) {
130
+ queryParams.variations_map = JSON.stringify(variationsMap);
125
131
  }
126
132
  }
127
133
 
@@ -130,7 +136,7 @@ function createSearchUrl(query, parameters, options) {
130
136
  var queryString = qs.stringify(queryParams, {
131
137
  indices: false
132
138
  });
133
- return "".concat(serviceUrl, "/search/").concat(encodeURIComponent(query), "?").concat(queryString);
139
+ return "".concat(serviceUrl, "/search/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(query)), "?").concat(queryString);
134
140
  }
135
141
  /**
136
142
  * Interface to search related API calls
@@ -163,6 +169,7 @@ var Search = /*#__PURE__*/function () {
163
169
  * @param {object} [parameters.fmtOptions] - The format options used to refine result groups
164
170
  * @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
165
171
  * @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
172
+ * @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
166
173
  * @param {object} [networkParameters] - Parameters relevant to the network request
167
174
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
168
175
  * @returns {Promise}
@@ -187,34 +187,43 @@ var Tracker = /*#__PURE__*/function () {
187
187
 
188
188
  // Ensure parameters are provided (required)
189
189
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
190
- var url = "".concat(this.options.serviceUrl, "/behavior?");
191
- var queryParams = {
192
- action: 'item_detail_load'
193
- };
190
+ var requestUrlPath = "".concat(this.options.serviceUrl, "/v2/behavioral_action/item_detail_load?");
191
+ var queryParams = {};
192
+ var bodyParams = {};
194
193
  var item_name = parameters.item_name,
195
194
  name = parameters.name,
196
195
  item_id = parameters.item_id,
197
196
  customer_id = parameters.customer_id,
198
- variation_id = parameters.variation_id; // Ensure support for both item_name and name as parameters
197
+ variation_id = parameters.variation_id,
198
+ url = parameters.url; // Ensure support for both item_name and name as parameters
199
199
 
200
200
  if (item_name) {
201
- queryParams.name = item_name;
201
+ bodyParams.item_name = item_name;
202
202
  } else if (name) {
203
- queryParams.name = name;
203
+ bodyParams.item_name = name;
204
204
  } // Ensure support for both item_id and customer_id as parameters
205
205
 
206
206
 
207
207
  if (item_id) {
208
- queryParams.customer_id = item_id;
208
+ bodyParams.item_id = item_id;
209
209
  } else if (customer_id) {
210
- queryParams.customer_id = customer_id;
210
+ bodyParams.item_id = customer_id;
211
211
  }
212
212
 
213
213
  if (variation_id) {
214
- queryParams.variation_id = variation_id;
214
+ bodyParams.variation_id = variation_id;
215
215
  }
216
216
 
217
- this.requests.queue("".concat(url).concat(applyParamsAsString(queryParams, this.options)), undefined, undefined, networkParameters);
217
+ if (url) {
218
+ bodyParams.url = url;
219
+ }
220
+
221
+ var requestURL = "".concat(requestUrlPath).concat(applyParamsAsString(queryParams, this.options));
222
+ var requestMethod = 'POST';
223
+ var requestBody = applyParams(bodyParams, _objectSpread(_objectSpread({}, this.options), {}, {
224
+ requestMethod: requestMethod
225
+ }));
226
+ this.requests.queue(requestURL, requestMethod, requestBody, networkParameters);
218
227
  this.requests.send();
219
228
  return true;
220
229
  }
@@ -259,7 +268,7 @@ var Tracker = /*#__PURE__*/function () {
259
268
  if (term && typeof term === 'string') {
260
269
  // Ensure parameters are provided (required)
261
270
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
262
- var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.ourEncodeURIComponent(term), "/select?");
271
+ var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term)), "/select?");
263
272
  var queryParams = {};
264
273
  var original_query = parameters.original_query,
265
274
  section = parameters.section,
@@ -332,7 +341,7 @@ var Tracker = /*#__PURE__*/function () {
332
341
  if (term && typeof term === 'string') {
333
342
  // Ensure parameters are provided (required)
334
343
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
335
- var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.ourEncodeURIComponent(term), "/search?");
344
+ var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term)), "/search?");
336
345
  var queryParams = {};
337
346
  var original_query = parameters.original_query,
338
347
  group_id = parameters.group_id,
@@ -439,6 +448,7 @@ var Tracker = /*#__PURE__*/function () {
439
448
  * @param {string} parameters.item_id - Product item unique identifier
440
449
  * @param {string} [parameters.variation_id] - Product item variation unique identifier
441
450
  * @param {string} [parameters.result_id] - Search result identifier (returned in response from Constructor)
451
+ * @param {string} [parameters.item_is_convertible] - Whether or not an item is available for a conversion
442
452
  * @param {object} [networkParameters] - Parameters relevant to the network request
443
453
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
444
454
  * @returns {(true|Error)}
@@ -463,14 +473,15 @@ var Tracker = /*#__PURE__*/function () {
463
473
  if (term && typeof term === 'string') {
464
474
  // Ensure parameters are provided (required)
465
475
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
466
- var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.ourEncodeURIComponent(term), "/click_through?");
476
+ var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term)), "/click_through?");
467
477
  var queryParams = {};
468
478
  var item_name = parameters.item_name,
469
479
  name = parameters.name,
470
480
  item_id = parameters.item_id,
471
481
  customer_id = parameters.customer_id,
472
482
  variation_id = parameters.variation_id,
473
- result_id = parameters.result_id; // Ensure support for both item_name and name as parameters
483
+ result_id = parameters.result_id,
484
+ item_is_convertible = parameters.item_is_convertible; // Ensure support for both item_name and name as parameters
474
485
 
475
486
  if (item_name) {
476
487
  queryParams.name = item_name;
@@ -493,6 +504,10 @@ var Tracker = /*#__PURE__*/function () {
493
504
  queryParams.result_id = result_id;
494
505
  }
495
506
 
507
+ if (typeof item_is_convertible === 'boolean') {
508
+ queryParams.item_is_convertible = item_is_convertible;
509
+ }
510
+
496
511
  this.requests.queue("".concat(url).concat(applyParamsAsString(queryParams, this.options)), undefined, undefined, networkParameters);
497
512
  this.requests.send();
498
513
  return true;
@@ -547,7 +562,7 @@ var Tracker = /*#__PURE__*/function () {
547
562
 
548
563
  // Ensure parameters are provided (required)
549
564
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
550
- var searchTerm = helpers.ourEncodeURIComponent(term) || 'TERM_UNKNOWN';
565
+ var searchTerm = term || 'TERM_UNKNOWN';
551
566
  var requestPath = "".concat(this.options.serviceUrl, "/v2/behavioral_action/conversion?");
552
567
  var queryParams = {};
553
568
  var bodyParams = {};
@@ -5,27 +5,20 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
6
6
 
7
7
  /* eslint-disable no-param-reassign */
8
- var qs = require('qs');
9
-
10
8
  var CRC32 = require('crc-32');
11
9
 
12
10
  var store = require('./store');
13
11
 
14
12
  var purchaseEventStorageKey = '_constructorio_purchase_order_ids';
15
13
  var utils = {
16
- ourEncodeURIComponent: function ourEncodeURIComponent(str) {
17
- if (str && typeof str === 'string') {
18
- var cleanedString = str.replace(/\[/g, '%5B') // Replace [
19
- .replace(/\]/g, '%5D') // Replace ]
20
- .replace(/&/g, '%26'); // Replace &
21
-
22
- var trimmedCleanedString = cleanedString.trim();
23
- var parsedStrObj = qs.parse("s=".concat(trimmedCleanedString));
24
- parsedStrObj.s = parsedStrObj.s.replace(/\s/g, ' ');
25
- return qs.stringify(parsedStrObj).split('=')[1];
26
- }
27
-
28
- return null;
14
+ trimNonBreakingSpaces: function trimNonBreakingSpaces(string) {
15
+ return string.replace(/\s/g, ' ').trim();
16
+ },
17
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
18
+ encodeURIComponentRFC3986: function encodeURIComponentRFC3986(string) {
19
+ return encodeURIComponent(string).replace(/[!'()*]/g, function (c) {
20
+ return "%".concat(c.charCodeAt(0).toString(16).toUpperCase());
21
+ });
29
22
  },
30
23
  cleanParams: function cleanParams(paramsObj) {
31
24
  var cleanedParams = {};
@@ -35,7 +28,7 @@ var utils = {
35
28
  if (typeof paramValue === 'string') {
36
29
  // Replace non-breaking spaces (or any other type of spaces caught by the regex)
37
30
  // - with a regular white space
38
- cleanedParams[paramKey] = decodeURIComponent(utils.ourEncodeURIComponent(paramValue));
31
+ cleanedParams[paramKey] = utils.trimNonBreakingSpaces(paramValue);
39
32
  } else {
40
33
  cleanedParams[paramKey] = paramValue;
41
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-client-javascript",
3
- "version": "2.27.13",
3
+ "version": "2.29.2",
4
4
  "description": "Constructor.io JavaScript client",
5
5
  "main": "lib/constructorio.js",
6
6
  "scripts": {