Skip to content
This repository was archived by the owner on Sep 25, 2020. It is now read-only.

Commit a6283b2

Browse files
committed
add setRemote() and getRemote()
1 parent 077239a commit a6283b2

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,21 @@ You can also call the process with
5353
### `var config = fetchConfig(dirname, opts)`
5454

5555
```ocaml
56-
playdoh-server/config := (dirname: String, opts?: {
56+
type Keypath : String | Array<String>
57+
58+
zero-config := (dirname: String, opts?: {
5759
argv?: Array<String>,
5860
dc?: String,
5961
blackList?: Array<String>,
6062
env?: Object<String, String>,
6163
seed?: Object<String, Any>
6264
}) => {
63-
get: (keypath?: String) => Any,
64-
set: (keypath: String, value: Any) => void,
65-
__state: Object<String, Any>
65+
get: (keypath?: Keypath) => Any,
66+
set: ((keypath: Keypath, value: Any) => void) &
67+
(value: Any) => void,
68+
getRemote: (keypath?: Keypath) => Any,
69+
setRemote: ((keypath: Keypath, value: Any) => void) &
70+
(value: Any) => void
6671
}
6772
```
6873

@@ -214,6 +219,33 @@ You can call `config.set("port", 9001)` to set the port value.
214219
You can call `config.set("playdoh-logger.kafka.port", 9001)` to
215220
set then nested kafka port config option.
216221

222+
Note you can also call `config.set(entireObject)` to merge an
223+
entire object into the `config` instance. This will use
224+
deep extend to set all the key / value pairs in `entireObject`
225+
onto the config instance.
226+
227+
#### `var value = config.getRemote(keypath)`
228+
229+
The same as `config.get()` but gets from a different in memory
230+
object then `config.get()`.
231+
232+
It's recommended that you use `config.get()` and `config.set()`
233+
for any local configuration that is static and effectively
234+
immutable after process startup.
235+
236+
You can use `config.getRemote()` and `config.setRemote()` for
237+
any dynamic configuration that is effectively controlled
238+
remotely outside your program.
239+
240+
#### `config.setRemote(keypath, value)`
241+
242+
The same as `config.set()` but sets to a different in memory
243+
objec then `config.set()`.
244+
245+
You can use `config.getRemote()` and `config.setRemote()` for
246+
any dynamic configuration that is effectively controlled
247+
remotely outside your program.
248+
217249
## Installation
218250

219251
`npm install zero-config`

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ function fetchConfigSync(dirname, opts) {
3535

3636
var configState = getConfigState(dirname, opts);
3737
var localConfigWrapper = ConfigWrapper(configState);
38+
var remoteConfigWrapper = ConfigWrapper({});
3839

3940
config.get = localConfigWrapper.get;
4041
config.set = localConfigWrapper.set;
42+
config.getRemote = remoteConfigWrapper.get;
43+
config.setRemote = remoteConfigWrapper.set;
4144

4245
// deprecated: __state, __tree
4346
config.__state = configState;

test/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,15 @@ test('config.set(weirdValue)', function t(assert) {
358358

359359
assert.end();
360360
});
361+
362+
test('config.setRemote()', function t(assert) {
363+
var config = fetchConfig(__dirname);
364+
365+
config.setRemote('foo', 'bar');
366+
367+
assert.deepEqual(config.getRemote(), {
368+
'foo': 'bar'
369+
});
370+
371+
assert.end();
372+
});

0 commit comments

Comments
 (0)