@constructor-io/constructorio-node 4.2.0 → 4.3.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.2.0",
3
+ "version": "4.3.0",
4
4
  "description": "Constructor.io Node.js client",
5
5
  "main": "src/constructorio.js",
6
6
  "scripts": {
@@ -54,6 +54,6 @@
54
54
  "form-data": "^4.0.0",
55
55
  "node-abort-controller": "^3.0.0",
56
56
  "node-fetch": "^2.6.2",
57
- "qs": "6.7.2"
57
+ "qs": "6.9.7"
58
58
  }
59
59
  }
@@ -1,4 +1,5 @@
1
1
  /* eslint-disable camelcase, no-unneeded-ternary, max-len */
2
+ const nodeFetch = require('node-fetch').default;
2
3
 
3
4
  // Modules
4
5
  const Search = require('./modules/search');
@@ -55,7 +56,7 @@ class ConstructorIO {
55
56
  securityToken: securityToken || '',
56
57
  version: version || global.CLIENT_VERSION || `cio-node-${packageVersion}`,
57
58
  serviceUrl: (serviceUrl && serviceUrl.replace(/\/$/, '')) || 'https://ac.cnstrc.com',
58
- fetch,
59
+ fetch: fetch || nodeFetch,
59
60
  networkParameters: networkParameters || {},
60
61
  };
61
62
 
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable object-curly-newline, no-underscore-dangle */
2
2
  const qs = require('qs');
3
- const nodeFetch = require('node-fetch').default;
4
3
  const { AbortController } = require('node-abort-controller');
5
4
  const helpers = require('../utils/helpers');
6
5
 
