wj-config
Version:
Javascript configuration module for NodeJS and browser frameworks such as React that works like ASP.net configuration where data sources are specified (usually JSON files) and environment variables can contribute/overwrite values by following a naming con
37 lines (36 loc) • 1.03 kB
JavaScript
import { isConfigNode } from "../helpers.js";
import { DataSource } from "./DataSource.js";
/**
* Configuration data source class that injects a pre-build JavaScript object into the configuration build chain.
*/
export class ObjectDataSource extends DataSource {
/**
* The object to inject.
*/
_obj;
#validateObject(obj) {
if (!isConfigNode(obj)) {
throw new Error('The provided object is not suitable as configuration data source.');
}
}
/**
* Initializes a new instance of this class.
* @param obj Data object to inject into the configuration build chain.
*/
constructor(obj) {
super('Object');
if (typeof obj !== 'function') {
this.#validateObject(obj);
}
this._obj = obj;
}
async getObject() {
let obj = this._obj;
if (typeof obj === 'function') {
obj = await obj();
}
this.#validateObject(obj);
return Promise.resolve(obj);
}
;
}