From 2969099957fce82fc54c598a015764f812d02b1b Mon Sep 17 00:00:00 2001 From: Loule | Louis <35641311+Loule95450@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:33:30 +0100 Subject: [PATCH] Update apiCall.ts --- src/service/apiCall.js | 21 --------------------- src/service/apiCall.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 21 deletions(-) delete mode 100644 src/service/apiCall.js create mode 100644 src/service/apiCall.ts diff --git a/src/service/apiCall.js b/src/service/apiCall.js deleted file mode 100644 index 123118aa..00000000 --- a/src/service/apiCall.js +++ /dev/null @@ -1,21 +0,0 @@ -const apiUrl = 'https://world.openfoodfacts.org/api/v2/product/'; - -export async function getProduct(barcode) -{ - let data; - await fetch(apiUrl + barcode) - .then(result => result.json()) - .then(json => { - data.code = json.code; - data.name = json.product.generic_name_fr ? json.product.generic_name_fr : json.product.generic_name_en ? json.product.generic_name_en : json.product.generic_name; - data.nutriscore = json.product.nutriscore_grade; - data.categories = json.product.categories; - data.nutriments = json.product.nutriments; - }) - .catch(error => { - console.error(error); - data = null; - }); - - return data; -} \ No newline at end of file diff --git a/src/service/apiCall.ts b/src/service/apiCall.ts new file mode 100644 index 00000000..c3f8ae60 --- /dev/null +++ b/src/service/apiCall.ts @@ -0,0 +1,33 @@ +const apiUrl = 'https://world.openfoodfacts.org/api/v2/product/'; + +export async function getProduct(barcode: string) { + let data: + | { + code: any; + name: any; + nutriscore: any; + categories: any; + nutriments: any; + } + | any; + + await fetch(apiUrl + barcode) + .then(result => result.json()) + .then(json => { + data.code = json.code; + data.name = json.product.generic_name_fr + ? json.product.generic_name_fr + : json.product.generic_name_en + ? json.product.generic_name_en + : json.product.generic_name; + data.nutriscore = json.product.nutriscore_grade; + data.categories = json.product.categories; + data.nutriments = json.product.nutriments; + }) + .catch(error => { + console.error(error); + data = null; + }); + + return data; +}