I think the solution here is to is to put some common logic in a package/function that you can use in each. You don't want to include code for one Lambda function in another. As much as possible you want to reduce this size of your code so that cold starts are faster. Granted, if your code is small it won't be a big difference, but it's a bit of an anti-pattern package the same code in multiple functions, and have the code do something different based on how it's called.
With that said, you can always include multiple entry points into your function and then pass something along to decide what to call from there, or even pass the function you want to call. So something like this:
export const emailHandler = (event, context, callback) => {
genericHandler(event, context, callback, email);
}
export const loginHandler = (event, context, callback) => {
genericHandler(event, context, callback, login);
}
export const genericHandler => (event, context, callback, function) => {
console.log('Event', event);
// How can I also make this work for login handler?
return function(event, context, callback)
}
const email = (event, context, callback) => {
// email code
}
const login = (event, context, callback) => {
// login code
}
const logEvent = fn => (event, ...args) => { console.log(event); return fn(event, ...args); }and thenemailHandler = logEvent((event, context, callback) => { ... })