commercelayer
Advanced tools
Comparing version 1.5.5 to 1.5.6
{ | ||
"name": "commercelayer", | ||
"version": "1.5.5", | ||
"version": "1.5.6", | ||
"description": "The Official Commerce Layer JS library", | ||
@@ -8,3 +8,3 @@ "main": "./src/main.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "webpack --progress --mode=production", | ||
"build": "webpack --progress", | ||
"watch": "webpack --progress --watch" | ||
@@ -29,11 +29,10 @@ }, | ||
"dependencies": { | ||
"axios": "^0.18.0", | ||
"@commercelayer/sdk": "^1.3.1", | ||
"js-cookie": "^2.2.0", | ||
"json-api-normalize": "^1.1.5", | ||
"lodash": "^4.17.11" | ||
}, | ||
"devDependencies": { | ||
"webpack": "^4.26.1", | ||
"webpack-cli": "^3.1.2" | ||
"webpack": "^4.28.4", | ||
"webpack-cli": "^3.2.1" | ||
} | ||
} |
241
src/api.js
const elements = require('./elements') | ||
const axios = require('axios') | ||
const auth = require('./auth') | ||
const normalize = require('json-api-normalize') | ||
const config = require('./config') | ||
@@ -9,3 +6,6 @@ const utils = require('./utils') | ||
const clsdk = require('@commercelayer/commercelayer-js-sdk') | ||
module.exports = { | ||
getPrices: function() { | ||
@@ -17,3 +17,3 @@ | ||
skuCodes = [] | ||
let skuCodes = [] | ||
@@ -24,5 +24,8 @@ $prices.forEach(function ($price) { | ||
skus = [] | ||
skusEndpoint = '/api/skus?filter[codes]=' + skuCodes.join(',') +'&include=prices&page[size]=25' | ||
skuAttributes = [ | ||
let qf = new clsdk.query.QueryFilter() | ||
.filter('codes', skuCodes.join(',')) | ||
.include('prices') | ||
.page(null, 25); | ||
let skuAttributes = [ | ||
'id', | ||
@@ -36,25 +39,23 @@ 'code', | ||
axios | ||
.get(skusEndpoint) | ||
.then(function(response) { | ||
clsdk.listSkus(qf.build()) | ||
.then(data => { | ||
ui.updatePrices(normalize(response.data).get(skuAttributes)) | ||
ui.updatePrices(data.get(skuAttributes)) | ||
pageCount = response.data.meta.page_count | ||
let pageCount = data.dataset.meta.page_count; | ||
if (pageCount > 1) { | ||
for (p=2; p<=pageCount; p++ ) { | ||
skusEndpointWithPage = skusEndpoint + '&page[number]=' + p | ||
axios | ||
.get(skusEndpointWithPage) | ||
.then(function(response) { | ||
ui.updatePrices(normalize(response.data).get(skuAttributes)) | ||
}) | ||
for (p=2; p<=pageCount; p++) { | ||
qf.pageNumber(p); | ||
clsdk.listSkus(qf) | ||
.then(data => ui.updatePrices(data.get(skuAttributes))); | ||
} | ||
} | ||
}) | ||
} | ||
); | ||
} | ||
}, | ||
getVariants: function() { | ||
@@ -68,3 +69,3 @@ | ||
skuCodes = [] | ||
let skuCodes = [] | ||
@@ -76,10 +77,9 @@ $variants.forEach(function (variant) { | ||
axios | ||
.get('/api/skus?filter[codes]=' + skuCodes.join(',')) | ||
.then(function(response) { | ||
skus = normalize(response.data).get([ | ||
'id', | ||
'code' | ||
]) | ||
let qf = new clsdk.query.QueryFilter().filter('codes', skuCodes.join(',')); | ||
clsdk.listSkus(qf.build()) | ||
.then(data => { | ||
let skus = data.get(['id', 'code' ]); | ||
for (i = 0; i < skus.length; i++) { | ||
@@ -96,12 +96,15 @@ | ||
} | ||
}) | ||
} | ||
); | ||
} | ||
}, | ||
getInventory: function(skuId, skuName) { | ||
axios | ||
.get('/api/skus/' + skuId + '?fields[skus]=inventory') | ||
.then(function(response) { | ||
sku = response.data.data | ||
ui.updateAvailabilityMessage(sku.attributes.inventory) | ||
if (sku.attributes.inventory.available) { | ||
clsdk.retrieveSku(skuId, {'fields[skus]' : 'inventory'}) | ||
.then((data) => { | ||
let inventory = data.get('inventory'); | ||
ui.updateAvailabilityMessage(inventory) | ||
if (inventory.available) { | ||
ui.updateAddToBagSKU(skuId, skuName) | ||
@@ -114,2 +117,3 @@ ui.enableAddToBag() | ||
}, | ||
selectVariant: function(variant) { | ||
@@ -134,30 +138,23 @@ switch(variant.tagName) { | ||
}, | ||
createOrder: function() { | ||
return axios | ||
.post('/api/orders', { | ||
data: { | ||
type: 'orders', | ||
attributes: { | ||
shipping_country_code_lock: config.countryCode(), | ||
language_code: config.languageCode(), | ||
cart_url: config.cartUrl(), | ||
return_url: config.returnUrl(), | ||
privacy_url: config.privacyUrl(), | ||
terms_url: config.termsUrl() | ||
} | ||
} | ||
},{ | ||
headers: { | ||
'Content-Type': 'application/vnd.api+json' | ||
} | ||
clsdk.createOrder({ | ||
type: 'orders', | ||
shipping_country_code_lock: config.countryCode(), | ||
language_code: config.languageCode(), | ||
cart_url: config.cartUrl(), | ||
return_url: config.returnUrl(), | ||
privacy_url: config.privacyUrl(), | ||
terms_url: config.termsUrl() | ||
} | ||
).then(function(response) { | ||
utils.setOrderToken(response.data.data.attributes.token) | ||
return(response.data.data) | ||
).then(data => { | ||
utils.setOrderToken(data.get('token')) | ||
return(data) | ||
}) | ||
}, | ||
refreshOrder: function() { | ||
if (utils.getOrderToken()) { | ||
this.getOrder().then(function(order) { | ||
if (order && order.attributes.status == 'placed') { | ||
if (order && order.get('status') == 'placed') { | ||
utils.deleteOrderToken() | ||
@@ -169,79 +166,66 @@ ui.clearShoppingBag() | ||
}, | ||
createLineItem: function(orderId, skuId, skuName, skuImageUrl) { | ||
return axios | ||
.post('/api/line_items', { | ||
data: { | ||
type: 'line_items', | ||
attributes: { | ||
quantity: 1, | ||
name: skuName, | ||
image_url: skuImageUrl, | ||
_update_quantity: 1 | ||
return clsdk.createLineItem({ | ||
data: { | ||
type: 'line_items', | ||
attributes: { | ||
quantity: 1, | ||
name: skuName, | ||
image_url: skuImageUrl, | ||
_update_quantity: 1 | ||
}, | ||
relationships: { | ||
order: { | ||
data: { | ||
type: 'orders', | ||
id: orderId | ||
} | ||
}, | ||
relationships: { | ||
order: { | ||
data: { | ||
type: 'orders', | ||
id: orderId | ||
} | ||
}, | ||
item: { | ||
data: { | ||
type: 'skus', | ||
id: skuId | ||
} | ||
item: { | ||
data: { | ||
type: 'skus', | ||
id: skuId | ||
} | ||
} | ||
} | ||
},{ | ||
headers: { | ||
'Content-Type': 'application/vnd.api+json' | ||
} | ||
} | ||
) | ||
.then(function(response) { | ||
return(response.data) | ||
}) | ||
}, | ||
deleteLineItem: function(lineItemId) { | ||
return axios | ||
.delete('/api/line_items/' + lineItemId) | ||
.then(function(response) { | ||
return clsdk.deleteLineItem(lineItemId) | ||
.then(function() { | ||
return true | ||
}) | ||
}, | ||
updateLineItem: function(lineItemId, attributes) { | ||
return axios | ||
.patch('/api/line_items/' + lineItemId, { | ||
data: { | ||
type: 'line_items', | ||
id: lineItemId, | ||
attributes: attributes | ||
} | ||
},{ | ||
headers: { | ||
'Content-Type': 'application/vnd.api+json' | ||
} | ||
return clsdk.updateLineItem(lineItemId, { | ||
data: { | ||
type: 'line_items', | ||
id: lineItemId, | ||
attributes: attributes | ||
} | ||
) | ||
.then(function(response) { | ||
return(response.data) | ||
}) | ||
}, | ||
updateLineItemQty: function(lineItemId, quantity) { | ||
api = this | ||
api.updateLineItem(lineItemId, { quantity: quantity }).then(function(lineItem){ | ||
api.getOrder() | ||
}) | ||
.catch(function(error) { | ||
if (error.response) { | ||
switch(error.response.status) { | ||
case 422: | ||
ui.displayShoppingBagUnavailableMessage() | ||
break | ||
api.updateLineItem(lineItemId, { quantity: quantity }) | ||
.then(function(){ | ||
api.getOrder() | ||
}) | ||
.catch(function(error) { | ||
if (error) { | ||
switch(error.status) { | ||
case 422: | ||
ui.displayShoppingBagUnavailableMessage() | ||
break | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}, | ||
}, | ||
updateShoppingBagItems: function(order) { | ||
@@ -252,3 +236,3 @@ api = this | ||
normalized_order = normalize(order).get([ | ||
normalized_order = order.get([ | ||
'id', | ||
@@ -339,2 +323,3 @@ 'formatted_subtotal_amount', | ||
}, | ||
getOrder: function() { | ||
@@ -344,18 +329,24 @@ | ||
return axios | ||
.get('/api/orders?include=line_items&filter[token]=' + utils.getOrderToken()) | ||
let qf = new clsdk.query.QueryFilter(); | ||
qf.include('line_items').filter('token', utils.getOrderToken()) | ||
return clsdk.listOrders(qf) | ||
.then(function(response) { | ||
if (response.data.data.length > 0) { | ||
api.updateShoppingBagItems(response.data) | ||
if (response.get(['line_items']).length > 0) { | ||
api.updateShoppingBagItems(response) | ||
ui.hideShoppingBagUnavailableMessage() // refactor | ||
ui.updateShoppingBagSummary(response.data.data[0]) | ||
ui.updateShoppingBagCheckout(response.data) | ||
if (response.data.data[0].attributes.skus_count == 0) { | ||
ui.updateShoppingBagSummary(response.dataset.data[0]) | ||
ui.updateShoppingBagCheckout(response) | ||
if (response.get('skus_count') == 0) { | ||
ui.clearShoppingBag() | ||
} | ||
return response.data.data[0] | ||
return response; | ||
} | ||
}) | ||
} | ||
) | ||
} | ||
} |
@@ -0,0 +0,0 @@ const elements = require('./elements') |
module.exports = { | ||
config: document.querySelector('#clayer-config'), | ||
@@ -3,0 +4,0 @@ main: document.querySelector('#clayer-main'), |
const elements = require('./elements') | ||
const axios = require('axios') | ||
const auth = require('./auth') | ||
const ui = require('./ui') | ||
@@ -9,2 +7,3 @@ const api = require('./api') | ||
module.exports = { | ||
setVariantSelect: function() { | ||
@@ -28,2 +27,3 @@ | ||
}, | ||
setAddToShoppingBag: function() { | ||
@@ -33,3 +33,3 @@ $addToBag = elements.addToBag | ||
if ($addToBag) { | ||
$addToBag.addEventListener('click', function(event){ | ||
$addToBag.addEventListener('click', function(event) { | ||
event.preventDefault() | ||
@@ -40,3 +40,3 @@ | ||
orderPromise.then(function(order){ | ||
api.createLineItem(order.id, $addToBag.dataset.skuId, $addToBag.dataset.skuName, $addToBag.dataset.skuImageUrl).then(function(lineItem){ | ||
api.createLineItem(order.get('id')[0], $addToBag.dataset.skuId, $addToBag.dataset.skuName, $addToBag.dataset.skuImageUrl).then(function(lineItem){ | ||
api.getOrder() | ||
@@ -58,2 +58,3 @@ ui.openShoppingBag() | ||
}, | ||
setShoppingBagToggle: function() { | ||
@@ -68,2 +69,3 @@ $shoppingBagToggle = elements.shoppingBagToggle | ||
} | ||
} |
function initCommercelayer() { | ||
const api = require('./api') | ||
const listeners = require('./listeners') | ||
const utils = require('./utils') | ||
const config = require('./config') | ||
const clsdk = require('@commercelayer/commercelayer-js-sdk') | ||
exports.init = function() { | ||
clsdk.initialize({ | ||
client_id : config.clientId(), | ||
market_id : config.marketId(), | ||
base_url : config.baseUrl(), | ||
country_code : config.countryCode(), | ||
language_code : config.languageCode() | ||
}); | ||
clsdk.settings.debug = true; | ||
clsdk.settings.console = true; | ||
clsdk.settings.trace = true; | ||
api.getPrices() | ||
@@ -13,5 +28,6 @@ api.getVariants() | ||
api.refreshOrder() | ||
} | ||
window.commercelayer = module.exports | ||
// window.commercelayer = module.exports | ||
module.exports.init() | ||
@@ -18,0 +34,0 @@ } |
@@ -88,2 +88,3 @@ const elements = require('./elements') | ||
}, | ||
updateShoppingBagSummary: function(order) { | ||
@@ -119,6 +120,7 @@ $shoppingBagItemsCount = elements.shoppingBagItemsCount | ||
}, | ||
updateShoppingBagCheckout: function(order) { | ||
$shoppingBagCheckout = elements.shoppingBagCheckout | ||
if ($shoppingBagCheckout) { | ||
normalized_order = normalize(order).get([ | ||
normalized_order = /*normalize(order)*/order.get([ | ||
'line_items.id', | ||
@@ -125,0 +127,0 @@ 'checkout_url' |
@@ -0,0 +0,0 @@ const config = require('./config') |
const path = require('path') | ||
module.exports = { | ||
var devConfig = { | ||
mode: 'development', | ||
entry: './src/main.js', | ||
output: { | ||
filename: 'commercelayer.dev.js', | ||
path: path.resolve(__dirname, "dist") | ||
} | ||
} | ||
var prodConfig = { | ||
mode: 'production', | ||
@@ -11,1 +20,6 @@ entry: './src/main.js', | ||
} | ||
module.exports = [ | ||
devConfig, | ||
prodConfig | ||
] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
77522
3
17
1255
1
16
+ Added@commercelayer/sdk@^1.3.1
+ Added@commercelayer/sdk@1.6.3(transitive)
- Removedaxios@^0.18.0
- Removedjson-api-normalize@^1.1.5