Created
May 14, 2018 15:20
-
-
Save thefln/cd06f4b5cda10c8d5f26d8b138aac205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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