diff --git a/.env.example b/.env.example index 62631d230..dd613d67b 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ VITE_FOLKSONOMY_API_URL=https://api.folksonomy.openfoodfacts.net VITE_OFF_BASE_URL=https://world.openfoodfacts.org +VITE_API_VERSION=3 diff --git a/README.md b/README.md index bd4540c37..b90ae1338 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,12 @@ cp .env.example .env Edit the `.env` file as needed to configure your development environment. +#### Development Configuration + +- Set `VITE_API_VERSION` in `.env` file to test different versions of the Open Food Facts API (default: 3) +- For example, to test the API v2: `VITE_API_VERSION=2` +- This is useful for testing new API versions before updating the default version used in the application. + Then start the development server: ```bash diff --git a/src/lib/api/product.ts b/src/lib/api/product.ts index 5cece31db..b2a39bbfc 100644 --- a/src/lib/api/product.ts +++ b/src/lib/api/product.ts @@ -11,7 +11,8 @@ export class ProductsApi { } async getProductAttributes(barcode: string): Promise { - const url = `${API_HOST}/api/v2/product/${barcode}?fields=product_name,code,attribute_groups_en`; + const apiVersion = import.meta.env.VITE_API_VERSION || '3'; + const url = `${API_HOST}/api/v${apiVersion}/product/${barcode}?fields=product_name,code,attribute_groups_en`; const res = await this.fetch(url); diff --git a/src/lib/const.ts b/src/lib/const.ts index d9976b0e3..20cba501b 100644 --- a/src/lib/const.ts +++ b/src/lib/const.ts @@ -9,5 +9,8 @@ export const USER_AGENT = `Open Food Facts Explorer (${import.meta.env.PACKAGE_V export const KP_ATTRIBUTE_IMG = (img: string) => `${STATIC_HOST}/images/attributes/dist/${img}`; export const TAXONOMY_URL = (taxo: string) => `${STATIC_HOST}/data/taxonomies/${taxo}.json`; -export const PRODUCT_URL = (barcode: string) => `${API_HOST}/api/v3/product/${barcode}.json`; +export const PRODUCT_URL = (barcode: string) => { + const apiVersion = import.meta.env.VITE_API_VERSION || '3'; + return `${API_HOST}/api/v${apiVersion}/product/${barcode}.json`; +}; export const PRODUCT_IMAGE_URL = (path: string) => `${IMAGE_HOST}/images/products/${path}`;