@constructor-io/constructorio-client-javascript 2.37.0 → 2.37.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.
@@ -1,114 +1,109 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
6
-
7
5
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
8
-
9
6
  /* eslint-disable complexity */
10
-
11
7
  /* eslint-disable max-len */
12
-
13
8
  /* eslint-disable object-curly-newline, no-underscore-dangle */
14
9
  var EventDispatcher = require('../utils/event-dispatcher');
10
+ var helpers = require('../utils/helpers');
15
11
 
16
- var helpers = require('../utils/helpers'); // Create URL from supplied query (term) and parameters
17
-
18
-
12
+ // Create URL from supplied query (term) and parameters
19
13
  function createSearchUrl(query, parameters, options) {
20
14
  var isVoiceSearch = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
21
15
  var apiKey = options.apiKey,
22
- version = options.version,
23
- serviceUrl = options.serviceUrl,
24
- sessionId = options.sessionId,
25
- clientId = options.clientId,
26
- userId = options.userId,
27
- segments = options.segments,
28
- testCells = options.testCells;
16
+ version = options.version,
17
+ serviceUrl = options.serviceUrl,
18
+ sessionId = options.sessionId,
19
+ clientId = options.clientId,
20
+ userId = options.userId,
21
+ segments = options.segments,
22
+ testCells = options.testCells;
29
23
  var queryParams = {
30
24
  c: version
31
25
  };
32
26
  queryParams.key = apiKey;
33
27
  queryParams.i = clientId;
34
- queryParams.s = sessionId; // Validate query (term) is provided
28
+ queryParams.s = sessionId;
35
29
 
30
+ // Validate query (term) is provided
36
31
  if (!query || typeof query !== 'string') {
37
32
  throw new Error('query is a required parameter of type string');
38
- } // Pull test cells from options
39
-
33
+ }
40
34
 
35
+ // Pull test cells from options
41
36
  if (testCells) {
42
37
  Object.keys(testCells).forEach(function (testCellKey) {
43
38
  queryParams["ef-".concat(testCellKey)] = testCells[testCellKey];
44
39
  });
45
- } // Pull user segments from options
46
-
40
+ }
47
41
 
42
+ // Pull user segments from options
48
43
  if (segments && segments.length) {
49
44
  queryParams.us = segments;
50
- } // Pull user id from options and ensure string
51
-
45
+ }
52
46
 
47
+ // Pull user id from options and ensure string
53
48
  if (userId) {
54
49
  queryParams.ui = String(userId);
55
50
  }
56
-
57
51
  if (parameters) {
58
52
  var offset = parameters.offset,
59
- page = parameters.page,
60
- resultsPerPage = parameters.resultsPerPage,
61
- filters = parameters.filters,
62
- sortBy = parameters.sortBy,
63
- sortOrder = parameters.sortOrder,
64
- section = parameters.section,
65
- fmtOptions = parameters.fmtOptions,
66
- hiddenFields = parameters.hiddenFields,
67
- hiddenFacets = parameters.hiddenFacets,
68
- variationsMap = parameters.variationsMap,
69
- qsParam = parameters.qsParam,
70
- preFilterExpression = parameters.preFilterExpression; // Pull offset from parameters
71
-
53
+ page = parameters.page,
54
+ resultsPerPage = parameters.resultsPerPage,
55
+ filters = parameters.filters,
56
+ sortBy = parameters.sortBy,
57
+ sortOrder = parameters.sortOrder,
58
+ section = parameters.section,
59
+ fmtOptions = parameters.fmtOptions,
60
+ hiddenFields = parameters.hiddenFields,
61
+ hiddenFacets = parameters.hiddenFacets,
62
+ variationsMap = parameters.variationsMap,
63
+ qsParam = parameters.qsParam,
64
+ preFilterExpression = parameters.preFilterExpression;
65
+
66
+ // Pull offset from parameters
72
67
  if (!helpers.isNil(offset)) {
73
68
  queryParams.offset = offset;
74
- } // Pull page from parameters
75
-
69
+ }
76
70
 
71
+ // Pull page from parameters
77
72
  if (!helpers.isNil(page)) {
78
73
  queryParams.page = page;
79
- } // Pull results per page from parameters
80
-
74
+ }
81
75
 
