@constructor-io/constructorio-node 3.6.1 → 3.8.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/package.json +1 -1
- package/src/constructorio.js +3 -0
- package/src/modules/tasks.js +141 -0
- package/src/modules/tracker.js +9 -4
- package/src/utils/helpers.js +3 -1
package/package.json
CHANGED
package/src/constructorio.js
CHANGED
|
@@ -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
|
|
|
@@ -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;
|
package/src/modules/tracker.js
CHANGED
|
@@ -477,6 +477,7 @@ class Tracker {
|
|
|
477
477
|
const url = `${this.options.serviceUrl}/behavior?`;
|
|
478
478
|
const queryParams = { action: 'search-results', term };
|
|
479
479
|
const { num_results, customer_ids, item_ids } = parameters;
|
|
480
|
+
let customerIDs;
|
|
480
481
|
|
|
481
482
|
if (!helpers.isNil(num_results)) {
|
|
482
483
|
queryParams.num_results = num_results;
|
|
@@ -484,9 +485,13 @@ class Tracker {
|
|
|
484
485
|
|
|
485
486
|
// Ensure support for both item_ids and customer_ids as parameters
|
|
486
487
|
if (item_ids && Array.isArray(item_ids)) {
|
|
487
|
-
|
|
488
|
+
customerIDs = item_ids;
|
|
488
489
|
} else if (customer_ids && Array.isArray(customer_ids)) {
|
|
489
|
-
|
|
490
|
+
customerIDs = customer_ids;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (customerIDs && Array.isArray(customerIDs) && customerIDs.length) {
|
|
494
|
+
queryParams.customer_ids = customerIDs.slice(0, 100).join(',');
|
|
490
495
|
}
|
|
491
496
|
|
|
492
497
|
const requestUrl = `${url}${applyParamsAsString(queryParams, userParameters, this.options)}`;
|
|
@@ -777,7 +782,7 @@ class Tracker {
|
|
|
777
782
|
}
|
|
778
783
|
|
|
779
784
|
if (items && Array.isArray(items)) {
|
|
780
|
-
bodyParams.items = items;
|
|
785
|
+
bodyParams.items = items.slice(0, 100);
|
|
781
786
|
}
|
|
782
787
|
|
|
783
788
|
if (revenue) {
|
|
@@ -1156,7 +1161,7 @@ class Tracker {
|
|
|
1156
1161
|
}
|
|
1157
1162
|
|
|
1158
1163
|
if (items && Array.isArray(items)) {
|
|
1159
|
-
bodyParams.items = items;
|
|
1164
|
+
bodyParams.items = items.slice(0, 100);
|
|
1160
1165
|
}
|
|
1161
1166
|
|
|
1162
1167
|
const requestUrl = `${requestPath}${applyParamsAsString({}, userParameters, this.options)}`;
|
package/src/utils/helpers.js
CHANGED
|
@@ -58,7 +58,9 @@ const utils = {
|
|
|
58
58
|
// Abort network request based on supplied timeout interval (in milliseconds)
|
|
59
59
|
// - method call parameter takes precedence over global options parameter
|
|
60
60
|
applyNetworkTimeout: (options = {}, networkParameters = {}, controller = undefined) => {
|
|
61
|
-
const
|
|
61
|
+
const optionsTimeout = options && options.networkParameters && options.networkParameters.timeout;
|
|
62
|
+
const networkParametersTimeout = networkParameters && networkParameters.timeout;
|
|
63
|
+
const timeout = optionsTimeout || networkParametersTimeout;
|
|
62
64
|
|
|
63
65
|
if (typeof timeout === 'number') {
|
|
64
66
|
setTimeout(() => controller.abort(), timeout);
|