instantsearch.js 4.54.0 → 4.55.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 (104) hide show
  1. package/cjs/components/Hits/Hits.js +16 -7
  2. package/cjs/components/InfiniteHits/InfiniteHits.js +13 -2
  3. package/cjs/components/Pagination/Pagination.js +35 -17
  4. package/cjs/components/Template/Template.js +6 -3
  5. package/cjs/connectors/autocomplete/connectAutocomplete.js +1 -1
  6. package/cjs/connectors/geo-search/connectGeoSearch.js +1 -1
  7. package/cjs/connectors/hierarchical-menu/connectHierarchicalMenu.js +1 -1
  8. package/cjs/connectors/hits/connectHits.js +1 -1
  9. package/cjs/connectors/infinite-hits/connectInfiniteHits.js +39 -31
  10. package/cjs/connectors/menu/connectMenu.js +1 -1
  11. package/cjs/connectors/numeric-menu/connectNumericMenu.js +1 -1
  12. package/cjs/connectors/rating-menu/connectRatingMenu.js +7 -3
  13. package/cjs/connectors/refinement-list/connectRefinementList.js +1 -1
  14. package/cjs/connectors/toggle-refinement/connectToggleRefinement.js +14 -4
  15. package/cjs/helpers/highlight.js +5 -0
  16. package/cjs/helpers/insights.js +3 -4
  17. package/cjs/helpers/reverseHighlight.js +5 -0
  18. package/cjs/helpers/reverseSnippet.js +5 -0
  19. package/cjs/helpers/snippet.js +5 -0
  20. package/cjs/lib/InstantSearch.js +18 -2
  21. package/cjs/lib/insights/listener.js +43 -36
  22. package/cjs/lib/routers/history.js +1 -0
  23. package/cjs/lib/server.js +60 -0
  24. package/cjs/lib/stateMappings/simple.js +1 -0
  25. package/cjs/lib/stateMappings/singleIndex.js +1 -0
  26. package/cjs/lib/utils/createSendEventForFacet.js +12 -2
  27. package/cjs/lib/utils/createSendEventForHits.js +34 -11
  28. package/cjs/lib/utils/index.js +11 -0
  29. package/cjs/lib/utils/walkIndex.js +18 -0
  30. package/cjs/lib/version.js +1 -1
  31. package/cjs/middlewares/createInsightsMiddleware.js +135 -33
  32. package/cjs/middlewares/createMetadataMiddleware.js +17 -5
  33. package/cjs/middlewares/createRouterMiddleware.js +5 -1
  34. package/cjs/widgets/hits/hits.js +1 -2
  35. package/cjs/widgets/infinite-hits/infinite-hits.js +1 -2
  36. package/cjs/widgets/pagination/pagination.js +51 -39
  37. package/cjs/widgets/panel/panel.js +0 -2
  38. package/dist/instantsearch.development.d.ts +94 -12
  39. package/dist/instantsearch.development.js +543 -312
  40. package/dist/instantsearch.development.js.map +1 -1
  41. package/dist/instantsearch.production.d.ts +94 -12
  42. package/dist/instantsearch.production.min.d.ts +94 -12
  43. package/dist/instantsearch.production.min.js +2 -2
  44. package/dist/instantsearch.production.min.js.map +1 -1
  45. package/es/components/Hits/Hits.d.ts +5 -5
  46. package/es/components/Hits/Hits.js +15 -5
  47. package/es/components/InfiniteHits/InfiniteHits.d.ts +3 -2
  48. package/es/components/InfiniteHits/InfiniteHits.js +13 -2
  49. package/es/components/Pagination/Pagination.js +34 -17
  50. package/es/components/Panel/Panel.d.ts +1 -1
  51. package/es/components/Template/Template.js +6 -3
  52. package/es/connectors/autocomplete/connectAutocomplete.js +1 -1
  53. package/es/connectors/geo-search/connectGeoSearch.js +1 -1
  54. package/es/connectors/hierarchical-menu/connectHierarchicalMenu.js +1 -1
  55. package/es/connectors/hits/connectHits.js +1 -1
  56. package/es/connectors/infinite-hits/connectInfiniteHits.js +39 -31
  57. package/es/connectors/menu/connectMenu.js +1 -1
  58. package/es/connectors/numeric-menu/connectNumericMenu.js +1 -1
  59. package/es/connectors/rating-menu/connectRatingMenu.js +7 -3
  60. package/es/connectors/refinement-list/connectRefinementList.js +1 -1
  61. package/es/connectors/toggle-refinement/connectToggleRefinement.js +13 -3
  62. package/es/helpers/highlight.d.ts +3 -0
  63. package/es/helpers/highlight.js +5 -0
  64. package/es/helpers/insights.d.ts +2 -1
  65. package/es/helpers/insights.js +3 -3
  66. package/es/helpers/reverseHighlight.d.ts +3 -0
  67. package/es/helpers/reverseHighlight.js +5 -0
  68. package/es/helpers/reverseSnippet.d.ts +3 -0
  69. package/es/helpers/reverseSnippet.js +5 -0
  70. package/es/helpers/snippet.d.ts +3 -0
  71. package/es/helpers/snippet.js +5 -0
  72. package/es/lib/InstantSearch.d.ts +11 -1
  73. package/es/lib/InstantSearch.js +18 -2
  74. package/es/lib/insights/listener.d.ts +10 -6
  75. package/es/lib/insights/listener.js +42 -36
  76. package/es/lib/routers/history.d.ts +1 -0
  77. package/es/lib/routers/history.js +1 -0
  78. package/es/lib/server.d.ts +10 -0
  79. package/es/lib/server.js +53 -0
  80. package/es/lib/stateMappings/simple.js +1 -0
  81. package/es/lib/stateMappings/singleIndex.js +1 -0
  82. package/es/lib/utils/createSendEventForFacet.js +12 -2
  83. package/es/lib/utils/createSendEventForHits.d.ts +8 -0
  84. package/es/lib/utils/createSendEventForHits.js +33 -11
  85. package/es/lib/utils/index.d.ts +1 -0
  86. package/es/lib/utils/index.js +1 -0
  87. package/es/lib/utils/walkIndex.d.ts +5 -0
  88. package/es/lib/utils/walkIndex.js +12 -0
  89. package/es/lib/version.d.ts +1 -1
  90. package/es/lib/version.js +1 -1
  91. package/es/middlewares/createInsightsMiddleware.d.ts +12 -12
  92. package/es/middlewares/createInsightsMiddleware.js +136 -34
  93. package/es/middlewares/createMetadataMiddleware.d.ts +3 -1
  94. package/es/middlewares/createMetadataMiddleware.js +17 -5
  95. package/es/middlewares/createRouterMiddleware.js +5 -1
  96. package/es/types/insights.d.ts +19 -1
  97. package/es/types/middleware.d.ts +16 -0
  98. package/es/types/router.d.ts +8 -0
  99. package/es/widgets/hits/hits.js +2 -3
  100. package/es/widgets/infinite-hits/infinite-hits.js +2 -3
  101. package/es/widgets/pagination/pagination.d.ts +12 -5
  102. package/es/widgets/pagination/pagination.js +51 -39
  103. package/es/widgets/panel/panel.js +0 -2
  104. package/package.json +6 -6
