@constructor-io/constructorio-node 3.8.0 → 3.8.3
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/package.json +4 -6
- package/src/modules/browse.js +77 -5
- package/src/modules/catalog.js +7 -8
- package/src/modules/tracker.js +5 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@constructor-io/constructorio-node",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.3",
|
|
4
4
|
"description": "Constructor.io Node.js client",
|
|
5
5
|
"main": "src/constructorio.js",
|
|
6
6
|
"scripts": {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"precoverage": "rm -rf ./coverage && rm -rf ./.nyc_output",
|
|
12
12
|
"coverage": "nyc --all --reporter=html npm run test",
|
|
13
13
|
"postcoverage": "serve --listen 8080 --config ./serve.json && rm -rf test",
|
|
14
|
-
"docs": "jsdoc --configure ./.jsdoc.json ./README.md --recurse ./src --destination ./docs"
|
|
14
|
+
"docs": "jsdoc --configure ./.jsdoc.json ./README.md --recurse ./src --destination ./docs",
|
|
15
|
+
"prepare": "husky install"
|
|
15
16
|
},
|
|
16
17
|
"repository": {
|
|
17
18
|
"type": "git",
|
|
@@ -26,9 +27,6 @@
|
|
|
26
27
|
"engines": {
|
|
27
28
|
"node": ">=8.3.0"
|
|
28
29
|
},
|
|
29
|
-
"pre-push": [
|
|
30
|
-
"lint"
|
|
31
|
-
],
|
|
32
30
|
"files": [
|
|
33
31
|
"src/**/*"
|
|
34
32
|
],
|
|
@@ -39,6 +37,7 @@
|
|
|
39
37
|
"eslint": "^8.2.0",
|
|
40
38
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
41
39
|
"eslint-plugin-import": "^2.24.2",
|
|
40
|
+
"husky": "^7.0.4",
|
|
42
41
|
"jsdoc": "^3.6.7",
|
|
43
42
|
"jsdom": "^15.1.1",
|
|
44
43
|
"license-checker": "^25.0.1",
|
|
@@ -47,7 +46,6 @@
|
|
|
47
46
|
"mocha": "^9.1.3",
|
|
48
47
|
"mocha-jsdom": "^2.0.0",
|
|
49
48
|
"nyc": "^15.1.0",
|
|
50
|
-
"pre-push": "^0.1.1",
|
|
51
49
|
"serve": "^13.0.2",
|
|
52
50
|
"sinon": "^7.5.0",
|
|
53
51
|
"sinon-chai": "^3.7.0",
|
package/src/modules/browse.js
CHANGED
|
@@ -118,11 +118,11 @@ function createBrowseUrlFromFilter(filterName, filterValue, parameters, userPara
|
|
|
118
118
|
return `${serviceUrl}/browse/${encodeURIComponent(filterName)}/${encodeURIComponent(filterValue)}?${queryString}`;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
-
// Create URL from supplied
|
|
121
|
+
// Create URL from supplied IDs and parameters
|
|
122
122
|
function createBrowseUrlFromIDs(itemIds, parameters, userParameters, options) {
|
|
123
123
|
const { serviceUrl } = options;
|
|
124
124
|
|
|
125
|
-
// Validate item
|
|
125
|
+
// Validate item IDs are provided
|
|
126
126
|
if (!itemIds || !(itemIds instanceof Array) || !itemIds.length) {
|
|
127
127
|
throw new Error('itemIds is a required parameter of type array');
|
|
128
128
|
}
|
|
@@ -133,7 +133,7 @@ function createBrowseUrlFromIDs(itemIds, parameters, userParameters, options) {
|
|
|
133
133
|
return `${serviceUrl}/browse/items?${queryString}`;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
// Create URL from supplied
|
|
136
|
+
// Create URL from supplied parameters
|
|
137
137
|
function createBrowseUrlForFacets(parameters, userParameters, options) {
|
|
138
138
|
const { serviceUrl } = options;
|
|
139
139
|
const queryParams = { ...createQueryParams(parameters, userParameters, options) };
|
|
@@ -141,9 +141,28 @@ function createBrowseUrlForFacets(parameters, userParameters, options) {
|
|
|
141
141
|
delete queryParams._dt;
|
|
142
142
|
|
|
143
143
|
const queryString = qs.stringify(queryParams, { indices: false });
|
|
144
|
+
|
|
144
145
|
return `${serviceUrl}/browse/facets?${queryString}`;
|
|
145
146
|
}
|
|
146
147
|
|
|
148
|
+
// Create URL from supplied facet name and parameters
|
|
149
|
+
function createBrowseUrlForFacetOptions(facetName, parameters, userParameters, options) {
|
|
150
|
+
const { serviceUrl } = options;
|
|
151
|
+
|
|
152
|
+
// Validate facet name is provided
|
|
153
|
+
if (!facetName || typeof facetName !== 'string') {
|
|
154
|
+
throw new Error('facetName is a required parameter of type string');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const queryParams = { ...createQueryParams(parameters, userParameters, options) };
|
|
158
|
+
|
|
159
|
+
delete queryParams._dt;
|
|
160
|
+
|
|
161
|
+
const queryString = qs.stringify(queryParams, { indices: false });
|
|
162
|
+
|
|
163
|
+
return `${serviceUrl}/browse/facet_options?facet_name=${facetName}&${queryString}`;
|
|
164
|
+
}
|
|
165
|
+
|
|
147
166
|
// Create request headers using supplied options and user parameters
|
|
148
167
|
function createHeaders(options, userParameters) {
|
|
149
168
|
const headers = {};
|
|
@@ -257,10 +276,10 @@ class Browse {
|
|
|
257
276
|
}
|
|
258
277
|
|
|
259
278
|
/**
|
|
260
|
-
* Retrieve browse results from API using item
|
|
279
|
+
* Retrieve browse results from API using item IDs
|
|
261
280
|
*
|
|
262
281
|
* @function getBrowseResultsForItemIds
|
|
263
|
-
* @param {string[]} itemIds - Item
|
|
282
|
+
* @param {string[]} itemIds - Item IDs of results to fetch
|
|
264
283
|
* @param {object} [parameters] - Additional parameters to refine result set
|
|
265
284
|
* @param {number} [parameters.page] - The page number of the results
|
|
266
285
|
* @param {number} [parameters.resultsPerPage] - The number of results per page to return
|
|
@@ -434,6 +453,59 @@ class Browse {
|
|
|
434
453
|
return helpers.throwHttpErrorFromResponse(new Error(), response);
|
|
435
454
|
});
|
|
436
455
|
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Retrieve facet options from API
|
|
459
|
+
*
|
|
460
|
+
* @function getBrowseFacetOptions
|
|
461
|
+
* @param {string} facetName - Name of the facet whose options to return
|
|
462
|
+
* @param {object} [parameters] - Additional parameters to refine result set
|
|
463
|
+
* @param {object} [parameters.fmtOptions] - The format options used to refine result groups
|
|
464
|
+
* @param {boolean} [parameters.fmtOptions.show_hidden_facets] - Include facets configured as hidden
|
|
465
|
+
* @param {boolean} [parameters.fmtOptions.show_protected_facets] - Include facets configured as protected
|
|
466
|
+
* @param {object} [userParameters] - Parameters relevant to the user request
|
|
467
|
+
* @param {number} [userParameters.sessionId] - Session ID, utilized to personalize results
|
|
468
|
+
* @param {number} [userParameters.clientId] - Client ID, utilized to personalize results
|
|
469
|
+
* @param {string} [userParameters.userId] - User ID, utilized to personalize results
|
|
470
|
+
* @param {string} [userParameters.segments] - User segments
|
|
471
|
+
* @param {string} [userParameters.testCells] - User test cells
|
|
472
|
+
* @param {string} [userParameters.userIp] - Origin user IP, from client
|
|
473
|
+
* @param {string} [userParameters.userAgent] - Origin user agent, from client
|
|
474
|
+
* @param {object} [networkParameters] - Parameters relevant to the network request
|
|
475
|
+
* @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
|
|
476
|
+
* @returns {Promise}
|
|
477
|
+
* @see https://docs.constructor.io/rest_api/browse/facet_options
|
|
478
|
+
* @example
|
|
479
|
+
* constructorio.browse.getBrowseFacetOptions('price', {
|
|
480
|
+
* fmtOptions: { ... },
|
|
481
|
+
* });
|
|
482
|
+
*/
|
|
483
|
+
getBrowseFacetOptions(facetName, parameters = {}, userParameters = {}, networkParameters = {}) {
|
|
484
|
+
let requestUrl;
|
|
485
|
+
const fetch = (this.options && this.options.fetch) || nodeFetch;
|
|
486
|
+
const controller = new AbortController();
|
|
487
|
+
const { signal } = controller;
|
|
488
|
+
|
|
489
|
+
try {
|
|
490
|
+
requestUrl = createBrowseUrlForFacetOptions(facetName, parameters, userParameters, this.options);
|
|
491
|
+
} catch (e) {
|
|
492
|
+
return Promise.reject(e);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Handle network timeout if specified
|
|
496
|
+
helpers.applyNetworkTimeout(this.options, networkParameters, controller);
|
|
497
|
+
|
|
498
|
+
return fetch(requestUrl, {
|
|
499
|
+
headers: helpers.createAuthHeader(this.options),
|
|
500
|
+
signal,
|
|
501
|
+
}).then((response) => {
|
|
502
|
+
if (response.ok) {
|
|
503
|
+
return response.json();
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
return helpers.throwHttpErrorFromResponse(new Error(), response);
|
|
507
|
+
});
|
|
508
|
+
}
|
|
437
509
|
}
|
|
438
510
|
|
|
439
511
|
module.exports = Browse;
|
package/src/modules/catalog.js
CHANGED
|
@@ -4,6 +4,7 @@ const nodeFetch = require('node-fetch').default;
|
|
|
4
4
|
const { AbortController } = require('node-abort-controller');
|
|
5
5
|
const FormData = require('form-data');
|
|
6
6
|
const fs = require('fs');
|
|
7
|
+
const { Duplex } = require('stream');
|
|
7
8
|
const helpers = require('../utils/helpers');
|
|
8
9
|
|
|
9
10
|
// Create URL from supplied path and options
|
|
@@ -59,17 +60,17 @@ async function createQueryParamsAndFormData(parameters) {
|
|
|
59
60
|
|
|
60
61
|
try {
|
|
61
62
|
// Convert items to buffer if passed as stream
|
|
62
|
-
if (items instanceof fs.ReadStream) {
|
|
63
|
+
if (items instanceof fs.ReadStream || items instanceof Duplex) {
|
|
63
64
|
items = await convertToBuffer(items);
|
|
64
65
|
}
|
|
65
66
|
|
|
66
67
|
// Convert variations to buffer if passed as stream
|
|
67
|
-
if (variations instanceof fs.ReadStream) {
|
|
68
|
+
if (variations instanceof fs.ReadStream || variations instanceof Duplex) {
|
|
68
69
|
variations = await convertToBuffer(variations);
|
|
69
70
|
}
|
|
70
71
|
|
|
71
72
|
// Convert item groups to buffer if passed as stream
|
|
72
|
-
if (itemGroups instanceof fs.ReadStream) {
|
|
73
|
+
if (itemGroups instanceof fs.ReadStream || itemGroups instanceof Duplex) {
|
|
73
74
|
itemGroups = await convertToBuffer(itemGroups);
|
|
74
75
|
}
|
|
75
76
|
} catch (e) {
|
|
@@ -140,7 +141,7 @@ class Catalog {
|
|
|
140
141
|
* @param {string} [parameters.url] - A URL to directly send the user after selecting the item
|
|
141
142
|
* @param {string} [parameters.image_url] - A URL that points to an image you'd like displayed next to some item (only applicable when URL is supplied)
|
|
142
143
|
* @param {string} [parameters.description] - A description for some item (only applicable when URL is supplied)
|
|
143
|
-
* @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own
|
|
144
|
+
* @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own IDs of the items to more easily access them in other API calls
|
|
144
145
|
* @param {object} [parameters.facets] - Key/value pairs that can be associated with an item and used to filter them during a search. You can associate multiple values with the same key, by making values a list. Facets can be used as filters in search, autosuggest, and browse requests
|
|
145
146
|
* @param {object} [parameters.metadata] - You can associate schema-less data with items by passing in an object of keys and values. To configure search and display of this data reach out to support@constructor.io
|
|
146
147
|
* @param {string[]} [parameters.group_ids] - You can associate each item with one or more groups (i.e. categories). To set up a group hierarchy please contact support@constructor.io. group_ids can be used as filters in search, autosuggest, and browse requests
|
|
@@ -194,7 +195,7 @@ class Catalog {
|
|
|
194
195
|
* @param {string} [parameters.url] - A URL to directly send the user after selecting the item
|
|
195
196
|
* @param {string} [parameters.image_url] - A URL that points to an image you'd like displayed next to some item (only applicable when URL is supplied)
|
|
196
197
|
* @param {string} [parameters.description] - A description for some item (only applicable when URL is supplied)
|
|
197
|
-
* @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own
|
|
198
|
+
* @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own IDs of the items to more easily access them in other API calls
|
|
198
199
|
* @param {object} [parameters.facets] - Key/value pairs that can be associated with an item and used to filter them during a search. You can associate multiple values with the same key, by making values a list. Facets can be used as filters in search, autosuggest, and browse requests
|
|
199
200
|
* @param {object} [parameters.metadata] - You can associate schema-less data with items by passing in an object of keys and values. To configure search and display of this data reach out to support@constructor.io
|
|
200
201
|
* @param {string[]} [parameters.group_ids] - You can associate each item with one or more groups (i.e. categories). To set up a group hierarchy please contact support@constructor.io. group_ids can be used as filters in search, autosuggest, and browse requests
|
|
@@ -294,7 +295,7 @@ class Catalog {
|
|
|
294
295
|
* @param {string} [parameters.url] - A URL to directly send the user after selecting the item
|
|
295
296
|
* @param {string} [parameters.image_url] - A URL that points to an image you'd like displayed next to some item (only applicable when URL is supplied)
|
|
296
297
|
* @param {string} [parameters.description] - A description for some item (only applicable when URL is supplied)
|
|
297
|
-
* @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own
|
|
298
|
+
* @param {string} [parameters.id] - An arbitrary ID you would like associated with this item. You can use this field to store your own IDs of the items to more easily access them in other API calls
|
|
298
299
|
* @param {object} [parameters.facets] - Key/value pairs that can be associated with an item and used to filter them during a search. You can associate multiple values with the same key, by making values a list. Facets can be used as filters in search, autosuggest, and browse requests
|
|
299
300
|
* @param {object} [parameters.metadata] - You can associate schema-less data with items by passing in an object of keys and values. To configure search and display of this data reach out to support@constructor.io
|
|
300
301
|
* @param {string[]} [parameters.group_ids] - You can associate each item with one or more groups (i.e. categories). To set up a group hierarchy please contact support@constructor.io. group_ids can be used as filters in search, autosuggest, and browse requests
|
|
@@ -2211,7 +2212,6 @@ class Catalog {
|
|
|
2211
2212
|
return fetch(requestUrl, {
|
|
2212
2213
|
method: 'DELETE',
|
|
2213
2214
|
headers: {
|
|
2214
|
-
'Content-Type': 'application/json',
|
|
2215
2215
|
...helpers.createAuthHeader(this.options),
|
|
2216
2216
|
},
|
|
2217
2217
|
signal,
|
|
@@ -2564,7 +2564,6 @@ class Catalog {
|
|
|
2564
2564
|
return fetch(requestUrl, {
|
|
2565
2565
|
method: 'DELETE',
|
|
2566
2566
|
headers: {
|
|
2567
|
-
'Content-Type': 'application/json',
|
|
2568
2567
|
...helpers.createAuthHeader(this.options),
|
|
2569
2568
|
},
|
|
2570
2569
|
signal,
|
package/src/modules/tracker.js
CHANGED
|
@@ -439,7 +439,7 @@ class Tracker {
|
|
|
439
439
|
* @function trackSearchResultsLoaded
|
|
440
440
|
* @param {string} term - Search results query term
|
|
441
441
|
* @param {object} parameters - Additional parameters to be sent with request
|
|
442
|
-
* @param {number} parameters.num_results -
|
|
442
|
+
* @param {number} parameters.num_results - Total number of results
|
|
443
443
|
* @param {string[]} [parameters.item_ids] - List of product item unique identifiers in search results listing
|
|
444
444
|
* @param {object} userParameters - Parameters relevant to the user request
|
|
445
445
|
* @param {number} userParameters.sessionId - Session ID, utilized to personalize results
|
|
@@ -822,7 +822,7 @@ class Tracker {
|
|
|
822
822
|
* @param {string} parameters.url - Current page URL
|
|
823
823
|
* @param {string} parameters.pod_id - Pod identifier
|
|
824
824
|
* @param {number} parameters.num_results_viewed - Number of results viewed
|
|
825
|
-
* @param {number} [parameters.result_count] -
|
|
825
|
+
* @param {number} [parameters.result_count] - Total number of results
|
|
826
826
|
* @param {number} [parameters.result_page] - Page number of results
|
|
827
827
|
* @param {string} [parameters.result_id] - Recommendation result identifier (returned in response from Constructor)
|
|
828
828
|
* @param {string} [parameters.section="Products"] - Results section
|
|
@@ -932,6 +932,7 @@ class Tracker {
|
|
|
932
932
|
* @param {string} [parameters.variation_id] - Product item variation unique identifier
|
|
933
933
|
* @param {string} [parameters.section="Products"] - Index section
|
|
934
934
|
* @param {string} [parameters.result_id] - Recommendation result identifier (returned in response from Constructor)
|
|
935
|
+
* @param {number} [parameters.result_count] - Total number of results
|
|
935
936
|
* @param {number} [parameters.result_page] - Page number of results
|
|
936
937
|
* @param {number} [parameters.result_position_on_page] - Position of result on page
|
|
937
938
|
* @param {number} [parameters.num_results_per_page] - Number of results on page
|
|
@@ -1057,7 +1058,7 @@ class Tracker {
|
|
|
1057
1058
|
* @param {string} parameters.filter_name - Filter name
|
|
1058
1059
|
* @param {string} parameters.filter_value - Filter value
|
|
1059
1060
|
* @param {string} [parameters.section="Products"] - Index section
|
|
1060
|
-
* @param {number} [parameters.result_count] -
|
|
1061
|
+
* @param {number} [parameters.result_count] - Total number of results
|
|
1061
1062
|
* @param {number} [parameters.result_page] - Page number of results
|
|
1062
1063
|
* @param {string} [parameters.result_id] - Browse result identifier (returned in response from Constructor)
|
|
1063
1064
|
* @param {object} [parameters.selected_filters] - Selected filters
|
|
@@ -1194,7 +1195,7 @@ class Tracker {
|
|
|
1194
1195
|
* @param {string} [parameters.section="Products"] - Index section
|
|
1195
1196
|
* @param {string} [parameters.variation_id] - Product item variation unique identifier
|
|
1196
1197
|
* @param {string} [parameters.result_id] - Browse result identifier (returned in response from Constructor)
|
|
1197
|
-
* @param {number} [parameters.result_count] -
|
|
1198
|
+
* @param {number} [parameters.result_count] - Total number of results
|
|
1198
1199
|
* @param {number} [parameters.result_page] - Page number of results
|
|
1199
1200
|
* @param {number} [parameters.result_position_on_page] - Position of clicked item
|
|
1200
1201
|
* @param {number} [parameters.num_results_per_page] - Number of results shown
|