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

feat: allow buildPlugin to accept array of secret names #138

Merged
merged 5 commits into from
Oct 18, 2021

Conversation

xkal36
Copy link
Contributor

@xkal36 xkal36 commented Oct 15, 2021

closes #136

I took this approach so as to make this change backwards compatible: fastify.secrets, when we pass an array of secret names to buildPlugin, will now have keys that are integers (previous behaviour), as well as new keys which are the actual names of the secrets.

pMap might have been useful instead of pProps, but because of wanting to maintain backwards compatibility and not wanting to add a dependency, I went with this approach.

Please let me know if you feel it is necessary to add tests for other permutations and combinations: I just added a test for the case where there is no namespace.

@xkal36 xkal36 requested review from simoneb and Eomm October 15, 2021 15:55
Copy link
Member

@simoneb simoneb left a comment

Choose a reason for hiding this comment

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

Good job so far!

I would definitely go with pMap and introduce a new dep. the implementation would be extremely easy (conditionally call either pProps or pMap based on whether opts.secrets is an array or not) and it would avoid the index properties which a little ugly.

@xkal36
Copy link
Contributor Author

xkal36 commented Oct 15, 2021

Good job so far!

I would definitely go with pMap and introduce a new dep. the implementation would be extremely easy (conditionally call either pProps or pMap based on whether opts.secrets is an array or not) and it would avoid the index properties which a little ugly.

I tried with that and it gives the same behaviour as pProps

@xkal36 xkal36 requested a review from simoneb October 15, 2021 16:38
@simoneb
Copy link
Member

simoneb commented Oct 15, 2021

Good job so far!
I would definitely go with pMap and introduce a new dep. the implementation would be extremely easy (conditionally call either pProps or pMap based on whether opts.secrets is an array or not) and it would avoid the index properties which a little ugly.

I tried with that and it gives the same behaviour as pProps

Ok I wrote pMap just as a reference to what you mentioned earlier, but maybe pMap is not the right one. Here we're getting an array as input and we want to turn it into an object, right? Hence, the operation is a reduce operation, meaning, p-reduce https://github.com/sindresorhus/p-reduce

t.ok(decorate.called, 'decorates fastify')
t.ok(
decorate.calledWith('secrets', {
0: 'content for secret1-name',
Copy link

Choose a reason for hiding this comment

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

I would not expect these numbers properties, don't you think they are a surplus?

@@ -33,6 +33,12 @@ function buildPlugin(Client, pluginOpts) {

const secrets = await pProps(opts.secrets, (value) => client.get(value), { concurrency })

if (Array.isArray(opts.secrets)) {
for (let i = 0; i < opts.secrets.length; i++) {
secrets[opts.secrets[i]] = secrets[i]
Copy link

Choose a reason for hiding this comment

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

Moving this logic before the pProps call to compose an input parameter, would remove the index properties from fastify.secrets.

I think it would be more compliant to the principle of least astonishment

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have these index properties here for backwards compatibility. Do we not want to maintain backwards compatibility? This way, code like fastify.secrets[0] would still work

@simoneb simoneb merged commit a0b7864 into master Oct 18, 2021
@simoneb simoneb deleted the feat/accept-secrets-array branch October 18, 2021 08:37
@simoneb
Copy link
Member

simoneb commented Oct 18, 2021

Before releasing this change we should consider implementing sindresorhus/p-reduce#4 and using concurrency in the p-reduce usage as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: map secrets name to 1:1
4 participants