configcat-common
Version:
ConfigCat is a configuration as a service that lets you manage your features and configurations without actually deploying new code.
225 lines (224 loc) • 19.3 kB
JavaScript
import { __makeTemplateObject } from "tslib";
import { errorToString } from "./Utils";
/**
* Specifies event severity levels for the `IConfigCatLogger` interface.
* The levels are interpreted as minimum levels in the case of event filtering.
*/
export var LogLevel;
(function (LogLevel) {
/** All events are logged. */
LogLevel[LogLevel["Debug"] = 4] = "Debug";
/** Info, Warn and Error are logged. Debug events are discarded. */
LogLevel[LogLevel["Info"] = 3] = "Info";
/** Warn and Error events are logged. Info and Debug events are discarded. */
LogLevel[LogLevel["Warn"] = 2] = "Warn";
/** Error events are logged. All other events are discarded. */
LogLevel[LogLevel["Error"] = 1] = "Error";
/** No events are logged. */
LogLevel[LogLevel["Off"] = -1] = "Off";
})(LogLevel || (LogLevel = {}));
/** Represents a log message format with named arguments. */
var FormattableLogMessage = /** @class */ (function () {
function FormattableLogMessage(strings, argNames, argValues) {
this.strings = strings;
this.argNames = argNames;
this.argValues = argValues;
}
FormattableLogMessage.from = function () {
var argNames = [];
for (var _i = 0; _i < arguments.length; _i++) {
argNames[_i] = arguments[_i];
}
return function (strings) {
var argValues = [];
for (var _i = 1; _i < arguments.length; _i++) {
argValues[_i - 1] = arguments[_i];
}
return new FormattableLogMessage(strings, argNames, argValues);
};
};
Object.defineProperty(FormattableLogMessage.prototype, "defaultFormattedMessage", {
get: function () {
var cachedMessage = this.cachedDefaultFormattedMessage;
if (cachedMessage === void 0) {
// This logic should give exactly the same result as string interpolation.
cachedMessage = "";
var _a = this, strings = _a.strings, argValues = _a.argValues;
var i = 0;
for (; i < strings.length - 1; i++) {
cachedMessage += strings[i];
cachedMessage += argValues[i];
}
cachedMessage += strings[i];
this.cachedDefaultFormattedMessage = cachedMessage;
}
return cachedMessage;
},
enumerable: false,
configurable: true
});
FormattableLogMessage.prototype.toString = function () { return this.defaultFormattedMessage; };
return FormattableLogMessage;
}());
export { FormattableLogMessage };
var LoggerWrapper = /** @class */ (function () {
function LoggerWrapper(logger, hooks) {
this.logger = logger;
this.hooks = hooks;
}
Object.defineProperty(LoggerWrapper.prototype, "level", {
get: function () {
var _a;
return (_a = this.logger.level) !== null && _a !== void 0 ? _a : LogLevel.Warn;
},
enumerable: false,
configurable: true
});
Object.defineProperty(LoggerWrapper.prototype, "eol", {
get: function () {
var _a;
return (_a = this.logger.eol) !== null && _a !== void 0 ? _a : "\n";
},
enumerable: false,
configurable: true
});
LoggerWrapper.prototype.isEnabled = function (logLevel) {
return this.level >= logLevel;
};
/** @inheritdoc */
LoggerWrapper.prototype.log = function (level, eventId, message, exception) {
var _a;
if (this.isEnabled(level)) {
this.logger.log(level, eventId, message, exception);
}
if (level === LogLevel.Error) {
(_a = this.hooks) === null || _a === void 0 ? void 0 : _a.emit("clientError", message.toString(), exception);
}
return message;
};
/**
* Shorthand method for `logger.logEvent(LogLevel.Debug, 0, message);`
*/
LoggerWrapper.prototype.debug = function (message) {
this.log(LogLevel.Debug, 0, message);
};
/* Common error messages (1000-1999) */
LoggerWrapper.prototype.configJsonIsNotPresent = function (defaultReturnValue) {
return this.log(LogLevel.Error, 1000, FormattableLogMessage.from("DEFAULT_RETURN_VALUE")(templateObject_1 || (templateObject_1 = __makeTemplateObject(["Config JSON is not present. Returning ", "."], ["Config JSON is not present. Returning ", "."])), defaultReturnValue));
};
LoggerWrapper.prototype.configJsonIsNotPresentSingle = function (key, defaultParamName, defaultParamValue) {
return this.log(LogLevel.Error, 1000, FormattableLogMessage.from("KEY", "DEFAULT_PARAM_NAME", "DEFAULT_PARAM_VALUE")(templateObject_2 || (templateObject_2 = __makeTemplateObject(["Config JSON is not present when evaluating setting '", "'. Returning the `", "` parameter that you specified in your application: '", "'."], ["Config JSON is not present when evaluating setting '", "'. Returning the \\`", "\\` parameter that you specified in your application: '", "'."])), key, defaultParamName, defaultParamValue));
};
LoggerWrapper.prototype.settingEvaluationFailedDueToMissingKey = function (key, defaultParamName, defaultParamValue, availableKeys) {
return this.log(LogLevel.Error, 1001, FormattableLogMessage.from("KEY", "DEFAULT_PARAM_NAME", "DEFAULT_PARAM_VALUE", "AVAILABLE_KEYS")(templateObject_3 || (templateObject_3 = __makeTemplateObject(["Failed to evaluate setting '", "' (the key was not found in config JSON). Returning the `", "` parameter that you specified in your application: '", "'. Available keys: [", "]."], ["Failed to evaluate setting '", "' (the key was not found in config JSON). Returning the \\`", "\\` parameter that you specified in your application: '", "'. Available keys: [", "]."])), key, defaultParamName, defaultParamValue, availableKeys));
};
LoggerWrapper.prototype.settingEvaluationError = function (methodName, defaultReturnValue, ex) {
return this.log(LogLevel.Error, 1002, FormattableLogMessage.from("METHOD_NAME", "DEFAULT_RETURN_VALUE")(templateObject_4 || (templateObject_4 = __makeTemplateObject(["Error occurred in the `", "` method. Returning ", "."], ["Error occurred in the \\`", "\\` method. Returning ", "."])), methodName, defaultReturnValue), ex);
};
LoggerWrapper.prototype.settingEvaluationErrorSingle = function (methodName, key, defaultParamName, defaultParamValue, ex) {
return this.log(LogLevel.Error, 1002, FormattableLogMessage.from("METHOD_NAME", "KEY", "DEFAULT_PARAM_NAME", "DEFAULT_PARAM_VALUE")(templateObject_5 || (templateObject_5 = __makeTemplateObject(["Error occurred in the `", "` method while evaluating setting '", "'. Returning the `", "` parameter that you specified in your application: '", "'."], ["Error occurred in the \\`", "\\` method while evaluating setting '", "'. Returning the \\`", "\\` parameter that you specified in your application: '", "'."])), methodName, key, defaultParamName, defaultParamValue), ex);
};
LoggerWrapper.prototype.forceRefreshError = function (methodName, ex) {
return this.log(LogLevel.Error, 1003, FormattableLogMessage.from("METHOD_NAME")(templateObject_6 || (templateObject_6 = __makeTemplateObject(["Error occurred in the `", "` method."], ["Error occurred in the \\`", "\\` method."])), methodName), ex);
};
LoggerWrapper.prototype.fetchFailedDueToInvalidSdkKey = function () {
return this.log(LogLevel.Error, 1100, "Your SDK Key seems to be wrong. You can find the valid SDK Key at https://app.configcat.com/sdkkey");
};
LoggerWrapper.prototype.fetchFailedDueToUnexpectedHttpResponse = function (statusCode, reasonPhrase) {
return this.log(LogLevel.Error, 1101, FormattableLogMessage.from("STATUS_CODE", "REASON_PHRASE")(templateObject_7 || (templateObject_7 = __makeTemplateObject(["Unexpected HTTP response was received while trying to fetch config JSON: ", " ", ""], ["Unexpected HTTP response was received while trying to fetch config JSON: ", " ", ""])), statusCode, reasonPhrase));
};
LoggerWrapper.prototype.fetchFailedDueToRequestTimeout = function (timeoutMs, ex) {
return this.log(LogLevel.Error, 1102, FormattableLogMessage.from("TIMEOUT")(templateObject_8 || (templateObject_8 = __makeTemplateObject(["Request timed out while trying to fetch config JSON. Timeout value: ", "ms"], ["Request timed out while trying to fetch config JSON. Timeout value: ", "ms"])), timeoutMs), ex);
};
LoggerWrapper.prototype.fetchFailedDueToUnexpectedError = function (ex) {
return this.log(LogLevel.Error, 1103, "Unexpected error occurred while trying to fetch config JSON. It is most likely due to a local network issue. Please make sure your application can reach the ConfigCat CDN servers (or your proxy server) over HTTP.", ex);
};
LoggerWrapper.prototype.fetchFailedDueToRedirectLoop = function () {
return this.log(LogLevel.Error, 1104, "Redirection loop encountered while trying to fetch config JSON. Please contact us at https://configcat.com/support/");
};
LoggerWrapper.prototype.fetchReceived200WithInvalidBody = function (ex) {
return this.log(LogLevel.Error, 1105, "Fetching config JSON was successful but the HTTP response content was invalid.", ex);
};
LoggerWrapper.prototype.fetchReceived304WhenLocalCacheIsEmpty = function (statusCode, reasonPhrase) {
return this.log(LogLevel.Error, 1106, FormattableLogMessage.from("STATUS_CODE", "REASON_PHRASE")(templateObject_9 || (templateObject_9 = __makeTemplateObject(["Unexpected HTTP response was received when no config JSON is cached locally: ", " ", ""], ["Unexpected HTTP response was received when no config JSON is cached locally: ", " ", ""])), statusCode, reasonPhrase));
};
LoggerWrapper.prototype.autoPollConfigServiceErrorDuringPolling = function (ex) {
return this.log(LogLevel.Error, 1200, "Error occurred during auto polling.", ex);
};
/* SDK-specific error messages (2000-2999) */
LoggerWrapper.prototype.settingForVariationIdIsNotPresent = function (variationId) {
return this.log(LogLevel.Error, 2011, FormattableLogMessage.from("VARIATION_ID")(templateObject_10 || (templateObject_10 = __makeTemplateObject(["Could not find the setting for the specified variation ID: '", "'."], ["Could not find the setting for the specified variation ID: '", "'."])), variationId));
};
LoggerWrapper.prototype.configServiceCacheReadError = function (ex) {
return this.log(LogLevel.Error, 2200, "Error occurred while reading the cache.", ex);
};
LoggerWrapper.prototype.configServiceCacheWriteError = function (ex) {
return this.log(LogLevel.Error, 2201, "Error occurred while writing the cache.", ex);
};
/* Common warning messages (3000-3999) */
LoggerWrapper.prototype.clientIsAlreadyCreated = function (sdkKey) {
return this.log(LogLevel.Warn, 3000, FormattableLogMessage.from("SDK_KEY")(templateObject_11 || (templateObject_11 = __makeTemplateObject(["There is an existing client instance for the specified SDK Key. No new client instance will be created and the specified options are ignored. Returning the existing client instance. SDK Key: '", "'."], ["There is an existing client instance for the specified SDK Key. No new client instance will be created and the specified options are ignored. Returning the existing client instance. SDK Key: '", "'."])), sdkKey));
};
LoggerWrapper.prototype.userObjectIsMissing = function (key) {
return this.log(LogLevel.Warn, 3001, FormattableLogMessage.from("KEY")(templateObject_12 || (templateObject_12 = __makeTemplateObject(["Cannot evaluate targeting rules and % options for setting '", "' (User Object is missing). You should pass a User Object to the evaluation methods like `getValueAsync()` in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/"], ["Cannot evaluate targeting rules and % options for setting '", "' (User Object is missing). You should pass a User Object to the evaluation methods like \\`getValueAsync()\\` in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/"])), key));
};
LoggerWrapper.prototype.dataGovernanceIsOutOfSync = function () {
return this.log(LogLevel.Warn, 3002, "The `dataGovernance` parameter specified at the client initialization is not in sync with the preferences on the ConfigCat Dashboard. Read more: https://configcat.com/docs/advanced/data-governance/");
};
LoggerWrapper.prototype.userObjectAttributeIsMissingPercentage = function (key, attributeName) {
return this.log(LogLevel.Warn, 3003, FormattableLogMessage.from("KEY", "ATTRIBUTE_NAME", "ATTRIBUTE_NAME")(templateObject_13 || (templateObject_13 = __makeTemplateObject(["Cannot evaluate % options for setting '", "' (the User.", " attribute is missing). You should set the User.", " attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/"], ["Cannot evaluate % options for setting '", "' (the User.", " attribute is missing). You should set the User.", " attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/"])), key, attributeName, attributeName));
};
LoggerWrapper.prototype.userObjectAttributeIsMissingCondition = function (condition, key, attributeName) {
return this.log(LogLevel.Warn, 3003, FormattableLogMessage.from("CONDITION", "KEY", "ATTRIBUTE_NAME", "ATTRIBUTE_NAME")(templateObject_14 || (templateObject_14 = __makeTemplateObject(["Cannot evaluate condition (", ") for setting '", "' (the User.", " attribute is missing). You should set the User.", " attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/"], ["Cannot evaluate condition (", ") for setting '", "' (the User.", " attribute is missing). You should set the User.", " attribute in order to make targeting work properly. Read more: https://configcat.com/docs/advanced/user-object/"])), condition, key, attributeName, attributeName));
};
LoggerWrapper.prototype.userObjectAttributeIsInvalid = function (condition, key, reason, attributeName) {
return this.log(LogLevel.Warn, 3004, FormattableLogMessage.from("CONDITION", "KEY", "REASON", "ATTRIBUTE_NAME")(templateObject_15 || (templateObject_15 = __makeTemplateObject(["Cannot evaluate condition (", ") for setting '", "' (", "). Please check the User.", " attribute and make sure that its value corresponds to the comparison operator."], ["Cannot evaluate condition (", ") for setting '", "' (", "). Please check the User.", " attribute and make sure that its value corresponds to the comparison operator."])), condition, key, reason, attributeName));
};
LoggerWrapper.prototype.userObjectAttributeIsAutoConverted = function (condition, key, attributeName, attributeValue) {
return this.log(LogLevel.Warn, 3005, FormattableLogMessage.from("CONDITION", "KEY", "ATTRIBUTE_NAME", "ATTRIBUTE_VALUE")(templateObject_16 || (templateObject_16 = __makeTemplateObject(["Evaluation of condition (", ") for setting '", "' may not produce the expected result (the User.", " attribute is not a string value, thus it was automatically converted to the string value '", "'). Please make sure that using a non-string value was intended."], ["Evaluation of condition (", ") for setting '", "' may not produce the expected result (the User.", " attribute is not a string value, thus it was automatically converted to the string value '", "'). Please make sure that using a non-string value was intended."])), condition, key, attributeName, attributeValue));
};
LoggerWrapper.prototype.configServiceCannotInitiateHttpCalls = function () {
return this.log(LogLevel.Warn, 3200, "Client is in offline mode, it cannot initiate HTTP calls.");
};
LoggerWrapper.prototype.configServiceMethodHasNoEffectDueToDisposedClient = function (methodName) {
return this.log(LogLevel.Warn, 3201, FormattableLogMessage.from("METHOD_NAME")(templateObject_17 || (templateObject_17 = __makeTemplateObject(["The client object is already disposed, thus `", "()` has no effect."], ["The client object is already disposed, thus \\`", "()\\` has no effect."])), methodName));
};
LoggerWrapper.prototype.configServiceMethodHasNoEffectDueToOverrideBehavior = function (overrideBehavior, methodName) {
return this.log(LogLevel.Warn, 3202, FormattableLogMessage.from("OVERRIDE_BEHAVIOR", "METHOD_NAME")(templateObject_18 || (templateObject_18 = __makeTemplateObject(["Client is configured to use the `", "` override behavior, thus `", "()` has no effect."], ["Client is configured to use the \\`", "\\` override behavior, thus \\`", "()\\` has no effect."])), overrideBehavior, methodName));
};
/* SDK-specific warning messages (4000-4999) */
/* Common info messages (5000-5999) */
LoggerWrapper.prototype.settingEvaluated = function (evaluateLog) {
return this.log(LogLevel.Info, 5000, FormattableLogMessage.from("EVALUATE_LOG")(templateObject_19 || (templateObject_19 = __makeTemplateObject(["", ""], ["", ""])), evaluateLog));
};
LoggerWrapper.prototype.configServiceStatusChanged = function (status) {
return this.log(LogLevel.Info, 5200, FormattableLogMessage.from("MODE")(templateObject_20 || (templateObject_20 = __makeTemplateObject(["Switched to ", " mode."], ["Switched to ", " mode."])), status.toUpperCase()));
};
return LoggerWrapper;
}());
export { LoggerWrapper };
var ConfigCatConsoleLogger = /** @class */ (function () {
/**
* Create an instance of ConfigCatConsoleLogger
*/
function ConfigCatConsoleLogger(level, eol) {
if (level === void 0) { level = LogLevel.Warn; }
if (eol === void 0) { eol = "\n"; }
this.level = level;
this.eol = eol;
this.SOURCE = "ConfigCat";
}
/** @inheritdoc */
ConfigCatConsoleLogger.prototype.log = function (level, eventId, message, exception) {
var _a = level === LogLevel.Debug ? [console.info, "DEBUG"] :
level === LogLevel.Info ? [console.info, "INFO"] :
level === LogLevel.Warn ? [console.warn, "WARN"] :
level === LogLevel.Error ? [console.error, "ERROR"] :
[console.log, LogLevel[level].toUpperCase()], logMethod = _a[0], levelString = _a[1];
var exceptionString = exception !== void 0 ? this.eol + errorToString(exception, true) : "";
logMethod(this.SOURCE + " - " + levelString + " - [" + eventId + "] " + message + exceptionString);
};
return ConfigCatConsoleLogger;
}());
export { ConfigCatConsoleLogger };
var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6, templateObject_7, templateObject_8, templateObject_9, templateObject_10, templateObject_11, templateObject_12, templateObject_13, templateObject_14, templateObject_15, templateObject_16, templateObject_17, templateObject_18, templateObject_19, templateObject_20;