@constructor-io/constructorio-node 3.7.2 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-node",
3
- "version": "3.7.2",
3
+ "version": "3.8.0",
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
 
@@ -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;