food/src/api/index.js

20 lines
580 B
JavaScript
Raw Normal View History

2017-09-19 04:28:26 +02:00
require('es6-promise').polyfill();
require('isomorphic-fetch');
/**
2017-09-22 03:05:48 +02:00
* TODO: Handle country/locale
* TODO: Handle errors
2017-09-19 04:28:26 +02:00
*/
export default function (EAN) {
return fetch(
`https://world.openfoodfacts.org/api/v0/product/${EAN}.json`,
)
.then(response => response.json())
.then(response => ({
// Return a clean object with only interesting fields
icon: response.product.image_thumb_url,
name: response.product.product_name,
}))
2017-09-19 04:28:26 +02:00
.catch(exc => console.error(`Unable to fetch product: ${exc}.`));
}