76
+ // Pull results per page from parameters
82
77
  if (!helpers.isNil(resultsPerPage)) {
83
78
  queryParams.num_results_per_page = resultsPerPage;
84
- } // Pull filters from parameters
85
-
79
+ }
86
80
 
81
+ // Pull filters from parameters
87
82
  if (filters) {
88
83
  queryParams.filters = filters;
89
- } // Pull sort by from parameters
90
-
84
+ }
91
85
 
86
+ // Pull sort by from parameters
92
87
  if (sortBy) {
93
88
  queryParams.sort_by = sortBy;
94
- } // Pull sort order from parameters
95
-
89
+ }
96
90
 
91
+ // Pull sort order from parameters
97
92
  if (sortOrder) {
98
93
  queryParams.sort_order = sortOrder;
99
- } // Pull section from parameters
100
-
94
+ }
101
95
 
96
+ // Pull section from parameters
102
97
  if (section) {
103
98
  queryParams.section = section;
104
- } // Pull format options from parameters
105
-
99
+ }
106
100
 
101
+ // Pull format options from parameters
107
102
  if (fmtOptions) {
108
103
  queryParams.fmt_options = fmtOptions;
109
- } // Pull hidden fields from parameters
110
-
104
+ }
111
105
 
106
+ // Pull hidden fields from parameters
112
107
  if (hiddenFields) {
113
108
  if (queryParams.fmt_options) {
114
109
  queryParams.fmt_options.hidden_fields = hiddenFields;
@@ -117,9 +112,9 @@ function createSearchUrl(query, parameters, options) {
117
112
  hidden_fields: hiddenFields
118
113
  };
119
114
  }
120
- } // Pull hidden facets from parameters
121
-
115
+ }
122
116
 
117
+ // Pull hidden facets from parameters
123
118
  if (hiddenFacets) {
124
119
  if (queryParams.fmt_options) {
125
120
  queryParams.fmt_options.hidden_facets = hiddenFacets;
@@ -128,30 +123,30 @@ function createSearchUrl(query, parameters, options) {
128
123
  hidden_facets: hiddenFacets
129
124
  };
130
125
  }
131
- } // Pull variations map from parameters
132
-
126
+ }
133
127
 
128
+ // Pull variations map from parameters
134
129
  if (variationsMap) {
135
130
  queryParams.variations_map = JSON.stringify(variationsMap);
136
- } // Pull pre_filter_expression from parameters
137
-
131
+ }
138
132
 
133
+ // Pull pre_filter_expression from parameters
139
134
  if (preFilterExpression) {
140
135
  queryParams.pre_filter_expression = JSON.stringify(preFilterExpression);
141
- } // pull qs param from parameters
142
-
136
+ }
143
137
 
138
+ // pull qs param from parameters
144
139
  if (qsParam) {
145
140
  queryParams.qs = JSON.stringify(qsParam);
146
141
  }
147
142
  }
148
-
149
143
  queryParams._dt = Date.now();
150
144
  queryParams = helpers.cleanParams(queryParams);
151
145
  var queryString = helpers.stringify(queryParams);
152
146
  var searchUrl = isVoiceSearch ? 'search/natural_language' : 'search';
153
147
  return "".concat(serviceUrl, "/").concat(searchUrl, "/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(query)), "?").concat(queryString);
154
148
  }
149
+
155
150
  /**
156
151
  * Interface to search related API calls
157
152
  *
@@ -159,14 +154,13 @@ function createSearchUrl(query, parameters, options) {
159
154
  * @inner
160
155
  * @returns {object}
161
156
  */
162
-
163
-
164
157
  var Search = /*#__PURE__*/function () {
165
158
  function Search(options) {
166
159
  (0, _classCallCheck2["default"])(this, Search);
167
160
  this.options = options || {};
168
161
  this.eventDispatcher = new EventDispatcher(options.eventDispatcher);
169
162
  }
163
+
170
164
  /**
171
165
  * Retrieve search results from API
172
166
  *
@@ -199,38 +193,32 @@ var Search = /*#__PURE__*/function () {
199
193
  * },
200
194
  * });
201
195
  */
