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
63 lines (62 loc) • 2.53 kB
JavaScript
import { DictionaryDataSource } from "../dataSources/DictionaryDataSource.js";
import { EnvironmentDataSource } from "../dataSources/EnvironmentDataSource.js";
import { FetchedDataSource } from "../dataSources/FetchedDataSource.js";
import { JsonDataSource } from "../dataSources/JsonDataSource.js";
import { ObjectDataSource } from "../dataSources/ObjectDataSource.js";
import { SingleValueDataSource } from "../dataSources/SingleValueDataSource.js";
import { BuilderImpl } from "./BuilderImpl.js";
import { EnvAwareBuilder } from "./EnvAwareBuilder.js";
export class Builder {
#impl = new BuilderImpl();
add(dataSource) {
this.#impl.add(dataSource);
return this;
}
addObject(obj) {
return this.add(new ObjectDataSource(obj));
}
addDictionary(dictionary, hierarchySeparator, prefixOrPredicate) {
return this.add(new DictionaryDataSource(dictionary, hierarchySeparator ?? ':', prefixOrPredicate));
}
addEnvironment(env, prefix = 'OPT_') {
// @ts-expect-error InflateDictionary's resulting type, for some reason, always asserts true against "unknown". TS bug?
return this.add(new EnvironmentDataSource(env, prefix));
}
addFetched(input, required = true, init, procesFn) {
return this.add(new FetchedDataSource(input, required, init, procesFn));
}
addJson(json, jsonParser, reviver) {
return this.add(new JsonDataSource(json, jsonParser, reviver));
}
addSingleValue(path, valueOrHierarchySeparator, hierarchySeparator) {
return this.add(new SingleValueDataSource(path, valueOrHierarchySeparator, typeof path === 'function' ? valueOrHierarchySeparator : hierarchySeparator));
}
postMerge(fn) {
this.#impl.postMerge(fn);
return this;
}
name(name) {
this.#impl.name(name);
return this;
}
when(predicate, dataSourceName) {
this.#impl.when(predicate, dataSourceName);
return this;
}
includeEnvironment(env, propertyName = 'environment') {
this.#impl._lastCallWasDsAdd = false;
const envSource = {
name: propertyName,
environment: env
};
return new EnvAwareBuilder(envSource, this.#impl);
}
createUrlFunctions(wsPropertyNames, routeValuesRegExp) {
this.#impl.createUrlFunctions(wsPropertyNames, routeValuesRegExp);
return this;
}
build(traceValueSources = false) {
return this.#impl.build(traceValueSources, p => p());
}
}
;