This API route fetches a user’s latest meal plan and enriches it with nutritional data. It provides a structured breakdown of calories, protein, fat, and other values for each meal in the plan.
The endpoint:
- Authenticates the user via their token.
- Retrieves the most recent meal plan from the database.
- Fetches nutrition data from the CalorieNinjas API for each meal.
- Merges and sums nutritional values across multiple items if the API splits a query into parts.
- Returns a clean, structured response with per-meal nutrition or appropriate error messages.
This project was a real challenge and learning milestone for me:
- Finding the right API was not easy.
- The CalorieNinjas response breaks down queries (e.g., sending
banana chicken suya pizzareturns nutrition for each item separately). - The main difficulty was summing and restructuring the results into one usable format.
- I had to revisit and apply JavaScript fundamentals like
map,reduce, and object spreading to merge data effectively.
- User requests their nutrition data.
- API checks authentication.
- API fetches the user’s latest meal plan.
- For each meal title, the CalorieNinjas API is queried.
- Responses are summed and merged.
- API responds with structured nutrition data.