@constructor-io/constructorio-node 4.17.1 → 5.0.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.17.1",
3
+ "version": "5.0.0",
4
4
  "description": "Constructor.io Node.js client",
5
5
  "main": "src/constructorio.js",
6
6
  "types": "src/types/constructorio.d.ts",
@@ -210,7 +210,7 @@ class Catalog {
210
210
  * ],
211
211
  * });
212
212
  */
213
- createOrReplaceItems(parameters = {}, networkParameters = {}) {
213
+ async createOrReplaceItems(parameters = {}, networkParameters = {}) {
214
214
  let requestUrl;
215
215
  const { fetch } = this.options;
216
216
  const controller = new AbortController();
@@ -244,21 +244,27 @@ class Catalog {
244
244
  // Handle network timeout if specified
245
245
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
246
246
 
247
- return fetch(requestUrl, {
248
- method: 'PUT',
249
- body: JSON.stringify({ items }),
250
- headers: {
251
- 'Content-Type': 'application/json',
252
- ...helpers.createAuthHeader(this.options),
253
- },
254
- signal,
255
- }).then((response) => {
247
+ try {
248
+ const response = await fetch(requestUrl, {
249
+ method: 'PUT',
250
+ body: JSON.stringify({ items }),
251
+ headers: {
252
+ 'Content-Type': 'application/json',
253
+ ...helpers.createAuthHeader(this.options),
254
+ },
255
+ signal,
256
+ });
257
+
256
258
  if (response.ok) {
257
- return Promise.resolve();
259
+ const body = await response.json();
260
+
261
+ return Promise.resolve(body);
258
262
  }
259
263
 
260
264
  return helpers.throwHttpErrorFromResponse(new Error(), response);
261
- });
265
+ } catch (error) {
266
+ return Promise.reject(error);
267
+ }
262
268
  }
263
269
 
264
270
  /**
@@ -293,7 +299,7 @@ class Catalog {
293
299
  * section: 'Products',
294
300
  * });
295
301
  */
296
- updateItems(parameters = {}, networkParameters = {}) {
302
+ async updateItems(parameters = {}, networkParameters = {}) {
297
303
  let requestUrl;
298
304
  const { fetch } = this.options;
299
305
  const controller = new AbortController();
@@ -336,21 +342,26 @@ class Catalog {
336
342
  // Handle network timeout if specified
337
343
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
338
344
 
339
- return fetch(requestUrl, {
340
- method: 'PATCH',
341
- body: JSON.stringify({ items }),
342
- headers: {
343
- 'Content-Type': 'application/json',
344
- ...helpers.createAuthHeader(this.options),
345
- },
346
- signal,
347
- }).then((response) => {
345
+ try {
346
+ const response = await fetch(requestUrl, {
347
+ method: 'PATCH',
348
+ body: JSON.stringify({ items }),
349
+ headers: {
350
+ 'Content-Type': 'application/json',
351
+ ...helpers.createAuthHeader(this.options),
352
+ },
353
+ signal,
354
+ });
355
+
348
356
  if (response.ok) {
349
- return Promise.resolve();
357
+ const body = await response.json();
358
+ return Promise.resolve(body);
350
359
  }
351
360
 
352
361
  return helpers.throwHttpErrorFromResponse(new Error(), response);
353
- });
362
+ } catch (error) {
363
+ return Promise.reject(error);
364
+ }
354
365
  }
355
366
 
356
367
  /**
@@ -375,7 +386,7 @@ class Catalog {
375
386
  * section: 'Products',
376
387
  * });
377
388
  */
378
- deleteItems(parameters = {}, networkParameters = {}) {
389
+ async deleteItems(parameters = {}, networkParameters = {}) {
379
390
  let requestUrl;
380
391
  const { fetch } = this.options;
381
392
  const controller = new AbortController();
@@ -409,21 +420,26 @@ class Catalog {
409
420
  // Handle network timeout if specified
410
421
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
411
422
 
412
- return fetch(requestUrl, {
413
- method: 'DELETE',
414
- body: JSON.stringify({ items }),
415
- headers: {
416
- 'Content-Type': 'application/json',
417
- ...helpers.createAuthHeader(this.options),
418
- },
419
- signal,
420
- }).then((response) => {
423
+ try {
424
+ const response = await fetch(requestUrl, {
425
+ method: 'DELETE',
426
+ body: JSON.stringify({ items }),
427
+ headers: {
428
+ 'Content-Type': 'application/json',
429
+ ...helpers.createAuthHeader(this.options),
430
+ },
431
+ signal,
432
+ });
433
+
421
434
  if (response.ok) {
422
- return Promise.resolve();
435
+ const body = await response.json();
436
+ return Promise.resolve(body);
423
437
  }
424
438
 
425
439
  return helpers.throwHttpErrorFromResponse(new Error(), response);
426
- });
440
+ } catch (error) {
441
+ return Promise.reject(error);
442
+ }
427
443
  }
428
444
 
429
445
  /**
@@ -534,7 +550,7 @@ class Catalog {
534
550
  * section: 'Products',
535
551
  * });
536
552
  */
537
- createOrReplaceVariations(parameters = {}, networkParameters = {}) {
553
+ async createOrReplaceVariations(parameters = {}, networkParameters = {}) {
538
554
  let requestUrl;
539
555
  const { fetch } = this.options;
540
556
  const controller = new AbortController();
@@ -568,21 +584,26 @@ class Catalog {
568
584
  // Handle network timeout if specified
569
585
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
570
586
 
571
- return fetch(requestUrl, {
572
- method: 'PUT',
573
- body: JSON.stringify({ variations }),
574
- headers: {
575
- 'Content-Type': 'application/json',
576
- ...helpers.createAuthHeader(this.options),
577
- },
578
- signal,
579
- }).then((response) => {
587
+ try {
588
+ const response = await fetch(requestUrl, {
589
+ method: 'PUT',
590
+ body: JSON.stringify({ variations }),
591
+ headers: {
592
+ 'Content-Type': 'application/json',
593
+ ...helpers.createAuthHeader(this.options),
594
+ },
595
+ signal,
596
+ });
597
+
580
598
  if (response.ok) {
581
- return Promise.resolve();
599
+ const body = await response.json();
600
+ return Promise.resolve(body);
582
601
  }
583
602
 
584
603
  return helpers.throwHttpErrorFromResponse(new Error(), response);
585
- });
604
+ } catch (error) {
605
+ return Promise.reject(error);
606
+ }
586
607
  }
587
608
 
588
609
  /**
@@ -618,7 +639,7 @@ class Catalog {
618
639
  * section: 'Products',
619
640
  * });
620
641
  */
621
- updateVariations(parameters = {}, networkParameters = {}) {
642
+ async updateVariations(parameters = {}, networkParameters = {}) {
622
643
  let requestUrl;
623
644
  const { fetch } = this.options;
624
645
  const controller = new AbortController();
@@ -661,21 +682,26 @@ class Catalog {
661
682
  // Handle network timeout if specified
662
683
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
663
684
 
664
- return fetch(requestUrl, {
665
- method: 'PATCH',
666
- body: JSON.stringify({ variations }),
667
- headers: {
668
- 'Content-Type': 'application/json',
669
- ...helpers.createAuthHeader(this.options),
670
- },
671
- signal,
672
- }).then((response) => {
685
+ try {
686
+ const response = await fetch(requestUrl, {
687
+ method: 'PATCH',
688
+ body: JSON.stringify({ variations }),
689
+ headers: {
690
+ 'Content-Type': 'application/json',
691
+ ...helpers.createAuthHeader(this.options),
692
+ },
693
+ signal,
694
+ });
695
+
673
696
  if (response.ok) {
674
- return Promise.resolve();
697
+ const body = await response.json();
698
+ return Promise.resolve(body);
675
699
  }
676
700
 
677
701
  return helpers.throwHttpErrorFromResponse(new Error(), response);
678
- });
702
+ } catch (error) {
703
+ return Promise.reject(error);
704
+ }
679
705
  }
680
706
 
681
707
  /**
@@ -700,7 +726,7 @@ class Catalog {
700
726
  * section: 'Products',
701
727
  * });
702
728
  */
703
- deleteVariations(parameters = {}, networkParameters = {}) {
729
+ async deleteVariations(parameters = {}, networkParameters = {}) {
704
730
  let requestUrl;
705
731
  const { fetch } = this.options;
706
732
  const controller = new AbortController();
@@ -734,21 +760,26 @@ class Catalog {
734
760
  // Handle network timeout if specified
735
761
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
736
762
 
737
- return fetch(requestUrl, {
738
- method: 'DELETE',
739
- body: JSON.stringify({ variations }),
740
- headers: {
741
- 'Content-Type': 'application/json',
742
- ...helpers.createAuthHeader(this.options),
743
- },
744
- signal,
745
- }).then((response) => {
763
+ try {
764
+ const response = await fetch(requestUrl, {
765
+ method: 'DELETE',
766
+ body: JSON.stringify({ variations }),
767
+ headers: {
768
+ 'Content-Type': 'application/json',
769
+ ...helpers.createAuthHeader(this.options),
770
+ },
771
+ signal,
772
+ });
773
+
746
774
  if (response.ok) {
747
- return Promise.resolve();
775
+ const body = await response.json();
776
+ return Promise.resolve(body);
748
777
  }
749
778
 
750
779
  return helpers.throwHttpErrorFromResponse(new Error(), response);
751
- });
780
+ } catch (error) {
781
+ return Promise.reject(error);
782
+ }
752
783
  }
753
784
 
754
785
  /**
@@ -570,9 +570,16 @@ class Tracker {
570
570
  * @function trackSearchResultsLoaded
571
571
  * @param {string} term - Search results query term
572
572
  * @param {object} parameters - Additional parameters to be sent with request
573
- * @param {number} parameters.numResults - Total number of results
574
- * @param {string[]} parameters.itemIds - List of product item unique identifiers in search results listing
575
- * @param {string} [parameters.section] - Index section
573
+ * @param {object[]} parameters.items - List of product item unique identifiers in search results listing
574
+ * @param {string} [parameters.url] - URL of the search results page
575
+ * @param {number} [parameters.resultCount] - Total number of results
576
+ * @param {number} [parameters.resultPage] - Current page of search results
577
+ * @param {string} [parameters.resultId] - Browse result identifier (returned in response from Constructor)
578
+ * @param {object} [parameters.selectedFilters] - Selected filters
579
+ * @param {string} [parameters.sortOrder] - Sort order ('ascending' or 'descending')
580
+ * @param {string} [parameters.sortBy] - Sorting method
581
+ * @param {string} [parameters.section] - The section name for the item Ex. "Products"
582
+ * @param {object} [parameters.analyticsTags] - Pass additional analytics data
576
583
  * @param {object} userParameters - Parameters relevant to the user request
577
584
  * @param {number} userParameters.sessionId - Session ID, utilized to personalize results
578
585
  * @param {string} userParameters.clientId - Client ID, utilized to personalize results
@@ -593,8 +600,8 @@ class Tracker {
593
600
  * constructorio.tracker.trackSearchResultsLoaded(
594
601
  * 'T-Shirt',
595
602
  * {
596
- * numResults: 167,
597
- * itemIds: ['KMH876', 'KMH140', 'KMH437'],
603
+ * resultCount: 167,
604
+ * items: [{ itemId: 'KMH876' }, { itemId: 'KMH140' }],
598
605
  * },
599
606
  * {
600
607
  * sessionId: 1,
@@ -610,45 +617,73 @@ class Tracker {
610
617
  if (term && typeof term === 'string') {
611
618
  // Ensure parameters are provided (required)
612
619
  if (parameters && typeof parameters === 'object' && !Array.isArray(parameters)) {
613
- const url = `${this.options.serviceUrl}/behavior?`;
614
- const queryParams = { action: 'search-results', term };
620
+ const baseUrl = `${this.options.serviceUrl}/v2/behavioral_action/search_result_load?`;
615
621
  const {
616
622
  num_results,
617
623
  numResults = num_results,
618
- customer_ids,
619
- customerIds = customer_ids,
620
- item_ids,
621
- itemIds = item_ids,
624
+ result_count,
625
+ customerIds,
626
+ customer_ids = customerIds,
627
+ itemIds,
628
+ item_ids = itemIds,
629
+ items = customer_ids || item_ids,
630
+ result_page,
631
+ resultPage = result_page,
632
+ result_id,
633
+ resultId = result_id,
634
+ sort_order,
635
+ sortOrder = sort_order,
636
+ sort_by,
637
+ sortBy = sort_by,
638
+ selected_filters,
639
+ selectedFilters = selected_filters,
640
+ url = 'N/A',
622
641
  section,
642
+ analyticsTags,
643
+ resultCount = numResults || result_count || items?.length || 0,
623
644
  } = parameters;
624
- let customerIDs;
645
+ const queryParams = {};
646
+ let transformedItems;
625
647
 
626
- if (!helpers.isNil(numResults)) {
627
- queryParams.num_results = numResults;
628
- }
648
+ if (items && Array.isArray(items) && items.length !== 0) {
649
+ const trimmedItems = items.slice(0, 100);
629
650
 
630
- // Ensure support for both item_ids and customer_ids as parameters
631
- if (itemIds && Array.isArray(itemIds)) {
632
- customerIDs = itemIds;
633
- } else if (customerIds && Array.isArray(customerIds)) {
634
- customerIDs = customerIds;
635
- }
636
-
637
- if (customerIDs && Array.isArray(customerIDs) && customerIDs.length) {
638
- queryParams.customer_ids = customerIDs.slice(0, 100).join(',');
651
+ if (typeof items[0] === 'string' || typeof items[0] === 'number') {
652
+ transformedItems = trimmedItems.map((itemId) => ({ item_id: String(itemId) }));
653
+ } else {
654
+ transformedItems = trimmedItems.map((item) => helpers.toSnakeCaseKeys(item, false));
655
+ }
639
656
  }
640
657
 
641
658
  if (section) {
642
659
  queryParams.section = section;
643
660
  }
644
661
 
645
- const requestUrl = `${url}${applyParamsAsString(queryParams, userParameters, this.options)}`;
662
+ const bodyParams = {
663
+ search_term: term,
664
+ result_count: resultCount,
665
+ items: transformedItems,
666
+ result_page: resultPage,
667
+ result_id: resultId,
668
+ sort_order: sortOrder,
669
+ sort_by: sortBy,
670
+ selected_filters: selectedFilters,
671
+ analytics_tags: analyticsTags,
672
+ url,
673
+ section,
674
+ };
675
+
676
+ const requestUrl = `${baseUrl}${applyParamsAsString({}, userParameters, this.options)}`;
677
+ const requestMethod = 'POST';
678
+ const requestBody = applyParams(bodyParams, userParameters, { ...this.options, requestMethod });
646
679
 
647
680
  send.call(
648
681
  this,
649
682
  requestUrl,
650
683
  userParameters,
651
684
  networkParameters,
685
+ requestMethod,
686
+ requestBody,
652
687
  );
653
688
 
654
689
  return true;
@@ -1041,7 +1076,7 @@ class Tracker {
1041
1076
  * @param {object} [networkParameters] - Parameters relevant to the network request
1042
1077
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
1043
1078
  * @returns {(true|Error)}
1044
- * @description User clicked a result that appeared within a search product listing page
1079
+ * @description User viewed a set of recommendations
1045
1080
  * @example
1046
1081
  * constructorio.tracker.trackRecommendationView(
1047
1082
  * {
@@ -1628,7 +1663,7 @@ class Tracker {
1628
1663
  * @param {object} [networkParameters] - Parameters relevant to the network request
1629
1664
  * @param {number} [networkParameters.timeout] - Request timeout (in milliseconds)
1630
1665
  * @returns {(true|Error)}
1631
- * @description User clicked a result that appeared within a browse product listing page
1666
+ * @description User clicked a result that appeared outside of the scope of search / browse / recommendations
1632
1667
  * @example
1633
1668
  * constructorio.tracker.trackGenericResultClick(
1634
1669
  * {
@@ -272,6 +272,12 @@ export interface PatchSearchabilitiesParameters {
272
272
  section?: string;
273
273
  }
274
274
 
275
+ interface CatalogMutationResponse {
276
+ task_id: string;
277
+ task_status_path: string;
278
+ [key: string]: any;
279
+ }
280
+
275
281
  declare class Catalog {
276
282
  constructor(options: ConstructorClientOptions);
277
283
 
@@ -280,17 +286,17 @@ declare class Catalog {
280
286
  createOrReplaceItems(
281
287
  parameters: CreateOrReplaceItemsParameters,
282
288
  networkParameters?: NetworkParameters
283
- ): Promise<void>;
289
+ ): Promise<CatalogMutationResponse>;
284
290
 
285
291
  updateItems(
286
292
  parameters: UpdateItemsParameters,
287
293
  networkParameters?: NetworkParameters
288
- ): Promise<void>;
294
+ ): Promise<CatalogMutationResponse>;
289
295
 
290
296
  deleteItems(
291
297
  parameters: DeleteItemsParameters,
292
298
  networkParameters?: NetworkParameters
293
- ): Promise<void>;
299
+ ): Promise<CatalogMutationResponse>;
294
300
 
295
301
  retrieveItems(
296
302
  parameters: RetrieveItemsParameters,
@@ -300,17 +306,17 @@ declare class Catalog {
300
306
  createOrReplaceVariations(
301
307
  parameters: CreateOrReplaceVariationsParameters,
302
308
  networkParameters?: NetworkParameters
303
- ): Promise<void>;
309
+ ): Promise<CatalogMutationResponse>;
304
310
 
305
311
  updateVariations(
306
312
  parameters: UpdateVariationsParameters,
307
313
  networkParameters?: NetworkParameters
308
- ): Promise<void>;
314
+ ): Promise<CatalogMutationResponse>;
309
315
 
310
316
  deleteVariations(
311
317
  parameters: DeleteVariationsParameters,
312
318
  networkParameters?: NetworkParameters
313
- ): Promise<void>;
319
+ ): Promise<CatalogMutationResponse>;
314
320
 
315
321
  retrieveVariations(
316
322
  parameters: RetrieveVariationsParameters,
@@ -479,56 +485,32 @@ declare class Catalog {
479
485
  replaceCatalog(
480
486
  parameters: ReplaceCatalogParameters,
481
487
  networkParameters?: NetworkParameters
482
- ): Promise<{
483
- task_id: string;
484
- task_status_path: string;
485
- [key: string]: any;
486
- }>;
488
+ ): Promise<CatalogMutationResponse>;
487
489
 
488
490
  updateCatalog(
489
491
  parameters: UpdateCatalogParameters,
490
492
  networkParameters?: NetworkParameters
491
- ): Promise<{
492
- task_id: string;
493
- task_status_path: string;
494
- [key: string]: any;
495
- }>;
493
+ ): Promise<CatalogMutationResponse>;
496
494
 
497
495
  patchCatalog(
498
496
  parameters: PatchCatalogParameters,
499
497
  networkParameters?: NetworkParameters
500
- ): Promise<{
501
- task_id: string;
502
- task_status_path: string;
503
- [key: string]: any;
504
- }>;
498
+ ): Promise<CatalogMutationResponse>;
505
499
 
506
500
  replaceCatalogUsingTarArchive(
507
501
  parameters: ReplaceCatalogUsingTarArchiveParameters,
508
502
  networkParameters?: NetworkParameters
509
- ): Promise<{
510
- task_id: string;
511
- task_status_path: string;
512
- [key: string]: any;
513
- }>;
503
+ ): Promise<CatalogMutationResponse>;
514
504
 
515
505
  updateCatalogUsingTarArchive(
516
506
  parameters: UpdateCatalogUsingTarArchiveParameters,
517
507
  networkParameters?: NetworkParameters
518
- ): Promise<{
519
- task_id: string;
520
- task_status_path: string;
521
- [key: string]: any;
522
- }>;
508
+ ): Promise<CatalogMutationResponse>;
523
509
 
524
510
  patchCatalogUsingTarArchive(
525
511
  parameters: PatchCatalogUsingTarArchiveParameters,
526
512
  networkParameters?: NetworkParameters
527
- ): Promise<{
528
- task_id: string;
529
- task_status_path: string;
530
- [key: string]: any;
531
- }>;
513
+ ): Promise<CatalogMutationResponse>;
532
514
 
533
515
  addFacetConfiguration(
534
516
  parameters: FacetConfiguration,
@@ -74,9 +74,16 @@ declare class Tracker {
74
74
  trackSearchResultsLoaded(
75
75
  term: string,
76
76
  parameters: {
77
- numResults: number;
78
- itemIds: string[];
77
+ url: string;
78
+ items: ItemTracked[];
79
+ resultCount?: number;
80
+ resultPage?: number;
81
+ resultId?: string;
82
+ selectedFilters?: Record<string, any>;
83
+ sortOrder?: string;
84
+ sortBy?: string;
79
85
  section?: string;
86
+ analyticsTags?: Record<string, string>;
80
87
  },
81
88
  userParameters?: TrackerUserParameters,
82
89
  networkParameters?: NetworkParameters
@@ -97,7 +104,7 @@ declare class Tracker {
97
104
  ): true | Error;
98
105
 
99
106
  trackConversion(
100
- term : string | null | undefined,
107
+ term: string | null | undefined,
101
108
  parameters: {
102
109
  itemId: string;
103
110
  revenue?: number;