@bitblit/ratchet-aws
Version:
Common tools for use with AWS browser and node
52 lines • 2.31 kB
JavaScript
import { RequireRatchet } from '@bitblit/ratchet-common/lang/require-ratchet';
import { ErrorRatchet } from '@bitblit/ratchet-common/lang/error-ratchet';
import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet';
export class GlobalVariableOverrideRuntimeParameterProvider {
wrapped;
options = {
globalTTL: 1,
separator: '.',
prefix: 'RuntimeEnv-',
suffix: '',
};
constructor(wrapped, opts) {
this.wrapped = wrapped;
RequireRatchet.notNullOrUndefined(this.wrapped, 'wrapped');
RequireRatchet.notNullOrUndefined(global?.process?.env, '"process" not found - this only runs in Node, not the browser');
if (opts) {
this.options = opts;
}
RequireRatchet.notNullOrUndefined(this.options.globalTTL, 'this.options.globalTTL');
RequireRatchet.notNullOrUndefined(this.options.separator, 'this.options.separator');
RequireRatchet.true(this.options.globalTTL > 0, 'this.options.globalTTL must be larger than 0');
}
generateName(groupId, paramKey) {
return (StringRatchet.trimToEmpty(this.options.prefix) +
groupId +
StringRatchet.trimToEmpty(this.options.separator) +
paramKey +
StringRatchet.trimToEmpty(this.options.suffix));
}
async readParameter(groupId, paramKey) {
const asString = StringRatchet.trimToNull(process.env[this.generateName(groupId, paramKey)]);
if (asString && !StringRatchet.canParseAsJson(asString)) {
ErrorRatchet.throwFormattedErr('Cannot parse ENV override (%s / %s) as JSON - did you forget the quotes on a string?', groupId, paramKey);
}
const rval = asString
? {
groupId: groupId,
paramKey: paramKey,
paramValue: asString,
ttlSeconds: this.options.globalTTL,
}
: await this.wrapped.readParameter(groupId, paramKey);
return rval;
}
async readAllParametersForGroup(groupId) {
return this.wrapped.readAllParametersForGroup(groupId);
}
async writeParameter(toStore) {
return this.wrapped.writeParameter(toStore);
}
}
//# sourceMappingURL=global-variable-override-runtime-parameter-provider.js.map