Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: onChange in v2 #612

Merged
merged 9 commits into from
Feb 4, 2021
Merged

Fix: onChange in v2 #612

merged 9 commits into from
Feb 4, 2021

Conversation

willfarrell
Copy link
Member

Fixes #611

@@ -57,7 +57,7 @@ npm install --save @middy/ssm
- `cacheExpiry` (number) (default `-1`): How long fetch data responses should be cached for. `-1`: cache forever, `0`: never cache, `n`: cache for n ms.
- `setToEnv` (boolean) (default `false`): Store role tokens to `process.env`. **Storing secrets in `process.env` is considered security bad practice**
- `setToContext` (boolean) (default `false`): Store role tokes to `handler.context`.
- `onChange` (function) (optional): Calls function when role tokens change after being initially set.
- `onChange` (function(data)) (optional): Calls function when role tokens change after being initially set.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

role token doesn't make sense in this context

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe parameter?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I also need to make another tweak to ensure it doesn't call on init.

@willfarrell willfarrell marked this pull request as draft February 2, 2021 16:59
@willfarrell
Copy link
Member Author

@lmammino @theburningmonk
Just thinking on this. onChange isn't the best descriptor, onFetch might be better as the result may not have changed between fetches. Thoughts?

I currently have it implemented in this PR to pass in the data to onChange, but it feels like a hack for the use case in #611.

This is a better solution:

middy(originalHandler)
  .use(ssm({
    fetchData:{
      key:'path'
    },
    setToContext: true
  }))
  .before((handler) => {
    process.env.defaults = handler.context.key.defaults
  })

For this specific use case I don't see a need for onChange.

... to chain middlewares that need to know when an upstream middleware requests a new value ...

Are there other known use cases that should be considered? Maybe we don't need an onChange hook right now?

@lmammino
Copy link
Member

lmammino commented Feb 4, 2021

What about removing onChange entirely and documenting this approach in the middleware?

@willfarrell
Copy link
Member Author

I agree, I think we can remove it entirely. This pattern is kinda already documented in the best practice section on the main readme. I'll update to remove the onChange, add an extra example to ssm, but leave in the hook to detect if it was pulled from cache for those building custom middleware that need to chain.

@willfarrell willfarrell marked this pull request as ready for review February 4, 2021 15:32
@willfarrell willfarrell merged commit a3ba702 into release/2.x Feb 4, 2021
@willfarrell willfarrell deleted the feature/on-change branch February 4, 2021 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants