Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Sep 20, 2021
1 parent 520cffa commit a058f81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 67 deletions.
30 changes: 13 additions & 17 deletions integrations/Sentry/browser.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/* eslint-disable object-shorthand */
/* eslint-disable func-names */
/* eslint-disable class-methods-use-this */
/* eslint-disable no-unused-expressions */

import get from "get-value";
import logger from "../../utils/logUtil";
import {
SentryScriptLoader,
identifierPayloadBuilder,
convertObjectToArray,
} from "./utils";
import { SentryScriptLoader, convertObjectToArray } from "./utils";
import { removeUndefinedAndNullValues } from "../utils/commonUtils";
import { getDefinedTraits, isObject } from "../../utils/utils";

class Sentry {
Expand All @@ -35,7 +33,8 @@ class Sentry {
}
SentryScriptLoader(
"Sentry",
`https://browser.sentry-cdn.com/6.12.0/bundle.min.js`
`https://browser.sentry-cdn.com/6.12.0/bundle.min.js`,
`sha384-S3qfdh3AsT1UN84WIYNuOX9vVOoFg3nB17Jp5/pTFGDBGBt+dtz7MGAV845efkZr`
);

const formattedAllowUrls = convertObjectToArray(this.allowUrls);
Expand Down Expand Up @@ -73,14 +72,14 @@ class Sentry {
}

if (includePaths.length > 0) {
this.integrations.push(
window.Sentry.integrations.push(
new window.Sentry.Integrations.RewriteFrames({
// eslint-disable-next-line object-shorthand
iteratee: function (frame) {
// eslint-disable-next-line consistent-return
includePaths.forEach((i) => {
includePaths.forEach((path) => {
try {
if (frame.filename.match(includePaths[i])) {
if (frame.filename.match(path)) {
// eslint-disable-next-line no-param-reassign
frame.in_app = true;
return frame;
Expand Down Expand Up @@ -127,20 +126,17 @@ class Sentry {
get(rudderElement.message, "traits.ip_address") ||
get(rudderElement.message, "context.traits.ip_address");

const userIdentifierPayload = identifierPayloadBuilder(
userId,
email,
name,
ipAddress
);
const finalPayload = {
...userIdentifierPayload,
id: userId,
email: email,
username: name,
ip_address: ipAddress,
...traits,
};
if (this.logger) {
window.Sentry.setTag("logger", logger);
}
window.Sentry.setUser(finalPayload);
window.Sentry.setUser(removeUndefinedAndNullValues(finalPayload));
}
}
export default Sentry;
57 changes: 7 additions & 50 deletions integrations/Sentry/utils.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
import logger from "../../utils/logUtil";

const convertObjectToArray = (objectInput) => {
const list = [];
objectInput.forEach(function (objectItem) {
const x = objectItem.eventCustomProperties;
list.push(x);
});
return list;
return objectInput.map((objectItem) => objectItem.eventCustomProperties);
};

const SentryRewriteFramesLoader = (id, src) => {
logger.debug(`In rewriteFrame loader === ${id}`);
const js = document.createElement("script");
js.src = src;
js.integrity =
"sha384-WOm9k3kzVt1COFAB/zCXOFx4lDMtJh/2vmEizIwgog7OW0P/dPwl3s8f6MdwrD7q";
js.crossorigin = "anonymous";
js.async = true;
js.type = "text/javascript";
js.id = id;
const e = document.getElementsByTagName("script")[0];
logger.debug("==parent script==", e);
logger.debug("==adding script==", js);
e.parentNode.insertBefore(js, e);
};

const SentryScriptLoader = (id, src) => {
const SentryScriptLoader = (id, src, integrity) => {
logger.debug(`in script loader=== ${id}`);
const js = document.createElement("script");
js.src = src;
js.integrity =
"sha384-S3qfdh3AsT1UN84WIYNuOX9vVOoFg3nB17Jp5/pTFGDBGBt+dtz7MGAV845efkZr";
js.integrity = integrity;
js.crossorigin = "anonymous";
js.async = true;
js.type = "text/javascript";
Expand All @@ -39,32 +17,11 @@ const SentryScriptLoader = (id, src) => {
logger.debug("==parent script==", e);
logger.debug("==adding script==", js);
e.parentNode.insertBefore(js, e);
SentryRewriteFramesLoader(
SentryScriptLoader(
"Sentry",
`https://browser.sentry-cdn.com/6.12.0/rewriteframes.min.js`
`https://browser.sentry-cdn.com/6.12.0/rewriteframes.min.js`,
`sha384-WOm9k3kzVt1COFAB/zCXOFx4lDMtJh/2vmEizIwgog7OW0P/dPwl3s8f6MdwrD7q`
);
};

const identifierPayloadBuilder = (userId, email, name, ipAddress) => {
const userIdentifierPayload = {};
if (userId) {
userIdentifierPayload.id = userId;
}
if (email) {
userIdentifierPayload.email = email;
}
if (name) {
userIdentifierPayload.username = name;
}
if (ipAddress) {
userIdentifierPayload.ip_address = ipAddress;
}
return userIdentifierPayload;
};

export {
SentryScriptLoader,
identifierPayloadBuilder,
SentryRewriteFramesLoader,
convertObjectToArray,
};
export { SentryScriptLoader, convertObjectToArray };

0 comments on commit a058f81

Please sign in to comment.