conf
conf copied to clipboard
[Feature Request] Getting multiple values from the store
Assume we have a schema;
schema: {
foo: { bar: 'baz' },
bar: { baz: 'foo' },
baz: 'bar'
}
I think it would be nice to get all these values at once like;
const [value1, value2, value3] = conf.get('foo.bar', 'bar.baz', 'baz')
OR
const [value1, value2, value3] = conf.get(['foo.bar', 'bar.baz', 'baz'])
What do you think about this approach?
I'm ok with supporting conf.get(['foo.bar', 'bar.baz', 'baz'])
if someone does a good pull request with docs, TypeScript types, and tests.
@sindresorhus I'd like to work on this, do you have any suggestion about how to handle default values?
One idea could be to do something like;
conf.get(['foo.bar', 'bar.baz', 'baz'], ['default1', 'default2', 'default'3])
But it would be easy to do mistakes.
Another suggestion is something like;
conf.get([['foo.bar', 'default1'], ['bar.baz', 'default2'], ['baz', 'default3']])
But I didn't quite like it either.
Neither are very nice. Out of those two, I would pick the latter. You have to keep in mind that it has to be possible to correctly type it too. The types for .get()
are already a bit complicated: https://github.com/sindresorhus/conf/blob/43c644f6fb0cd96a9c2e1f49b2ee0780cd089b4c/source/index.ts#L167-L173
Thinking about it again, it's really tricky to make it type-safe with [key, defaultValue]
syntax.
So I implemented something like below, wrote tests, they are already passing and will write TS types, LMK what do you think about this approach;
conf.getMany([{ key: 'foo', defaultValue: 'baz' }, { key: 'foo.bar', defaultValue: 'tar' }])
OR
conf.getMany([{ key: 'foo' }, { key: 'foo.bar' }])
Looks ok to me. I think we should let users specify a string if they don't need the default value:
conf.getMany(['foo', 'bar'])
Many users use the defaults
or schema
option and don't need the defaultValue
argument.
Not sure whether we should allow mixing string and object though. Not sure if it's even possible type-wise.
@sindresorhus I managed to create a similar concept with strong type-safety. Would you like to check and let me know if we can extract something from there? https://github.com/BatuhanW/haf