Skip to content

feat(parameters): add adaptive types to SecretsProvider #1411

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

Merged
merged 2 commits into from
Apr 14, 2023

Conversation

dreamorosi
Copy link
Contributor

@dreamorosi dreamorosi commented Apr 14, 2023

Description of your changes

As discussed in the linked issue, the return types for the SecretsProvider part of the Parameters utility were fairly generic and could be improved by applying some heuristics based on the method used and some of the parameters passed. In addition to that, we could also provide a way for users to explicitly set the return type when they know the shape of the values they are retrieving.

This PR introduces new adaptive types for the getSecret() function as well as its class-based corespondent SecretsProvider.get()`.

The new types use generics and modify the return type of the functions/methods based on the arguments passed to them or based on an explicitly set type.

For instance, if users pass transform: json, then we can assume that the return type should be an object (Record<K, V>). Conversely, if no transform or a binary (base64) transform is applied, the result will be a string.

Finally, if the user doesn't specify any transform then, given tht SecretsManager is able to store both SecretString and SecretBinary data types, then we can only narrow down the return type to one of string or Uint8Array.

There are however also cases in which users might know more about the values they are retrieving than the compiler does, and for those instances this PR introduces the ability to specify the return type while using either the function or method like so:

const value = getSecret<number>('my-secret', { transform: 'json' });

In the example above the value constant will have type number, this is because the user has specified a type and so this will take precedence over any type-heuristic we do behind the scenes.

Note
This PR is part of a series that covers the same topic for all Parameters providers. Once this work is done I'll open I'll add a new section to the docs that explains the behavior above.

Once merged this PR closes #1409

Related issues, RFCs

Issue number: #1409

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding changes to the examples
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published
  • The PR title follows the conventional commit semantics

Breaking change checklist

Is it a breaking change?: NO

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@dreamorosi dreamorosi linked an issue Apr 14, 2023 that may be closed by this pull request
2 tasks
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Apr 14, 2023
@dreamorosi dreamorosi self-assigned this Apr 14, 2023
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label Apr 14, 2023
@dreamorosi dreamorosi added the parameters This item relates to the Parameters Utility label Apr 14, 2023
@dreamorosi dreamorosi marked this pull request as ready for review April 14, 2023 15:46
@dreamorosi dreamorosi merged commit 5c6d527 into main Apr 14, 2023
@dreamorosi dreamorosi deleted the 1409-new-return-types-for-secrets-manager-parameters branch April 14, 2023 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature PRs that introduce new features or minor changes parameters This item relates to the Parameters Utility size/L PRs between 100-499 LOC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: improve return types for Secrets Manager Parameters
1 participant