@nuskin/cs-product-lib 1.2.0 → 1.3.0-brw-4716.1
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 +2 -1
- package/src/csProduct.js +14 -4
- package/src/formatProduct.js +5 -3
- package/src/formatVariant.js +4 -2
- package/src/helpers.js +56 -21
- package/src/transformBundle.js +3 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nuskin/cs-product-lib",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0-brw-4716.1",
|
|
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": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@contentstack/json-rte-serializer": "2.0.1",
|
|
43
|
+
"@nuskin/configuration-sdk": "3.0.0",
|
|
43
44
|
"@nuskin/ns-common-lib": "1.4.5",
|
|
44
45
|
"axios": "1.3.0",
|
|
45
46
|
"axios-retry": "3.4.0",
|
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
|
|
|
@@ -621,7 +630,8 @@ const csProduct = (props) => {
|
|
|
621
630
|
getBundle,
|
|
622
631
|
getBundles,
|
|
623
632
|
getCategoryPageTranslations,
|
|
624
|
-
formatProductWithSchema
|
|
633
|
+
formatProductWithSchema,
|
|
634
|
+
getUseDATFromConfig : helpers.getUseDATFromConfig
|
|
625
635
|
}
|
|
626
636
|
}
|
|
627
637
|
|
package/src/formatProduct.js
CHANGED
|
@@ -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),
|
package/src/formatVariant.js
CHANGED
|
@@ -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
|
@@ -9,12 +9,40 @@ const templateSettings = require('lodash/templateSettings')
|
|
|
9
9
|
// const config = require('config')
|
|
10
10
|
const _ = require('lodash')
|
|
11
11
|
const { isNullOrEmpty } = require('@nuskin/ns-common-lib')
|
|
12
|
+
const { getPartialConfig } = require('@nuskin/configuration-sdk')
|
|
12
13
|
|
|
13
14
|
// CONSTS for helpers
|
|
14
15
|
const TEXT_COLOR = 'textColor';
|
|
15
16
|
const BACKGROUND_COLOR = 'backgroundColor';
|
|
16
17
|
const ALL = 'all';
|
|
17
18
|
|
|
19
|
+
let datConfigValue = null
|
|
20
|
+
|
|
21
|
+
const getUseDATFromConfig = async (environment, client_id) => {
|
|
22
|
+
environment = environment || 'public-dev'
|
|
23
|
+
if (!client_id) {
|
|
24
|
+
return false
|
|
25
|
+
}
|
|
26
|
+
const env = environment.replace('public-', '')
|
|
27
|
+
if (datConfigValue === null) {
|
|
28
|
+
//get config value
|
|
29
|
+
const options = {
|
|
30
|
+
country: 'US',
|
|
31
|
+
environment: env,
|
|
32
|
+
clientId: client_id
|
|
33
|
+
}
|
|
34
|
+
const { use_dat_images } = (
|
|
35
|
+
await getPartialConfig({
|
|
36
|
+
Product_Graphql: ['use_dat_images']
|
|
37
|
+
},
|
|
38
|
+
options
|
|
39
|
+
)
|
|
40
|
+
).Product_Graphql
|
|
41
|
+
datConfigValue = use_dat_images
|
|
42
|
+
}
|
|
43
|
+
return datConfigValue
|
|
44
|
+
}
|
|
45
|
+
|
|
18
46
|
function mapBrandStyle(feature, type) {
|
|
19
47
|
if (!feature)
|
|
20
48
|
return null
|
|
@@ -131,7 +159,7 @@ function mapSubscriptions(subscriptions) {
|
|
|
131
159
|
/* istanbul ignore next */
|
|
132
160
|
function mapProductImages(parent) {
|
|
133
161
|
if ((parent?.product_images_bynder || []).length) {
|
|
134
|
-
return parent?.product_images_bynder.map((img) => mapImage(img, true, 600, 600, 95))
|
|
162
|
+
return parent?.product_images_bynder.map((img) => mapImage(img, true, 600, 600, 95, null, parent?.useDAT))
|
|
135
163
|
}
|
|
136
164
|
if ((parent?.product_images || []).length) {
|
|
137
165
|
return parent?.product_images.map((img) => mapImage(img))
|
|
@@ -254,7 +282,7 @@ function mapCsStatus(variant) {
|
|
|
254
282
|
const getProductImages = (parent = {}) => {
|
|
255
283
|
const { product_images_bynder = [], product_images = [] } = parent
|
|
256
284
|
if (product_images_bynder?.length)
|
|
257
|
-
return product_images_bynder?.map((img) => mapImage(img, true, 600, 600, 95))
|
|
285
|
+
return product_images_bynder?.map((img) => mapImage(img, true, 600, 600, 95, null, parent?.useDAT))
|
|
258
286
|
|
|
259
287
|
return product_images?.map((img) => mapImage(img))
|
|
260
288
|
}
|
|
@@ -317,7 +345,8 @@ function mapVariants(parent) {
|
|
|
317
345
|
: [(parent?.product_details || {}).long_description],
|
|
318
346
|
status: variant.status || {},
|
|
319
347
|
marketData: variant.marketData
|
|
320
|
-
}
|
|
348
|
+
},
|
|
349
|
+
useDAT: parent?.useDAT
|
|
321
350
|
}))
|
|
322
351
|
return _variants
|
|
323
352
|
}
|
|
@@ -498,10 +527,15 @@ function mapPoints(parent) {
|
|
|
498
527
|
}
|
|
499
528
|
|
|
500
529
|
/* istanbul ignore next */
|
|
501
|
-
function mapImage(image, useWebp
|
|
530
|
+
function mapImage(image, useWebp, width, height, quality, transform, useDAT) {
|
|
502
531
|
if (Array.isArray(image)) {
|
|
503
532
|
image = image[0]
|
|
504
533
|
}
|
|
534
|
+
useWebp = !!useWebp
|
|
535
|
+
quality = quality || 100
|
|
536
|
+
transform = transform || 'fit'
|
|
537
|
+
useDAT = !!useDAT
|
|
538
|
+
|
|
505
539
|
if ((image || {}).type) {
|
|
506
540
|
if (image.type === 'VIDEO') {
|
|
507
541
|
const derivatives = image.derivatives || {}
|
|
@@ -527,7 +561,7 @@ function mapImage(image, useWebp = false, width, height, quality = 100, transfor
|
|
|
527
561
|
let thumbnail = derivatives.thumbnail
|
|
528
562
|
if (isSvg) {
|
|
529
563
|
imgURL = derivatives.original
|
|
530
|
-
} else if (derivatives.transformBaseUrl) {
|
|
564
|
+
} else if (derivatives.transformBaseUrl && useDAT) {
|
|
531
565
|
imgURL = thumbnail = `${derivatives.transformBaseUrl}${useWebp ? '.webp' : ''}`
|
|
532
566
|
if (width || height) {
|
|
533
567
|
imgURL += `?quality=${quality}&io=transform:${transform}${width ? `,width:${width}` : ''}${height ? `,height:${height}` : ''}`
|
|
@@ -600,7 +634,7 @@ function mapProductDetails(productOrVariant) {
|
|
|
600
634
|
const includedItems = productDetails.included_items
|
|
601
635
|
const highlights = (productDetails.highlights || []).map((highlight) => ({
|
|
602
636
|
iconUrl: !isNullOrEmpty(highlight.icon_bynder)
|
|
603
|
-
? (mapImage(highlight.icon_bynder, true, 60, 60, 95) || {}).url
|
|
637
|
+
? (mapImage(highlight.icon_bynder, true, 60, 60, 95, null, productOrVariant?.useDAT) || {}).url
|
|
604
638
|
: (highlight.icon || {}).url,
|
|
605
639
|
label: highlight.text
|
|
606
640
|
}))
|
|
@@ -631,7 +665,7 @@ function featureImage(feature) {
|
|
|
631
665
|
function mapFeatures(productOrVariant) {
|
|
632
666
|
return ((productOrVariant || {}).features || []).map((feature = {}) => ({
|
|
633
667
|
// eslint-disable-next-line no-nested-ternary
|
|
634
|
-
image: mapImage(featureImage(feature), true, null, 455, 95),
|
|
668
|
+
image: mapImage(featureImage(feature), true, null, 455, 95, null, productOrVariant?.useDAT),
|
|
635
669
|
subtitle: feature.header,
|
|
636
670
|
features: feature.features,
|
|
637
671
|
backgroundColor: mapBrandStyle(feature, BACKGROUND_COLOR),
|
|
@@ -723,7 +757,7 @@ function mapBenefitsAndFeatures(productOrVariant) {
|
|
|
723
757
|
: jsonBenefits
|
|
724
758
|
|
|
725
759
|
return {
|
|
726
|
-
image: mapImage(benefitsMedia.image, true, null, 460, 95),
|
|
760
|
+
image: mapImage(benefitsMedia.image, true, null, 460, 95, null, productOrVariant?.useDAT),
|
|
727
761
|
youTubeVideoId: benefitsMedia.youTubeVideoId,
|
|
728
762
|
benefits: benefitsToMarkdown(legacyBenefits, jsonBenefits),
|
|
729
763
|
backgroundColor: productOrVariant?.background_color,
|
|
@@ -746,8 +780,8 @@ function mapResults(productOrVariant) {
|
|
|
746
780
|
},
|
|
747
781
|
image: mapImage(
|
|
748
782
|
(Array.isArray(results.image_or_video) &&
|
|
749
|
-
results.image_or_video.length > 0)
|
|
750
|
-
results.image, true, null, 460, 95
|
|
783
|
+
results.image_or_video.length > 0) ? results.image_or_video :
|
|
784
|
+
results.image, true, null, 460, 95, null, productOrVariant?.useDAT
|
|
751
785
|
),
|
|
752
786
|
youTubeVideoId: results.youtube_video_id
|
|
753
787
|
}
|
|
@@ -759,7 +793,7 @@ function mapSustainability(product) {
|
|
|
759
793
|
const image = mapImage(
|
|
760
794
|
Array.isArray(sustainability.image)
|
|
761
795
|
? sustainability.image[0]
|
|
762
|
-
: sustainability.image, true, null, 460, 95
|
|
796
|
+
: sustainability.image, true, null, 460, 95, null, product?.useDAT
|
|
763
797
|
)
|
|
764
798
|
const richText = sustainability.description
|
|
765
799
|
const description = hasRichTextContent(richText)
|
|
@@ -769,7 +803,7 @@ function mapSustainability(product) {
|
|
|
769
803
|
image: mapImage(
|
|
770
804
|
Array.isArray(highlight.image)
|
|
771
805
|
? highlight.image[0]
|
|
772
|
-
: highlight.image, true, 60, 60, 95
|
|
806
|
+
: highlight.image, true, 60, 60, 95, null, product?.useDAT
|
|
773
807
|
),
|
|
774
808
|
description: hasRichTextContent(highlight.description)
|
|
775
809
|
? createMarkdown(highlight.description)
|
|
@@ -794,7 +828,7 @@ function mapUsage(productOrVariant, legacyUsage) {
|
|
|
794
828
|
recommendations: decodeHTML(usage.recommendations),
|
|
795
829
|
warnings: decodeHTML(usage.warnings),
|
|
796
830
|
additionalText: usage.additional_text,
|
|
797
|
-
image: mapImage(usage.image_or_video || usage.image, true, null, 460, 95),
|
|
831
|
+
image: mapImage(usage.image_or_video || usage.image, true, null, 460, 95, null, productOrVariant?.useDAT),
|
|
798
832
|
youTubeVideoId: usage.youtube_video_id,
|
|
799
833
|
markdown: legacyUsage ? htmlToMarkdown(legacyUsage) : null
|
|
800
834
|
}
|
|
@@ -857,13 +891,13 @@ function mapResources(productOrVariant) {
|
|
|
857
891
|
}
|
|
858
892
|
|
|
859
893
|
/* istanbul ignore next */
|
|
860
|
-
function mapIngredients(ings, legacyIngredients) {
|
|
894
|
+
function mapIngredients(ings, legacyIngredients, useDAT) {
|
|
861
895
|
if (Array.isArray(ings)) {
|
|
862
896
|
return ings.map((ingredients) => ({
|
|
863
897
|
keyIngredients: ((ingredients || {}).key_ingredients || []).map(
|
|
864
898
|
(keyIngredient) => ({
|
|
865
899
|
image: !isNullOrEmpty(keyIngredient.icon_bynder)
|
|
866
|
-
? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95)
|
|
900
|
+
? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95, null, useDAT)
|
|
867
901
|
: {
|
|
868
902
|
url: (keyIngredient.icon || {}).url,
|
|
869
903
|
alt: (keyIngredient.icon || {}).title
|
|
@@ -875,7 +909,7 @@ function mapIngredients(ings, legacyIngredients) {
|
|
|
875
909
|
nutritionInformationImage: !isNullOrEmpty(
|
|
876
910
|
(ingredients || {}).nutrition_information_image_bynder
|
|
877
911
|
)
|
|
878
|
-
? mapImage(ingredients.nutrition_information_image_bynder, true, null, 1200, 95)
|
|
912
|
+
? mapImage(ingredients.nutrition_information_image_bynder, true, null, 1200, 95, null, useDAT)
|
|
879
913
|
: (ingredients || {}).nutrition_information_image
|
|
880
914
|
? {
|
|
881
915
|
url: ingredients.nutrition_information_image.url,
|
|
@@ -901,7 +935,7 @@ function mapIngredients(ings, legacyIngredients) {
|
|
|
901
935
|
{
|
|
902
936
|
keyIngredients: keyIngredients.map((keyIngredient) => ({
|
|
903
937
|
image: !isNullOrEmpty(keyIngredient.icon_bynder)
|
|
904
|
-
? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95)
|
|
938
|
+
? mapImage(keyIngredient.icon_bynder, true, 100, 100, 95, null, useDAT)
|
|
905
939
|
: {
|
|
906
940
|
url: (keyIngredient.icon || {}).url,
|
|
907
941
|
alt: (keyIngredient.icon || {}).title
|
|
@@ -912,7 +946,7 @@ function mapIngredients(ings, legacyIngredients) {
|
|
|
912
946
|
nutritionInformationImage: !isNullOrEmpty(
|
|
913
947
|
(ings || {}).nutrition_information_image_bynder
|
|
914
948
|
)
|
|
915
|
-
? mapImage(ings.nutrition_information_image_bynder, true, null, 1200, 95)
|
|
949
|
+
? mapImage(ings.nutrition_information_image_bynder, true, null, 1200, 95, null, useDAT)
|
|
916
950
|
: (ings || {}).nutrition_information_image
|
|
917
951
|
? {
|
|
918
952
|
url: ings.nutrition_information_image.url,
|
|
@@ -951,7 +985,7 @@ function mapSalesDisclaimer(salesDisclaimer) {
|
|
|
951
985
|
}
|
|
952
986
|
}
|
|
953
987
|
|
|
954
|
-
function mapDisclaimerWarnings(disclaimerWarnings) {
|
|
988
|
+
function mapDisclaimerWarnings(disclaimerWarnings, useDAT) {
|
|
955
989
|
disclaimerWarnings = disclaimerWarnings || []
|
|
956
990
|
return disclaimerWarnings
|
|
957
991
|
.map((disclaimerWarning) => {
|
|
@@ -960,7 +994,7 @@ function mapDisclaimerWarnings(disclaimerWarnings) {
|
|
|
960
994
|
if (!text && !hasRichTextContent(richText)) {
|
|
961
995
|
return null
|
|
962
996
|
}
|
|
963
|
-
const icon = mapImage(disclaimerWarning.icon, true, 25, 25, 85)
|
|
997
|
+
const icon = mapImage(disclaimerWarning.icon, true, 25, 25, 85, null, useDAT)
|
|
964
998
|
const boldText = disclaimerWarning.bold_text || ''
|
|
965
999
|
const link = mapLink(disclaimerWarning.link)
|
|
966
1000
|
const markdownText = createMarkdown(richText, text, boldText, link)
|
|
@@ -1220,5 +1254,6 @@ module.exports = {
|
|
|
1220
1254
|
mapFaqs,
|
|
1221
1255
|
createMarkdownFromRTE,
|
|
1222
1256
|
benefitsToMarkdown,
|
|
1223
|
-
mapActiveEventDisclaimer
|
|
1257
|
+
mapActiveEventDisclaimer,
|
|
1258
|
+
getUseDATFromConfig
|
|
1224
1259
|
}
|
package/src/transformBundle.js
CHANGED
|
@@ -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),
|