instantsearch.js 4.58.0 → 4.60.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.
- package/README.md +1 -1
- package/cjs/components/Pagination/Pagination.js +4 -4
- package/cjs/components/RefinementList/RefinementList.js +2 -1
- package/cjs/components/SearchBox/SearchBox.js +5 -2
- package/cjs/components/Selector/Selector.js +4 -2
- package/cjs/components/Slider/Slider.js +3 -1
- package/cjs/lib/InstantSearch.js +27 -5
- package/cjs/lib/utils/createSendEventForFacet.js +12 -5
- package/cjs/lib/utils/createSendEventForHits.js +12 -6
- package/cjs/lib/utils/hydrateSearchClient.js +116 -0
- package/cjs/lib/utils/index.js +11 -0
- package/cjs/lib/version.js +1 -1
- package/cjs/middlewares/createInsightsMiddleware.js +15 -4
- package/cjs/widgets/sort-by/sort-by.js +2 -1
- package/dist/instantsearch.development.d.ts +4 -3
- package/dist/instantsearch.development.js +184 -34
- package/dist/instantsearch.development.js.map +1 -1
- package/dist/instantsearch.production.d.ts +4 -3
- package/dist/instantsearch.production.min.d.ts +4 -3
- package/dist/instantsearch.production.min.js +2 -2
- package/dist/instantsearch.production.min.js.map +1 -1
- package/es/components/Pagination/Pagination.js +4 -4
- package/es/components/RefinementList/RefinementList.js +2 -1
- package/es/components/SearchBox/SearchBox.d.ts +3 -0
- package/es/components/SearchBox/SearchBox.js +5 -2
- package/es/components/Selector/Selector.d.ts +2 -1
- package/es/components/Selector/Selector.js +4 -2
- package/es/components/Slider/Slider.js +3 -1
- package/es/lib/InstantSearch.d.ts +1 -0
- package/es/lib/InstantSearch.js +28 -6
- package/es/lib/utils/createSendEventForFacet.d.ts +1 -1
- package/es/lib/utils/createSendEventForFacet.js +12 -5
- package/es/lib/utils/createSendEventForHits.d.ts +2 -2
- package/es/lib/utils/createSendEventForHits.js +12 -6
- package/es/lib/utils/hydrateSearchClient.d.ts +2 -0
- package/es/lib/utils/hydrateSearchClient.js +110 -0
- package/es/lib/utils/index.d.ts +1 -0
- package/es/lib/utils/index.js +1 -0
- package/es/lib/version.d.ts +1 -1
- package/es/lib/version.js +1 -1
- package/es/middlewares/createInsightsMiddleware.js +15 -4
- package/es/widgets/sort-by/sort-by.js +2 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<a href="https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/">
|
|
3
|
-
<img alt="InstantSearch.js" src=".github/banner.png">
|
|
3
|
+
<img alt="InstantSearch.js" src="https://github.com/algolia/instantsearch/blob/master/.github/banner.png?raw=true">
|
|
4
4
|
</a>
|
|
5
5
|
|
|
6
6
|
<p align="center">
|
|
@@ -26,7 +26,7 @@ function Pagination(props) {
|
|
|
26
26
|
}, (0, _preact.h)("ul", {
|
|
27
27
|
className: props.cssClasses.list
|
|
28
28
|
}, props.showFirst && (0, _preact.h)(PaginationLink, {
|
|
29
|
-
ariaLabel: "First",
|
|
29
|
+
ariaLabel: "First Page",
|
|
30
30
|
className: props.cssClasses.firstPageItem,
|
|
31
31
|
isDisabled: props.isFirstPage,
|
|
32
32
|
templates: props.templates,
|
|
@@ -36,7 +36,7 @@ function Pagination(props) {
|
|
|
36
36
|
cssClasses: props.cssClasses,
|
|
37
37
|
createClickHandler: createClickHandler
|
|
38
38
|
}), props.showPrevious && (0, _preact.h)(PaginationLink, {
|
|
39
|
-
ariaLabel: "Previous",
|
|
39
|
+
ariaLabel: "Previous Page",
|
|
40
40
|
className: props.cssClasses.previousPageItem,
|
|
41
41
|
isDisabled: props.isFirstPage,
|
|
42
42
|
templates: props.templates,
|
|
@@ -59,7 +59,7 @@ function Pagination(props) {
|
|
|
59
59
|
createClickHandler: createClickHandler
|
|
60
60
|
});
|
|
61
61
|
}), props.showNext && (0, _preact.h)(PaginationLink, {
|
|
62
|
-
ariaLabel: "Next",
|
|
62
|
+
ariaLabel: "Next Page",
|
|
63
63
|
className: props.cssClasses.nextPageItem,
|
|
64
64
|
isDisabled: props.isLastPage,
|
|
65
65
|
templates: props.templates,
|
|
@@ -69,7 +69,7 @@ function Pagination(props) {
|
|
|
69
69
|
cssClasses: props.cssClasses,
|
|
70
70
|
createClickHandler: createClickHandler
|
|
71
71
|
}), props.showLast && (0, _preact.h)(PaginationLink, {
|
|
72
|
-
ariaLabel: "Last",
|
|
72
|
+
ariaLabel: "Last Page, Page ".concat(props.nbPages),
|
|
73
73
|
className: props.cssClasses.lastPageItem,
|
|
74
74
|
isDisabled: props.isLastPage,
|
|
75
75
|
templates: props.templates,
|
|
@@ -209,7 +209,8 @@ var RefinementList = /*#__PURE__*/function (_Component) {
|
|
|
209
209
|
// This sets the search box to a controlled state because
|
|
210
210
|
// we don't rely on the `refine` prop but on `onChange`.
|
|
211
211
|
,
|
|
212
|
-
searchAsYouType: false
|
|
212
|
+
searchAsYouType: false,
|
|
213
|
+
ariaLabel: "Search for filters"
|
|
213
214
|
}));
|
|
214
215
|
var facetValues = this.props.facetValues && this.props.facetValues.length > 0 && (0, _preact.h)("ul", {
|
|
215
216
|
className: this.props.cssClasses.list
|
|
@@ -31,6 +31,7 @@ var defaultProps = {
|
|
|
31
31
|
searchAsYouType: true,
|
|
32
32
|
isSearchStalled: false,
|
|
33
33
|
disabled: false,
|
|
34
|
+
ariaLabel: 'Search',
|
|
34
35
|
onChange: _utils.noop,
|
|
35
36
|
onSubmit: _utils.noop,
|
|
36
37
|
onReset: _utils.noop,
|
|
@@ -147,7 +148,8 @@ var SearchBox = /*#__PURE__*/function (_Component) {
|
|
|
147
148
|
showReset = _this$props4.showReset,
|
|
148
149
|
showLoadingIndicator = _this$props4.showLoadingIndicator,
|
|
149
150
|
templates = _this$props4.templates,
|
|
150
|
-
isSearchStalled = _this$props4.isSearchStalled
|
|
151
|
+
isSearchStalled = _this$props4.isSearchStalled,
|
|
152
|
+
ariaLabel = _this$props4.ariaLabel;
|
|
151
153
|
return (0, _preact.h)("div", {
|
|
152
154
|
className: cssClasses.root
|
|
153
155
|
}, (0, _preact.h)("form", {
|
|
@@ -174,7 +176,8 @@ var SearchBox = /*#__PURE__*/function (_Component) {
|
|
|
174
176
|
maxLength: 512,
|
|
175
177
|
onInput: this.onInput,
|
|
176
178
|
onBlur: this.onBlur,
|
|
177
|
-
onFocus: this.onFocus
|
|
179
|
+
onFocus: this.onFocus,
|
|
180
|
+
"aria-label": ariaLabel
|
|
178
181
|
}), (0, _preact.h)(_Template.default, {
|
|
179
182
|
templateKey: "submit",
|
|
180
183
|
rootTagName: "button",
|
|
@@ -10,13 +10,15 @@ function Selector(_ref) {
|
|
|
10
10
|
var currentValue = _ref.currentValue,
|
|
11
11
|
options = _ref.options,
|
|
12
12
|
cssClasses = _ref.cssClasses,
|
|
13
|
-
setValue = _ref.setValue
|
|
13
|
+
setValue = _ref.setValue,
|
|
14
|
+
ariaLabel = _ref.ariaLabel;
|
|
14
15
|
return (0, _preact.h)("select", {
|
|
15
16
|
className: (0, _uiComponentsShared.cx)(cssClasses.select),
|
|
16
17
|
onChange: function onChange(event) {
|
|
17
18
|
return setValue(event.target.value);
|
|
18
19
|
},
|
|
19
|
-
value: "".concat(currentValue)
|
|
20
|
+
value: "".concat(currentValue),
|
|
21
|
+
"aria-label": ariaLabel
|
|
20
22
|
}, options.map(function (option) {
|
|
21
23
|
return (0, _preact.h)("option", {
|
|
22
24
|
className: (0, _uiComponentsShared.cx)(cssClasses.option),
|
|
@@ -56,8 +56,10 @@ var Slider = /*#__PURE__*/function (_Component) {
|
|
|
56
56
|
parseFloat(props['aria-valuenow']) * 100) / 100;
|
|
57
57
|
var value = _typeof(tooltips) === 'object' && tooltips.format ? tooltips.format(roundedValue) : roundedValue;
|
|
58
58
|
var className = (0, _uiComponentsShared.cx)(props.className, props['data-handle-key'] === 0 && 'rheostat-handle-lower', props['data-handle-key'] === 1 && 'rheostat-handle-upper');
|
|
59
|
+
var ariaLabel = props['data-handle-key'] === 0 ? 'Minimum Filter Handle' : 'Maximum Filter Handle';
|
|
59
60
|
return (0, _preact.h)("div", _extends({}, props, {
|
|
60
|
-
className: className
|
|
61
|
+
className: className,
|
|
62
|
+
"aria-label": ariaLabel
|
|
61
63
|
}), tooltips && (0, _preact.h)("div", {
|
|
62
64
|
className: "rheostat-tooltip"
|
|
63
65
|
}, value));
|
package/cjs/lib/InstantSearch.js
CHANGED
|
@@ -81,6 +81,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
81
81
|
_defineProperty(_assertThisInitialized(_this), "_createURL", void 0);
|
|
82
82
|
_defineProperty(_assertThisInitialized(_this), "_searchFunction", void 0);
|
|
83
83
|
_defineProperty(_assertThisInitialized(_this), "_mainHelperSearch", void 0);
|
|
84
|
+
_defineProperty(_assertThisInitialized(_this), "_insights", void 0);
|
|
84
85
|
_defineProperty(_assertThisInitialized(_this), "middleware", []);
|
|
85
86
|
_defineProperty(_assertThisInitialized(_this), "sendEventToInsights", void 0);
|
|
86
87
|
/**
|
|
@@ -131,7 +132,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
131
132
|
_options$routing = options.routing,
|
|
132
133
|
routing = _options$routing === void 0 ? null : _options$routing,
|
|
133
134
|
_options$insights = options.insights,
|
|
134
|
-
insights = _options$insights === void 0 ?
|
|
135
|
+
insights = _options$insights === void 0 ? undefined : _options$insights,
|
|
135
136
|
searchFunction = options.searchFunction,
|
|
136
137
|
_options$stalledSearc = options.stalledSearchDelay,
|
|
137
138
|
stalledSearchDelay = _options$stalledSearc === void 0 ? 200 : _options$stalledSearc,
|
|
@@ -187,6 +188,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
187
188
|
_this._createURL = defaultCreateURL;
|
|
188
189
|
_this._initialUiState = initialUiState;
|
|
189
190
|
_this._initialResults = null;
|
|
191
|
+
_this._insights = insights;
|
|
190
192
|
if (searchFunction) {
|
|
191
193
|
process.env.NODE_ENV === 'development' ? (0, _utils.warning)(false, "The `searchFunction` option is deprecated. Use `onStateChange` instead.") : void 0;
|
|
192
194
|
_this._searchFunction = searchFunction;
|
|
@@ -198,8 +200,9 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
198
200
|
_this.use((0, _createRouterMiddleware.createRouterMiddleware)(routerOptions));
|
|
199
201
|
}
|
|
200
202
|
|
|
201
|
-
// This is the default middleware,
|
|
202
|
-
//
|
|
203
|
+
// This is the default Insights middleware,
|
|
204
|
+
// added when `insights` is set to true by the user.
|
|
205
|
+
// Any user-provided middleware will be added later and override this one.
|
|
203
206
|
if (insights) {
|
|
204
207
|
var insightsOptions = typeof insights === 'boolean' ? {} : insights;
|
|
205
208
|
insightsOptions.$$internal = true;
|
|
@@ -454,6 +457,7 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
454
457
|
uiState: this._initialUiState
|
|
455
458
|
});
|
|
456
459
|
if (this._initialResults) {
|
|
460
|
+
(0, _utils.hydrateSearchClient)(this.client, this._initialResults);
|
|
457
461
|
var originalScheduleSearch = this.scheduleSearch;
|
|
458
462
|
// We don't schedule a first search when initial results are provided
|
|
459
463
|
// because we already have the results to render. This skips the initial
|
|
@@ -491,6 +495,24 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
491
495
|
var instance = _ref6.instance;
|
|
492
496
|
instance.started();
|
|
493
497
|
});
|
|
498
|
+
|
|
499
|
+
// This is the automatic Insights middleware,
|
|
500
|
+
// added when `insights` is unset and the initial results possess `queryID`.
|
|
501
|
+
// Any user-provided middleware will be added later and override this one.
|
|
502
|
+
if (typeof this._insights === 'undefined') {
|
|
503
|
+
mainHelper.derivedHelpers[0].once('result', function () {
|
|
504
|
+
var hasAutomaticInsights = _this3.mainIndex.getScopedResults().some(function (_ref7) {
|
|
505
|
+
var results = _ref7.results;
|
|
506
|
+
return results === null || results === void 0 ? void 0 : results._automaticInsights;
|
|
507
|
+
});
|
|
508
|
+
if (hasAutomaticInsights) {
|
|
509
|
+
_this3.use((0, _createInsightsMiddleware.createInsightsMiddleware)({
|
|
510
|
+
$$internal: true,
|
|
511
|
+
$$automatic: true
|
|
512
|
+
}));
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
}
|
|
494
516
|
}
|
|
495
517
|
|
|
496
518
|
/**
|
|
@@ -520,8 +542,8 @@ var InstantSearch = /*#__PURE__*/function (_EventEmitter) {
|
|
|
520
542
|
(_this$mainHelper2 = this.mainHelper) === null || _this$mainHelper2 === void 0 ? void 0 : _this$mainHelper2.removeAllListeners();
|
|
521
543
|
this.mainHelper = null;
|
|
522
544
|
this.helper = null;
|
|
523
|
-
this.middleware.forEach(function (
|
|
524
|
-
var instance =
|
|
545
|
+
this.middleware.forEach(function (_ref8) {
|
|
546
|
+
var instance = _ref8.instance;
|
|
525
547
|
instance.unsubscribe();
|
|
526
548
|
});
|
|
527
549
|
}
|
|
@@ -5,6 +5,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.createSendEventForFacet = createSendEventForFacet;
|
|
7
7
|
var _isFacetRefined = require("./isFacetRefined");
|
|
8
|
+
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; }
|
|
9
|
+
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; }
|
|
10
|
+
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; }
|
|
11
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
12
|
+
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); }
|
|
8
13
|
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); }
|
|
9
14
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
10
15
|
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."); }
|
|
@@ -23,7 +28,9 @@ function createSendEventForFacet(_ref) {
|
|
|
23
28
|
}
|
|
24
29
|
var facetValue = args[1],
|
|
25
30
|
_args$ = args[2],
|
|
26
|
-
eventName = _args$ === void 0 ? 'Filter Applied' : _args
|
|
31
|
+
eventName = _args$ === void 0 ? 'Filter Applied' : _args$,
|
|
32
|
+
_args$2 = args[3],
|
|
33
|
+
additionalData = _args$2 === void 0 ? {} : _args$2;
|
|
27
34
|
var _args$0$split = args[0].split(':'),
|
|
28
35
|
_args$0$split2 = _slicedToArray(_args$0$split, 2),
|
|
29
36
|
eventType = _args$0$split2[0],
|
|
@@ -31,7 +38,7 @@ function createSendEventForFacet(_ref) {
|
|
|
31
38
|
var attribute = typeof attr === 'string' ? attr : attr(facetValue);
|
|
32
39
|
if (args.length === 1 && _typeof(args[0]) === 'object') {
|
|
33
40
|
instantSearchInstance.sendEventToInsights(args[0]);
|
|
34
|
-
} else if (eventType === 'click' &&
|
|
41
|
+
} else if (eventType === 'click' && args.length >= 2 && args.length <= 4) {
|
|
35
42
|
if (!(0, _isFacetRefined.isFacetRefined)(helper, attribute, facetValue)) {
|
|
36
43
|
// send event only when the facet is being checked "ON"
|
|
37
44
|
instantSearchInstance.sendEventToInsights({
|
|
@@ -39,16 +46,16 @@ function createSendEventForFacet(_ref) {
|
|
|
39
46
|
widgetType: widgetType,
|
|
40
47
|
eventType: eventType,
|
|
41
48
|
eventModifier: eventModifier,
|
|
42
|
-
payload: {
|
|
49
|
+
payload: _objectSpread({
|
|
43
50
|
eventName: eventName,
|
|
44
51
|
index: helper.getIndex(),
|
|
45
52
|
filters: ["".concat(attribute, ":").concat(facetValue)]
|
|
46
|
-
},
|
|
53
|
+
}, additionalData),
|
|
47
54
|
attribute: attribute
|
|
48
55
|
});
|
|
49
56
|
}
|
|
50
57
|
} else if (process.env.NODE_ENV === 'development') {
|
|
51
|
-
throw new Error("You need to pass two arguments like:\n sendEvent('click', facetValue);\n\nIf you want to send a custom payload, you can pass one object: sendEvent(customPayload);\n");
|
|
58
|
+
throw new Error("You need to pass between two and four arguments like:\n sendEvent('click', facetValue, eventName?, additionalData?);\n\nIf you want to send a custom payload, you can pass one object: sendEvent(customPayload);\n");
|
|
52
59
|
}
|
|
53
60
|
};
|
|
54
61
|
return sendEventForFacet;
|
|
@@ -7,6 +7,11 @@ exports._buildEventPayloadsForHits = _buildEventPayloadsForHits;
|
|
|
7
7
|
exports.createBindEventForHits = createBindEventForHits;
|
|
8
8
|
exports.createSendEventForHits = createSendEventForHits;
|
|
9
9
|
var _serializer = require("./serializer");
|
|
10
|
+
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; }
|
|
11
|
+
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; }
|
|
12
|
+
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; }
|
|
13
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
14
|
+
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); }
|
|
10
15
|
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
11
16
|
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."); }
|
|
12
17
|
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); }
|
|
@@ -38,6 +43,7 @@ function _buildEventPayloadsForHits(_ref) {
|
|
|
38
43
|
eventModifier = _args$0$split2[1];
|
|
39
44
|
var hits = args[1];
|
|
40
45
|
var eventName = args[2];
|
|
46
|
+
var additionalData = args[3] || {};
|
|
41
47
|
if (!hits) {
|
|
42
48
|
if (process.env.NODE_ENV === 'development') {
|
|
43
49
|
throw new Error("You need to pass hit or hits as the second argument like:\n ".concat(methodName, "(eventType, hit);\n "));
|
|
@@ -77,11 +83,11 @@ function _buildEventPayloadsForHits(_ref) {
|
|
|
77
83
|
insightsMethod: 'viewedObjectIDs',
|
|
78
84
|
widgetType: widgetType,
|
|
79
85
|
eventType: eventType,
|
|
80
|
-
payload: {
|
|
86
|
+
payload: _objectSpread({
|
|
81
87
|
eventName: eventName || 'Hits Viewed',
|
|
82
88
|
index: index,
|
|
83
89
|
objectIDs: objectIDsByChunk[i]
|
|
84
|
-
},
|
|
90
|
+
}, additionalData),
|
|
85
91
|
hits: batch,
|
|
86
92
|
eventModifier: eventModifier
|
|
87
93
|
};
|
|
@@ -92,13 +98,13 @@ function _buildEventPayloadsForHits(_ref) {
|
|
|
92
98
|
insightsMethod: 'clickedObjectIDsAfterSearch',
|
|
93
99
|
widgetType: widgetType,
|
|
94
100
|
eventType: eventType,
|
|
95
|
-
payload: {
|
|
101
|
+
payload: _objectSpread({
|
|
96
102
|
eventName: eventName || 'Hit Clicked',
|
|
97
103
|
index: index,
|
|
98
104
|
queryID: queryID,
|
|
99
105
|
objectIDs: objectIDsByChunk[i],
|
|
100
106
|
positions: positionsByChunk[i]
|
|
101
|
-
},
|
|
107
|
+
}, additionalData),
|
|
102
108
|
hits: batch,
|
|
103
109
|
eventModifier: eventModifier
|
|
104
110
|
};
|
|
@@ -109,12 +115,12 @@ function _buildEventPayloadsForHits(_ref) {
|
|
|
109
115
|
insightsMethod: 'convertedObjectIDsAfterSearch',
|
|
110
116
|
widgetType: widgetType,
|
|
111
117
|
eventType: eventType,
|
|
112
|
-
payload: {
|
|
118
|
+
payload: _objectSpread({
|
|
113
119
|
eventName: eventName || 'Hit Converted',
|
|
114
120
|
index: index,
|
|
115
121
|
queryID: queryID,
|
|
116
122
|
objectIDs: objectIDsByChunk[i]
|
|
117
|
-
},
|
|
123
|
+
}, additionalData),
|
|
118
124
|
hits: batch,
|
|
119
125
|
eventModifier: eventModifier
|
|
120
126
|
};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.hydrateSearchClient = hydrateSearchClient;
|
|
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); }
|
|
8
|
+
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; }
|
|
9
|
+
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; }
|
|
10
|
+
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; }
|
|
11
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
12
|
+
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); }
|
|
13
|
+
// @ts-nocheck (types to be fixed during actual implementation)
|
|
14
|
+
|
|
15
|
+
function hydrateSearchClient(client, results) {
|
|
16
|
+
if (!results) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Disable cache hydration on:
|
|
21
|
+
// - Algoliasearch API Client < v4 with cache disabled
|
|
22
|
+
// - Third party clients (detected by the `addAlgoliaAgent` function missing)
|
|
23
|
+
|
|
24
|
+
if ((!client.transporter || client._cacheHydrated) && (!client._useCache || typeof client.addAlgoliaAgent !== 'function')) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Algoliasearch API Client >= v4
|
|
29
|
+
// To hydrate the client we need to populate the cache with the data from
|
|
30
|
+
// the server (done in `hydrateSearchClientWithMultiIndexRequest` or
|
|
31
|
+
// `hydrateSearchClientWithSingleIndexRequest`). But since there is no way
|
|
32
|
+
// for us to compute the key the same way as `algoliasearch-client` we need
|
|
33
|
+
// to populate it on a custom key and override the `search` method to
|
|
34
|
+
// search on it first.
|
|
35
|
+
if (client.transporter && !client._cacheHydrated) {
|
|
36
|
+
client._cacheHydrated = true;
|
|
37
|
+
var baseMethod = client.search;
|
|
38
|
+
client.search = function (requests) {
|
|
39
|
+
for (var _len = arguments.length, methodArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
40
|
+
methodArgs[_key - 1] = arguments[_key];
|
|
41
|
+
}
|
|
42
|
+
var requestsWithSerializedParams = requests.map(function (request) {
|
|
43
|
+
return _objectSpread(_objectSpread({}, request), {}, {
|
|
44
|
+
params: serializeQueryParameters(request.params)
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
return client.transporter.responsesCache.get({
|
|
48
|
+
method: 'search',
|
|
49
|
+
args: [requestsWithSerializedParams].concat(methodArgs)
|
|
50
|
+
}, function () {
|
|
51
|
+
return baseMethod.apply(void 0, [requests].concat(methodArgs));
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Populate the cache with the data from the server
|
|
56
|
+
client.transporter.responsesCache.set({
|
|
57
|
+
method: 'search',
|
|
58
|
+
args: [Object.keys(results).reduce(function (acc, key) {
|
|
59
|
+
return acc.concat(results[key].results.map(function (request) {
|
|
60
|
+
return {
|
|
61
|
+
indexName: request.index,
|
|
62
|
+
params: request.params
|
|
63
|
+
};
|
|
64
|
+
}));
|
|
65
|
+
}, [])]
|
|
66
|
+
}, {
|
|
67
|
+
results: Object.keys(results).reduce(function (acc, key) {
|
|
68
|
+
return acc.concat(results[key].results);
|
|
69
|
+
}, [])
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Algoliasearch API Client < v4
|
|
74
|
+
// Prior to client v4 we didn't have a proper API to hydrate the client
|
|
75
|
+
// cache from the outside. The following code populates the cache with
|
|
76
|
+
// a single-index result. You can find more information about the
|
|
77
|
+
// computation of the key inside the client (see link below).
|
|
78
|
+
// https://github.com/algolia/algoliasearch-client-javascript/blob/c27e89ff92b2a854ae6f40dc524bffe0f0cbc169/src/AlgoliaSearchCore.js#L232-L240
|
|
79
|
+
if (!client.transporter) {
|
|
80
|
+
var cacheKey = "/1/indexes/*/queries_body_".concat(JSON.stringify({
|
|
81
|
+
requests: Object.keys(results).reduce(function (acc, key) {
|
|
82
|
+
return acc.concat(results[key].rawResults.map(function (request) {
|
|
83
|
+
return {
|
|
84
|
+
indexName: request.index,
|
|
85
|
+
params: request.params
|
|
86
|
+
};
|
|
87
|
+
}));
|
|
88
|
+
}, [])
|
|
89
|
+
}));
|
|
90
|
+
client.cache = _objectSpread(_objectSpread({}, client.cache), {}, _defineProperty({}, cacheKey, JSON.stringify({
|
|
91
|
+
results: Object.keys(results).reduce(function (acc, key) {
|
|
92
|
+
return acc.concat(results[key].rawResults);
|
|
93
|
+
}, [])
|
|
94
|
+
})));
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// This function is copied from the algoliasearch v4 API Client. If modified,
|
|
99
|
+
// consider updating it also in `serializeQueryParameters` from `@algolia/transporter`.
|
|
100
|
+
function serializeQueryParameters(parameters) {
|
|
101
|
+
var isObjectOrArray = function isObjectOrArray(value) {
|
|
102
|
+
return Object.prototype.toString.call(value) === '[object Object]' || Object.prototype.toString.call(value) === '[object Array]';
|
|
103
|
+
};
|
|
104
|
+
var encode = function encode(format) {
|
|
105
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
|
|
106
|
+
args[_key2 - 1] = arguments[_key2];
|
|
107
|
+
}
|
|
108
|
+
var i = 0;
|
|
109
|
+
return format.replace(/%s/g, function () {
|
|
110
|
+
return encodeURIComponent(args[i++]);
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
return Object.keys(parameters).map(function (key) {
|
|
114
|
+
return encode('%s=%s', key, isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]);
|
|
115
|
+
}).join('&');
|
|
116
|
+
}
|
package/cjs/lib/utils/index.js
CHANGED
|
@@ -322,6 +322,17 @@ Object.keys(_hitsQueryId).forEach(function (key) {
|
|
|
322
322
|
}
|
|
323
323
|
});
|
|
324
324
|
});
|
|
325
|
+
var _hydrateSearchClient = require("./hydrateSearchClient");
|
|
326
|
+
Object.keys(_hydrateSearchClient).forEach(function (key) {
|
|
327
|
+
if (key === "default" || key === "__esModule") return;
|
|
328
|
+
if (key in exports && exports[key] === _hydrateSearchClient[key]) return;
|
|
329
|
+
Object.defineProperty(exports, key, {
|
|
330
|
+
enumerable: true,
|
|
331
|
+
get: function get() {
|
|
332
|
+
return _hydrateSearchClient[key];
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
});
|
|
325
336
|
var _isDomElement = require("./isDomElement");
|
|
326
337
|
Object.keys(_isDomElement).forEach(function (key) {
|
|
327
338
|
if (key === "default" || key === "__esModule") return;
|
package/cjs/lib/version.js
CHANGED
|
@@ -30,7 +30,9 @@ function createInsightsMiddleware() {
|
|
|
30
30
|
insightsInitParams = props.insightsInitParams,
|
|
31
31
|
onEvent = props.onEvent,
|
|
32
32
|
_props$$$internal = props.$$internal,
|
|
33
|
-
$$internal = _props$$$internal === void 0 ? false : _props$$$internal
|
|
33
|
+
$$internal = _props$$$internal === void 0 ? false : _props$$$internal,
|
|
34
|
+
_props$$$automatic = props.$$automatic,
|
|
35
|
+
$$automatic = _props$$$automatic === void 0 ? false : _props$$$automatic;
|
|
34
36
|
var potentialInsightsClient = _insightsClient;
|
|
35
37
|
if (!_insightsClient && _insightsClient !== null) {
|
|
36
38
|
(0, _utils.safelyRunOnBrowser)(function (_ref) {
|
|
@@ -121,6 +123,7 @@ function createInsightsMiddleware() {
|
|
|
121
123
|
return {
|
|
122
124
|
$$type: 'ais.insights',
|
|
123
125
|
$$internal: $$internal,
|
|
126
|
+
$$automatic: $$automatic,
|
|
124
127
|
onStateChange: function onStateChange() {},
|
|
125
128
|
subscribe: function subscribe() {
|
|
126
129
|
if (!insightsClient.shouldAddScript) return;
|
|
@@ -146,9 +149,14 @@ function createInsightsMiddleware() {
|
|
|
146
149
|
userToken: helper.state.userToken,
|
|
147
150
|
clickAnalytics: helper.state.clickAnalytics
|
|
148
151
|
};
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
|
|
153
|
+
// We don't want to force clickAnalytics when the insights is enabled from the search response.
|
|
154
|
+
// This means we don't enable insights for indices that don't opt in
|
|
155
|
+
if (!$$automatic) {
|
|
156
|
+
helper.overrideStateWithoutTriggeringChangeEvent(_objectSpread(_objectSpread({}, helper.state), {}, {
|
|
157
|
+
clickAnalytics: true
|
|
158
|
+
}));
|
|
159
|
+
}
|
|
152
160
|
if (!$$internal) {
|
|
153
161
|
instantSearchInstance.scheduleSearch();
|
|
154
162
|
}
|
|
@@ -215,6 +223,9 @@ function createInsightsMiddleware() {
|
|
|
215
223
|
} else if (event.insightsMethod) {
|
|
216
224
|
// Source is used to differentiate events sent by instantsearch from those sent manually.
|
|
217
225
|
event.payload.algoliaSource = ['instantsearch'];
|
|
226
|
+
if ($$automatic) {
|
|
227
|
+
event.payload.algoliaSource.push('instantsearch-automatic');
|
|
228
|
+
}
|
|
218
229
|
if (event.eventModifier === 'internal') {
|
|
219
230
|
event.payload.algoliaSource.push('instantsearch-internal');
|
|
220
231
|
}
|
|
@@ -531,11 +531,11 @@ declare type BrowserHistoryArgs<TRouteState> = {
|
|
|
531
531
|
push?: (url: string) => void;
|
|
532
532
|
};
|
|
533
533
|
|
|
534
|
-
declare type BuiltInBindEventForHits = (eventType: string, hits: Hit | Hit[], eventName?: string) => string;
|
|
534
|
+
declare type BuiltInBindEventForHits = (eventType: string, hits: Hit | Hit[], eventName?: string, additionalData?: Record<string, any>) => string;
|
|
535
535
|
|
|
536
|
-
declare type BuiltInSendEventForFacet = (eventType: string, facetValue: string, eventName?: string) => void;
|
|
536
|
+
declare type BuiltInSendEventForFacet = (eventType: string, facetValue: string, eventName?: string, additionalData?: Record<string, any>) => void;
|
|
537
537
|
|
|
538
|
-
declare type BuiltInSendEventForHits = (eventType: string, hits: Hit | Hit[], eventName?: string) => void;
|
|
538
|
+
declare type BuiltInSendEventForHits = (eventType: string, hits: Hit | Hit[], eventName?: string, additionalData?: Record<string, any>) => void;
|
|
539
539
|
|
|
540
540
|
declare type BuiltInSendEventForToggle = (eventType: string, isRefined: boolean, eventName?: string) => void;
|
|
541
541
|
|
|
@@ -2377,6 +2377,7 @@ declare class InstantSearch<TUiState extends UiState = UiState, TRouteState = TU
|
|
|
2377
2377
|
_createURL: CreateURL<TUiState>;
|
|
2378
2378
|
_searchFunction?: InstantSearchOptions['searchFunction'];
|
|
2379
2379
|
_mainHelperSearch?: AlgoliaSearchHelper['search'];
|
|
2380
|
+
_insights: InstantSearchOptions['insights'];
|
|
2380
2381
|
middleware: Array<{
|
|
2381
2382
|
creator: Middleware<TUiState>;
|
|
2382
2383
|
instance: MiddlewareDefinition<TUiState>;
|