instantsearch.js 4.33.1 → 4.36.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +24 -1
  3. package/cjs/connectors/autocomplete/connectAutocomplete.js +2 -1
  4. package/cjs/connectors/dynamic-widgets/connectDynamicWidgets.js +30 -5
  5. package/cjs/connectors/infinite-hits/connectInfiniteHits.js +1 -1
  6. package/cjs/connectors/numeric-menu/connectNumericMenu.js +6 -4
  7. package/cjs/connectors/pagination/connectPagination.js +3 -3
  8. package/cjs/connectors/search-box/connectSearchBox.js +4 -3
  9. package/cjs/connectors/sort-by/connectSortBy.js +2 -1
  10. package/cjs/connectors/stats/connectStats.js +4 -4
  11. package/cjs/connectors/toggle-refinement/connectToggleRefinement.js +1 -1
  12. package/cjs/lib/InstantSearch.js +23 -2
  13. package/cjs/lib/version.js +1 -1
  14. package/cjs/widgets/dynamic-widgets/dynamic-widgets.js +8 -5
  15. package/cjs/widgets/index/index.js +20 -2
  16. package/dist/instantsearch.development.d.ts +23 -2
  17. package/dist/instantsearch.development.js +105 -37
  18. package/dist/instantsearch.development.js.map +1 -1
  19. package/dist/instantsearch.development.min.d.ts +23 -2
  20. package/dist/instantsearch.production.d.ts +23 -2
  21. package/dist/instantsearch.production.min.d.ts +23 -2
  22. package/dist/instantsearch.production.min.js +2 -2
  23. package/dist/instantsearch.production.min.js.map +1 -1
  24. package/es/connectors/autocomplete/connectAutocomplete.js +2 -1
  25. package/es/connectors/dynamic-widgets/connectDynamicWidgets.d.ts +14 -0
  26. package/es/connectors/dynamic-widgets/connectDynamicWidgets.js +31 -6
  27. package/es/connectors/infinite-hits/connectInfiniteHits.js +1 -1
  28. package/es/connectors/numeric-menu/connectNumericMenu.js +6 -4
  29. package/es/connectors/pagination/connectPagination.js +3 -3
  30. package/es/connectors/search-box/connectSearchBox.js +4 -3
  31. package/es/connectors/sort-by/connectSortBy.js +2 -1
  32. package/es/connectors/stats/connectStats.js +4 -4
  33. package/es/connectors/toggle-refinement/connectToggleRefinement.js +1 -1
  34. package/es/lib/InstantSearch.d.ts +3 -3
  35. package/es/lib/InstantSearch.js +23 -2
  36. package/es/lib/version.d.ts +1 -1
  37. package/es/lib/version.js +1 -1
  38. package/es/types/results.d.ts +7 -0
  39. package/es/widgets/dynamic-widgets/dynamic-widgets.js +8 -5
  40. package/es/widgets/index/index.js +20 -2
  41. package/package.json +10 -7
