@@ -2,16 +2,20 @@ import { toast } from "@/hooks/use-toast";
22
33const PROXY_BASE_URL = import . meta. env . VITE_SPARKY_FITNESS_SERVER_URL || "http://localhost:3010/api/fatsecret" ; // URL of your Node.js proxy server
44
5- interface FatSecretFoodItem {
5+ export interface FatSecretFoodItem {
66 food_id : string ;
77 food_name : string ;
88 brand_name ?: string ;
99 food_type : string ;
1010 food_url : string ;
1111 food_description : string ;
12- servings ?: {
13- serving : FatSecretServing [ ] ;
14- } ;
12+ // Add parsed basic nutrients from food_description
13+ calories ?: number ;
14+ protein ?: number ;
15+ carbs ?: number ;
16+ fat ?: number ;
17+ serving_size ?: number ;
18+ serving_unit ?: string ;
1519}
1620
1721interface FatSecretServing {
@@ -65,6 +69,33 @@ interface FatSecretFoodGetResponse {
6569 } ;
6670}
6771
72+ // Helper function to parse food_description for nutrients and serving info
73+ const parseFoodDescription = ( description : string ) => {
74+ const caloriesMatch = description . match ( / C a l o r i e s : ( \d + ) k c a l / ) ;
75+ const fatMatch = description . match ( / F a t : ( [ \d . ] + ) g / ) ;
76+ const carbsMatch = description . match ( / C a r b s : ( [ \d . ] + ) g / ) ;
77+ const proteinMatch = description . match ( / P r o t e i n : ( [ \d . ] + ) g / ) ;
78+
79+ // Extract serving size and unit (e.g., "Per 1 serving", "Per 100g")
80+ const servingMatch = description . match ( / P e r ( [ \d . ] + ) ( .+ ?) - / ) ;
81+ let serving_size : number | undefined ;
82+ let serving_unit : string | undefined ;
83+
84+ if ( servingMatch ) {
85+ serving_size = parseFloat ( servingMatch [ 1 ] ) ;
86+ serving_unit = servingMatch [ 2 ] . trim ( ) ;
87+ }
88+
89+ return {
90+ calories : caloriesMatch ? parseFloat ( caloriesMatch [ 1 ] ) : 0 ,
91+ fat : fatMatch ? parseFloat ( fatMatch [ 1 ] ) : 0 ,
92+ carbs : carbsMatch ? parseFloat ( carbsMatch [ 1 ] ) : 0 ,
93+ protein : proteinMatch ? parseFloat ( proteinMatch [ 1 ] ) : 0 ,
94+ serving_size,
95+ serving_unit,
96+ } ;
97+ } ;
98+
6899export const searchFatSecretFoods = async ( query : string , providerId : string ) => {
69100 try {
70101 const response = await fetch (
@@ -90,42 +121,23 @@ export const searchFatSecretFoods = async (query: string, providerId: string) =>
90121
91122 const data : FatSecretSearchResponse = await response . json ( ) ;
92123 if ( data . foods && data . foods . food ) {
93- const detailedFoods : any [ ] = [ ] ;
94- for ( const item of data . foods . food ) {
95- // For each food found in search, fetch its detailed nutrients using food.get.v4
96- const nutrientData = await getFatSecretNutrients ( item . food_id , providerId ) ;
97- if ( nutrientData ) {
98- detailedFoods . push ( {
99- id : item . food_id ,
100- name : nutrientData . name ,
101- brand : nutrientData . brand || null ,
102- food_type : item . food_type , // Keep original food_type from search
103- description : item . food_description , // Keep original description from search
104- source : "FatSecret" ,
105- calories : nutrientData . calories ,
106- protein : nutrientData . protein ,
107- carbs : nutrientData . carbohydrates ,
108- fat : nutrientData . fat ,
109- serving_size : nutrientData . serving_qty ,
110- serving_unit : nutrientData . serving_unit ,
111- // Include other detailed nutrients if needed for initial display
112- saturated_fat : nutrientData . saturated_fat ,
113- polyunsaturated_fat : nutrientData . polyunsaturated_fat ,
114- monounsaturated_fat : nutrientData . monounsaturated_fat ,
115- trans_fat : nutrientData . trans_fat ,
116- cholesterol : nutrientData . cholesterol ,
117- sodium : nutrientData . sodium ,
118- potassium : nutrientData . potassium ,
119- dietary_fiber : nutrientData . dietary_fiber ,
120- sugars : nutrientData . sugars ,
121- vitamin_a : nutrientData . vitamin_a ,
122- vitamin_c : nutrientData . vitamin_c ,
123- calcium : nutrientData . calcium ,
124- iron : nutrientData . iron ,
125- } ) ;
126- }
127- }
128- return detailedFoods ;
124+ return data . foods . food . map ( item => {
125+ const parsedData = parseFoodDescription ( item . food_description ) ;
126+ return {
127+ food_id : item . food_id ,
128+ food_name : item . food_name ,
129+ brand_name : item . brand_name || null ,
130+ food_type : item . food_type ,
131+ food_url : item . food_url ,
132+ food_description : item . food_description , // Keep original for now, will remove from display in frontend
133+ calories : parsedData . calories ,
134+ protein : parsedData . protein ,
135+ carbs : parsedData . carbs ,
136+ fat : parsedData . fat ,
137+ serving_size : parsedData . serving_size ,
138+ serving_unit : parsedData . serving_unit ,
139+ } ;
140+ } ) ;
129141 }
130142 return [ ] ;
131143 } catch ( error ) {
0 commit comments