Skip to content

Instantly share code, notes, and snippets.

@thefln
Created May 14, 2018 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thefln/cd06f4b5cda10c8d5f26d8b138aac205 to your computer and use it in GitHub Desktop.
Save thefln/cd06f4b5cda10c8d5f26d8b138aac205 to your computer and use it in GitHub Desktop.
var butter = require('buttercms')("{buttercms public API key}");
exports.handler = function(event, context, callback) {
// snipcart send a GET request when crawling products
if(event.httpMethod !== 'GET') {
return callback(null, {
statusCode: 200,
body: ''
});
}
// list products the same way we do on the website
butter.page.list('product')
.then((res) => {
var products = res.data.data.map((product) => {
return {
id: product.fields.product_id,
name: product.fields.name,
price: product.fields.price,
description: product.fields.description,
image: product.fields.image,
url: 'https://snipcart-buttercms-demo.netlify.com/.netlify/functions/snipcart_json_crawler',
};
});
callback(null, {
statusCode: 200,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(products),
});
}); // we would normally handle errors right? ;)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment