Skip to content

Commit

Permalink
Add addPerEnvironment()
Browse files Browse the repository at this point in the history
- Also shortened the Quickstart
-Also updated the README.
  • Loading branch information
webJose committed Dec 24, 2022
1 parent c099aa1 commit a4e5207
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ export default class Builder implements IBuilder {
return this.add(new SingleValueDataSource(path, value, hierarchySeparator));
}

addPerEnvironment(addDs: (builder: IBuilder, envName: string) => boolean | string): IBuilder {
if (!this._envSource) {
throw new Error('Using addPerEnvironment() requires a prior call to includeEnvironment().');
}
this._envSource.environment.all.forEach(n => {
const result = addDs(this, n);
if (result !== false) {
this.forEnvironment(n, typeof result === 'string' ? result : undefined);
}
});
return this;
}

name(name: string): IBuilder {
if (!this._lastCallWasDsAdd) {
throw new Error('Names for data sources must be set immediately after adding the data source or setting its conditional.');
Expand Down
11 changes: 11 additions & 0 deletions src/wj-config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@ declare module 'wj-config' {
*/
addSingleValue(path: string, value: ConfigurationValue, hierarchySeparator: string = ':'): IBuilder;

/**
* Special function that allows the developer the opportunity to add one data source per defined environment.
*
* The function iterates through all possible environments and calls the addDs function for each one. It is
* assumed that the addDs function will add zero or one data source. To signal no data source was added,
* addDs must return the boolean value "false".
* @param addDs Function that is meant to add a single data source of any type that is associated to the
* provided environment name.
*/
addPerEnvironment(addDs: (builder: IBuilder, envName: string) => boolean | string): IBuilder;

/**
* Sets the data source name of the last data source added to the builder.
* @param name Name for the data source.
Expand Down

0 comments on commit a4e5207

Please sign in to comment.