@dynatrace/react-native-plugin
Version:
This plugin gives you the ability to use the Dynatrace Mobile agent in your react native application.
116 lines (115 loc) • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isKeyCustomProperty = exports.isNotObject = exports.doesNotExceedKeySize = exports.restrictingValueSize = exports.isKeySyntaxAllowed = exports.getReactNativeVersion = exports.containEventPropertiesInArray = exports.addIsApiReported = exports.flagEventProperties = exports.containEventProperties = exports.containSessionProperties = exports.trimString = exports.isObject = void 0;
const ConsoleLogger_1 = require("../../../core/logging/ConsoleLogger");
const EventSpecContstants_1 = require("../spec/EventSpecContstants");
const logger = new ConsoleLogger_1.ConsoleLogger('EventModifierUtil');
const isObject = (obj) => {
if (obj === null || typeof obj !== 'object') {
return false;
}
return Object.prototype.toString.call(obj) === '[object Object]';
};
exports.isObject = isObject;
const trimString = (key, str, maxLength) => {
const stringToTrim = str.toString();
if (stringToTrim.length <= maxLength) {
return stringToTrim;
}
else {
logger.debug(`trimString(): Limiting value of ${key} as maximum value length (${maxLength}) is reached!`);
return stringToTrim.substring(0, maxLength);
}
};
exports.trimString = trimString;
const containSessionProperties = (jsonData) => {
for (const key in jsonData) {
if (key.startsWith(`${"session_properties"}.`)) {
return true;
}
}
return false;
};
exports.containSessionProperties = containSessionProperties;
const containEventProperties = (jsonData) => {
for (const key in jsonData) {
if (key.startsWith(`${"event_properties"}.`)) {
return true;
}
}
return false;
};
exports.containEventProperties = containEventProperties;
const flagEventProperties = (event) => {
if (event !== null && (0, exports.containEventProperties)(event)) {
event["characteristics.has_event_properties"] =
true;
}
};
exports.flagEventProperties = flagEventProperties;
const addIsApiReported = (event) => {
if (event != null) {
event["characteristics.is_api_reported"] = true;
}
};
exports.addIsApiReported = addIsApiReported;
const containEventPropertiesInArray = (eventEntries) => {
for (const [key, value] of eventEntries) {
if (key.startsWith(`${"event_properties"}.`)) {
return true;
}
}
return false;
};
exports.containEventPropertiesInArray = containEventPropertiesInArray;
const getReactNativeVersion = () => {
const candidates = [
() => require('react-native').ReactNativeVersion,
() => require('react-native/Libraries/Core/ReactNativeVersion').version,
];
for (const get of candidates) {
try {
const v = get();
if (v)
return toVersionString(v);
}
catch (_a) { }
}
logger.debug('Unable to determine React Native version');
return undefined;
};
exports.getReactNativeVersion = getReactNativeVersion;
const isKeySyntaxAllowed = (entry) => {
const [key] = entry;
const rV = EventSpecContstants_1.KEY_NAME_REGEX.test(key);
if (!rV) {
logger.debug(`isKeySyntaxAllowed(): Filtering key ${key} as it doesnt fulfill character rules!`);
}
return rV;
};
exports.isKeySyntaxAllowed = isKeySyntaxAllowed;
const restrictingValueSize = (entry) => {
const [key, value] = entry;
if (typeof value === 'string' &&
value.length > EventSpecContstants_1.MAX_CUSTOM_EVENT_VALUE_LENGTH) {
logger.debug(`restrictingValueSize(): Limiting value of ${key} as maximum value length (${EventSpecContstants_1.MAX_CUSTOM_EVENT_VALUE_LENGTH}) is reached!`);
entry[1] = value.slice(0, EventSpecContstants_1.MAX_CUSTOM_EVENT_VALUE_LENGTH);
}
};
exports.restrictingValueSize = restrictingValueSize;
const doesNotExceedKeySize = ([key]) => {
if (key.length <= EventSpecContstants_1.MAX_CUSTOM_EVENT_KEY_LENGTH) {
return true;
}
else {
logger.debug(`doesNotExceedKeySize(): Dropping key ${key} as maximum key length (${EventSpecContstants_1.MAX_CUSTOM_EVENT_KEY_LENGTH}) is reached!`);
return false;
}
};
exports.doesNotExceedKeySize = doesNotExceedKeySize;
const isNotObject = ([, val]) => typeof val !== 'object' || val === null;
exports.isNotObject = isNotObject;
const isKeyCustomProperty = (key) => key.startsWith("event_properties") ||
key.startsWith("session_properties");
exports.isKeyCustomProperty = isKeyCustomProperty;
const toVersionString = (v) => `${v.major}.${v.minor}.${v.patch}${v.prerelease ? `-${v.prerelease}` : ''}`;