@constructor-io/constructorio-node 4.12.3 → 4.14.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": "4.12.3",
3
+ "version": "4.14.0",
4
4
  "description": "Constructor.io Node.js client",
5
5
  "main": "src/constructorio.js",
6
6
  "types": "src/types/constructorio.d.ts",
@@ -11,6 +11,7 @@ const Catalog = require('./modules/catalog');
11
11
  const Tasks = require('./modules/tasks');
12
12
  const Quizzes = require('./modules/quizzes');
13
13
  const { version: packageVersion } = require('../package.json');
14
+ const utils = require('./utils/helpers');
14
15
 
15
16
  /**
16
17
  * Class to instantiate the ConstructorIO client.
@@ -50,12 +51,14 @@ class ConstructorIO {
50
51
  throw new Error('API key is a required parameter of type string');
51
52
  }
52
53
 
54
+ const normalizedServiceUrl = serviceUrl && serviceUrl.replace(/\/$/, '');
55
+
53
56
  this.options = {
54
57
  apiKey,
55
58
  apiToken: apiToken || '',
56
59
  securityToken: securityToken || '',
57
60
  version: version || global.CLIENT_VERSION || `cio-node-${packageVersion}`,
58
- serviceUrl: (serviceUrl && serviceUrl.replace(/\/$/, '')) || 'https://ac.cnstrc.com',
61
+ serviceUrl: utils.addHTTPSToString(normalizedServiceUrl) || 'https://ac.cnstrc.com',
59
62
  fetch: fetch || nodeFetch,
60
63
  networkParameters: networkParameters || {},
61
64
  };
@@ -1443,7 +1443,6 @@ class Catalog {
1443
1443
  return fetch(requestUrl, {
1444
1444
  method: 'DELETE',
1445
1445
  headers: {
1446
- 'Content-Type': 'application/json',
1447
1446
  ...helpers.createAuthHeader(this.options),
1448
1447
  },
1449
1448
  signal,
@@ -1485,7 +1484,6 @@ class Catalog {
1485
1484
  return fetch(requestUrl, {
1486
1485
  method: 'DELETE',
1487
1486
  headers: {
1488
- 'Content-Type': 'application/json',
1489
1487
  ...helpers.createAuthHeader(this.options),
1490
1488
  },
1491
1489
  signal,
@@ -199,13 +199,15 @@ class Recommendations {
199
199
  * Get all recommendation pods
200
200
  *
201
201
  * @function getRecommendationPods
202
+ * @param {object} [parameters] - Parameters relevant to the network request
203
+ * @param {string} [parameters.section] - Recommendations section
202
204
  * @param {object} [networkParameters] - Parameters relevant to the network request
203
205
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
204
206
  * @returns {Promise}
205
207
  * @example
206
208
  * constructorio.recommendations.getRecommendationPods();
207
209
  */
208
- getRecommendationPods(networkParameters = {}) {
210
+ getRecommendationPods(parameters = {}, networkParameters = {}) {
209
211
  const {
210
212
  apiKey,
211
213
  serviceUrl,
@@ -214,9 +216,26 @@ class Recommendations {
214
216
  const controller = new AbortController();
215
217
  const { signal } = controller;
216
218
  const headers = {};
217
- const requestUrl = `${serviceUrl}/v1/recommendation_pods?key=${apiKey}`;
219
+ const url = `${serviceUrl}/v1/recommendation_pods`;
218
220
 
219
- Object.assign(headers, helpers.combineCustomHeaders(this.options, networkParameters));
221
+ // For backwards compatibility we allow only "networkParameters" to be passed, meaning "parameters" should be
222
+ // copied to networkParameters. If both parameters and networkParameters are passed we leave them as is
223
+ let parsedNetworkParameters = networkParameters;
224
+ if (parameters.timeout || parameters.headers) {
225
+ parsedNetworkParameters = parameters;
226
+ }
227
+
228
+ const { section } = parameters;
229
+
230
+ let queryParams = {
231
+ key: apiKey,
232
+ };
233
+
234
+ if (section) {
235
+ queryParams.section = section;
236
+ }
237
+
238
+ Object.assign(headers, helpers.combineCustomHeaders(this.options, parsedNetworkParameters));
220
239
 
221
240
  // Append security token as 'x-cnstrc-token' if available
222
241
  if (this.options.securityToken && typeof this.options.securityToken === 'string') {
@@ -224,7 +243,11 @@ class Recommendations {
224
243
  }
225
244
 
226
245
  // Handle network timeout if specified
227
- helpers.applyNetworkTimeout(this.options, networkParameters, controller);
246
+ helpers.applyNetworkTimeout(this.options, parsedNetworkParameters, controller);
247
+
248
+ queryParams = helpers.cleanParams(queryParams);
249
+ const queryString = qs.stringify(queryParams, { indices: false });
250
+ const requestUrl = `${url}?${queryString}`;
228
251
 
229
252
  return fetch(requestUrl, { headers: { ...headers, ...helpers.createAuthHeader(this.options) }, signal })
230
253
  .then((response) => {
@@ -5,6 +5,7 @@ import {
5
5
  RequestFeatureVariant,
6
6
  UserParameters,
7
7
  VariationsMap,
8
+ FilterExpression,
8
9
  } from '.';
9
10
 
10
11
  export default Autocomplete;
@@ -338,3 +338,8 @@ export interface ItemTracked {
338
338
  itemId?: string;
339
339
  variationId?: string;
340
340
  }
341
+
342
+ export interface ItemTrackedPurchased extends ItemTracked {
343
+ price: number,
344
+ count: number
345
+ }
@@ -1,5 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
- import { ConstructorClientOptions, NetworkParameters, ItemTracked } from '.';
2
+ import { ConstructorClientOptions, NetworkParameters, ItemTracked, ItemTrackedPurchased } from '.';
3
3
 
4
4
  export default Tracker;
5
5
 
@@ -116,7 +116,7 @@ declare class Tracker {
116
116
 
117
117
  trackPurchase(
118
118
  parameters: {
119
- items: ItemTracked & {quantity: number}[];
119
+ items: ItemTrackedPurchased[];
120
120
  revenue: number;
121
121
  orderId?: string;
122
122
  section?: string;
@@ -114,6 +114,26 @@ const utils = {
114
114
 
115
115
  return false;
116
116
  },
117
+ addHTTPSToString(url) {
118
+ if (typeof url !== 'string') {
119
+ return null;
120
+ }
121
+
122
+ const doesUrlIncludeHTTPS = url.startsWith('https://');
123
+ const doesUrlStartWithHTTP = url.startsWith('http://');
124
+
125
+ if (!doesUrlIncludeHTTPS && doesUrlStartWithHTTP) {
126
+ return url.replace('http', 'https');
127
+ }
128
+
129
+ if (!doesUrlStartWithHTTP && !doesUrlIncludeHTTPS) {
130
+ const urlWithHttps = `https://${url}`;
131
+
132
+ return urlWithHttps;
133
+ }
134
+
135
+ return url;
136
+ },
117
137
  };
118
138
 
119
139
  module.exports = utils;