@nuskin/ns-product-lib 1.6.0-cx24-2382.1 → 2.0.0-cx24-2382.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/CHANGELOG.md +11 -0
- package/package.json +1 -1
- package/src/contentstack/contentstack.js +121 -0
- package/src/productData.js +61 -118
package/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
# [2.0.0-cx24-2382.1](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/compare/v1.6.0-cx24-2382.1...v2.0.0-cx24-2382.1) (2022-09-06)
|
2
|
+
|
3
|
+
|
4
|
+
### Breaking
|
5
|
+
|
6
|
+
* replace runConfig and marketConfig parameters of getProductData with locale and market (CX24-2382) ([713bbb6](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/commit/713bbb6b52e20727ce846d4df5c84d837fb931d5))
|
7
|
+
|
8
|
+
### New
|
9
|
+
|
10
|
+
* add equinox-enabled market validation (CX24-2382) ([6633eb5](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/commit/6633eb5824ec621a7316aff401d29876761ffc38))
|
11
|
+
|
1
12
|
# [1.6.0-cx24-2382.1](https://code.tls.nuskin.io/ns-am/product/js-libs/ns-product-lib/compare/v1.5.0...v1.6.0-cx24-2382.1) (2022-09-01)
|
2
13
|
|
3
14
|
|
package/package.json
CHANGED
@@ -0,0 +1,121 @@
|
|
1
|
+
// This module can be bundled into a package for better reusability.
|
2
|
+
const axios = require('axios');
|
3
|
+
const { getEnvironmentFromUrl } = require('@nuskin/ns-common-lib');
|
4
|
+
|
5
|
+
/**
|
6
|
+
* getAEMConfig fetches the market config from contentstack.
|
7
|
+
*
|
8
|
+
* @typedef MarketConfig
|
9
|
+
* @property {string} aws_url
|
10
|
+
* @property {string} checkout_client_id
|
11
|
+
* @property {string} checkout_client_secret
|
12
|
+
*
|
13
|
+
* @export
|
14
|
+
* @return {Promise<null|MarketConfig>}
|
15
|
+
*/
|
16
|
+
async function getAEMConfig() {
|
17
|
+
try {
|
18
|
+
const env = getEnvironmentFromUrl('test.nuskin.com');
|
19
|
+
const marketConfig = `market_config_${env}`;
|
20
|
+
const variables = { uid: 'blt52b0b83a89763791' };
|
21
|
+
const query = `query EquinoxMarketsByUid($uid: String!) {
|
22
|
+
aem_config(uid: $uid) {
|
23
|
+
title
|
24
|
+
${marketConfig} {
|
25
|
+
checkout_client_secret
|
26
|
+
checkout_client_id
|
27
|
+
aws_url
|
28
|
+
}
|
29
|
+
}
|
30
|
+
}`;
|
31
|
+
|
32
|
+
const { data } = await contentstackService({
|
33
|
+
method: 'post',
|
34
|
+
data: { variables, query }
|
35
|
+
});
|
36
|
+
|
37
|
+
return data.aem_config[marketConfig];
|
38
|
+
} catch (error) {
|
39
|
+
console.error(error);
|
40
|
+
return null;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
/**
|
45
|
+
* getEquinoxMarkets will get the list of Equinox-enabled markets from contentstack
|
46
|
+
*
|
47
|
+
* @typedef EquinoxEnabledMarket
|
48
|
+
* @property {string} market_name
|
49
|
+
* @property {string} country_code
|
50
|
+
* @property {string} store_id
|
51
|
+
*
|
52
|
+
* @typedef EquinoxEnabledMarkets
|
53
|
+
* @property {EquinoxEnabledMarket[]} [dev_markets]
|
54
|
+
* @property {EquinoxEnabledMarket[]} [test_markets]
|
55
|
+
* @property {EquinoxEnabledMarket[]} [staging_markets]
|
56
|
+
* @property {EquinoxEnabledMarket[]} [prod_markets]
|
57
|
+
*
|
58
|
+
* @export
|
59
|
+
* @return {Promise<EquinoxEnabledMarkets>}
|
60
|
+
*/
|
61
|
+
async function getEquinoxMarkets() {
|
62
|
+
try {
|
63
|
+
const env = getEnvironmentFromUrl('test.nuskin.com');
|
64
|
+
const marketConfig = `${env}_markets`;
|
65
|
+
const variables = { uid: 'blt5633ff9e560f14ef' };
|
66
|
+
const query = `query EquinoxMarketsByUid($uid: String!) {
|
67
|
+
equinox_markets(uid: $uid) {
|
68
|
+
${marketConfig} {
|
69
|
+
market_name
|
70
|
+
country_code
|
71
|
+
store_id
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}`;
|
75
|
+
|
76
|
+
const { data } = await contentstackService({
|
77
|
+
method: 'post',
|
78
|
+
data: { variables, query }
|
79
|
+
});
|
80
|
+
|
81
|
+
return data.equinox_markets[marketConfig];
|
82
|
+
} catch (error) {
|
83
|
+
console.error(error);
|
84
|
+
return [];
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
|
89
|
+
|
90
|
+
/**
|
91
|
+
* Contentstack HTTP - axios wrapper for contentstack request
|
92
|
+
*
|
93
|
+
* @param {Object} config - required param
|
94
|
+
* @return {Promise<import('axios').AxiosResponse>} Promise that resolves to market_list_eq object
|
95
|
+
*/
|
96
|
+
async function contentstackService(config) {
|
97
|
+
const baseURL = 'https://graphql.contentstack.com';
|
98
|
+
const deliveryToken = 'cs74599c107573ea7f6c7a1413';
|
99
|
+
const apiKey = 'blt7d4c4f4a1bf5a819';
|
100
|
+
const env = 'public-dev';
|
101
|
+
const defaultUrl = `${baseURL}/stacks/${apiKey}?environment=${env}`;
|
102
|
+
|
103
|
+
config.method = config.method || 'get'
|
104
|
+
config.url = config.url || defaultUrl
|
105
|
+
config.headers = {
|
106
|
+
'access_token': `${deliveryToken}`,
|
107
|
+
'Content-Type': 'application/json'
|
108
|
+
}
|
109
|
+
|
110
|
+
try {
|
111
|
+
const response = await axios(config);
|
112
|
+
return response.data
|
113
|
+
} catch (error) {
|
114
|
+
console.error(error.response.data.errors);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
module.exports = {
|
119
|
+
getAEMConfig,
|
120
|
+
getEquinoxMarkets
|
121
|
+
}
|
package/src/productData.js
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
"use strict";
|
2
|
-
const { getEnvironmentFromUrl } = require('@nuskin/ns-common-lib');
|
3
2
|
const axios = require("axios");
|
3
|
+
const contentstack = require('./contentstack/contentstack');
|
4
4
|
const product = require("./product");
|
5
5
|
|
6
|
-
const CS_BASE_URL = 'https://graphql.contentstack.com';
|
7
|
-
const CS_DELIVERY_TOKEN = 'cs74599c107573ea7f6c7a1413';
|
8
|
-
const CS_API_KEY = 'blt7d4c4f4a1bf5a819';
|
9
|
-
const CS_DEFAULT_ENV = 'public-dev';
|
10
|
-
|
11
6
|
/** @type {*} */
|
12
7
|
const ProductData = {
|
13
8
|
/**
|
@@ -16,63 +11,91 @@ const ProductData = {
|
|
16
11
|
* @param {*} marketConfig
|
17
12
|
* @param {boolean} isEquinoxEnabled
|
18
13
|
*/
|
19
|
-
getProductData: async function (skus,
|
20
|
-
const
|
21
|
-
const enabledMarkets = await
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
14
|
+
getProductData: async function (skus, locale, market, isEquinoxEnabled = false) {
|
15
|
+
const localeMarket = `${locale}_${market}`;
|
16
|
+
const enabledMarkets = await contentstack.getEquinoxMarkets();
|
17
|
+
const isMarketEnabled = enabledMarkets
|
18
|
+
.map(m => m.country_code.toLowerCase())
|
19
|
+
.includes(market.toLowerCase());
|
20
|
+
|
21
|
+
if (isEquinoxEnabled && isMarketEnabled) {
|
22
|
+
return await this.getProductFromEquinox(skus, localeMarket, 406);
|
26
23
|
}
|
27
24
|
|
28
|
-
return await this.getProductFromLegacy(skus,
|
25
|
+
return await this.getProductFromLegacy(skus, localeMarket);
|
29
26
|
},
|
30
27
|
|
31
|
-
getProductFromEquinox: async function (skus, locale,
|
32
|
-
const filter =
|
33
|
-
"filters": [{
|
34
|
-
"field": "index_key_skuId",
|
35
|
-
"operation": "IN",
|
36
|
-
"value": "${skus.toString()}"
|
37
|
-
}]
|
38
|
-
}`;
|
28
|
+
getProductFromEquinox: async function (skus, locale, storeID) {
|
29
|
+
const filter = '{"filters":[{"field":"index_key_skuId","operation":"IN","value":"' + skus.toString() + '"}]}';
|
39
30
|
axios.defaults.withCredentials = true;
|
40
|
-
const
|
31
|
+
const url = `https://storefront.api.wde.nuskin.io/orchestrationservices/storefront/catalogs/search/`;
|
32
|
+
const href = `${url}?filter=${encodeURI(filter)}`;
|
33
|
+
const response = await axios.request({
|
41
34
|
method: 'get',
|
42
|
-
url:
|
35
|
+
url: href,
|
43
36
|
params: {
|
44
37
|
locale,
|
45
|
-
storeId: storeID
|
46
|
-
filter
|
38
|
+
storeId: storeID
|
47
39
|
},
|
48
|
-
headers: this.
|
49
|
-
responseType:
|
40
|
+
headers: this.getEquinoxRequestHeaders(),
|
41
|
+
responseType: 'json'
|
50
42
|
});
|
51
43
|
|
52
|
-
|
44
|
+
if (response.data.product) {
|
45
|
+
return this.eqProductMapper(response.data.product);
|
46
|
+
}
|
47
|
+
|
48
|
+
return {
|
49
|
+
status: 400,
|
50
|
+
messages: [response.data.message],
|
51
|
+
data: { count: 0, products: [] }
|
52
|
+
}
|
53
|
+
},
|
54
|
+
|
55
|
+
getEquinoxRequestHeaders() {
|
56
|
+
return {
|
57
|
+
authorization: `Bearer ${localStorage.getItem('equinox-okta-token')}`,
|
58
|
+
'Content-Type': 'application/json',
|
59
|
+
'x-sk-session-id': localStorage.getItem('x-sk-session-id')
|
60
|
+
};
|
53
61
|
},
|
54
62
|
|
55
|
-
getProductFromLegacy: async function (skus, locale
|
56
|
-
const
|
63
|
+
getProductFromLegacy: async function (skus, locale) {
|
64
|
+
const marketConfig = await contentstack.getAEMConfig();
|
65
|
+
|
66
|
+
if (marketConfig === null) {
|
67
|
+
return null;
|
68
|
+
}
|
69
|
+
|
70
|
+
const lambdaUrl = `${marketConfig.aws_url}/productData/v1`;
|
57
71
|
const payload = { locale, skus };
|
58
72
|
|
59
73
|
return await axios.request({
|
60
|
-
method:
|
74
|
+
method: 'post',
|
61
75
|
url: lambdaUrl,
|
62
76
|
data: payload,
|
63
|
-
headers: this.
|
64
|
-
responseType:
|
77
|
+
headers: this.getLegacyRequestHeaders(marketConfig),
|
78
|
+
responseType: 'json'
|
65
79
|
});
|
66
80
|
},
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
81
|
+
|
82
|
+
/**
|
83
|
+
* @param {contentstack.MarketConfig} marketConfig
|
84
|
+
*/
|
85
|
+
getLegacyRequestHeaders: function(marketConfig) {
|
86
|
+
return {
|
87
|
+
'Content-Type': 'application/json',
|
88
|
+
client_id: marketConfig.checkout_client_id,
|
89
|
+
client_secret: marketConfig.checkout_client_secret
|
90
|
+
};
|
91
|
+
},
|
71
92
|
|
72
93
|
eqProductMapper: function (productDataResponse) {
|
73
94
|
let prodArr = [];
|
74
95
|
let prod = [];
|
75
96
|
let count = 0;
|
97
|
+
|
98
|
+
// we can use Array.map for this one
|
76
99
|
productDataResponse.forEach((data, index) => {
|
77
100
|
count++
|
78
101
|
|
@@ -198,87 +221,7 @@ const ProductData = {
|
|
198
221
|
"messages": [],
|
199
222
|
"data": data
|
200
223
|
}
|
201
|
-
}
|
202
|
-
|
203
|
-
},
|
204
|
-
|
205
|
-
getProductDataRequestHeaders: function (marketConfig) {
|
206
|
-
return {
|
207
|
-
"Content-Type": "application/json",
|
208
|
-
client_id: marketConfig.checkout.clientId,
|
209
|
-
client_secret: marketConfig.checkout.clientSecret
|
210
224
|
};
|
211
|
-
},
|
212
|
-
|
213
|
-
/**
|
214
|
-
* Get equinox enabled market list from contentstack
|
215
|
-
*
|
216
|
-
* @return {Promise<null|import('@nuskin/ns-util/src/local-storage/equinox').MarketList>}
|
217
|
-
* @public
|
218
|
-
*/
|
219
|
-
getEquinoxEnabledMarkets: async function () {
|
220
|
-
try {
|
221
|
-
const env = getEnvironmentFromUrl(window.location.origin);
|
222
|
-
const variables = { uid: 'blt5633ff9e560f14ef' }
|
223
|
-
const query = `query EquinoxMarketsByUid($uid: String!) {
|
224
|
-
equinox_markets(uid: $uid) {
|
225
|
-
${env}_markets {
|
226
|
-
market_name
|
227
|
-
country_code
|
228
|
-
store_id
|
229
|
-
}
|
230
|
-
}
|
231
|
-
}`;
|
232
|
-
|
233
|
-
const { data } = await contentStackHttp({
|
234
|
-
method: 'post',
|
235
|
-
data: { variables, query }
|
236
|
-
});
|
237
|
-
|
238
|
-
return data.equinox_markets[`${env}_markets`];
|
239
|
-
} catch (error) {
|
240
|
-
console.error(error);
|
241
|
-
}
|
242
|
-
}
|
243
|
-
}
|
244
|
-
|
245
|
-
/**
|
246
|
-
* Contentstack HTTP - axios wrapper for contentstack request
|
247
|
-
*
|
248
|
-
* @typedef EquinoxEnabledMarket
|
249
|
-
* @property {string} market_name
|
250
|
-
* @property {string} country_code
|
251
|
-
* @property {string} store_id
|
252
|
-
*
|
253
|
-
* @typedef EquinoxEnabledMarkets
|
254
|
-
* @property {EquinoxEnabledMarket[]} [dev_markets]
|
255
|
-
* @property {EquinoxEnabledMarket[]} [test_markets]
|
256
|
-
* @property {EquinoxEnabledMarket[]} [staging_markets]
|
257
|
-
* @property {EquinoxEnabledMarket[]} [prod_markets]
|
258
|
-
*
|
259
|
-
* @typedef EquinoxEnvironmentEnabledMarkets
|
260
|
-
* @property {EquinoxEnabledMarkets} equinox_markets
|
261
|
-
*
|
262
|
-
* @typedef EquinoxEnabledMarketsResponse
|
263
|
-
* @property {EquinoxEnvironmentEnabledMarkets} data
|
264
|
-
*
|
265
|
-
* @param {Object} config - required param
|
266
|
-
* @return {Promise<EquinoxEnabledMarketsResponse>} Promise that resolves to market_list_eq object
|
267
|
-
*/
|
268
|
-
async function contentStackHttp(config) {
|
269
|
-
const defaultUrl = `${CS_BASE_URL}/stacks/${CS_API_KEY}?environment=${CS_DEFAULT_ENV}`;
|
270
|
-
|
271
|
-
config.method = config.method || 'get'
|
272
|
-
config.url = config.url || defaultUrl
|
273
|
-
config.headers = {
|
274
|
-
'access_token': `${CS_DELIVERY_TOKEN}`,
|
275
|
-
'Content-Type': 'application/json'
|
276
|
-
}
|
277
|
-
try {
|
278
|
-
const response = await axios(config);
|
279
|
-
return response.data
|
280
|
-
} catch (error) {
|
281
|
-
console.error(error.response.data.errors);
|
282
225
|
}
|
283
226
|
}
|
284
227
|
|