@algolia/autocomplete-preset-algolia 1.8.3 → 1.9.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.
@@ -1,4 +1,4 @@
1
- /*! @algolia/autocomplete-preset-algolia 1.8.3 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
1
+ /*! @algolia/autocomplete-preset-algolia 1.9.0 | MIT License | © Algolia, Inc. and contributors | https://github.com/algolia/autocomplete */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
4
4
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
@@ -7,17 +7,14 @@
7
7
 
8
8
  function ownKeys(object, enumerableOnly) {
9
9
  var keys = Object.keys(object);
10
-
11
10
  if (Object.getOwnPropertySymbols) {
12
11
  var symbols = Object.getOwnPropertySymbols(object);
13
12
  enumerableOnly && (symbols = symbols.filter(function (sym) {
14
13
  return Object.getOwnPropertyDescriptor(object, sym).enumerable;
15
14
  })), keys.push.apply(keys, symbols);
16
15
  }
17
-
18
16
  return keys;
19
17
  }
20
-
21
18
  function _objectSpread2(target) {
22
19
  for (var i = 1; i < arguments.length; i++) {
23
20
  var source = null != arguments[i] ? arguments[i] : {};
@@ -27,11 +24,19 @@
27
24
  Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
28
25
  });
29
26
  }
30
-
31
27
  return target;
32
28
  }