202
-
203
-
204
196
  (0, _createClass2["default"])(Search, [{
205
197
  key: "getSearchResults",
206
198
  value: function getSearchResults(query, parameters) {
207
199
  var _this = this;
208
-
209
200
  var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
210
201
  var requestUrl;
211
202
  var fetch = this.options.fetch;
212
203
  var signal;
213
-
214
204
  if (typeof AbortController === 'function') {
215
205
  var controller = new AbortController();
216
- signal = controller && controller.signal; // Handle network timeout if specified
206
+ signal = controller && controller.signal;
217
207
 
208
+ // Handle network timeout if specified
218
209
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
219
210
  }
220
-
221
211
  try {
222
212
  requestUrl = createSearchUrl(query, parameters, this.options);
223
213
  } catch (e) {
224
214
  return Promise.reject(e);
225
215
  }
226
-
227
216
  return fetch(requestUrl, {
228
217
  signal: signal
229
218
  }).then(function (response) {
230
219
  if (response.ok) {
231
220
  return response.json();
232
221
  }
233
-
234
222
  return helpers.throwHttpErrorFromResponse(new Error(), response);
235
223
  }).then(function (json) {
236
224
  // Search results
@@ -242,22 +230,19 @@ var Search = /*#__PURE__*/function () {
242
230
  result.result_id = json.result_id;
243
231
  });
244
232
  }
245
-
246
233
  _this.eventDispatcher.queue('search.getSearchResults.completed', json);
247
-
248
234
  return json;
249
- } // Redirect rules
250
-
235
+ }
251
236
 
237
+ // Redirect rules
252
238
  if (json.response && json.response.redirect) {
253
239
  _this.eventDispatcher.queue('search.getSearchResults.completed', json);
254
-
255
240
  return json;
256
241
  }
257
-
258
242
  throw new Error('getSearchResults response data is malformed');
259
243
  });
260
244
  }
245
+
261
246
  /**
262
247
  * Retrieve voice search results from API
263
248
  *
@@ -282,38 +267,33 @@ var Search = /*#__PURE__*/function () {
282
267
  * @example
283
268
  * constructorio.search.getVoiceSearchResults('show me lipstick');
284
269
  */
285
-
286
270
  }, {
287
271
  key: "getVoiceSearchResults",
288
272
  value: function getVoiceSearchResults(query, parameters) {
289
273
  var _this2 = this;
290
-
291
274
  var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
292
275
  var requestUrl;
293
276
  var fetch = this.options.fetch;
294
277
  var signal;
295
-
296
278
  if (typeof AbortController === 'function') {
297
279
  var controller = new AbortController();
298
- signal = controller && controller.signal; // Handle network timeout if specified
280
+ signal = controller && controller.signal;
299
281
 
282
+ // Handle network timeout if specified
300
283
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
301
284
  }
302
-
303
285
  try {
304
286
  var isVoiceSearch = true;
305
287
  requestUrl = createSearchUrl(query, parameters, this.options, isVoiceSearch);
306
288
  } catch (e) {
307
289
  return Promise.reject(e);
308
290
  }
309
-
310
291
  return fetch(requestUrl, {
311
292
  signal: signal
312
293
  }).then(function (response) {
313
294
  if (response.ok) {
314
295
  return response.json();
315
296
  }
316
-
317
297
  return helpers.throwHttpErrorFromResponse(new Error(), response);
318
298
  }).then(function (json) {
319
299
  // Search results
@@ -325,24 +305,19 @@ var Search = /*#__PURE__*/function () {
325
305
  result.result_id = json.result_id;
326
306
  });
327
307
  }
328
-
329
308
  _this2.eventDispatcher.queue('search.getVoiceSearchResults.completed', json);
330
-
331
309
  return json;
332
- } // Redirect rules
333
-
310
+ }
334
311
 
312
+ // Redirect rules
335
313
  if (json.response && json.response.redirect) {
336
314
  _this2.eventDispatcher.queue('search.getVoiceSearchResults.completed', json);
337
-
338
315
  return json;
339
316
  }
340
-
341
317
  throw new Error('getVoiceSearchResults response data is malformed');
342
318
  });
343
319
  }
344
320
  }]);
345
321
  return Search;
346
322
  }();
347
-
348
323
  module.exports = Search;