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

Feature: ability to add custom accessors #71

Closed
todofixthis opened this issue Aug 27, 2019 · 2 comments · Fixed by #72
Closed

Feature: ability to add custom accessors #71

todofixthis opened this issue Aug 27, 2019 · 2 comments · Fixed by #72

Comments

@todofixthis
Copy link
Contributor

todofixthis commented Aug 27, 2019

As a developer, I would like to define custom accessors, so that I can work with env vars in formats specific to my application's requirements.

For example:

const env = from(process.env, {
  asNodes: (raiseError, value, type = Set) => {
    return new type(
      value.split(',')
        .map(s => s.trim())
        .map(s => s.split(':', 2))
        .map(([ip, port = 80]) => ({ ip, port }))
    )
  }
})

const bootnodes = env.get('BOOTNODES').asNodes()

Proposed acceptance criteria:

  • A developer can call from() and provide an optional object as a second parameter. The object defines additional accessors for any value returned by that instance's get().
  • If the name matches an existing accessor, the existing accessor is replaced by the new one for all subsequent calls to env.get().
@evanshortiss
Copy link
Owner

@todofixthis I like this, there's a guide in the README for making a PR adding custom accessors, but sometimes they are application specific so this is great 👍

Will take a look at the PR

@todofixthis
Copy link
Contributor Author

Per #72 (comment) modifying the acceptance criteria so that extra accessors are provided as a parameter to from() rather than using a method to mutate the object state.

evanshortiss pushed a commit that referenced this issue Sep 5, 2019
* [#71] Added ability to attach custom accessors.

* [#71] Added ability to attach custom accessors.

* [#71] Document that accessors are instance-specific.

* [#71] Document that accessors are instance-specific.

* Address PR comments.

- Drop support for Node 6 & 7 (no longer LTS).
- Address some issues causing code coverage drop.

* [#71] Updated documentation.

* [#71] Tightened up documentation a teeny bit 🔧

* [#74] Remove `raiseError` accessor param.

If an accessor cannot process an input value, it should just throw an exception instead.

There is a trade-off to this approach, in that the stacktrace for the accessor-thrown error is not preserved (replaced by `_raiseError()`), but accessors tend to be fairly simple in implementation, so it shouldn' be too difficult to locate the source of an error.

* [#71] Replace `addAccessor` w/ extra param for `from`.

* [#71] Updated documentation.

* [#74] Refactored now-unnecessarily-complex function 🍃

* [#71] Add compat for #74.

* [#71] Removed obsolete link ⛓

* [#71] Fixed incorrect `@param` for `extraAccessors` 🤖

* [#71] Fixed obsolete documentation 🚂

* Bifurcating rabbits.
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 a pull request may close this issue.

2 participants