@constructor-io/constructorio-node 3.7.0 → 3.8.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-node",
3
- "version": "3.7.0",
3
+ "version": "3.8.1",
4
4
  "description": "Constructor.io Node.js client",
5
5
  "main": "src/constructorio.js",
6
6
  "scripts": {
@@ -7,6 +7,7 @@ const Autocomplete = require('./modules/autocomplete');
7
7
  const Recommendations = require('./modules/recommendations');
8
8
  const Tracker = require('./modules/tracker');
9
9
  const Catalog = require('./modules/catalog');
10
+ const Tasks = require('./modules/tasks');
10
11
  const { version: packageVersion } = require('../package.json');
11
12
 
12
13
  /**
@@ -28,6 +29,7 @@ class ConstructorIO {
28
29
  * @property {object} recommendations - Interface to {@link module:recommendations}
29
30
  * @property {object} tracker - Interface to {@link module:tracker}
30
31
  * @property {object} catalog - Interface to {@link module:catalog}
32
+ * @property {object} tasks - Interface to {@link module:tasks}
31
33
  * @returns {class}
32
34
  */
33
35
  constructor(options = {}) {
@@ -62,6 +64,7 @@ class ConstructorIO {
62
64
  this.recommendations = new Recommendations(this.options);
63
65
  this.tracker = new Tracker(this.options);
64
66
  this.catalog = new Catalog(this.options);
67
+ this.tasks = new Tasks(this.options);
65
68
  }
66
69
  }
67
70
 
@@ -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) {
@@ -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,
@@ -0,0 +1,141 @@
1
+ /* eslint-disable object-curly-newline, no-underscore-dangle, max-len */
2
+ const qs = require('qs');
3
+ const nodeFetch = require('node-fetch').default;
4
+ const { AbortController } = require('node-abort-controller');
5
+ const helpers = require('../utils/helpers');
6
+
7
+ // Create URL from supplied path and options
8
+ function createTaskUrl(path, options, additionalQueryParams = {}, apiVersion = 'v1') {
9
+ const {
10
+ apiKey,
11
+ serviceUrl,
12
+ } = options;
13
+ let queryParams = {
14
+ ...additionalQueryParams,
15
+ };
16
+
17
+ // Validate path is provided
18
+ if (!path || typeof path !== 'string') {
19
+ throw new Error('path is a required parameter of type string');
20
+ }
21
+
22
+ queryParams.key = apiKey;
23
+ queryParams = helpers.cleanParams(queryParams);
24
+
25
+ const queryString = qs.stringify(queryParams, { indices: false });
26
+
27
+ return `${serviceUrl}/${encodeURIComponent(apiVersion)}/${encodeURIComponent(path)}?${queryString}`;
28
+ }
29
+
30
+ /**
31
+ * Interface to task related API calls
32
+ *
33
+ * @module tasks
34
+ * @inner
35
+ * @returns {object}
36
+ */
37
+ class Tasks {
38
+ constructor(options) {
39
+ this.options = options || {};
40
+ }
41
+
42
+ /**
43
+ * Retrieve all tasks from index
44
+ *
45
+ * @function getAllTasks
46
+ * @param {object} parameters - Additional parameters for task details
47
+ * @param {number} [parameters.num_results_per_page = 20] - The number of tasks to return - maximum value 100
48
+ * @param {number} [parameters.page = 1] - The page of results to return
49
+ * @param {object} [networkParameters] - Parameters relevant to the network request
50
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
51
+ * @returns {Promise}
52
+ * @see https://docs.constructor.io/rest_api/tasks/#retrieve-all-tasks
53
+ */
54
+ getAllTasks(parameters = {}, networkParameters = {}) {
55
+ const queryParams = {};
56
+ let requestUrl;
57
+ const fetch = (this.options && this.options.fetch) || nodeFetch;
58
+ const controller = new AbortController();
59
+ const { signal } = controller;
60
+
61
+ if (parameters) {
62
+ const { num_results_per_page: numResultsPerPage, page } = parameters;
63
+
64
+ // Pull number of results per page from parameters
65
+ if (numResultsPerPage) {
66
+ queryParams.num_results_per_page = numResultsPerPage;
67
+ }
68
+
69
+ // Pull page from parameters
70
+ if (page) {
71
+ queryParams.page = page;
72
+ }
73
+ }
74
+
75
+ try {
76
+ requestUrl = createTaskUrl('tasks', this.options, queryParams);
77
+ } catch (e) {
78
+ return Promise.reject(e);
79
+ }
80
+
81
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
82
+
83
+ return fetch(requestUrl, {
84
+ method: 'GET',
85
+ headers: {
86
+ 'Content-Type': 'application/json',
87
+ ...helpers.createAuthHeader(this.options),
88
+ },
89
+ signal,
90
+ }).then((response) => {
91
+ if (response.ok) {
92
+ return response.json();
93
+ }
94
+
95
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
96
+ }).then((json) => json);
97
+ }
98
+
99
+ /**
100
+ * Retrieve task given a specific id
101
+ *
102
+ * @function getTask
103
+ * @param {object} parameters - Additional parameters for task details
104
+ * @param {string} parameters.id - The ID of the task to be retrieved
105
+ * @param {object} [networkParameters] - Parameters relevant to the network request
106
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
107
+ * @returns {Promise}
108
+ * @see https://docs.constructor.io/rest_api/tasks/#retrieve-by-specific-task-id
109
+ */
110
+ getTask(parameters = {}, networkParameters = {}) {
111
+ let requestUrl;
112
+ const fetch = (this.options && this.options.fetch) || nodeFetch;
113
+ const controller = new AbortController();
114
+ const { signal } = controller;
115
+
116
+ try {
117
+ requestUrl = createTaskUrl(`tasks/${parameters.id}`, this.options);
118
+ } catch (e) {
119
+ return Promise.reject(e);
120
+ }
121
+
122
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
123
+
124
+ return fetch(requestUrl, {
125
+ method: 'GET',
126
+ headers: {
127
+ 'Content-Type': 'application/json',
128
+ ...helpers.createAuthHeader(this.options),
129
+ },
130
+ signal,
131
+ }).then((response) => {
132
+ if (response.ok) {
133
+ return response.json();
134
+ }
135
+
136
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
137
+ }).then((json) => json);
138
+ }
139
+ }
140
+
141
+ module.exports = Tasks;
@@ -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 - Number of search results in total
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
@@ -782,7 +782,7 @@ class Tracker {
782
782
  }
783
783
 
784
784
  if (items && Array.isArray(items)) {
785
- bodyParams.items = items;
785
+ bodyParams.items = items.slice(0, 100);
786
786
  }
787
787
 
788
788
  if (revenue) {
@@ -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] - Number of results displayed
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] - Number of results displayed
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] - Number of results displayed
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