36 lines
817 B
TypeScript
36 lines
817 B
TypeScript
import { apiClient, withAuth } from "./client";
|
|
import { API_ENDPOINTS } from "../config/api";
|
|
|
|
export interface ScannedFoodProduct {
|
|
barcode: string;
|
|
name: string;
|
|
brand: string | null;
|
|
imageUrl: string | null;
|
|
servingSize: string;
|
|
caloriesPerServing: number;
|
|
macros: {
|
|
protein: number | null;
|
|
carbs: number | null;
|
|
fat: number | null;
|
|
};
|
|
source: "openfoodfacts";
|
|
}
|
|
|
|
interface FoodLookupResponse {
|
|
success: boolean;
|
|
data: ScannedFoodProduct;
|
|
}
|
|
|
|
export async function lookupFoodByBarcode(
|
|
barcode: string,
|
|
token: string | null,
|
|
): Promise<ScannedFoodProduct> {
|
|
const normalized = barcode.replace(/\D/g, "");
|
|
const response = await apiClient.get<FoodLookupResponse>(
|
|
API_ENDPOINTS.FOOD.LOOKUP_BARCODE(normalized),
|
|
withAuth(token),
|
|
);
|
|
|
|
return response.data.data;
|
|
}
|