Skip to content

Commit

Permalink
Update apiCall.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Loule95450 committed Feb 27, 2024
1 parent 25d918a commit 2969099
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
21 changes: 0 additions & 21 deletions src/service/apiCall.js

This file was deleted.

33 changes: 33 additions & 0 deletions src/service/apiCall.ts
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 2969099

Please sign in to comment.