@nuskin/cs-product-lib 1.2.1 → 1.2.2

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": "@nuskin/cs-product-lib",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Library for pulling and parsing product content (and applying business logic) retrieved from Contentstack",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/csProduct.js CHANGED
@@ -14,10 +14,12 @@ const { formatDiscounts } = require('./helpers/formatDiscounts');
14
14
  const validateConfig = (props) => {
15
15
  if (!props) {
16
16
  throw new Error(
17
- `This will not work without a valid configuration {apiKey, environment, deliveryToken, homeUrl, awsBase, clientId}.`
17
+ `This will not work without a valid configuration {apiKey, environment, deliveryToken, homeUrl, awsBase, clientId, useDAT}.`
18
18
  )
19
19
  }
20
20
 
21
+ props.useDAT = !!props.useDAT
22
+
21
23
  const requiredProps = [
22
24
  'apiKey',
23
25
  'environment',
@@ -31,7 +33,7 @@ const validateConfig = (props) => {
31
33
  if (!props[prop]) {
32
34
  throw new Error(`Missing required property ${prop} in config.`)
33
35
  }
34
- })
36
+ })
35
37
  }
36
38
 
37
39
  /* istanbul ignore next */
@@ -75,7 +77,8 @@ const csProduct = (props) => {
75
77
  deliveryToken,
76
78
  homeUrl,
77
79
  awsBase,
78
- clientId
80
+ clientId,
81
+ useDAT
79
82
  } = props
80
83
  const axios = deliveryAxios({ apiKey, deliveryToken })
81
84
 
@@ -345,6 +348,7 @@ const csProduct = (props) => {
345
348
  }
346
349
  }
