121 lines
4.2 KiB
JavaScript
121 lines
4.2 KiB
JavaScript
const axios = require('axios');
|
|
|
|
/**
|
|
* Busca recetas por nombre utilizando la API de TheMealDB.
|
|
* @param {String} name - Nombre de la receta a buscar.
|
|
* @returns {Array} - Lista de recetas encontradas.
|
|
* @throws {Error} - Si no se encuentran recetas o hay un error en la solicitud.
|
|
*/
|
|
const searchMealByName = async (name) => {
|
|
try {
|
|
const response = await axios.get('https://www.themealdb.com/api/json/v1/1/search.php', {
|
|
params: { s: name }
|
|
});
|
|
|
|
if (!response.data.meals) {
|
|
throw new Error('No se encontraron recetas para tu búsqueda.');
|
|
}
|
|
|
|
return response.data.meals; // Devuelve todas las recetas encontradas
|
|
} catch (error) {
|
|
throw new Error(`Error al buscar la receta: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Obtiene una receta aleatoria utilizando la API de TheMealDB.
|
|
* @returns {Object} - Objeto de receta aleatoria.
|
|
* @throws {Error} - Si hay un error en la solicitud.
|
|
*/
|
|
const getRandomMeal = async () => {
|
|
try {
|
|
const response = await axios.get('https://www.themealdb.com/api/json/v1/1/random.php');
|
|
if (!response.data.meals) {
|
|
throw new Error('No se pudo obtener una receta aleatoria.');
|
|
}
|
|
return response.data.meals[0];
|
|
} catch (error) {
|
|
throw new Error(`Error al obtener una receta aleatoria: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Filtra recetas por ingrediente principal utilizando la API de TheMealDB.
|
|
* @param {String} ingredient - Ingrediente principal para filtrar recetas.
|
|
* @returns {Array} - Lista de recetas que contienen el ingrediente especificado.
|
|
* @throws {Error} - Si no se encuentran recetas o hay un error en la solicitud.
|
|
*/
|
|
const filterByIngredient = async (ingredient) => {
|
|
try {
|
|
const response = await axios.get('https://www.themealdb.com/api/json/v1/1/filter.php', {
|
|
params: {
|
|
i: ingredient
|
|
}
|
|
});
|
|
|
|
if (!response.data.meals) {
|
|
throw new Error('No se encontraron recetas con ese ingrediente.');
|
|
}
|
|
|
|
return response.data.meals;
|
|
} catch (error) {
|
|
throw new Error(`Error al filtrar por ingrediente: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
/*
|
|
* Categorías saludables para filtrar recetas.
|
|
*/
|
|
const healthyCategories = ['Salad', 'Chicken', 'Eggs', 'Fish', 'Seafood', 'Pasta', 'Rice', 'Beef', 'Pork', 'Lamb'];
|
|
|
|
/**
|
|
* Obtiene una receta aleatoria de categorías saludables.
|
|
* @returns {Object} - Objeto de receta aleatoria saludable.
|
|
* @throws {Error} - Si no se encuentra una receta o hay un error en la solicitud.
|
|
*/
|
|
const getRandomHealthyMeal = async () => {
|
|
try {
|
|
// Escoge una categoría saludable al azar
|
|
const randomCategory = healthyCategories[Math.floor(Math.random() * healthyCategories.length)];
|
|
|
|
// Filtra las recetas por la categoría saludable seleccionada
|
|
const response = await axios.get('https://www.themealdb.com/api/json/v1/1/filter.php', {
|
|
params: { c: randomCategory }
|
|
});
|
|
|
|
if (!response.data.meals || response.data.meals.length === 0) {
|
|
throw new Error('No se encontraron recetas saludables.');
|
|
}
|
|
|
|
// Escoge una receta aleatoria dentro de las recetas saludables
|
|
const randomMeal = response.data.meals[Math.floor(Math.random() * response.data.meals.length)];
|
|
return await lookupMealById(randomMeal.idMeal);
|
|
} catch (error) {
|
|
throw new Error(`Error al obtener una receta saludable: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Busca los detalles completos de una receta por ID.
|
|
* @param {String} id - ID de la receta.
|
|
* @returns {Object} - Objeto de receta con detalles completos.
|
|
*/
|
|
const lookupMealById = async (id) => {
|
|
try {
|
|
const response = await axios.get(`https://www.themealdb.com/api/json/v1/1/lookup.php?i=${id}`);
|
|
if (!response.data.meals) {
|
|
throw new Error('No se encontraron detalles para la receta.');
|
|
}
|
|
return response.data.meals[0];
|
|
} catch (error) {
|
|
throw new Error(`Error al obtener detalles de la receta: ${error.message}`);
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
searchMealByName,
|
|
getRandomMeal,
|
|
filterByIngredient,
|
|
getRandomHealthyMeal,
|
|
lookupMealById
|
|
}; |