33
-
29
+ function _typeof(obj) {
30
+ "@babel/helpers - typeof";
31
+
32
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
33
+ return typeof obj;
34
+ } : function (obj) {
35
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
36
+ }, _typeof(obj);
37
+ }
34
38
  function _defineProperty(obj, key, value) {
39
+ key = _toPropertyKey(key);
35
40
  if (key in obj) {
36
41
  Object.defineProperty(obj, key, {
37
42
  value: value,
@@ -42,35 +47,26 @@
42
47
  } else {
43
48
  obj[key] = value;
44
49
  }
45
-
46
50
  return obj;
47
51
  }
48
-
49
52
  function _objectWithoutPropertiesLoose(source, excluded) {
50
53
  if (source == null) return {};
51
54
  var target = {};
52
55
  var sourceKeys = Object.keys(source);
53
56
  var key, i;
54
-
55
57
  for (i = 0; i < sourceKeys.length; i++) {
56
58
  key = sourceKeys[i];
57
59
  if (excluded.indexOf(key) >= 0) continue;
58
60
  target[key] = source[key];
59
61
  }
60
-
61
62
  return target;
62
63
  }
63
-
64
64
  function _objectWithoutProperties(source, excluded) {
65
65
  if (source == null) return {};
66
-
67
66
  var target = _objectWithoutPropertiesLoose(source, excluded);
68
-
69
67
  var key, i;
70
-
71
68
  if (Object.getOwnPropertySymbols) {
72
69
  var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
73
-
74
70
  for (i = 0; i < sourceSymbolKeys.length; i++) {
75
71
  key = sourceSymbolKeys[i];
76
72
  if (excluded.indexOf(key) >= 0) continue;
@@ -78,22 +74,17 @@
78
74
  target[key] = source[key];
79
75
  }
80
76
  }
81
-
82
77
  return target;
83
78
  }
84
-
85
79
  function _toConsumableArray(arr) {
86
80
  return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
87
81
  }
88
-
89
82
  function _arrayWithoutHoles(arr) {
90
83
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
91
84
  }
92
-
93
85
  function _iterableToArray(iter) {
94
86
  if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
95
87
  }
96
-
97
88
  function _unsupportedIterableToArray(o, minLen) {
98
89
  if (!o) return;
99
90
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
@@ -102,18 +93,28 @@
102
93
  if (n === "Map" || n === "Set") return Array.from(o);
103
94
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
104
95
  }
105
-
106
96
  function _arrayLikeToArray(arr, len) {
107
97
  if (len == null || len > arr.length) len = arr.length;
108
-
109
98
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
110
-
111
99
  return arr2;
112
100
  }
113
-
114
101
  function _nonIterableSpread() {
115
102
  throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
116
103
  }
104
+ function _toPrimitive(input, hint) {
105
+ if (typeof input !== "object" || input === null) return input;
106
+ var prim = input[Symbol.toPrimitive];
107
+ if (prim !== undefined) {
108
+ var res = prim.call(input, hint || "default");
109
+ if (typeof res !== "object") return res;
110
+ throw new TypeError("@@toPrimitive must return a primitive value.");
111
+ }
112
+ return (hint === "string" ? String : Number)(input);
113
+ }
114
+ function _toPropertyKey(arg) {
115
+ var key = _toPrimitive(arg, "string");
116
+ return typeof key === "symbol" ? key : String(key);
117
+ }
117
118
 
118
119
  function getAttributeValueByPath(record, path) {
119
120
  return path.reduce(function (current, key) {
@@ -121,7 +122,18 @@
121
122
  }, record);
122
123
  }
123
124
 
124
- var version = '1.8.3';
125
+ /**
126
+ * Throws an error if the condition is not met in development mode.
127
+ * This is used to make development a better experience to provide guidance as
128
+ * to where the error comes from.
129
+ */
130
+ function invariant(condition, message) {
131
+ if (!condition) {
132
+ throw new Error("[Autocomplete] ".concat(typeof message === 'function' ? message() : message));
133
+ }
134
+ }
135
+
136
+ var version = '1.9.0';
125
137
 
126
138
  var userAgents = [{
127
139
  segment: 'autocomplete-core',
@@ -131,23 +143,21 @@
131
143
  var warnCache = {
132
144
  current: {}
133
145
  };
146
+
134
147
  /**
135
148
  * Logs a warning if the condition is not met.
136
149
  * This is used to log issues in development environment only.
137
150
  */
138
-
139
151
  function warn(condition, message) {
140
-
141
152
  if (condition) {
142
153
  return;
143
154
  }
144
-
145
155
  var sanitizedMessage = message.trim();
146
156
  var hasAlreadyPrinted = warnCache.current[sanitizedMessage];
147
-
148
157
  if (!hasAlreadyPrinted) {
149
- warnCache.current[sanitizedMessage] = true; // eslint-disable-next-line no-console
158
+ warnCache.current[sanitizedMessage] = true;
150
159
 
160
+ // eslint-disable-next-line no-console
151
161
  console.warn("[Autocomplete] ".concat(sanitizedMessage));
152
162
  }
153
163
  }
@@ -168,7 +178,6 @@
168
178
  },
169
179
  add: function add(part) {
170
180
  var lastPart = value[value.length - 1];
171
-
172
181
  if ((lastPart === null || lastPart === void 0 ? void 0 : lastPart.isHighlighted) === part.isHighlighted) {
173
182
  value[value.length - 1] = {
174
183
  value: lastPart.value + part.value,
@@ -180,7 +189,6 @@
180
189
  }
181
190
  };
182
191
  }
183
-
184
192
  function parseAttribute(_ref) {
185
193
  var highlightedValue = _ref.highlightedValue;
186
194
  var preTagParts = highlightedValue.split(HIGHLIGHT_PRE_TAG);
@@ -195,7 +203,6 @@
195
203
  value: postTagParts[0],
196
204
  isHighlighted: true
197
205
  });
198
-
199
206
  if (postTagParts[1] !== '') {
200
207
  parts.add({
201
208
  value: postTagParts[1],
@@ -208,15 +215,13 @@
208
215
 
209
216
  function parseAlgoliaHitHighlight(_ref) {
210
217
  var hit = _ref.hit,
211
- attribute = _ref.attribute;
218
+ attribute = _ref.attribute;
212
219
  var path = Array.isArray(attribute) ? attribute : [attribute];
213
220
  var highlightedValue = getAttributeValueByPath(hit, ['_highlightResult'].concat(_toConsumableArray(path), ['value']));
214
-
215
221
  if (typeof highlightedValue !== 'string') {
216
222
  "development" !== 'production' ? warn(false, "The attribute \"".concat(path.join('.'), "\" described by the path ").concat(JSON.stringify(path), " does not exist on the hit. Did you set it in `attributesToHighlight`?") + '\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/') : void 0;
217
223
  highlightedValue = getAttributeValueByPath(hit, path) || '';
218
224
  }
219
-
220
225
  return parseAttribute({
221
226
  highlightedValue: highlightedValue
222
227
  });
@@ -232,24 +237,19 @@
232
237
  var hasAlphanumeric = new RegExp(/\w/i);
233
238
  var regexEscapedHtml = /&(amp|quot|lt|gt|#39);/g;
234
239
  var regexHasEscapedHtml = RegExp(regexEscapedHtml.source);
235
-
236
240
  function unescape(value) {
237
241
  return value && regexHasEscapedHtml.test(value) ? value.replace(regexEscapedHtml, function (character) {
238
242
  return htmlEscapes[character];
239
243
  }) : value;
240
244
  }
241
-
242
245
  function isPartHighlighted(parts, i) {
243
246
  var _parts, _parts2;
244
-
245
247
  var current = parts[i];
246
248
  var isNextHighlighted = ((_parts = parts[i + 1]) === null || _parts === void 0 ? void 0 : _parts.isHighlighted) || true;
247
249
  var isPreviousHighlighted = ((_parts2 = parts[i - 1]) === null || _parts2 === void 0 ? void 0 : _parts2.isHighlighted) || true;
248
-
249
250
  if (!hasAlphanumeric.test(unescape(current.value)) && isPreviousHighlighted === isNextHighlighted) {
250
251
  return isPreviousHighlighted;
251
252
  }
252
-
253
253
  return current.isHighlighted;
254
254
  }
255
255
 
@@ -264,7 +264,6 @@
264
264
  });
265
265
  });
266
266
  }
267
-
268
267
  return parts.map(function (part, i) {
269
268
  return _objectSpread2(_objectSpread2({}, part), {}, {
270
269
  isHighlighted: !isPartHighlighted(parts, i)
@@ -278,15 +277,13 @@
278
277
 
279
278
  function parseAlgoliaHitSnippet(_ref) {
280
279
  var hit = _ref.hit,
281
- attribute = _ref.attribute;
280
+ attribute = _ref.attribute;
282
281
  var path = Array.isArray(attribute) ? attribute : [attribute];
283
282
  var highlightedValue = getAttributeValueByPath(hit, ['_snippetResult'].concat(_toConsumableArray(path), ['value']));
284
-
285
283
  if (typeof highlightedValue !== 'string') {
286
284
  "development" !== 'production' ? warn(false, "The attribute \"".concat(path.join('.'), "\" described by the path ").concat(JSON.stringify(path), " does not exist on the hit. Did you set it in `attributesToSnippet`?") + '\nSee https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/') : void 0;
287
285
  highlightedValue = getAttributeValueByPath(hit, path) || '';
288
286
  }
289
-
290
287
  return parseAttribute({
291
288
  highlightedValue: highlightedValue
292
289
  });
@@ -306,8 +303,8 @@
306
303
  }).then(function (responses) {
307
304
  return responses.map(function (response, index) {
308
305
  var _fetcherParams$reques = fetcherParams.requests[index],
309
- sourceId = _fetcherParams$reques.sourceId,
310
- transformResponse = _fetcherParams$reques.transformResponse;
306
+ sourceId = _fetcherParams$reques.sourceId,
307
+ transformResponse = _fetcherParams$reques.transformResponse;
311
308
  return {
312
309
  items: response,
313
310
  sourceId: sourceId,
@@ -316,7 +313,6 @@
316
313
  });
317
314
  });
318
315
  }
319
-
320
316
  return function createSpecifiedRequester(requesterParams) {
321
317
  return function requester(requestParams) {
322
318
  return _objectSpread2(_objectSpread2({
@@ -327,26 +323,40 @@
327
323
  };
328
324
  }
329
325
 
326
+ function getAppIdAndApiKey(searchClient) {
327
+ var _searchClient$transpo = searchClient.transporter,
328
+ headers = _searchClient$transpo.headers,
329
+ queryParameters = _searchClient$transpo.queryParameters;
330
+ var APP_ID = 'x-algolia-application-id';
331
+ var API_KEY = 'x-algolia-api-key';
332
+ var appId = headers[APP_ID] || queryParameters[APP_ID];
333
+ var apiKey = headers[API_KEY] || queryParameters[API_KEY];
334
+ return {
335
+ appId: appId,
336
+ apiKey: apiKey
337
+ };
338
+ }
339
+
330
340
  var _excluded = ["params"];
331
341
  function fetchAlgoliaResults(_ref) {
332
342
  var searchClient = _ref.searchClient,
333
- queries = _ref.queries,
334
- _ref$userAgents = _ref.userAgents,
335
- userAgents$1 = _ref$userAgents === void 0 ? [] : _ref$userAgents;
336
-
343
+ queries = _ref.queries,
344
+ _ref$userAgents = _ref.userAgents,
345
+ userAgents$1 = _ref$userAgents === void 0 ? [] : _ref$userAgents;
337
346
  if (typeof searchClient.addAlgoliaAgent === 'function') {
338
347
  var algoliaAgents = [].concat(_toConsumableArray(userAgents), _toConsumableArray(userAgents$1));
339
348
  algoliaAgents.forEach(function (_ref2) {
340
349
  var segment = _ref2.segment,
341
- version = _ref2.version;
350
+ version = _ref2.version;
342
351
  searchClient.addAlgoliaAgent(segment, version);
343
352
  });
344
353
  }
345
-
354
+ var _getAppIdAndApiKey = getAppIdAndApiKey(searchClient),
355
+ appId = _getAppIdAndApiKey.appId,
356
+ apiKey = _getAppIdAndApiKey.apiKey;
346
357
  return searchClient.search(queries.map(function (searchParameters) {
347
358
  var params = searchParameters.params,
348
- headers = _objectWithoutProperties(searchParameters, _excluded);
349
-
359
+ headers = _objectWithoutProperties(searchParameters, _excluded);
350
360
  return _objectSpread2(_objectSpread2({}, headers), {}, {
351
361
  params: _objectSpread2({
352
362
  hitsPerPage: 5,
@@ -355,7 +365,19 @@
355
365
  }, params)
356
366
  });
357
367
  })).then(function (response) {
358
- return response.results;
368
+ return response.results.map(function (result) {
369
+ var _result$hits;
370
+ return _objectSpread2(_objectSpread2({}, result), {}, {
371
+ hits: (_result$hits = result.hits) === null || _result$hits === void 0 ? void 0 : _result$hits.map(function (hit) {
372
+ return _objectSpread2(_objectSpread2({}, hit), {}, {
373
+ __autocomplete_algoliaCredentials: {
374
+ appId: appId,
375
+ apiKey: apiKey
376
+ }
377
+ });
378
+ })
379
+ });
380
+ });
359
381
  });
360
382
  }
361
383
 
@@ -365,6 +387,7 @@
365
387
  * Retrieves Algolia facet hits from multiple indices.
366
388
  */
367
389
  function getAlgoliaFacets(requestParams) {
390
+ invariant(_typeof(requestParams.searchClient) === 'object', 'The `searchClient` parameter is required for getAlgoliaFacets({ searchClient }).');
368
391
  var requester = createAlgoliaRequester({
369
392
  transformResponse: function transformResponse(response) {
370
393
  return response.facetHits;
@@ -383,12 +406,15 @@
383
406
  /**
384
407
  * Retrieves Algolia results from multiple indices.
385
408
  */
386
-
387
- var getAlgoliaResults = createAlgoliaRequester({
388
- transformResponse: function transformResponse(response) {
389
- return response.hits;
390
- }
391
- });
409
+ function getAlgoliaResults(requestParams) {
410
+ invariant(_typeof(requestParams.searchClient) === 'object', 'The `searchClient` parameter is required for getAlgoliaResults({ searchClient }).');
411
+ var requester = createAlgoliaRequester({
412
+ transformResponse: function transformResponse(response) {
413
+ return response.hits;
414
+ }
415
+ });
416
+ return requester(requestParams);
417
+ }
392
418
 
393
419
  exports.createRequester = createRequester;
394
420
  exports.fetchAlgoliaResults = fetchAlgoliaResults;