exports.handler
. Pass that just create other JS functions to call inside of that function, which should give what you want
const fetchAllProducts = async () => {
const categories = await GetActiveCategories();
const categoriesIdArray = categories.map(category => category.CategoryId);
const productsNested = await Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
return ProductsNested.flat()
}
const hotDeals = async () => {
const products = await fetchAllProducts();
return products.filter(product => product.DiscountPercentage > 15)
}
my lambda is to implement this functionality, the idea is to have one lambda that does a fetch of active categories, and then the next lambda get this data to calls the getSearchResults lambda to return a list of all products.
since we write normal javascript in a way that has seperate functions for each specific task, in a lambda are we supposed to not follow that pattern and write a big function that does everything in it?
import fetch from 'node-fetch';
const {API_TOKEN} = process.env;
exports.handler = async (event,context,callback) => {
const activeCategories = await fetch('/.netlify/functions/getActiveCategories')
const categoriesIdArray = categories.map(category => category.CategoryId);
const productsNested = await Promise.all(categoriesIdArray.map(categoryID => getSearchResults(' ', categoryID)));
callback(null,{
statusCode: 200,
body: JSON.stringify(const productsNested)
})
}
I also tried doing
import getActiveCategories from './getActiveCategories';
and then
const activeCategories = await getActiveCategories()
fetch
as if your calling it from the front-end. Or seperate out the logic in the getActiveCateogires
to an actual function and cal it like your last example
import fetch from 'node-fetch';
const {API_TOKEN} = process.env;
exports.handler = async (event,context,callback) => {
const activeCategoriesRes = await fetch('https://api.example.com/category/GetActiveCategories',{
method: 'GET',
headers: {'APIToken': API_TOKEN}
});
const activeCategoriesData = await activeCategoriesRes.json();
const categories = activeCategoriesData.result;
const categoriesIdArray = categories.map(category => category.CategoryId);
const productsNested = await Promise.all(categoriesIdArray.map(categoryID => {
const categoryProductsRes = await fetch(`http://api.example.com/Product/GetProductSearchByCategory`,{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'APIToken': API_TOKEN
},
body: JSON.stringify({
"CategoryID": CategoryID,
"Search": " ",
"lstBrand": [],
"lstColor": [],
"lstSize": [],
"PriceFrom": 0,
"PriceTo": 0,
"MaxPrice": 0,
"Page": 1,
"Show": 9999,
"UserID": ""
})
})
const categoryProductsData = await categoryProductsRes.json();
return categoryProductsData.result;
}));
const productsList = productsNested.flat();
const hotDeals = productsList.filter(product => product.DiscountPercentage > 15 && product.SellingPrice > 50);
callback(null,{
statusCode: 200,
body: JSON.stringify(hotDeals)
})
}
that it periodically fetches data from the original server, say once every 2 hours and then caches it
The netlify server instance shouldn't stay running longer than a few seconds, or minutes after each call between calls. Thus you will need something running "long term" if you want to cache it for 2 hours