@constructor-io/constructorio-client-javascript 2.27.12 → 2.29.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.
@@ -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
 
@@ -97,7 +105,9 @@ function createAutocompleteUrl(query, parameters, options) {
97
105
  var queryString = qs.stringify(queryParams, {
98
106
  indices: false
99
107
  });
100
- return "".concat(serviceUrl, "/autocomplete/").concat(encodeURIComponent(query), "?").concat(queryString);
108
+ var cleanedQuery = query.replace(/^\//, '|'); // For compatibility with backend API
109
+
110
+ return "".concat(serviceUrl, "/autocomplete/").concat(encodeURIComponentRFC3986(trimNonBreakingSpaces(cleanedQuery)), "?").concat(queryString);
101
111
  }
102
112
  /**
103
113
  * Interface to autocomplete related API calls.
@@ -125,6 +135,7 @@ var Autocomplete = /*#__PURE__*/function () {
125
135
  * @param {object} [parameters.filters] - Key / value mapping (dictionary) of filters used to refine results
126
136
  * @param {object} [parameters.resultsPerSection] - Number of results to return (value) per section (key)
127
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
128
139
  * @param {object} [networkParameters] - Parameters relevant to the network request
129
140
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
130
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}
@@ -259,7 +259,7 @@ var Tracker = /*#__PURE__*/function () {
259
259
  if (term && typeof term === 'string') {
260
260
  // Ensure parameters are provided (required)
261
261
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
262
- var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.ourEncodeURIComponent(term), "/select?");
262
+ var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term)), "/select?");
263
263
  var queryParams = {};
264
264
  var original_query = parameters.original_query,
265
265
  section = parameters.section,
@@ -332,7 +332,7 @@ var Tracker = /*#__PURE__*/function () {
332
332
  if (term && typeof term === 'string') {
333
333
  // Ensure parameters are provided (required)
334
334
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
335
- var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.ourEncodeURIComponent(term), "/search?");
335
+ var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term)), "/search?");
336
336
  var queryParams = {};
337
337
  var original_query = parameters.original_query,
338
338
  group_id = parameters.group_id,
@@ -463,7 +463,7 @@ var Tracker = /*#__PURE__*/function () {
463
463
  if (term && typeof term === 'string') {
464
464
  // Ensure parameters are provided (required)
465
465
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
466
- var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.ourEncodeURIComponent(term), "/click_through?");
466
+ var url = "".concat(this.options.serviceUrl, "/autocomplete/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(term)), "/click_through?");
467
467
  var queryParams = {};
468
468
  var item_name = parameters.item_name,
469
469
  name = parameters.name,
@@ -547,7 +547,7 @@ var Tracker = /*#__PURE__*/function () {
547
547
 
548
548
  // Ensure parameters are provided (required)
549
549
  if (parameters && (0, _typeof2["default"])(parameters) === 'object' && !Array.isArray(parameters)) {
550
- var searchTerm = helpers.ourEncodeURIComponent(term) || 'TERM_UNKNOWN';
550
+ var searchTerm = term || 'TERM_UNKNOWN';
551
551
  var requestPath = "".concat(this.options.serviceUrl, "/v2/behavioral_action/conversion?");
552
552
  var queryParams = {};
553
553
  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.12",
3
+ "version": "2.29.0",
4
4
  "description": "Constructor.io JavaScript client",
5
5
  "main": "lib/constructorio.js",
6
6
  "scripts": {