347
350
  if (products[p].product_bundle?.length > 0) {
351
+ products[p].product_bundle.useDAT = products[p].useDAT
348
352
  products[p].bundle = formatKitWithSchema(
349
353
  products[p].product_bundle,
350
354
  noFormat
@@ -405,6 +409,11 @@ const csProduct = (props) => {
405
409
 
406
410
  const queryProducts = async (query, locale) => {
407
411
  const products = await getEntriesByQuery({ query, locale })
412
+ if (products.length > 0) {
413
+ for (const product of products) {
414
+ product.useDAT = useDAT
415
+ }
416
+ }
408
417
  return products
409
418
  }
410
419
 
@@ -21,7 +21,7 @@ const formatProduct = (parent, homeUrl) => {
21
21
  features: helpers?.mapFeatures(parent),
22
22
  productDetails: helpers?.mapProductDetails(parent),
23
23
  benefitsMedia: {
24
- image: helpers?.mapImage(parent?.benefits_media?.image, true, null, 460, 95),
24
+ image: helpers?.mapImage(parent?.benefits_media?.image, true, null, 460, 95, parent?.useDAT),
25
25
  youTubeVideoId: parent?.benefits_media?.youtube_video_id
26
26
  },
27
27
  benefits: helpers?.mapBenefits(parent),
@@ -43,7 +43,8 @@ const formatProduct = (parent, homeUrl) => {
43
43
  })),
44
44
  ingredients: helpers?.mapIngredients(
45
45
  parent?.ingredients,
46
- parent?.legacy_ingredients
46
+ parent?.legacy_ingredients,
47
+ parent?.useDAT
47
48
  ),
48
49
  legacyBenefits: parent?.legacy_benefits,
49
50
  legacyLongDescription: parent?.legacy_long_description,
@@ -54,7 +55,8 @@ const formatProduct = (parent, homeUrl) => {
54
55
  legacyYouTube: parent?.legacy_youtube_video,
55
56
  disclaimers: parent?.disclaimers || [],
56
57
  disclaimerWarnings: helpers?.mapDisclaimerWarnings(
57
- parent?.disclaimer_warnings
58
+ parent?.disclaimer_warnings,
59
+ parent?.useDAT
58
60
  ),
59
61
  seoInformation: helpers?.mapSeoInformation(parent, homeUrl),
60
62
  variants: helpers?.mapVariants(parent),
@@ -57,7 +57,8 @@ const formatVariant = (parent) => {
57
57
  productDetails: helpers?.mapProductDetails(parent),
58
58
  ingredients: helpers?.mapIngredients(
59
59
  parent.ingredients_override,
60
- parent.legacy_ingredients
60
+ parent.legacy_ingredients,
61
+ parent.useDAT
61
62
  ),
62
63
  benefitsAndFeatures: helpers?.mapBenefitsAndFeatures(parent),
63
64
  results: helpers?.mapResults(parent),
@@ -66,7 +67,8 @@ const formatVariant = (parent) => {
66
67
  faqs: helpers?.mapFaqs(parent),
67
68
  disclaimers: parent?.disclaimers || [],
68
69
  disclaimerWarnings: helpers?.mapDisclaimerWarnings(
69
- parent.disclaimer_warnings
70
+ parent.disclaimer_warnings,
71
+ parent?.useDAT
70
72
  ),
71
73
  matchingVariant:
72
74
  parent.sap_data?.sku_data?.findIndex((s) =>
package/src/helpers.js CHANGED
@@ -131,7 +131,7 @@ function mapSubscriptions(subscriptions) {
131
131
  /* istanbul ignore next */
132
132
  function mapProductImages(parent) {
133
133
  if ((parent?.product_images_bynder || []).length) {
134
- return parent?.product_images_bynder.map((img) => mapImage(img, true, 600, 600, 95))
134
+ return parent?.product_images_bynder.map((img) => mapImage(img, true, 600, 600, 95, null, parent?.useDAT))
135
135
  }
136
136
  if ((parent?.product_images || []).length) {
137
137
  return parent?.product_images.map((img) => mapImage(img))
@@ -254,7 +254,7 @@ function mapCsStatus(variant) {
254
254
  const getProductImages = (parent = {}) => {
255
255
  const { product_images_bynder = [], product_images = [] } = parent
256
256
  if (product_images_bynder?.length)
257
- return product_images_bynder?.map((img) => mapImage(img, true, 600, 600, 95))
257
+ return product_images_bynder?.map((img) => mapImage(img, true, 600, 600, 95, null, parent?.useDAT))
258
258
 
259
259
  return product_images?.map((img) => mapImage(img))
260
260
  }
@@ -317,7 +317,8 @@ function mapVariants(parent) {
317
317
  : [(parent?.product_details || {}).long_description],
318
318
  status: variant.status || {},
319
319
  marketData: variant.marketData
320
- }
320
+ },
321
+ useDAT: parent?.useDAT
321
322
  }))
322
323
  return _variants
323
324
  }
@@ -498,10 +499,15 @@ function mapPoints(parent) {
498
499
  }
499
500
 
500
501
  /* istanbul ignore next */
501
- function mapImage(image, useWebp = false, width, height, quality = 100, transform = 'fit') {
502
+ function mapImage(image, useWebp, width, height, quality, transform, useDAT) {
502
503
  if (Array.isArray(image)) {
503
504
  image = image[0]
504
505
  }
506
+ useWebp = !!useWebp
507
+ quality = quality || 100
508
+ transform = transform || 'fit'
509
+ useDAT = !!useDAT
510
+
505
511
  if ((image || {}).type) {
506
512
  if (image.type === 'VIDEO') {
507
513
  const derivatives = image.derivatives || {}
@@ -527,7 +533,7 @@ function mapImage(image, useWebp = false, width, height, quality = 100, transfor
527
533
  let thumbnail = derivatives.thumbnail
528
534
  if (isSvg) {
529
535
  imgURL = derivatives.original
530
- } else if (derivatives.transformBaseUrl) {
536
+ } else if (derivatives.transformBaseUrl && useDAT) {
531
537
  imgURL = thumbnail = `${derivatives.transformBaseUrl}${useWebp ? '.webp' : ''}`
532
538
  if (width || height) {
533
539
  imgURL += `?quality=${quality}&io=transform:${transform}${width ? `,width:${width}` : ''}${height ? `,height:${height}` : ''}`
@@ -600,7 +606,7 @@ function mapProductDetails(productOrVariant) {
600
606
  const includedItems = productDetails.included_items
601
607
  const highlights = (productDetails.highlights || []).map((highlight) => ({
602
608
  iconUrl: !isNullOrEmpty(highlight.icon_bynder)
603
- ? (mapImage(highlight.icon_bynder, true, 60, 60, 95) || {}).url
609
+ ? (mapImage(highlight.icon_bynder, true, 60, 60, 95, null, productOrVariant?.useDAT) || {}).url
604
610
  : (highlight.icon || {}).url,
605
611
  label: highlight.text
606
612
  }))
@@ -631,7 +637,7 @@ function featureImage(feature) {
631
637
  function mapFeatures(productOrVariant) {
632
638
  return ((productOrVariant || {}).features || []).map((feature = {}) => ({
633
639
  // eslint-disable-next-line no-nested-ternary
634
- image: mapImage(featureImage(feature), true, null, 455, 95),
640
+ image: mapImage(featureImage(feature), true, null, 455, 95, null, productOrVariant?.useDAT),
635
641
  subtitle: feature.header,
636
642
  features: feature.features,
637
643
  backgroundColor: mapBrandStyle(feature, BACKGROUND_COLOR),
@@ -723,7 +729,7 @@ function mapBenefitsAndFeatures(productOrVariant) {
723
729
  : jsonBenefits
724
730
 
725
731
  return {
726
- image: mapImage(benefitsMedia.image, true, null, 460, 95),
732
+ image: mapImage(benefitsMedia.image, true, null, 460, 95, null, productOrVariant?.useDAT),
727
733
  youTubeVideoId: benefitsMedia.youTubeVideoId,
728
734
  benefits: benefitsToMarkdown(legacyBenefits, jsonBenefits),
729
735
  backgroundColor: productOrVariant?.background_color,
@@ -747,7 +753,7 @@ function mapResults(productOrVariant) {
747
753
  image: mapImage(
748
754
  (Array.isArray(results.image_or_video) &&
749
755
  results.image_or_video.length > 0) ? results.image_or_video :
750
- results.image, true, null, 460, 95
756
+ results.image, true, null, 460, 95, null, productOrVariant?.useDAT
751
757
  ),
752
758
  youTubeVideoId: results.youtube_video_id
753
759
  }
@@ -759,7 +765,7 @@ function mapSustainability(product) {
759
765
  const image = mapImage(
760
766
  Array.isArray(sustainability.image)
761
767
  ? sustainability.image[0]
762
- : sustainability.image, true, null, 460, 95
768
+ : sustainability.image, true, null, 460, 95, null, product?.useDAT
763
769
  )
764
770
  const richText = sustainability.description
765
771
  const description = hasRichTextContent(richText)
@@ -769,7 +775,7 @@ function mapSustainability(product) {
769
775
  image: mapImage(
770
776
  Array.isArray(highlight.image)
771
777
  ? highlight.image[0]
772
- : highlight.image, true, 60, 60, 95
778
+ : highlight.image, true, 60, 60, 95, null, product?.useDAT
773
779
  ),
774
780
  description: hasRichTextContent(highlight.description)
775
781
  ? createMarkdown(highlight.description)
@@ -794,7 +800,7 @@ function mapUsage(productOrVariant, legacyUsage) {
794
800
  recommendations: decodeHTML(usage.recommendations),
795
801
  warnings: decodeHTML(usage.warnings),
796
802
  additionalText: usage.additional_text,
797
- image: mapImage(usage.image_or_video || usage.image, true, null, 460, 95),
803
+ image: mapImage(usage.image_or_video || usage.image, true, null, 460, 95, null, productOrVariant?.useDAT),
798
804
  youTubeVideoId: usage.youtube_video_id,
799
805
  markdown: legacyUsage ? htmlToMarkdown(legacyUsage) : null
800
806
  }
@@ -857,13 +863,13 @@ function mapResources(productOrVariant) {
857
863
  }
858
864
 
859
865
  /* istanbul ignore next */
860
- function mapIngredients(ings, legacyIngredients) {
866
+ function mapIngredients(ings, legacyIngredients, useDAT) {
861
867
  if (Array.isArray(ings)) {
862
868
  return ings.map((ingredients) => ({
863
869
  keyIngredients: ((ingredients || {}).key_ingredients || []).map(
864
870
  (keyIngredient) => ({
865
871
  image: !isNullOrEmpty(keyIngredient.icon_bynder)
866
- ? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95)
872
+ ? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95, null, useDAT)
867
873
  : {
868
874
  url: (keyIngredient.icon || {}).url,
869
875
  alt: (keyIngredient.icon || {}).title
@@ -875,7 +881,7 @@ function mapIngredients(ings, legacyIngredients) {
875
881
  nutritionInformationImage: !isNullOrEmpty(
876
882
  (ingredients || {}).nutrition_information_image_bynder
877
883
  )
878
- ? mapImage(ingredients.nutrition_information_image_bynder, true, null, 1200, 95)
884
+ ? mapImage(ingredients.nutrition_information_image_bynder, true, null, 1200, 95, null, useDAT)
879
885
  : (ingredients || {}).nutrition_information_image
880
886
  ? {
881
887
  url: ingredients.nutrition_information_image.url,
@@ -901,7 +907,7 @@ function mapIngredients(ings, legacyIngredients) {
901
907
  {
902
908
  keyIngredients: keyIngredients.map((keyIngredient) => ({
903
909
  image: !isNullOrEmpty(keyIngredient.icon_bynder)
904
- ? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95)
910
+ ? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95, null, useDAT)
905
911
  : {
906
912
  url: (keyIngredient.icon || {}).url,
907
913
  alt: (keyIngredient.icon || {}).title
@@ -912,7 +918,7 @@ function mapIngredients(ings, legacyIngredients) {
912
918
  nutritionInformationImage: !isNullOrEmpty(
913
919
  (ings || {}).nutrition_information_image_bynder
914
920
  )
915
- ? mapImage(ings.nutrition_information_image_bynder, true, null, 1200, 95)
921
+ ? mapImage(ings.nutrition_information_image_bynder, true, null, 1200, 95, null, useDAT)
916
922
  : (ings || {}).nutrition_information_image
917
923
  ? {
918
924
  url: ings.nutrition_information_image.url,
@@ -951,7 +957,7 @@ function mapSalesDisclaimer(salesDisclaimer) {
951
957
  }
952
958
  }
953
959
 
954
- function mapDisclaimerWarnings(disclaimerWarnings) {
960
+ function mapDisclaimerWarnings(disclaimerWarnings, useDAT) {
955
961
  disclaimerWarnings = disclaimerWarnings || []
956
962
  return disclaimerWarnings
957
963
  .map((disclaimerWarning) => {
@@ -960,7 +966,7 @@ function mapDisclaimerWarnings(disclaimerWarnings) {
960
966
  if (!text && !hasRichTextContent(richText)) {
961
967
  return null
962
968
  }
963
- const icon = mapImage(disclaimerWarning.icon, true, 25, 25, 85)
969
+ const icon = mapImage(disclaimerWarning.icon, true, 25, 25, 85, null, useDAT)
964
970
  const boldText = disclaimerWarning.bold_text || ''
965
971
  const link = mapLink(disclaimerWarning.link)
966
972
  const markdownText = createMarkdown(richText, text, boldText, link)
@@ -22,14 +22,15 @@ const transformBundle = (parent, homeUrl) => ({
22
22
  salesLabel: parent?.sales_label,
23
23
  salesText: parent?.sales_text,
24
24
  features: {
25
- image: helpers?.mapImage(parent?.features?.image, true, null, 455, 95),
25
+ image: helpers?.mapImage(parent?.features?.image, true, null, 455, 95, null, parent?.useDAT),
26
26
  subtitle: parent?.features?.header,
27
27
  features: parent?.features?.features,
28
28
  backgroundColor: parent?.features?.background_color
29
29
  },
30
30
  disclaimers: parent?.disclaimers || [],
31
31
  disclaimerWarnings: helpers?.mapDisclaimerWarnings(
32
- parent?.disclaimer_warnings
32
+ parent?.disclaimer_warnings,
33
+ parent?.useDAT
33
34
  ),
34
35
  seoInformation: helpers?.mapSeoInformation(parent, homeUrl),
35
36
  resources: helpers?.mapResources(parent),