@constructor-io/constructorio-client-javascript 2.34.5 → 2.34.7
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/lib/modules/search.js
CHANGED
|
@@ -17,6 +17,7 @@ var helpers = require('../utils/helpers'); // Create URL from supplied query (te
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
function createSearchUrl(query, parameters, options) {
|
|
20
|
+
var isVoiceSearch = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
20
21
|
var apiKey = options.apiKey,
|
|
21
22
|
version = options.version,
|
|
22
23
|
serviceUrl = options.serviceUrl,
|
|
@@ -148,7 +149,8 @@ function createSearchUrl(query, parameters, options) {
|
|
|
148
149
|
queryParams._dt = Date.now();
|
|
149
150
|
queryParams = helpers.cleanParams(queryParams);
|
|
150
151
|
var queryString = helpers.stringify(queryParams);
|
|
151
|
-
|
|
152
|
+
var searchUrl = isVoiceSearch ? 'search/natural_language' : 'search';
|
|
153
|
+
return "".concat(serviceUrl, "/").concat(searchUrl, "/").concat(helpers.encodeURIComponentRFC3986(helpers.trimNonBreakingSpaces(query)), "?").concat(queryString);
|
|
152
154
|
}
|
|
153
155
|
/**
|
|
154
156
|
* Interface to search related API calls
|
|
@@ -256,6 +258,89 @@ var Search = /*#__PURE__*/function () {
|
|
|
256
258
|
throw new Error('getSearchResults response data is malformed');
|
|
257
259
|
});
|
|
258
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Retrieve voice search results from API
|
|
263
|
+
*
|
|
264
|
+
* @function getVoiceSearchResults
|
|
265
|
+
* @description Retrieve voice search results from Constructor.io API
|
|
266
|
+
* @param {string} query - Term to use to perform a voice search
|
|
267
|
+
* @param {object} [parameters] - Additional parameters to refine result set
|
|
268
|
+
* @param {number} [parameters.page] - The page number of the results (Can't be used together with offset)
|
|
269
|
+
* @param {number} [parameters.offset] - The number of results to skip from the beginning (Can't be used together with page)
|
|
270
|
+
* @param {number} [parameters.resultsPerPage] - The number of results per page to return
|
|
271
|
+
* @param {string} [parameters.section='Products'] - The section name for results
|
|
272
|
+
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups. Please refer to https://docs.constructor.io/rest_api/search/queries for details
|
|
273
|
+
* @param {object} [parameters.preFilterExpression] - Faceting expression to scope search results. Please refer to https://docs.constructor.io/rest_api/collections#add-items-dynamically
|
|
274
|
+
* @param {object} [parameters.variationsMap] - The variations map object to aggregate variations. Please refer to https://docs.constructor.io/rest_api/variations_mapping for details
|
|
275
|
+
* @param {string[]} [parameters.hiddenFields] - Hidden metadata fields to return
|
|
276
|
+
* @param {string[]} [parameters.hiddenFacets] - Hidden facets to return
|
|
277
|
+
* @param {object} [parameters.qs] - Parameters listed above can be serialized into a JSON object and parsed through this parameter. Please refer to https://docs.constructor.io/rest_api/search/queries/
|
|
278
|
+
* @param {object} [networkParameters] - Parameters relevant to the network request
|
|
279
|
+
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
|
|
280
|
+
* @returns {Promise}
|
|
281
|
+
* @see https://docs.constructor.io/rest_api/search/natural_language_search/
|
|
282
|
+
* @example
|
|
283
|
+
* constructorio.search.getVoiceSearchResults('show me lipstick');
|
|
284
|
+
*/
|
|
285
|
+
|
|
286
|
+
}, {
|
|
287
|
+
key: "getVoiceSearchResults",
|
|
288
|
+
value: function getVoiceSearchResults(query, parameters) {
|
|
289
|
+
var _this2 = this;
|
|
290
|
+
|
|
291
|
+
var networkParameters = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
292
|
+
var requestUrl;
|
|
293
|
+
var fetch = this.options.fetch;
|
|
294
|
+
var signal;
|
|
295
|
+
|
|
296
|
+
if (typeof AbortController === 'function') {
|
|
297
|
+
var controller = new AbortController();
|
|
298
|
+
signal = controller && controller.signal; // Handle network timeout if specified
|
|
299
|
+
|
|
300
|
+
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
var isVoiceSearch = true;
|
|
305
|
+
requestUrl = createSearchUrl(query, parameters, this.options, isVoiceSearch);
|
|
306
|
+
} catch (e) {
|
|
307
|
+
return Promise.reject(e);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return fetch(requestUrl, {
|
|
311
|
+
signal: signal
|
|
312
|
+
}).then(function (response) {
|
|
313
|
+
if (response.ok) {
|
|
314
|
+
return response.json();
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return helpers.throwHttpErrorFromResponse(new Error(), response);
|
|
318
|
+
}).then(function (json) {
|
|
319
|
+
// Search results
|
|
320
|
+
if (json.response && json.response.results) {
|
|
321
|
+
if (json.result_id) {
|
|
322
|
+
// Append `result_id` to each result item
|
|
323
|
+
json.response.results.forEach(function (result) {
|
|
324
|
+
// eslint-disable-next-line no-param-reassign
|
|
325
|
+
result.result_id = json.result_id;
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
_this2.eventDispatcher.queue('search.getVoiceSearchResults.completed', json);
|
|
330
|
+
|
|
331
|
+
return json;
|
|
332
|
+
} // Redirect rules
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
if (json.response && json.response.redirect) {
|
|
336
|
+
_this2.eventDispatcher.queue('search.getVoiceSearchResults.completed', json);
|
|
337
|
+
|
|
338
|
+
return json;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
throw new Error('getVoiceSearchResults response data is malformed');
|
|
342
|
+
});
|
|
343
|
+
}
|
|
259
344
|
}]);
|
|
260
345
|
return Search;
|
|
261
346
|
}();
|
|
@@ -2,6 +2,7 @@ import Search from './search';
|
|
|
2
2
|
import Browse from './browse';
|
|
3
3
|
import Autocomplete from './autocomplete';
|
|
4
4
|
import Recommendations from './recommendations';
|
|
5
|
+
import Quizzes from './quizzes';
|
|
5
6
|
import Tracker from './tracker';
|
|
6
7
|
import { ConstructorClientOptions } from '.';
|
|
7
8
|
|
|
@@ -20,11 +21,13 @@ declare class ConstructorIO {
|
|
|
20
21
|
|
|
21
22
|
recommendations: Recommendations;
|
|
22
23
|
|
|
24
|
+
quizzes: Quizzes;
|
|
25
|
+
|
|
23
26
|
tracker: Tracker;
|
|
24
27
|
|
|
25
28
|
setClientOptions(options: ConstructorClientOptions): void;
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
declare namespace ConstructorIO {
|
|
29
|
-
export { Search, Browse, Autocomplete, Recommendations, Tracker };
|
|
32
|
+
export { Search, Browse, Autocomplete, Recommendations, Quizzes, Tracker };
|
|
30
33
|
}
|
package/lib/types/search.d.ts
CHANGED
|
@@ -41,6 +41,12 @@ declare class Search {
|
|
|
41
41
|
parameters?: SearchParameters,
|
|
42
42
|
networkParameters?: NetworkParameters
|
|
43
43
|
): Promise<SearchResponse>;
|
|
44
|
+
|
|
45
|
+
getVoiceSearchResults(
|
|
46
|
+
query: string,
|
|
47
|
+
parameters?: Omit<SearchParameters, 'filters' | 'sortBy' | 'sortOrder'>,
|
|
48
|
+
networkParameters?: NetworkParameters
|
|
49
|
+
): Promise<SearchResponse>;
|
|
44
50
|
}
|
|
45
51
|
|
|
46
52
|
/** *********
|
|
@@ -75,6 +81,7 @@ export interface SearchRequestType extends Record<string, any> {
|
|
|
75
81
|
features: Partial<RequestFeature>;
|
|
76
82
|
feature_variants: Partial<RequestFeatureVariant>;
|
|
77
83
|
searchandized_items: Record<string, any>;
|
|
84
|
+
original_query?: string;
|
|
78
85
|
}
|
|
79
86
|
|
|
80
87
|
export interface Result extends Record<string, any> {
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructor-io/constructorio-client-javascript",
|
|
3
|
-
"version": "2.34.
|
|
3
|
+
"version": "2.34.7",
|
|
4
4
|
"description": "Constructor.io JavaScript client",
|
|
5
5
|
"main": "lib/constructorio.js",
|
|
6
6
|
"types": "lib/types/constructorio.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"clean": "sudo rm -rf node_modules package-lock.json",
|
|
9
|
-
"version": "
|
|
10
|
-
"check-
|
|
9
|
+
"version": "npm run verify-node-version && npm run docs && git add ./docs/* && npm run bundle && git add -A ./dist",
|
|
10
|
+
"check-license": "license-checker --production --onlyAllow 'Apache-2.0;BSD-3-Clause;MIT;0BSD;BSD-2-Clause'",
|
|
11
|
+
"verify-node-version": "chmod +x ./scripts/verify-node-version.sh && ./scripts/verify-node-version.sh",
|
|
11
12
|
"lint": "eslint 'src/**/*.js' 'spec/**/*.js' 'src/**/*.d.ts'",
|
|
12
13
|
"test": "npm run compile && mkdir -p test && cp -rf lib/* test && mocha ./spec/*",
|
|
13
14
|
"test:types": "tsd .",
|