-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Post login trigger according to recommendations in https://github…
- Loading branch information
Showing
1 changed file
with
39 additions
and
13 deletions.
There are no files selected for viewing
52 changes: 39 additions & 13 deletions
52
web/amplify/backend/function/pettracking426fb16cPostConfirmation/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
/* | ||
this file will loop through all js modules which are uploaded to the lambda resource, | ||
provided that the file names (without extension) are included in the "MODULES" env variable. | ||
"MODULES" is a comma-delimmited string. | ||
*/ | ||
const moduleNames = process.env.MODULES.split(','); | ||
const modules = moduleNames.map(name => require(`./${name}`)); | ||
/** | ||
* @fileoverview | ||
* | ||
* This CloudFormation Trigger creates a handler which awaits the other handlers | ||
* specified in the `MODULES` env var, located at `./${MODULE}`. | ||
*/ | ||
|
||
exports.handler = (event, context, callback) => { | ||
for (let i = 0; i < modules.length; i += 1) { | ||
const { handler } = modules[i]; | ||
handler(event, context, callback); | ||
} | ||
}; | ||
/** | ||
* The names of modules to load are stored as a comma-delimited string in the | ||
* `MODULES` env var. | ||
*/ | ||
const moduleNames = process.env.MODULES.split(','); | ||
/** | ||
* The array of imported modules. | ||
*/ | ||
const modules = moduleNames.map(name => require(`./${name}`)); | ||
|
||
/** | ||
* This async handler iterates over the given modules and awaits them. | ||
* | ||
* @see https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html#nodejs-handler-async | ||
* | ||
* @param {object} event | ||
* | ||
* The event that triggered this Lambda. | ||
* | ||
* @returns | ||
* | ||
* The handler response. | ||
*/ | ||
exports.handler = async event => { | ||
/** | ||
* Instead of naively iterating over all handlers, run them concurrently with | ||
* `await Promise.all(...)`. This would otherwise just be determined by the | ||
* order of names in the `MODULES` var. | ||
*/ | ||
await Promise.all(modules.map(module => module.handler(event))); | ||
return event; | ||
}; | ||
|