@@ -1,4 +1,4 @@
1
- /*! InstantSearch.js 4.33.1 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch.js */
1
+ /*! InstantSearch.js 4.36.0 | © Algolia, Inc. and contributors; MIT License | https://github.com/algolia/instantsearch.js */
2
2
  (function (global, factory) {
3
3
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
4
4
  typeof define === 'function' && define.amd ? define(factory) :
@@ -3475,7 +3475,7 @@
3475
3475
  var events = EventEmitter;
3476
3476
 
3477
3477
  // Backwards-compat with node 0.10.x
3478
- EventEmitter.EventEmitter = EventEmitter;
3478
+ // EventEmitter.EventEmitter = EventEmitter;
3479
3479
 
3480
3480
  EventEmitter.prototype._events = undefined;
3481
3481
  EventEmitter.prototype._maxListeners = undefined;
@@ -3779,7 +3779,7 @@
3779
3779
  this.lastResults = null;
3780
3780
  }
3781
3781
 
3782
- inherits_1(DerivedHelper, events.EventEmitter);
3782
+ inherits_1(DerivedHelper, events);
3783
3783
 
3784
3784
  /**
3785
3785
  * Detach this helper from the main helper
@@ -4115,7 +4115,7 @@
4115
4115
 
4116
4116
  var requestBuilder_1 = requestBuilder;
4117
4117
 
4118
- var version = '3.6.2';
4118
+ var version = '3.7.0';
4119
4119
 
4120
4120
  /**
4121
4121
  * Event triggered when a parameter is set or updated
@@ -4236,7 +4236,7 @@
4236
4236
  this._currentNbQueries = 0;
4237
4237
  }
4238
4238
 
4239
- inherits_1(AlgoliaSearchHelper, events.EventEmitter);
4239
+ inherits_1(AlgoliaSearchHelper, events);
4240
4240
 
4241
4241
  /**
4242
4242
  * Start the search with the parameters set in the state. When the
@@ -8186,7 +8186,8 @@
8186
8186
  return this;
8187
8187
  },
8188
8188
  init: function init(_ref2) {
8189
- var _this3 = this;
8189
+ var _this3 = this,
8190
+ _instantSearchInstanc;
8190
8191
 
8191
8192
  var instantSearchInstance = _ref2.instantSearchInstance,
8192
8193
  parent = _ref2.parent,
@@ -8244,11 +8245,21 @@
8244
8245
 
8245
8246
  derivedHelper = mainHelper.derive(function () {
8246
8247
  return merge$1.apply(void 0, _toConsumableArray(resolveSearchParameters(_this3)));
8247
- }); // Subscribe to the Helper state changes for the page before widgets
8248
+ });
8249
+ var indexInitialResults = (_instantSearchInstanc = instantSearchInstance._initialResults) === null || _instantSearchInstanc === void 0 ? void 0 : _instantSearchInstanc[this.getIndexId()];
8250
+
8251
+ if (indexInitialResults) {
8252
+ // We restore the shape of the results provided to the instance to respect
8253
+ // the helper's structure.
8254
+ var results = new algoliasearchHelper_1.SearchResults(new algoliasearchHelper_1.SearchParameters(indexInitialResults.state), indexInitialResults.results);
8255
+ derivedHelper.lastResults = results;
8256
+ helper.lastResults = results;
8257
+ } // Subscribe to the Helper state changes for the page before widgets
8248
8258
  // are initialized. This behavior mimics the original one of the Helper.
8249
8259
  // It makes sense to replicate it at the `init` step. We have another
8250
8260
  // listener on `change` below, once `init` is done.
8251
8261
 
8262
+
8252
8263
  helper.on('change', function (_ref3) {
8253
8264
  var isPageReset = _ref3.isPageReset;
8254
8265
 
@@ -8349,6 +8360,13 @@
8349
8360
  instantSearchInstance.onInternalStateChange();
8350
8361
  }
8351
8362
  });
8363
+
8364
+ if (indexInitialResults) {
8365
+ // If there are initial results, we're not notified of the next results
8366
+ // because we don't trigger an initial search. We therefore need to directly
8367
+ // schedule a render that will render the results injected on the helper.
8368
+ instantSearchInstance.scheduleRender();
8369
+ }
8352
8370
  },
8353
8371
  render: function render(_ref5) {
8354
8372
  var _this4 = this;
@@ -8465,7 +8483,7 @@
8465
8483
  instantSearchInstance.renderState = _objectSpread2(_objectSpread2({}, instantSearchInstance.renderState), {}, _defineProperty({}, parentIndexName, _objectSpread2(_objectSpread2({}, instantSearchInstance.renderState[parentIndexName]), renderState)));
8466
8484
  }
8467
8485
 
8468
- var version$1 = '4.33.1';
8486
+ var version$1 = '4.36.0';
8469
8487
 
8470
8488
  var NAMESPACE = 'ais';
8471
8489
  var component = function component(componentName) {
@@ -9962,6 +9980,8 @@
9962
9980
 
9963
9981
  _defineProperty(_assertThisInitialized(_this), "_initialUiState", void 0);
9964
9982
 
9983
+ _defineProperty(_assertThisInitialized(_this), "_initialResults", void 0);
9984
+
9965
9985
  _defineProperty(_assertThisInitialized(_this), "_createURL", void 0);
9966
9986
 
9967
9987
  _defineProperty(_assertThisInitialized(_this), "_searchFunction", void 0);
@@ -10066,6 +10086,7 @@
10066
10086
  _this._isSearchStalled = false;
10067
10087
  _this._createURL = defaultCreateURL;
10068
10088
  _this._initialUiState = initialUiState;
10089
+ _this._initialResults = null;
10069
10090
 
10070
10091
  if (searchFunction) {
10071
10092
  _this._searchFunction = searchFunction;
@@ -10308,9 +10329,27 @@
10308
10329
  parent: null,
10309
10330
  uiState: this._initialUiState
10310
10331
  });
10311
- this.scheduleSearch(); // Keep the previous reference for legacy purpose, some pattern use
10332
+
10333
+ if (this._initialResults) {
10334
+ var originalScheduleSearch = this.scheduleSearch; // We don't schedule a first search when initial results are provided
10335
+ // because we already have the results to render. This skips the initial
10336
+ // network request on the browser on `start`.
10337
+
10338
+ this.scheduleSearch = defer(noop); // We also skip the initial network request when widgets are dynamically
10339
+ // added in the first tick (that's the case in all the framework-based flavors).
10340
+ // When we add a widget to `index`, it calls `scheduleSearch`. We can rely
10341
+ // on our `defer` util to restore the original `scheduleSearch` value once
10342
+ // widgets are added to hook back to the regular lifecycle.
10343
+
10344
+ defer(function () {
10345
+ _this3.scheduleSearch = originalScheduleSearch;
10346
+ })();
10347
+ } else {
10348
+ this.scheduleSearch();
10349
+ } // Keep the previous reference for legacy purpose, some pattern use
10312
10350
  // the direct Helper access `search.helper` (e.g multi-index).
10313
10351
 
10352
+
10314
10353
  this.helper = this.mainIndex.getHelper(); // track we started the search if we add more widgets,
10315
10354
  // to init them directly after add
10316
10355
 
@@ -11563,7 +11602,7 @@
11563
11602
  index: helper.getIndex(),
11564
11603
  widgetType: this.$$type
11565
11604
  });
11566
- isFirstPage = helper.state.page === undefined || getFirstReceivedPage(helper.state, cachedHits) === 0;
11605
+ isFirstPage = state.page === undefined || getFirstReceivedPage(state, cachedHits) === 0;
11567
11606
  } else {
11568
11607
  var _state$page3 = state.page,
11569
11608
  _page = _state$page3 === void 0 ? 0 : _state$page3;
@@ -12068,6 +12107,8 @@
12068
12107
  if (option.start !== undefined && option.end !== undefined) {
12069
12108
  if (option.start === option.end) {
12070
12109
  return hasNumericRefinement(currentRefinements, '=', option.start);
12110
+ } else {
12111
+ return hasNumericRefinement(currentRefinements, '>=', option.start) && hasNumericRefinement(currentRefinements, '<=', option.end);
12071
12112
  }
12072
12113
  }
12073
12114
 
@@ -12121,17 +12162,17 @@
12121
12162
  if (refinedOption.start !== undefined) {
12122
12163
  if (hasNumericRefinement(currentRefinements, '>=', refinedOption.start)) {
12123
12164
  resolvedState = resolvedState.removeNumericRefinement(attribute, '>=', refinedOption.start);
12124
- } else {
12125
- resolvedState = resolvedState.addNumericRefinement(attribute, '>=', refinedOption.start);
12126
12165
  }
12166
+
12167
+ resolvedState = resolvedState.addNumericRefinement(attribute, '>=', refinedOption.start);
12127
12168
  }
12128
12169
 
12129
12170
  if (refinedOption.end !== undefined) {
12130
12171
  if (hasNumericRefinement(currentRefinements, '<=', refinedOption.end)) {
12131
12172
  resolvedState = resolvedState.removeNumericRefinement(attribute, '<=', refinedOption.end);
12132
- } else {
12133
- resolvedState = resolvedState.addNumericRefinement(attribute, '<=', refinedOption.end);
12134
12173
  }
12174
+
12175
+ resolvedState = resolvedState.addNumericRefinement(attribute, '<=', refinedOption.end);
12135
12176
  }
12136
12177
 
12137
12178
  if (typeof resolvedState.page === 'number') {
@@ -12289,6 +12330,7 @@
12289
12330
  getWidgetRenderState: function getWidgetRenderState(_ref6) {
12290
12331
  var results = _ref6.results,
12291
12332
  helper = _ref6.helper,
12333
+ state = _ref6.state,
12292
12334
  createURL = _ref6.createURL;
12293
12335
 
12294
12336
  if (!connectorState.refine) {
@@ -12299,14 +12341,13 @@
12299
12341
  }
12300
12342
 
12301
12343
  if (!connectorState.createURL) {
12302
- connectorState.createURL = function (state) {
12344
+ connectorState.createURL = function (helperState) {
12303
12345
  return function (page) {
12304
- return createURL(state.setPage(page));
12346
+ return createURL(helperState.setPage(page));
12305
12347
  };
12306
12348
  };
12307
12349
  }
12308
12350
 
12309
- var state = helper.state;
12310
12351
  var page = state.page || 0;
12311
12352
  var nbPages = getMaxPage(results || {
12312
12353
  nbPages: 0
@@ -13026,11 +13067,12 @@
13026
13067
  },
13027
13068
  getWidgetRenderState: function getWidgetRenderState(_ref3) {
13028
13069
  var helper = _ref3.helper,
13029
- searchMetadata = _ref3.searchMetadata;
13070
+ searchMetadata = _ref3.searchMetadata,
13071
+ state = _ref3.state;
13030
13072
 
13031
13073
  if (!_refine) {
13032
13074
  var setQueryAndSearch = function setQueryAndSearch(query) {
13033
- if (query !== helper.state.query) {
13075
+ if (query !== state.query) {
13034
13076
  helper.setQuery(query).search();
13035
13077
  }
13036
13078
  };
@@ -13047,7 +13089,7 @@
13047
13089
 
13048
13090
  _clear = clear(helper);
13049
13091
  return {
13050
- query: helper.state.query || '',
13092
+ query: state.query || '',
13051
13093
  refine: _refine,
13052
13094
  clear: _cachedClear,
13053
13095
  widgetParams: widgetParams,
@@ -13133,6 +13175,7 @@
13133
13175
  getWidgetRenderState: function getWidgetRenderState(_ref3) {
13134
13176
  var results = _ref3.results,
13135
13177
  helper = _ref3.helper,
13178
+ state = _ref3.state,
13136
13179
  parent = _ref3.parent;
13137
13180
 
13138
13181
  if (!connectorState.initialIndex && parent) {
@@ -13146,7 +13189,7 @@
13146
13189
  }
13147
13190
 
13148
13191
  return {
13149
- currentRefinement: helper.state.index,
13192
+ currentRefinement: state.index,
13150
13193
  options: transformItems(items),
13151
13194
  refine: connectorState.setIndex,
13152
13195
  hasNoResults: results ? results.nbHits === 0 : true,
@@ -13490,18 +13533,18 @@
13490
13533
  },
13491
13534
  getWidgetRenderState: function getWidgetRenderState(_ref) {
13492
13535
  var results = _ref.results,
13493
- helper = _ref.helper;
13536
+ state = _ref.state;
13494
13537
 
13495
13538
  if (!results) {
13496
13539
  return {
13497
- hitsPerPage: helper.state.hitsPerPage,
13540
+ hitsPerPage: state.hitsPerPage,
13498
13541
  nbHits: 0,
13499
13542
  nbSortedHits: undefined,
13500
13543
  areHitsSorted: false,
13501
13544
  nbPages: 0,
13502
- page: helper.state.page || 0,
13545
+ page: state.page || 0,
13503
13546
  processingTimeMS: -1,
13504
- query: helper.state.query || '',
13547
+ query: state.query || '',
13505
13548
  widgetParams: widgetParams
13506
13549
  };
13507
13550
  }
@@ -13694,7 +13737,7 @@
13694
13737
  createURL = _ref6.createURL,
13695
13738
  instantSearchInstance = _ref6.instantSearchInstance;
13696
13739
  var isRefined = results ? on.every(function (v) {
13697
- return helper.state.isDisjunctiveFacetRefined(attribute, v);
13740
+ return state.isDisjunctiveFacetRefined(attribute, v);
13698
13741
  }) : on.every(function (v) {
13699
13742
  return state.isDisjunctiveFacetRefined(attribute, v);
13700
13743
  });
@@ -14462,6 +14505,7 @@
14462
14505
  var _this = this;
14463
14506
 
14464
14507
  var helper = _ref4.helper,
14508
+ state = _ref4.state,
14465
14509
  scopedResults = _ref4.scopedResults,
14466
14510
  instantSearchInstance = _ref4.instantSearchInstance;
14467
14511
 
@@ -14489,7 +14533,7 @@
14489
14533
  };
14490
14534
  });
14491
14535
  return {
14492
- currentRefinement: helper.state.query || '',
14536
+ currentRefinement: state.query || '',
14493
14537
  indices: indices,
14494
14538
  refine: connectorState.refine,
14495
14539
  widgetParams: widgetParams
@@ -15177,12 +15221,17 @@
15177
15221
  name: 'dynamic-widgets',
15178
15222
  connector: true
15179
15223
  });
15224
+ var MAX_WILDCARD_FACETS = 20;
15180
15225
 
15181
15226
  var connectDynamicWidgets = function connectDynamicWidgets(renderFn) {
15182
15227
  var unmountFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : noop;
15183
15228
  checkRendering(renderFn, withUsage$r());
15184
15229
  return function (widgetParams) {
15185
15230
  var widgets = widgetParams.widgets,
15231
+ _widgetParams$maxValu = widgetParams.maxValuesPerFacet,
15232
+ maxValuesPerFacet = _widgetParams$maxValu === void 0 ? 20 : _widgetParams$maxValu,
15233
+ _widgetParams$facets = widgetParams.facets,
15234
+ facets = _widgetParams$facets === void 0 ? ['*'] : _widgetParams$facets,
15186
15235
  _widgetParams$transfo = widgetParams.transformItems,
15187
15236
  transformItems = _widgetParams$transfo === void 0 ? function (items) {
15188
15237
  return items;
@@ -15195,6 +15244,10 @@
15195
15244
  throw new Error(withUsage$r('The `widgets` option expects an array of widgets.'));
15196
15245
  }
15197
15246
 
15247
+ if (!(Array.isArray(facets) && facets.length <= 1 && (facets[0] === '*' || facets[0] === undefined))) {
15248
+ throw new Error(withUsage$r("The `facets` option only accepts [] or [\"*\"], you passed ".concat(JSON.stringify(facets))));
15249
+ }
15250
+
15198
15251
  var localWidgets = new Map();
15199
15252
  return {
15200
15253
  $$type: 'ais.dynamicWidgets',
@@ -15274,6 +15327,14 @@
15274
15327
  parent.removeWidgets(toRemove);
15275
15328
  unmountFn();
15276
15329
  },
15330
+ getWidgetSearchParameters: function getWidgetSearchParameters(state) {
15331
+ // broadening the scope of facets to avoid conflict between never and *
15332
+ return facets.reduce(function (acc, curr) {
15333
+ return acc.addFacet(curr);
15334
+ }, state.setQueryParameters({
15335
+ maxValuesPerFacet: Math.max(maxValuesPerFacet || 0, state.maxValuesPerFacet || 0)
15336
+ }));
15337
+ },
15277
15338
  getRenderState: function getRenderState(renderState, renderOptions) {
15278
15339
  return _objectSpread2(_objectSpread2({}, renderState), {}, {
15279
15340
  dynamicWidgets: this.getWidgetRenderState(renderOptions)
@@ -15282,7 +15343,8 @@
15282
15343
  getWidgetRenderState: function getWidgetRenderState(_ref4) {
15283
15344
  var _results$renderingCon, _results$renderingCon2, _results$renderingCon3, _results$renderingCon4;
15284
15345
 
15285
- var results = _ref4.results;
15346
+ var results = _ref4.results,
15347
+ state = _ref4.state;
15286
15348
 
15287
15349
  if (!results) {
15288
15350
  return {
@@ -15291,11 +15353,18 @@
15291
15353
  };
15292
15354
  }
15293
15355
 
15294
- var attributesToRender = (_results$renderingCon = (_results$renderingCon2 = results.renderingContent) === null || _results$renderingCon2 === void 0 ? void 0 : (_results$renderingCon3 = _results$renderingCon2.facetOrdering) === null || _results$renderingCon3 === void 0 ? void 0 : (_results$renderingCon4 = _results$renderingCon3.facets) === null || _results$renderingCon4 === void 0 ? void 0 : _results$renderingCon4.order) !== null && _results$renderingCon !== void 0 ? _results$renderingCon : [];
15356
+ var attributesToRender = transformItems((_results$renderingCon = (_results$renderingCon2 = results.renderingContent) === null || _results$renderingCon2 === void 0 ? void 0 : (_results$renderingCon3 = _results$renderingCon2.facetOrdering) === null || _results$renderingCon3 === void 0 ? void 0 : (_results$renderingCon4 = _results$renderingCon3.facets) === null || _results$renderingCon4 === void 0 ? void 0 : _results$renderingCon4.order) !== null && _results$renderingCon !== void 0 ? _results$renderingCon : [], {
15357
+ results: results
15358
+ });
15359
+
15360
+ if (!Array.isArray(attributesToRender)) {
15361
+ throw new Error(withUsage$r('The `transformItems` option expects a function that returns an Array.'));
15362
+ }
15363
+
15364
+ _warning(maxValuesPerFacet >= (state.maxValuesPerFacet || 0), "The maxValuesPerFacet set by dynamic widgets (".concat(maxValuesPerFacet, ") is smaller than one of the limits set by a widget (").concat(state.maxValuesPerFacet, "). This causes a mismatch in query parameters and thus an extra network request when that widget is mounted.")) ;
15365
+ _warning(attributesToRender.length <= MAX_WILDCARD_FACETS || widgetParams.facets !== undefined, "More than ".concat(MAX_WILDCARD_FACETS, " facets are requested to be displayed without explicitly setting which facets to retrieve. This could have a performance impact. Set \"facets\" to [] to do two smaller network requests, or explicitly to ['*'] to avoid this warning.")) ;
15295
15366
  return {
15296
- attributesToRender: transformItems(attributesToRender, {
15297
- results: results
15298
- }),
15367
+ attributesToRender: attributesToRender,
15299
15368
  widgetParams: widgetParams
15300
15369
  };
15301
15370
  }
@@ -16167,9 +16236,9 @@
16167
16236
  var dynamicWidgets = function dynamicWidgets(widgetParams) {
16168
16237
  var _ref = widgetParams || {},
16169
16238
  containerSelector = _ref.container,
16170
- transformItems = _ref.transformItems,
16171
16239
  widgets = _ref.widgets,
16172
- fallbackWidget = _ref.fallbackWidget;
16240
+ fallbackWidget = _ref.fallbackWidget,
16241
+ otherWidgetParams = _objectWithoutProperties(_ref, ["container", "widgets", "fallbackWidget"]);
16173
16242
 
16174
16243
  if (!containerSelector) {
16175
16244
  throw new Error(withUsage$x('The `container` option is required.'));
@@ -16204,8 +16273,7 @@
16204
16273
  }, function () {
16205
16274
  userContainer.removeChild(rootContainer);
16206
16275
  });
16207
- var widget = makeWidget({
16208
- transformItems: transformItems,
16276
+ var widget = makeWidget(_objectSpread2(_objectSpread2({}, otherWidgetParams), {}, {
16209
16277
  widgets: connectorWidgets,
16210
16278
  fallbackWidget: typeof fallbackWidget === 'function' ? function (_ref3) {
16211
16279
  var attribute = _ref3.attribute;
@@ -16216,7 +16284,7 @@
16216
16284
  container: container
16217
16285
  });
16218
16286
  } : undefined
16219
- });
16287
+ }));
16220
16288
  return _objectSpread2(_objectSpread2({}, widget), {}, {
16221
16289
  init: function init(initOptions) {
16222
16290
  widgets.forEach(function (cb) {