@@ -7,4 +7,7 @@ export type ReverseHighlightOptions = {
7
7
  highlighted: string;
8
8
  }>;
9
9
  };
10
+ /**
11
+ * @deprecated use html tagged templates and the ReverseHighlight component instead
12
+ */
10
13
  export default function reverseHighlight({ attribute, highlightedTagName, hit, cssClasses, }: ReverseHighlightOptions): string;
@@ -1,6 +1,10 @@
1
1
  import { component } from "../lib/suit.js";
2
2
  import { TAG_REPLACEMENT, getPropertyByPath, getHighlightedParts, reverseHighlightedParts, concatHighlightedParts, warning } from "../lib/utils/index.js";
3
3
  var suit = component('ReverseHighlight');
4
+
5
+ /**
6
+ * @deprecated use html tagged templates and the ReverseHighlight component instead
7
+ */
4
8
  export default function reverseHighlight(_ref) {
5
9
  var attribute = _ref.attribute,
6
10
  _ref$highlightedTagNa = _ref.highlightedTagName,
@@ -8,6 +12,7 @@ export default function reverseHighlight(_ref) {
8
12
  hit = _ref.hit,
9
13
  _ref$cssClasses = _ref.cssClasses,
10
14
  cssClasses = _ref$cssClasses === void 0 ? {} : _ref$cssClasses;
15
+ process.env.NODE_ENV === 'development' ? warning(false, "`instantsearch.reverseHighlight` function has been deprecated. It is still supported in 4.x releases, but not further. It is replaced by the `ReverseHighlight` component.\n\nFor more information, visit https://www.algolia.com/doc/guides/building-search-ui/upgrade-guides/js/?client=html+tagged+templates#upgrade-templates") : void 0;
11
16
  var highlightAttributeResult = getPropertyByPath(hit._highlightResult, attribute);
12
17
 
13
18
  // @MAJOR fallback to attribute value if highlight is not found
@@ -7,4 +7,7 @@ export type ReverseSnippetOptions = {
7
7
  highlighted: string;
8
8
  }>;
9
9
  };
10
+ /**
11
+ * @deprecated use html tagged templates and the ReverseSnippet component instead
12
+ */
10
13
  export default function reverseSnippet({ attribute, highlightedTagName, hit, cssClasses, }: ReverseSnippetOptions): string;
@@ -1,6 +1,10 @@
1
1
  import { component } from "../lib/suit.js";
2
2
  import { TAG_REPLACEMENT, getPropertyByPath, getHighlightedParts, reverseHighlightedParts, concatHighlightedParts, warning } from "../lib/utils/index.js";
3
3
  var suit = component('ReverseSnippet');
4
+
5
+ /**
6
+ * @deprecated use html tagged templates and the ReverseSnippet component instead
7
+ */
4
8
  export default function reverseSnippet(_ref) {
5
9
  var attribute = _ref.attribute,
6
10
  _ref$highlightedTagNa = _ref.highlightedTagName,
@@ -8,6 +12,7 @@ export default function reverseSnippet(_ref) {
8
12
  hit = _ref.hit,
9
13
  _ref$cssClasses = _ref.cssClasses,
10
14
  cssClasses = _ref$cssClasses === void 0 ? {} : _ref$cssClasses;
15
+ process.env.NODE_ENV === 'development' ? warning(false, "`instantsearch.reverseSnippet` function has been deprecated. It is still supported in 4.x releases, but not further. It is replaced by the `ReverseSnippet` component.\n\nFor more information, visit https://www.algolia.com/doc/guides/building-search-ui/upgrade-guides/js/?client=html+tagged+templates#upgrade-templates") : void 0;
11
16
  var snippetAttributeResult = getPropertyByPath(hit._snippetResult, attribute);
12
17
 
13
18
  // @MAJOR fallback to attribute value if snippet is not found
@@ -7,4 +7,7 @@ export type SnippetOptions = {
7
7
  highlighted?: string;
8
8
  };
9
9
  };
10
+ /**
11
+ * @deprecated use html tagged templates and the Snippet component instead
12
+ */
10
13
  export default function snippet({ attribute, highlightedTagName, hit, cssClasses, }: SnippetOptions): string;
@@ -1,6 +1,10 @@
1
1
  import { component } from "../lib/suit.js";
2
2
  import { TAG_REPLACEMENT, getPropertyByPath, warning } from "../lib/utils/index.js";
3
3
  var suit = component('Snippet');
4
+
5
+ /**
6
+ * @deprecated use html tagged templates and the Snippet component instead
7
+ */
4
8
  export default function snippet(_ref) {
5
9
  var attribute = _ref.attribute,
6
10
  _ref$highlightedTagNa = _ref.highlightedTagName,
@@ -8,6 +12,7 @@ export default function snippet(_ref) {
8
12
  hit = _ref.hit,
9
13
  _ref$cssClasses = _ref.cssClasses,
10
14
  cssClasses = _ref$cssClasses === void 0 ? {} : _ref$cssClasses;
15
+ process.env.NODE_ENV === 'development' ? warning(false, "`instantsearch.snippet` function has been deprecated. It is still supported in 4.x releases, but not further. It is replaced by the `Snippet` component.\n\nFor more information, visit https://www.algolia.com/doc/guides/building-search-ui/upgrade-guides/js/?client=html+tagged+templates#upgrade-templates") : void 0;
11
16
  var snippetAttributeResult = getPropertyByPath(hit._snippetResult, attribute);
12
17
 
13
18
  // @MAJOR fallback to attribute value if snippet is not found
@@ -1,5 +1,5 @@
1
1
  import EventEmitter from '@algolia/events';
2
- import type { InsightsEvent } from '../middlewares/createInsightsMiddleware';
2
+ import type { InsightsEvent, InsightsProps } from '../middlewares/createInsightsMiddleware';
3
3
  import type { RouterProps } from '../middlewares/createRouterMiddleware';
4
4
  import type { InsightsClient as AlgoliaInsightsClient, SearchClient, Widget, UiState, CreateURL, Middleware, MiddlewareDefinition, RenderState, InitialResults } from '../types';
5
5
  import type { IndexWidget } from '../widgets/index/index';
@@ -79,6 +79,16 @@ export type InstantSearchOptions<TUiState extends UiState = UiState, TRouteState
79
79
  * client side persistence. Passing `true` will use the default URL options.
80
80
  */
81
81
  routing?: RouterProps<TUiState, TRouteState> | boolean;
82
+ /**
83
+ * Enables the Insights middleware and loads the Insights library
84
+ * if not already loaded.
85
+ *
86
+ * The Insights middleware sends view and click events automatically, and lets
87
+ * you set up your own events.
88
+ *
89
+ * @default false
90
+ */
91
+ insights?: InsightsProps | boolean;
82
92
  /**
83
93
  * the instance of search-insights to use for sending insights events inside
84
94
  * widgets like `hits`.
@@ -16,6 +16,7 @@ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _ty
16
16
  function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
17
17
  import EventEmitter from '@algolia/events';
18
18
  import algoliasearchHelper from 'algoliasearch-helper';
19
+ import { createInsightsMiddleware } from "../middlewares/createInsightsMiddleware.js";
19
20
  import { createMetadataMiddleware, isMetadataEnabled } from "../middlewares/createMetadataMiddleware.js";
20
21
  import { createRouterMiddleware } from "../middlewares/createRouterMiddleware.js";
21
22
  import index from "../widgets/index/index.js";
@@ -105,6 +106,8 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
105
106
  initialUiState = _options$initialUiSta === void 0 ? {} : _options$initialUiSta,
106
107
  _options$routing = options.routing,
107
108
  routing = _options$routing === void 0 ? null : _options$routing,
109
+ _options$insights = options.insights,
110
+ insights = _options$insights === void 0 ? false : _options$insights,
108
111
  searchFunction = options.searchFunction,
109
112
  _options$stalledSearc = options.stalledSearchDelay,
110
113
  stalledSearchDelay = _options$stalledSearc === void 0 ? 200 : _options$stalledSearc,
@@ -160,11 +163,22 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
160
163
  }
161
164
  _this.sendEventToInsights = noop;
162
165
  if (routing) {
163
- var routerOptions = typeof routing === 'boolean' ? undefined : routing;
166
+ var routerOptions = typeof routing === 'boolean' ? {} : routing;
167
+ routerOptions.$$internal = true;
164
168
  _this.use(createRouterMiddleware(routerOptions));
165
169
  }
170
+
171
+ // This is the default middleware,
172
+ // any user-provided middleware will be added later and override this one.
173
+ if (insights) {
174
+ var insightsOptions = typeof insights === 'boolean' ? {} : insights;
175
+ insightsOptions.$$internal = true;
176
+ _this.use(createInsightsMiddleware(insightsOptions));
177
+ }
166
178
  if (isMetadataEnabled()) {
167
- _this.use(createMetadataMiddleware());
179
+ _this.use(createMetadataMiddleware({
180
+ $$internal: true
181
+ }));
168
182
  }
169
183
  return _this;
170
184
  }
@@ -200,6 +214,8 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
200
214
  }
201
215
  var newMiddlewareList = middleware.map(function (fn) {
202
216
  var newMiddleware = _objectSpread({
217
+ $$type: '__unknown__',
218
+ $$internal: false,
203
219
  subscribe: noop,
204
220
  started: noop,
205
221
  unsubscribe: noop,
@@ -2,10 +2,14 @@
2
2
  import { h } from 'preact';
3
3
  import type { InsightsEvent } from '../../middlewares/createInsightsMiddleware';
4
4
  import type { InsightsClient } from '../../types';
5
- type WithInsightsListenerProps = {
6
- [key: string]: unknown;
7
- insights: InsightsClient;
8
- sendEvent?: (event: InsightsEvent) => void;
5
+ export type InsightsEventHandlerOptions = {
6
+ insights?: InsightsClient;
7
+ sendEvent: (event: InsightsEvent) => void;
9
8
  };
10
- declare const insightsListener: (BaseComponent: any) => (props: WithInsightsListenerProps) => h.JSX.Element;
11
- export default insightsListener;
9
+ export declare const createInsightsEventHandler: ({ insights, sendEvent }: InsightsEventHandlerOptions) => (event: MouseEvent) => void;
10
+ /**
11
+ * @deprecated use `sendEvent` directly instead
12
+ */
13
+ export default function withInsightsListener(BaseComponent: any): (props: {
14
+ [key: string]: any;
15
+ } & InsightsEventHandlerOptions) => h.JSX.Element;
@@ -1,7 +1,34 @@
1
1
  import { h } from 'preact';
2
- import { readDataAttributes, hasDataAttributes } from "../../helpers/insights.js";
3
- import { deserializePayload } from "../utils/index.js";
4
- var findInsightsTarget = function findInsightsTarget(startElement, endElement, validator) {
2
+ import { readDataAttributes } from "../../helpers/insights.js";
3
+ import { deserializePayload, warning } from "../utils/index.js";
4
+ export var createInsightsEventHandler = function createInsightsEventHandler(_ref) {
5
+ var insights = _ref.insights,
6
+ sendEvent = _ref.sendEvent;
7
+ return function (event) {
8
+ // new way, e.g. bindEvent("click", hit, "Hit clicked")
9
+ var insightsThroughSendEvent = findInsightsTarget(event.target, event.currentTarget, function (element) {
10
+ return element.hasAttribute('data-insights-event');
11
+ });
12
+ if (insightsThroughSendEvent) {
13
+ var payload = parseInsightsEvent(insightsThroughSendEvent);
14
+ payload.forEach(function (single) {
15
+ return sendEvent(single);
16
+ });
17
+ }
18
+
19
+ // old way, e.g. instantsearch.insights("clickedObjectIDsAfterSearch", { .. })
20
+ var insightsThroughFunction = findInsightsTarget(event.target, event.currentTarget, function (element) {
21
+ return element.hasAttribute('data-insights-method') && element.hasAttribute('data-insights-payload');
22
+ });
23
+ if (insightsThroughFunction) {
24
+ var _readDataAttributes = readDataAttributes(insightsThroughFunction),
25
+ method = _readDataAttributes.method,
26
+ _payload = _readDataAttributes.payload;
27
+ insights(method, _payload);
28
+ }
29
+ };
30
+ };
31
+ function findInsightsTarget(startElement, endElement, validator) {
5
32
  var element = startElement;
6
33
  while (element && !validator(element)) {
7
34
  if (element === endElement) {
@@ -10,8 +37,8 @@ var findInsightsTarget = function findInsightsTarget(startElement, endElement, v
10
37
  element = element.parentElement;
11
38
  }
12
39
  return element;
13
- };
14
- var parseInsightsEvent = function parseInsightsEvent(element) {
40
+ }
41
+ function parseInsightsEvent(element) {
15
42
  var serializedPayload = element.getAttribute('data-insights-event');
16
43
  if (typeof serializedPayload !== 'string') {
17
44
  throw new Error('The insights middleware expects `data-insights-event` to be a base64-encoded JSON string.');
@@ -21,38 +48,17 @@ var parseInsightsEvent = function parseInsightsEvent(element) {
21
48
  } catch (error) {
22
49
  throw new Error('The insights middleware was unable to parse `data-insights-event`.');
23
50
  }
24
- };
25
- var insightsListener = function insightsListener(BaseComponent) {
26
- function WithInsightsListener(props) {
27
- var handleClick = function handleClick(event) {
28
- if (props.sendEvent) {
29
- // new way with insights middleware
30
- var targetWithEvent = findInsightsTarget(event.target, event.currentTarget, function (element) {
31
- return element.hasAttribute('data-insights-event');
32
- });
33
- if (targetWithEvent) {
34
- var payload = parseInsightsEvent(targetWithEvent);
35
- payload.forEach(function (single) {
36
- return props.sendEvent(single);
37
- });
38
- }
39
- }
51
+ }
40
52
 
41
- // old way, e.g. instantsearch.insights("clickedObjectIDsAfterSearch", { .. })
42
- var insightsTarget = findInsightsTarget(event.target, event.currentTarget, function (element) {
43
- return hasDataAttributes(element);
44
- });
45
- if (insightsTarget) {
46
- var _readDataAttributes = readDataAttributes(insightsTarget),
47
- method = _readDataAttributes.method,
48
- _payload = _readDataAttributes.payload;
49
- props.insights(method, _payload);
50
- }
51
- };
53
+ /**
54
+ * @deprecated use `sendEvent` directly instead
55
+ */
56
+ export default function withInsightsListener(BaseComponent) {
57
+ process.env.NODE_ENV === 'development' ? warning(false, 'The `withInsightsListener` function is deprecated and will be removed in the next major version. Please use `sendEvent` directly instead.') : void 0;
58
+ return function WithInsightsListener(props) {
59
+ var handleClick = createInsightsEventHandler(props);
52
60
  return h("div", {
53
61
  onClick: handleClick
54
62
  }, h(BaseComponent, props));
55
- }
56
- return WithInsightsListener;
57
- };
58
- export default insightsListener;
63
+ };
64
+ }
@@ -20,6 +20,7 @@ export type BrowserHistoryArgs<TRouteState> = {
20
20
  push?: (url: string) => void;
21
21
  };
22
22
  declare class BrowserHistory<TRouteState> implements Router<TRouteState> {
23
+ $$type: string;
23
24
  /**
24
25
  * Transforms a UI state into a title for the page.
25
26
  */
@@ -31,6 +31,7 @@ var BrowserHistory = /*#__PURE__*/function () {
31
31
  dispose = _ref.dispose,
32
32
  push = _ref.push;
33
33
  _classCallCheck(this, BrowserHistory);
34
+ _defineProperty(this, "$$type", 'ais.browser');
34
35
  _defineProperty(this, "windowTitle", void 0);
35
36
  _defineProperty(this, "writeDelay", void 0);
36
37
  _defineProperty(this, "_createURL", void 0);
@@ -0,0 +1,10 @@
1
+ import type { IndexWidget, InitialResults, InstantSearch } from '../types';
2
+ /**
3
+ * Waits for the results from the search instance to coordinate the next steps
4
+ * in `getServerState()`.
5
+ */
6
+ export declare function waitForResults(search: InstantSearch): Promise<void>;
7
+ /**
8
+ * Walks the InstantSearch root index to construct the initial results.
9
+ */
10
+ export declare function getInitialResults(rootIndex: IndexWidget): InitialResults;
@@ -0,0 +1,53 @@
1
+ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
6
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
7
+ import { walkIndex } from "./utils/index.js";
8
+ /**
9
+ * Waits for the results from the search instance to coordinate the next steps
10
+ * in `getServerState()`.
11
+ */
12
+ export function waitForResults(search) {
13
+ var helper = search.mainHelper;
14
+ helper.searchOnlyWithDerivedHelpers();
15
+ return new Promise(function (resolve, reject) {
16
+ // All derived helpers resolve in the same tick so we're safe only relying
17
+ // on the first one.
18
+ helper.derivedHelpers[0].on('result', function () {
19
+ resolve();
20
+ });
21
+
22
+ // However, we listen to errors that can happen on any derived helper because
23
+ // any error is critical.
24
+ helper.on('error', function (error) {
25
+ reject(error);
26
+ });
27
+ search.on('error', function (error) {
28
+ reject(error);
29
+ });
30
+ helper.derivedHelpers.forEach(function (derivedHelper) {
31
+ return derivedHelper.on('error', function (error) {
32
+ reject(error);
33
+ });
34
+ });
35
+ });
36
+ }
37
+
38
+ /**
39
+ * Walks the InstantSearch root index to construct the initial results.
40
+ */
41
+ export function getInitialResults(rootIndex) {
42
+ var initialResults = {};
43
+ walkIndex(rootIndex, function (widget) {
44
+ var searchResults = widget.getResults();
45
+ initialResults[widget.getIndexId()] = {
46
+ // We convert the Helper state to a plain object to pass parsable data
47
+ // structures from server to client.
48
+ state: _objectSpread({}, searchResults._state),
49
+ results: searchResults._rawResults
50
+ };
51
+ });
52
+ return initialResults;
53
+ }
@@ -18,6 +18,7 @@ function getIndexStateWithoutConfigure(uiState) {
18
18
  // which excludes "configure" as this function does.
19
19
  export default function simpleStateMapping() {
20
20
  return {
21
+ $$type: 'ais.simple',
21
22
  stateToRoute: function stateToRoute(uiState) {
22
23
  return Object.keys(uiState).reduce(function (state, indexId) {
23
24
  return _objectSpread(_objectSpread({}, state), {}, _defineProperty({}, indexId, getIndexStateWithoutConfigure(uiState[indexId])));
@@ -12,6 +12,7 @@ function getIndexStateWithoutConfigure(uiState) {
12
12
  }
13
13
  export default function singleIndexStateMapping(indexName) {
14
14
  return {
15
+ $$type: 'ais.singleIndex',
15
16
  stateToRoute: function stateToRoute(uiState) {
16
17
  return getIndexStateWithoutConfigure(uiState[indexName] || {});
17
18
  },
@@ -1,4 +1,10 @@
1
1
  function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
3
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
5
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
6
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
7
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
2
8
  import { isFacetRefined } from "./isFacetRefined.js";
3
9
  export function createSendEventForFacet(_ref) {
4
10
  var instantSearchInstance = _ref.instantSearchInstance,
@@ -9,10 +15,13 @@ export function createSendEventForFacet(_ref) {
9
15
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
10
16
  args[_key] = arguments[_key];
11
17
  }
12
- var eventType = args[0],
13
- facetValue = args[1],
18
+ var facetValue = args[1],
14
19
  _args$ = args[2],
15
20
  eventName = _args$ === void 0 ? 'Filter Applied' : _args$;
21
+ var _args$0$split = args[0].split(':'),
22
+ _args$0$split2 = _slicedToArray(_args$0$split, 2),
23
+ eventType = _args$0$split2[0],
24
+ eventModifier = _args$0$split2[1];
16
25
  var attribute = typeof attr === 'string' ? attr : attr(facetValue);
17
26
  if (args.length === 1 && _typeof(args[0]) === 'object') {
18
27
  instantSearchInstance.sendEventToInsights(args[0]);
@@ -23,6 +32,7 @@ export function createSendEventForFacet(_ref) {
23
32
  insightsMethod: 'clickedFilters',
24
33
  widgetType: widgetType,
25
34
  eventType: eventType,
35
+ eventModifier: eventModifier,
26
36
  payload: {
27
37
  eventName: eventName,
28
38
  index: helper.getIndex(),
@@ -1,3 +1,4 @@
1
+ import type { InsightsEvent } from '../../middlewares/createInsightsMiddleware';
1
2
  import type { InstantSearch, Hit } from '../../types';
2
3
  type BuiltInSendEventForHits = (eventType: string, hits: Hit | Hit[], eventName?: string) => void;
3
4
  type CustomSendEventForHits = (customPayload: any) => void;
@@ -5,6 +6,13 @@ export type SendEventForHits = BuiltInSendEventForHits & CustomSendEventForHits;
5
6
  export type BuiltInBindEventForHits = (eventType: string, hits: Hit | Hit[], eventName?: string) => string;
6
7
  export type CustomBindEventForHits = (customPayload: any) => string;
7
8
  export type BindEventForHits = BuiltInBindEventForHits & CustomBindEventForHits;
9
+ export declare function _buildEventPayloadsForHits({ index, widgetType, methodName, args, instantSearchInstance, }: {
10
+ widgetType: string;
11
+ index: string;
12
+ methodName: 'sendEvent' | 'bindEvent';
13
+ args: any[];
14
+ instantSearchInstance: InstantSearch;
15
+ }): InsightsEvent[];
8
16
  export declare function createSendEventForHits({ instantSearchInstance, index, widgetType, }: {
9
17
  instantSearchInstance: InstantSearch;
10
18
  index: string;
@@ -1,3 +1,9 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
1
7
  function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
2
8
  import { serializePayload } from "./serializer.js";
3
9
  function chunk(arr) {
@@ -8,7 +14,7 @@ function chunk(arr) {
8
14
  }
9
15
  return chunks;
10
16
  }
11
- var buildPayloads = function buildPayloads(_ref) {
17
+ export function _buildEventPayloadsForHits(_ref) {
12
18
  var index = _ref.index,
13
19
  widgetType = _ref.widgetType,
14
20
  methodName = _ref.methodName,
@@ -18,7 +24,10 @@ var buildPayloads = function buildPayloads(_ref) {
18
24
  if (args.length === 1 && _typeof(args[0]) === 'object') {
19
25
  return [args[0]];
20
26
  }
21
- var eventType = args[0];
27
+ var _args$0$split = args[0].split(':'),
28
+ _args$0$split2 = _slicedToArray(_args$0$split, 2),
29
+ eventType = _args$0$split2[0],
30
+ eventModifier = _args$0$split2[1];
22
31
  var hits = args[1];
23
32
  var eventName = args[2];
24
33
  if (!hits) {
@@ -65,7 +74,8 @@ var buildPayloads = function buildPayloads(_ref) {
65
74
  index: index,
66
75
  objectIDs: objectIDsByChunk[i]
67
76
  },
68
- hits: batch
77
+ hits: batch,
78
+ eventModifier: eventModifier
69
79
  };
70
80
  });
71
81
  } else if (eventType === 'click') {
@@ -75,13 +85,14 @@ var buildPayloads = function buildPayloads(_ref) {
75
85
  widgetType: widgetType,
76
86
  eventType: eventType,
77
87
  payload: {
78
- eventName: eventName,
88
+ eventName: eventName || 'Hit Clicked',
79
89
  index: index,
80
90
  queryID: queryID,
81
91
  objectIDs: objectIDsByChunk[i],
82
92
  positions: positionsByChunk[i]
83
93
  },
84
- hits: batch
94
+ hits: batch,
95
+ eventModifier: eventModifier
85
96
  };
86
97
  });
87
98
  } else if (eventType === 'conversion') {
@@ -91,12 +102,13 @@ var buildPayloads = function buildPayloads(_ref) {
91
102
  widgetType: widgetType,
92
103
  eventType: eventType,
93
104
  payload: {
94
- eventName: eventName,
105
+ eventName: eventName || 'Hit Converted',
95
106
  index: index,
96
107
  queryID: queryID,
97
108
  objectIDs: objectIDsByChunk[i]
98
109
  },
99
- hits: batch
110
+ hits: batch,
111
+ eventModifier: eventModifier
100
112
  };
101
113
  });
102
114
  } else if (process.env.NODE_ENV === 'development') {
@@ -104,16 +116,18 @@ var buildPayloads = function buildPayloads(_ref) {
104
116
  } else {
105
117
  return [];
106
118
  }
107
- };
119
+ }
108
120
  export function createSendEventForHits(_ref2) {
109
121
  var instantSearchInstance = _ref2.instantSearchInstance,
110
122
  index = _ref2.index,
111
123
  widgetType = _ref2.widgetType;
124
+ var sentEvents = {};
125
+ var timer = undefined;
112
126
  var sendEventForHits = function sendEventForHits() {
113
127
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
114
128
  args[_key] = arguments[_key];
115
129
  }
116
- var payloads = buildPayloads({
130
+ var payloads = _buildEventPayloadsForHits({
117
131
  widgetType: widgetType,
118
132
  index: index,
119
133
  methodName: 'sendEvent',
@@ -121,8 +135,16 @@ export function createSendEventForHits(_ref2) {
121
135
  instantSearchInstance: instantSearchInstance
122
136
  });
123
137
  payloads.forEach(function (payload) {
124
- return instantSearchInstance.sendEventToInsights(payload);
138
+ if (payload.eventType === 'click' && payload.eventModifier === 'internal' && sentEvents[payload.eventType]) {
139
+ return;
140
+ }
141
+ sentEvents[payload.eventType] = true;
142
+ instantSearchInstance.sendEventToInsights(payload);
125
143
  });
144
+ clearTimeout(timer);
145
+ timer = setTimeout(function () {
146
+ sentEvents = {};
147
+ }, 0);
126
148
  };
127
149
  return sendEventForHits;
128
150
  }
@@ -134,7 +156,7 @@ export function createBindEventForHits(_ref3) {
134
156
  for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
135
157
  args[_key2] = arguments[_key2];
136
158
  }
137
- var payloads = buildPayloads({
159
+ var payloads = _buildEventPayloadsForHits({
138
160
  widgetType: widgetType,
139
161
  index: index,
140
162
  methodName: 'bindEvent',
@@ -33,6 +33,7 @@ export * from './isFacetRefined';
33
33
  export * from './isFiniteNumber';
34
34
  export * from './isPlainObject';
35
35
  export * from './isSpecialClick';
36
+ export * from './walkIndex';
36
37
  export * from './logger';
37
38
  export * from './mergeSearchParameters';
38
39
  export * from './omit';
@@ -33,6 +33,7 @@ export * from "./isFacetRefined.js";
33
33
  export * from "./isFiniteNumber.js";
34
34
  export * from "./isPlainObject.js";
35
35
  export * from "./isSpecialClick.js";
36
+ export * from "./walkIndex.js";
36
37
  export * from "./logger.js";
37
38
  export * from "./mergeSearchParameters.js";
38
39
  export * from "./omit.js";
@@ -0,0 +1,5 @@
1
+ import type { IndexWidget } from '../../types';
2
+ /**
3
+ * Recurse over all child indices
4
+ */
5
+ export declare function walkIndex(indexWidget: IndexWidget, callback: (widget: IndexWidget) => void): void;
@@ -0,0 +1,12 @@
1
+ import { isIndexWidget } from "./isIndexWidget.js";
2
+ /**
3
+ * Recurse over all child indices
4
+ */
5
+ export function walkIndex(indexWidget, callback) {
6
+ callback(indexWidget);
7
+ indexWidget.getWidgets().forEach(function (widget) {
8
+ if (isIndexWidget(widget)) {
9
+ walkIndex(widget, callback);
10
+ }
11
+ });
12
+ }
@@ -1,2 +1,2 @@
1
- declare const _default: "4.54.0";
1
+ declare const _default: "4.55.0";
2
2
  export default _default;
package/es/lib/version.js CHANGED
@@ -1 +1 @@
1
- export default '4.54.0';
1
+ export default '4.55.0';
@@ -1,21 +1,21 @@
1
- import type { InsightsClient, InsightsClientMethod, InternalMiddleware, Hit } from '../types';
2
- export type InsightsEvent = {
3
- insightsMethod?: InsightsClientMethod;
4
- payload: any;
5
- widgetType: string;
6
- eventType: string;
7
- hits?: Hit[];
8
- attribute?: string;
9
- };
10
- export type InsightsProps<TInsightsClient extends null | InsightsClient = InsightsClient | null> = {
11
- insightsClient: TInsightsClient;
1
+ import type { InsightsClient, InsightsEvent as _InsightsEvent, InsightsMethod, InternalMiddleware } from '../types';
2
+ type ProvidedInsightsClient = InsightsClient | null | undefined;
3
+ export type InsightsEvent<TMethod extends InsightsMethod = InsightsMethod> = _InsightsEvent<TMethod>;
4
+ export type InsightsProps<TInsightsClient extends ProvidedInsightsClient = ProvidedInsightsClient> = {
5
+ insightsClient?: TInsightsClient;
12
6
  insightsInitParams?: {
13
7
  userHasOptedOut?: boolean;
14
8
  useCookie?: boolean;
9
+ anonymousUserToken?: boolean;
15
10
  cookieDuration?: number;
16
11
  region?: 'de' | 'us';
17
12
  };
18
13
  onEvent?: (event: InsightsEvent, insightsClient: TInsightsClient) => void;
19
14
  };
15
+ export type InsightsClientWithGlobals = InsightsClient & {
16
+ shouldAddScript?: boolean;
17
+ version?: string;
18
+ };
20
19
  export type CreateInsightsMiddleware = typeof createInsightsMiddleware;
21
- export declare function createInsightsMiddleware<TInsightsClient extends null | InsightsClient>(props: InsightsProps<TInsightsClient>): InternalMiddleware;
20
+ export declare function createInsightsMiddleware<TInsightsClient extends ProvidedInsightsClient>(props?: InsightsProps<TInsightsClient>): InternalMiddleware;
21
+ export {};