@@ -142,7 +141,7 @@ class Autocomplete {
142
141
  */
143
142
  getAutocompleteResults(query, parameters = {}, userParameters = {}, networkParameters = {}) {
144
143
  let requestUrl;
145
- const fetch = (this.options && this.options.fetch) || nodeFetch;
144
+ const { fetch } = this.options;
146
145
  const controller = new AbortController();
147
146
  const { signal } = controller;
148
147
  const headers = {};
@@ -1,7 +1,6 @@
1
1
  /* eslint-disable max-len */
2
2
  /* eslint-disable object-curly-newline, no-underscore-dangle, max-params */
3
3
  const qs = require('qs');
4
- const nodeFetch = require('node-fetch').default;
5
4
  const { AbortController } = require('node-abort-controller');
6
5
  const helpers = require('../utils/helpers');
7
6
 
@@ -272,7 +271,7 @@ class Browse {
272
271
  */
273
272
  getBrowseResults(filterName, filterValue, parameters = {}, userParameters = {}, networkParameters = {}) {
274
273
  let requestUrl;
275
- const fetch = (this.options && this.options.fetch) || nodeFetch;
274
+ const { fetch } = this.options;
276
275
  const controller = new AbortController();
277
276
  const { signal } = controller;
278
277
  const headers = createHeaders(this.options, userParameters, networkParameters);
@@ -356,7 +355,7 @@ class Browse {
356
355
  */
357
356
  getBrowseResultsForItemIds(itemIds, parameters = {}, userParameters = {}, networkParameters = {}) {
358
357
  let requestUrl;
359
- const fetch = (this.options && this.options.fetch) || nodeFetch;
358
+ const { fetch } = this.options;
360
359
  const controller = new AbortController();
361
360
  const { signal } = controller;
362
361
  const headers = createHeaders(this.options, userParameters, networkParameters);
@@ -427,7 +426,7 @@ class Browse {
427
426
  * });
428
427
  */
429
428
  getBrowseGroups(parameters = {}, userParameters = {}, networkParameters = {}) {
430
- const fetch = (this.options && this.options.fetch) || nodeFetch;
429
+ const { fetch } = this.options;
431
430
  const controller = new AbortController();
432
431
  const { signal } = controller;
433
432
  const headers = createHeaders(this.options, userParameters, networkParameters);
@@ -487,7 +486,7 @@ class Browse {
487
486
  */
488
487
  getBrowseFacets(parameters = {}, userParameters = {}, networkParameters = {}) {
489
488
  let requestUrl;
490
- const fetch = (this.options && this.options.fetch) || nodeFetch;
489
+ const { fetch } = this.options;
491
490
  const controller = new AbortController();
492
491
  const { signal } = controller;
493
492
  const headers = createHeaders(this.options, userParameters, networkParameters);
@@ -542,7 +541,7 @@ class Browse {
542
541
  */
543
542
  getBrowseFacetOptions(facetName, parameters = {}, userParameters = {}, networkParameters = {}) {
544
543
  let requestUrl;
545
- const fetch = (this.options && this.options.fetch) || nodeFetch;
544
+ const { fetch } = this.options;
546
545
  const controller = new AbortController();
547
546
  const { signal } = controller;
548
547
  const headers = createHeaders(this.options, userParameters, networkParameters);
@@ -1,7 +1,6 @@
1
1
  /* eslint-disable camelcase */
2
2
  /* eslint-disable object-curly-newline, no-underscore-dangle, max-len */
3
3
  const qs = require('qs');
4
- const nodeFetch = require('node-fetch').default;
5
4
  const { AbortController } = require('node-abort-controller');
6
5
  const FormData = require('form-data');
7
6
  const fs = require('fs');
@@ -34,14 +33,14 @@ function createCatalogUrl(path, options, additionalQueryParams = {}, apiVersion
34
33
  // Convert a read stream to buffer
35
34
  function convertToBuffer(stream) {
36
35
  return new Promise((resolve, reject) => {
37
- let buffer = '';
36
+ const chunks = [];
38
37
 
39
38
  stream.on('data', (chunk) => {
40
- buffer += chunk;
39
+ chunks.push(chunk);
41
40
  });
42
41
 
43
42
  stream.on('end', () => {
44
- resolve(buffer);
43
+ resolve(Buffer.concat(chunks));
45
44
  });
46
45
 
47
46
  stream.on('error', (err) => {
@@ -118,6 +117,37 @@ async function createQueryParamsAndFormData(parameters) {
118
117
  return { queryParams, formData };
119
118
  }
120
119
 
120
+ async function addTarArchiveToFormData(parameters, formData, operation, apiKey) {
121
+ try {
122
+ const { section } = parameters;
123
+ let { tarArchive } = parameters;
124
+
125
+ // Convert tarArchive to buffer if passed as stream
126
+ if (tarArchive instanceof fs.ReadStream || tarArchive instanceof Duplex) {
127
+ tarArchive = await convertToBuffer(tarArchive);
128
+ }
129
+
130
+ // Pull tarArchive from parameters
131
+ if (tarArchive && formData && operation && apiKey && section) {
132
+ // Convert timestamp to YYYY-MM-DD-HH-MM-SS format
133
+ const formattedDateTime = new Date()
134
+ .toISOString()
135
+ .replace('T', '-')
136
+ .replace(/:/g, '-')
137
+ .slice(0, 19);
138
+ const filename = `${apiKey}_${section}_${operation}_${formattedDateTime}.tar.gz`;
139
+
140
+ formData.append(filename, tarArchive, {
141
+ filename,
142
+ });
143
+ }
144
+ } catch (error) {
145
+ throw new Error(error);
146
+ }
147
+
148
+ return formData;
149
+ }
150
+
121
151
  /**
122
152
  * Interface to catalog related API calls
123
153
  *
@@ -162,7 +192,7 @@ class Catalog {
162
192
  */
163
193
  createOrReplaceItems(parameters = {}, networkParameters = {}) {
164
194
  let requestUrl;
165
- const fetch = (this.options && this.options.fetch) || nodeFetch;
195
+ const { fetch } = this.options;
166
196
  const controller = new AbortController();
167
197
  const { signal } = controller;
168
198
  const { items, section, force, notificationEmail } = parameters;
@@ -244,7 +274,7 @@ class Catalog {
244
274
  */
245
275
  updateItems(parameters = {}, networkParameters = {}) {
246
276
  let requestUrl;
247
- const fetch = (this.options && this.options.fetch) || nodeFetch;
277
+ const { fetch } = this.options;
248
278
  const controller = new AbortController();
249
279
  const { signal } = controller;
250
280
  const { items, section, force, notificationEmail } = parameters;
@@ -316,7 +346,7 @@ class Catalog {
316
346
  */
317
347
  deleteItems(parameters = {}, networkParameters = {}) {
318
348
  let requestUrl;
319
- const fetch = (this.options && this.options.fetch) || nodeFetch;
349
+ const { fetch } = this.options;
320
350
  const controller = new AbortController();
321
351
  const { signal } = controller;
322
352
  const { items, section, force, notificationEmail } = parameters;
@@ -394,7 +424,7 @@ class Catalog {
394
424
  */
395
425
  retrieveItems(parameters = {}, networkParameters = {}) {
396
426
  let requestUrl;
397
- const fetch = (this.options && this.options.fetch) || nodeFetch;
427
+ const { fetch } = this.options;
398
428
  const controller = new AbortController();
399
429
  const { signal } = controller;
400
430
  const { ids, section, numResultsPerPage, page } = parameters;
@@ -476,7 +506,7 @@ class Catalog {
476
506
  */
477
507
  createOrReplaceVariations(parameters = {}, networkParameters = {}) {
478
508
  let requestUrl;
479
- const fetch = (this.options && this.options.fetch) || nodeFetch;
509
+ const { fetch } = this.options;
480
510
  const controller = new AbortController();
481
511
  const { signal } = controller;
482
512
  const { section, force, notificationEmail, variations } = parameters;
@@ -559,7 +589,7 @@ class Catalog {
559
589
  */
560
590
  updateVariations(parameters = {}, networkParameters = {}) {
561
591
  let requestUrl;
562
- const fetch = (this.options && this.options.fetch) || nodeFetch;
592
+ const { fetch } = this.options;
563
593
  const controller = new AbortController();
564
594
  const { signal } = controller;
565
595
  const { section, force, notificationEmail, variations } = parameters;
@@ -632,7 +662,7 @@ class Catalog {
632
662
  */
633
663
  deleteVariations(parameters = {}, networkParameters = {}) {
634
664
  let requestUrl;
635
- const fetch = (this.options && this.options.fetch) || nodeFetch;
665
+ const { fetch } = this.options;
636
666
  const controller = new AbortController();
637
667
  const { signal } = controller;
638
668
  const { section, force, notificationEmail, variations } = parameters;
@@ -712,7 +742,7 @@ class Catalog {
712
742
  retrieveVariations(parameters = {}, networkParameters = {}) {
713
743
  let queryParams = {};
714
744
  let requestUrl;
715
- const fetch = (this.options && this.options.fetch) || nodeFetch;
745
+ const { fetch } = this.options;
716
746
  const controller = new AbortController();
717
747
  const { signal } = controller;
718
748
  const { ids, itemId, section, numResultsPerPage, page } = parameters;
@@ -785,7 +815,7 @@ class Catalog {
785
815
  */
786
816
  addItemGroup(parameters = {}, networkParameters = {}) {
787
817
  let requestUrl;
788
- const fetch = (this.options && this.options.fetch) || nodeFetch;
818
+ const { fetch } = this.options;
789
819
  const controller = new AbortController();
790
820
  const { signal } = controller;
791
821
  const { id, ...rest } = parameters;
@@ -843,7 +873,7 @@ class Catalog {
843
873
  */
844
874
  addItemGroups(parameters = {}, networkParameters = {}) {
845
875
  let requestUrl;
846
- const fetch = (this.options && this.options.fetch) || nodeFetch;
876
+ const { fetch } = this.options;
847
877
  const controller = new AbortController();
848
878
  const { signal } = controller;
849
879
 
@@ -890,7 +920,7 @@ class Catalog {
890
920
  */
891
921
  getItemGroup(parameters = {}, networkParameters = {}) {
892
922
  let requestUrl;
893
- const fetch = (this.options && this.options.fetch) || nodeFetch;
923
+ const { fetch } = this.options;
894
924
  const controller = new AbortController();
895
925
  const { signal } = controller;
896
926
 
@@ -932,7 +962,7 @@ class Catalog {
932
962
  */
933
963
  getItemGroups(networkParameters = {}) {
934
964
  let requestUrl;
935
- const fetch = (this.options && this.options.fetch) || nodeFetch;
965
+ const { fetch } = this.options;
936
966
  const controller = new AbortController();
937
967
  const { signal } = controller;
938
968
 
@@ -988,7 +1018,7 @@ class Catalog {
988
1018
  */
989
1019
  addOrUpdateItemGroups(parameters = {}, networkParameters = {}) {
990
1020
  let requestUrl;
991
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1021
+ const { fetch } = this.options;
992
1022
  const controller = new AbortController();
993
1023
  const { signal } = controller;
994
1024
 
@@ -1043,7 +1073,7 @@ class Catalog {
1043
1073
  */
1044
1074
  modifyItemGroup(parameters = {}, networkParameters = {}) {
1045
1075
  let requestUrl;
1046
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1076
+ const { fetch } = this.options;
1047
1077
  const controller = new AbortController();
1048
1078
  const { signal } = controller;
1049
1079
  const { id, ...rest } = parameters;
@@ -1087,7 +1117,7 @@ class Catalog {
1087
1117
  */
1088
1118
  removeItemGroups(networkParameters = {}) {
1089
1119
  let requestUrl;
1090
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1120
+ const { fetch } = this.options;
1091
1121
  const controller = new AbortController();
1092
1122
  const { signal } = controller;
1093
1123
 
@@ -1135,7 +1165,7 @@ class Catalog {
1135
1165
  */
1136
1166
  addOneWaySynonym(parameters = {}, networkParameters = {}) {
1137
1167
  let requestUrl;
1138
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1168
+ const { fetch } = this.options;
1139
1169
  const controller = new AbortController();
1140
1170
  const { signal } = controller;
1141
1171
  const { phrase, ...rest } = parameters;
@@ -1189,7 +1219,7 @@ class Catalog {
1189
1219
  */
1190
1220
  modifyOneWaySynonym(parameters = {}, networkParameters = {}) {
1191
1221
  let requestUrl;
1192
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1222
+ const { fetch } = this.options;
1193
1223
  const controller = new AbortController();
1194
1224
  const { signal } = controller;
1195
1225
  const { phrase, ...rest } = parameters;
@@ -1239,7 +1269,7 @@ class Catalog {
1239
1269
  const { phrase } = parameters;
1240
1270
  const queryParams = {};
1241
1271
  let requestUrl;
1242
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1272
+ const { fetch } = this.options;
1243
1273
  const controller = new AbortController();
1244
1274
  const { signal } = controller;
1245
1275
 
@@ -1288,7 +1318,7 @@ class Catalog {
1288
1318
  getOneWaySynonyms(parameters = {}, networkParameters = {}) {
1289
1319
  const queryParams = {};
1290
1320
  let requestUrl;
1291
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1321
+ const { fetch } = this.options;
1292
1322
  const controller = new AbortController();
1293
1323
  const { signal } = controller;
1294
1324
 
@@ -1349,7 +1379,7 @@ class Catalog {
1349
1379
  removeOneWaySynonym(parameters = {}, networkParameters = {}) {
1350
1380
  const { phrase } = parameters;
1351
1381
  let requestUrl;
1352
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1382
+ const { fetch } = this.options;
1353
1383
  const controller = new AbortController();
1354
1384
  const { signal } = controller;
1355
1385
 
@@ -1391,7 +1421,7 @@ class Catalog {
1391
1421
  */
1392
1422
  removeOneWaySynonyms(networkParameters = {}) {
1393
1423
  let requestUrl;
1394
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1424
+ const { fetch } = this.options;
1395
1425
  const controller = new AbortController();
1396
1426
  const { signal } = controller;
1397
1427
 
@@ -1437,7 +1467,7 @@ class Catalog {
1437
1467
  */
1438
1468
  addSynonymGroup(parameters = {}, networkParameters = {}) {
1439
1469
  let requestUrl;
1440
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1470
+ const { fetch } = this.options;
1441
1471
  const controller = new AbortController();
1442
1472
  const { signal } = controller;
1443
1473
 
@@ -1486,7 +1516,7 @@ class Catalog {
1486
1516
  */
1487
1517
  modifySynonymGroup(parameters = {}, networkParameters = {}) {
1488
1518
  let requestUrl;
1489
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1519
+ const { fetch } = this.options;
1490
1520
  const controller = new AbortController();
1491
1521
  const { signal } = controller;
1492
1522
  const { id, ...rest } = parameters;
@@ -1534,7 +1564,7 @@ class Catalog {
1534
1564
  */
1535
1565
  getSynonymGroup(parameters = {}, networkParameters = {}) {
1536
1566
  let requestUrl;
1537
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1567
+ const { fetch } = this.options;
1538
1568
  const controller = new AbortController();
1539
1569
  const { signal } = controller;
1540
1570
 
@@ -1583,7 +1613,7 @@ class Catalog {
1583
1613
  const queryParams = {};
1584
1614
  const { phrase } = parameters;
1585
1615
  let requestUrl;
1586
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1616
+ const { fetch } = this.options;
1587
1617
  const controller = new AbortController();
1588
1618
  const { signal } = controller;
1589
1619
 
@@ -1640,7 +1670,7 @@ class Catalog {
1640
1670
  */
1641
1671
  removeSynonymGroup(parameters = {}, networkParameters = {}) {
1642
1672
  let requestUrl;
1643
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1673
+ const { fetch } = this.options;
1644
1674
  const controller = new AbortController();
1645
1675
  const { signal } = controller;
1646
1676
  const { id } = parameters;
@@ -1680,7 +1710,7 @@ class Catalog {
1680
1710
  */
1681
1711
  removeSynonymGroups(networkParameters = {}) {
1682
1712
  let requestUrl;
1683
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1713
+ const { fetch } = this.options;
1684
1714
  const controller = new AbortController();
1685
1715
  const { signal } = controller;
1686
1716
 
@@ -1738,7 +1768,7 @@ class Catalog {
1738
1768
  */
1739
1769
  addRedirectRule(parameters = {}, networkParameters = {}) {
1740
1770
  let requestUrl;
1741
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1771
+ const { fetch } = this.options;
1742
1772
  const controller = new AbortController();
1743
1773
  const { signal } = controller;
1744
1774
 
@@ -1800,7 +1830,7 @@ class Catalog {
1800
1830
  */
1801
1831
  updateRedirectRule(parameters = {}, networkParameters = {}) {
1802
1832
  let requestUrl;
1803
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1833
+ const { fetch } = this.options;
1804
1834
  const controller = new AbortController();
1805
1835
  const { signal } = controller;
1806
1836
  const { id, ...rest } = parameters;
@@ -1860,7 +1890,7 @@ class Catalog {
1860
1890
  */
1861
1891
  modifyRedirectRule(parameters = {}, networkParameters = {}) {
1862
1892
  let requestUrl;
1863
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1893
+ const { fetch } = this.options;
1864
1894
  const controller = new AbortController();
1865
1895
  const { signal } = controller;
1866
1896
  const { id, ...rest } = parameters;
@@ -1908,7 +1938,7 @@ class Catalog {
1908
1938
  */
1909
1939
  getRedirectRule(parameters = {}, networkParameters = {}) {
1910
1940
  let requestUrl;
1911
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1941
+ const { fetch } = this.options;
1912
1942
  const controller = new AbortController();
1913
1943
  const { signal } = controller;
1914
1944
 
@@ -1958,7 +1988,7 @@ class Catalog {
1958
1988
  getRedirectRules(parameters = {}, networkParameters = {}) {
1959
1989
  const queryParams = {};
1960
1990
  let requestUrl;
1961
- const fetch = (this.options && this.options.fetch) || nodeFetch;
1991
+ const { fetch } = this.options;
1962
1992
  const controller = new AbortController();
1963
1993
  const { signal } = controller;
1964
1994
 
@@ -2025,7 +2055,7 @@ class Catalog {
2025
2055
  */
2026
2056
  removeRedirectRule(parameters = {}, networkParameters = {}) {
2027
2057
  let requestUrl;
2028
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2058
+ const { fetch } = this.options;
2029
2059
  const controller = new AbortController();
2030
2060
  const { signal } = controller;
2031
2061
 
@@ -2077,7 +2107,7 @@ class Catalog {
2077
2107
  */
2078
2108
  async replaceCatalog(parameters = {}, networkParameters = {}) {
2079
2109
  try {
2080
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2110
+ const { fetch } = this.options;
2081
2111
  const controller = new AbortController();
2082
2112
  const { signal } = controller;
2083
2113
  const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
@@ -2129,7 +2159,7 @@ class Catalog {
2129
2159
  */
2130
2160
  async updateCatalog(parameters = {}, networkParameters = {}) {
2131
2161
  try {
2132
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2162
+ const { fetch } = this.options;
2133
2163
  const controller = new AbortController();
2134
2164
  const { signal } = controller;
2135
2165
  const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
@@ -2181,7 +2211,7 @@ class Catalog {
2181
2211
  */
2182
2212
  async patchCatalog(parameters = {}, networkParameters = {}) {
2183
2213
  try {
2184
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2214
+ const { fetch } = this.options;
2185
2215
  const controller = new AbortController();
2186
2216
  const { signal } = controller;
2187
2217
  const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
@@ -2207,6 +2237,155 @@ class Catalog {
2207
2237
  }
2208
2238
  }
2209
2239
 
2240
+ /**
2241
+ * Send full catalog tar archive to replace the current catalog
2242
+ *
2243
+ * @function replaceCatalogUsingTarArchive
2244
+ * @param {object} parameters - Additional parameters for catalog details
2245
+ * @param {string} parameters.section - The section to update
2246
+ * @param {string} [parameters.notification_email] - An email address to receive an email notification if the task fails
2247
+ * @param {boolean} [parameters.force=false] - Process the catalog even if it will invalidate a large number of existing items
2248
+ * @param {file} [parameters.tarArchive] - The tar file that includes csv files
2249
+ * @param {object} [networkParameters] - Parameters relevant to the network request
2250
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
2251
+ * @returns {Promise}
2252
+ * @see https://docs.constructor.io/rest_api/full_catalog
2253
+ * @example
2254
+ * constructorio.catalog.replaceCatalogUsingTarArchive({
2255
+ * section: 'Products',
2256
+ * notification_email: 'notifications@example.com',
2257
+ * tarArchive: tarArchiveBufferOrStream,
2258
+ * });
2259
+ */
2260
+ async replaceCatalogUsingTarArchive(parameters = {}, networkParameters = {}) {
2261
+ try {
2262
+ const { fetch } = this.options;
2263
+ const apiKey = this.options && this.options.apiKey;
2264
+ const controller = new AbortController();
2265
+ const { signal } = controller;
2266
+ const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
2267
+ const formDataWithTarArchive = await addTarArchiveToFormData(parameters, formData, 'sync', apiKey);
2268
+ const requestUrl = createCatalogUrl('catalog', this.options, queryParams);
2269
+ // Handle network timeout if specified
2270
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2271
+
2272
+ const response = await fetch(requestUrl, {
2273
+ method: 'PUT',
2274
+ body: formDataWithTarArchive,
2275
+ headers: helpers.createAuthHeader(this.options),
2276
+ signal,
2277
+ });
2278
+
2279
+ if (response.ok) {
2280
+ return Promise.resolve(response.json());
2281
+ }
2282
+
2283
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
2284
+ } catch (error) {
2285
+ return Promise.reject(error);
2286
+ }
2287
+ }
2288
+
2289
+ /**
2290
+ * Send delta catalog tar archive to update the current catalog
2291
+ *
2292
+ * @function updateCatalogUsingTarArchive
2293
+ * @param {object} parameters - Additional parameters for catalog details
2294
+ * @param {string} parameters.section - The section to update
2295
+ * @param {string} [parameters.notification_email] - An email address to receive an email notification if the task fails
2296
+ * @param {boolean} [parameters.force=false] - Process the catalog even if it will invalidate a large number of existing items
2297
+ * @param {file} [parameters.tarArchive] - The tar file that includes csv files
2298
+ * @param {object} [networkParameters] - Parameters relevant to the network request
2299
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
2300
+ * @returns {Promise}
2301
+ * @see https://docs.constructor.io/rest_api/full_catalog
2302
+ * @example
2303
+ * constructorio.catalog.updateCatalogUsingTarArchive({
2304
+ * section: 'Products',
2305
+ * notification_email: 'notifications@example.com',
2306
+ * tarArchive: tarArchiveBufferOrStream,
2307
+ * });
2308
+ */
2309
+ async updateCatalogUsingTarArchive(parameters = {}, networkParameters = {}) {
2310
+ try {
2311
+ const { fetch } = this.options;
2312
+ const apiKey = this.options && this.options.apiKey;
2313
+ const controller = new AbortController();
2314
+ const { signal } = controller;
2315
+ const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
2316
+ const formDataWithTarArchive = await addTarArchiveToFormData(parameters, formData, 'delta', apiKey);
2317
+ const requestUrl = createCatalogUrl('catalog', this.options, queryParams);
2318
+
2319
+ // Handle network timeout if specified
2320
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2321
+
2322
+ const response = await fetch(requestUrl, {
2323
+ method: 'PATCH',
2324
+ body: formDataWithTarArchive,
2325
+ headers: helpers.createAuthHeader(this.options),
2326
+ signal,
2327
+ });
2328
+
2329
+ if (response.ok) {
2330
+ return Promise.resolve(response.json());
2331
+ }
2332
+
2333
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
2334
+ } catch (error) {
2335
+ return Promise.reject(error);
2336
+ }
2337
+ }
2338
+
2339
+ /**
2340
+ * Send patch delta tar archive to patch the current catalog
2341
+ *
2342
+ * @function patchCatalogUsingTarArchive
2343
+ * @param {object} parameters - Additional parameters for catalog details
2344
+ * @param {string} parameters.section - The section to update
2345
+ * @param {string} [parameters.notification_email] - An email address to receive an email notification if the task fails
2346
+ * @param {boolean} [parameters.force=false] - Process the catalog even if it will invalidate a large number of existing items
2347
+ * @param {file} [parameters.tarArchive] - The tar file that includes csv files
2348
+ * @param {object} [networkParameters] - Parameters relevant to the network request
2349
+ * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
2350
+ * @returns {Promise}
2351
+ * @see https://docs.constructor.io/rest_api/full_catalog
2352
+ * @example
2353
+ * constructorio.catalog.patchCatalogUsingTarArchive({
2354
+ * section: 'Products',
2355
+ * notification_email: 'notifications@example.com',
2356
+ * tarArchive: tarArchiveBufferOrStream,
2357
+ * });
2358
+ */
2359
+ async patchCatalogUsingTarArchive(parameters = {}, networkParameters = {}) {
2360
+ try {
2361
+ const { fetch } = this.options;
2362
+ const apiKey = this.options && this.options.apiKey;
2363
+ const controller = new AbortController();
2364
+ const { signal } = controller;
2365
+ const { queryParams, formData } = await createQueryParamsAndFormData(parameters);
2366
+ const formDataWithTarArchive = await addTarArchiveToFormData(parameters, formData, 'delta', apiKey);
2367
+ const requestUrl = createCatalogUrl('catalog', this.options, { ...queryParams, patch_delta: true });
2368
+
2369
+ // Handle network timeout if specified
2370
+ helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2371
+
2372
+ const response = await fetch(requestUrl, {
2373
+ method: 'PATCH',
2374
+ body: formDataWithTarArchive,
2375
+ headers: helpers.createAuthHeader(this.options),
2376
+ signal,
2377
+ });
2378
+
2379
+ if (response.ok) {
2380
+ return Promise.resolve(response.json());
2381
+ }
2382
+
2383
+ return helpers.throwHttpErrorFromResponse(new Error(), response);
2384
+ } catch (error) {
2385
+ return Promise.reject(error);
2386
+ }
2387
+ }
2388
+
2210
2389
  /**
2211
2390
  * Create a facet configuration
2212
2391
  *
@@ -2245,7 +2424,7 @@ class Catalog {
2245
2424
  */
2246
2425
  addFacetConfiguration(parameters = {}, networkParameters = {}) {
2247
2426
  let requestUrl;
2248
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2427
+ const { fetch } = this.options;
2249
2428
  const controller = new AbortController();
2250
2429
  const { signal } = controller;
2251
2430
  const { section, ...rest } = parameters;
@@ -2261,7 +2440,6 @@ class Catalog {
2261
2440
 
2262
2441
  // Handle network timeout if specified
2263
2442
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
2264
-
2265
2443
  return fetch(requestUrl, {
2266
2444
  method: 'POST',
2267
2445
  body: JSON.stringify(rest),
@@ -2299,7 +2477,7 @@ class Catalog {
2299
2477
  */
2300
2478
  getFacetConfigurations(parameters = {}, networkParameters = {}) {
2301
2479
  let requestUrl;
2302
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2480
+ const { fetch } = this.options;
2303
2481
  const controller = new AbortController();
2304
2482
  const { signal } = controller;
2305
2483
  const additionalQueryParams = {
@@ -2349,7 +2527,7 @@ class Catalog {
2349
2527
  */
2350
2528
  getFacetConfiguration(parameters = {}, networkParameters = {}) {
2351
2529
  let requestUrl;
2352
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2530
+ const { fetch } = this.options;
2353
2531
  const controller = new AbortController();
2354
2532
  const { signal } = controller;
2355
2533
  const { section, name } = parameters;
@@ -2412,7 +2590,7 @@ class Catalog {
2412
2590
  */
2413
2591
  modifyFacetConfigurations(parameters = {}, networkParameters = {}) {
2414
2592
  let requestUrl;
2415
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2593
+ const { fetch } = this.options;
2416
2594
  const controller = new AbortController();
2417
2595
  const { signal } = controller;
2418
2596
  const { section, facetConfigurations } = parameters;
@@ -2486,7 +2664,7 @@ class Catalog {
2486
2664
  */
2487
2665
  replaceFacetConfiguration(parameters = {}, networkParameters = {}) {
2488
2666
  let requestUrl;
2489
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2667
+ const { fetch } = this.options;
2490
2668
  const controller = new AbortController();
2491
2669
  const { signal } = controller;
2492
2670
  const { section, name, ...rest } = parameters;
@@ -2558,7 +2736,7 @@ class Catalog {
2558
2736
  */
2559
2737
  modifyFacetConfiguration(parameters = {}, networkParameters = {}) {
2560
2738
  let requestUrl;
2561
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2739
+ const { fetch } = this.options;
2562
2740
  const controller = new AbortController();
2563
2741
  const { signal } = controller;
2564
2742
  const { section, name, ...rest } = parameters;
@@ -2612,7 +2790,7 @@ class Catalog {
2612
2790
  */
2613
2791
  removeFacetConfiguration(parameters = {}, networkParameters = {}) {
2614
2792
  let requestUrl;
2615
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2793
+ const { fetch } = this.options;
2616
2794
  const controller = new AbortController();
2617
2795
  const { signal } = controller;
2618
2796
  const { section, name } = parameters;
@@ -2670,7 +2848,7 @@ class Catalog {
2670
2848
  */
2671
2849
  addFacetOptionConfiguration(parameters = {}, networkParameters = {}) {
2672
2850
  let requestUrl;
2673
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2851
+ const { fetch } = this.options;
2674
2852
  const controller = new AbortController();
2675
2853
  const { signal } = controller;
2676
2854
  const { facetGroupName, section, ...rest } = parameters;
@@ -2735,7 +2913,7 @@ class Catalog {
2735
2913
  */
2736
2914
  addOrModifyFacetOptionConfigurations(parameters = {}, networkParameters = {}) {
2737
2915
  let requestUrl;
2738
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2916
+ const { fetch } = this.options;
2739
2917
  const controller = new AbortController();
2740
2918
  const { signal } = controller;
2741
2919
  const { facetGroupName, section, facetOptionConfigurations } = parameters;
@@ -2791,7 +2969,7 @@ class Catalog {
2791
2969
  */
2792
2970
  getFacetOptionConfigurations(parameters = {}, networkParameters = {}) {
2793
2971
  let requestUrl;
2794
- const fetch = (this.options && this.options.fetch) || nodeFetch;
2972
+ const { fetch } = this.options;
2795
2973
  const controller = new AbortController();
2796
2974
  const { signal } = controller;
2797
2975
  const { facetGroupName, section } = parameters;
@@ -2844,7 +3022,7 @@ class Catalog {
2844
3022
  */
2845
3023
  getFacetOptionConfiguration(parameters = {}, networkParameters = {}) {
2846
3024
  let requestUrl;
2847
- const fetch = (this.options && this.options.fetch) || nodeFetch;
3025
+ const { fetch } = this.options;
2848
3026
  const controller = new AbortController();
2849
3027
  const { signal } = controller;
2850
3028
  const { facetGroupName, value, section } = parameters;
@@ -2903,7 +3081,7 @@ class Catalog {
2903
3081
  */
2904
3082
  replaceFacetOptionConfiguration(parameters = {}, networkParameters = {}) {
2905
3083
  let requestUrl;
2906
- const fetch = (this.options && this.options.fetch) || nodeFetch;
3084
+ const { fetch } = this.options;
2907
3085
  const controller = new AbortController();
2908
3086
  const { signal } = controller;
2909
3087
  const { facetGroupName, section, value, ...rest } = parameters;
@@ -2963,7 +3141,7 @@ class Catalog {
2963
3141
  */
2964
3142
  modifyFacetOptionConfiguration(parameters = {}, networkParameters = {}) {
2965
3143
  let requestUrl;
2966
- const fetch = (this.options && this.options.fetch) || nodeFetch;
3144
+ const { fetch } = this.options;
2967
3145
  const controller = new AbortController();
2968
3146
  const { signal } = controller;
2969
3147
  const { facetGroupName, section, value, ...rest } = parameters;
@@ -3017,7 +3195,7 @@ class Catalog {
3017
3195
  */
3018
3196
  removeFacetOptionConfiguration(parameters = {}, networkParameters = {}) {
3019
3197
  let requestUrl;
3020
- const fetch = (this.options && this.options.fetch) || nodeFetch;
3198
+ const { fetch } = this.options;
3021
3199
  const controller = new AbortController();
3022
3200
  const { signal } = controller;
3023
3201
  const { facetGroupName, value } = parameters;
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable object-curly-newline, no-underscore-dangle */
2
2
  const qs = require('qs');
3
- const nodeFetch = require('node-fetch').default;
4
3
  const helpers = require('../utils/helpers');
5
4
 
6
5
  // Create URL from supplied quizId and parameters
@@ -115,7 +114,7 @@ class Quizzes {
115
114
  getQuizNextQuestion(quizId, parameters, userParameters = {}, networkParameters = {}) {
116
115
  const headers = {};
117
116
  let requestUrl;
118
- const fetch = (this.options && this.options.fetch) || nodeFetch;
117
+ const { fetch } = this.options;
119
118
  const controller = new AbortController();
120
119
  const { signal } = controller;
121
120
 
@@ -191,7 +190,7 @@ class Quizzes {
191
190
  getQuizResults(quizId, parameters, userParameters = {}, networkParameters = {}) {
192
191
  let requestUrl;
193
192
  const headers = {};
194
- const fetch = (this.options && this.options.fetch) || nodeFetch;
193
+ const { fetch } = this.options;
195
194
  const controller = new AbortController();
196
195
  const { signal } = controller;
197
196
 
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable object-curly-newline, no-param-reassign */
2
2
  const qs = require('qs');
3
- const nodeFetch = require('node-fetch').default;
4
3
  const { AbortController } = require('node-abort-controller');
5
4
  const helpers = require('../utils/helpers');
6
5
 
@@ -129,7 +128,7 @@ class Recommendations {
129
128
  */
130
129
  getRecommendations(podId, parameters = {}, userParameters = {}, networkParameters = {}) {
131
130
  let requestUrl;
132
- const fetch = (this.options && this.options.fetch) || nodeFetch;
131
+ const { fetch } = this.options;
133
132
  const controller = new AbortController();
134
133
  const { signal } = controller;
135
134
  const headers = {};
@@ -205,7 +204,7 @@ class Recommendations {
205
204
  apiKey,
206
205
  serviceUrl,
207
206
  } = this.options;
208
- const fetch = (this.options && this.options.fetch) || nodeFetch;
207
+ const { fetch } = this.options;
209
208
  const controller = new AbortController();
210
209
  const { signal } = controller;
211
210
  const headers = {};
@@ -1,7 +1,6 @@
1
1
  /* eslint-disable max-len */
2
2
  /* eslint-disable object-curly-newline, no-underscore-dangle */
3
3
  const qs = require('qs');
4
- const nodeFetch = require('node-fetch').default;
5
4
  const { AbortController } = require('node-abort-controller');
6
5
  const helpers = require('../utils/helpers');
7
6
 
@@ -191,7 +190,7 @@ class Search {
191
190
 
192
191
  getSearchResults(query, parameters = {}, userParameters = {}, networkParameters = {}) {
193
192
  let requestUrl;
194
- const fetch = (this.options && this.options.fetch) || nodeFetch;
193
+ const { fetch } = this.options;
195
194
  const controller = new AbortController();
196
195
  const { signal } = controller;
197
196
  const headers = {};
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable object-curly-newline, no-underscore-dangle, max-len */
2
2
  const qs = require('qs');
3
- const nodeFetch = require('node-fetch').default;
4
3
  const { AbortController } = require('node-abort-controller');
5
4
  const helpers = require('../utils/helpers');
6
5
 
@@ -58,7 +57,7 @@ class Tasks {
58
57
  getAllTasks(parameters = {}, networkParameters = {}) {
59
58
  const queryParams = {};
60
59
  let requestUrl;
61
- const fetch = (this.options && this.options.fetch) || nodeFetch;
60
+ const { fetch } = this.options;
62
61
  const controller = new AbortController();
63
62
  const { signal } = controller;
64
63
  const headers = {
@@ -134,7 +133,7 @@ class Tasks {
134
133
  */
135
134
  getTask(parameters = {}, networkParameters = {}) {
136
135
  let requestUrl;
137
- const fetch = (this.options && this.options.fetch) || nodeFetch;
136
+ const { fetch } = this.options;
138
137
  const controller = new AbortController();
139
138
  const { signal } = controller;
140
139
  const headers = {
@@ -1,6 +1,5 @@
1
1
  /* eslint-disable camelcase, no-underscore-dangle, no-unneeded-ternary, brace-style */
2
2
  const qs = require('qs');
3
- const nodeFetch = require('node-fetch').default;
4
3
  const { AbortController } = require('node-abort-controller');
5
4
  const EventEmitter = require('events');
6
5
  const helpers = require('../utils/helpers');
@@ -79,7 +78,7 @@ function applyParamsAsString(parameters, userParameters, options) {
79
78
  // Send request to server
80
79
  function send(url, userParameters, networkParameters, method = 'GET', body = {}) { // eslint-disable-line max-params
81
80
  let request;
82
- const fetch = (this.options && this.options.fetch) || nodeFetch;
81
+ const { fetch } = this.options;
83
82
  const controller = new AbortController();
84
83
  const { signal } = controller;
85
84
  const headers = {};
@@ -273,6 +272,7 @@ class Tracker {
273
272
  * @param {object} parameters - Additional parameters to be sent with request
274
273
  * @param {string} parameters.item_name - Product item name
275
274
  * @param {string} parameters.item_id - Product item unique identifier
275
+ * @param {string} parameters.url - Current page URL
276
276
  * @param {string} [parameters.variation_id] - Product item variation unique identifier
277
277
  * @param {object} userParameters - Parameters relevant to the user request
278
278
  * @param {number} userParameters.sessionId - Session ID, utilized to personalize results
@@ -294,41 +294,57 @@ class Tracker {
294
294
  * {
295
295
  * item_name: 'Red T-Shirt',
296
296
  * item_id: 'KMH876',
297
+ * url: 'https://constructor.io/product/KMH876',
297
298
  * },
298
299
  * );
299
300
  */
300
301
  trackItemDetailLoad(parameters, userParameters, networkParameters = {}) {
301
302
  // Ensure parameters are provided (required)
302
303
  if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) {
303
- const url = `${this.options.serviceUrl}/behavior?`;
304
- const queryParams = { action: 'item_detail_load' };
305
- const { item_name, name, item_id, customer_id, variation_id } = parameters;
304
+ const requestPath = `${this.options.serviceUrl}/v2/behavioral_action/item_detail_load?`;
305
+ const bodyParams = {};
306
+ const {
307
+ item_name,
308
+ name,
309
+ item_id,
310
+ customer_id,
311
+ variation_id,
312
+ url,
313
+ } = parameters;
306
314
 
307
315
  // Ensure support for both item_name and name as parameters
308
316
  if (item_name) {
309
- queryParams.name = item_name;
317
+ bodyParams.item_name = item_name;
310
318
  } else if (name) {
311
- queryParams.name = name;
319
+ bodyParams.item_name = name;
312
320
  }
313
321
 
314
322
  // Ensure support for both item_id and customer_id as parameters
315
323
  if (item_id) {
316
- queryParams.customer_id = item_id;
324
+ bodyParams.item_id = item_id;
317
325
  } else if (customer_id) {
318
- queryParams.customer_id = customer_id;
326
+ bodyParams.item_id = customer_id;
319
327
  }
320
328
 
321
329
  if (variation_id) {
322
- queryParams.variation_id = variation_id;
330
+ bodyParams.variation_id = variation_id;
331
+ }
332
+
333
+ if (url) {
334
+ bodyParams.url = url;
323
335
  }
324
336
 
325
- const requestUrl = `${url}${applyParamsAsString(queryParams, userParameters, this.options)}`;
337
+ const requestUrl = `${requestPath}${applyParamsAsString({}, userParameters, this.options)}`;
338
+ const requestMethod = 'POST';
339
+ const requestBody = applyParams(bodyParams, userParameters, { ...this.options, requestMethod });
326
340
 
327
341
  send.call(
328
342
  this,
329
343
  requestUrl,
330
344
  userParameters,
331
345
  networkParameters,
346
+ requestMethod,
347
+ requestBody,
332
348
  );
333
349
 
334
350
  return true;
package/src/.DS_Store DELETED
Binary file