Skip to content

Commit

Permalink
crashlytics core data validation was updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Lopez committed Mar 25, 2024
1 parent 826189a commit d89f9e5
Show file tree
Hide file tree
Showing 12 changed files with 18,603 additions and 13,841 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ module.exports = {
es6: true,
jest: true,
},
extends: ['airbnb', 'prettier'],
extends: ['prettier'],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly',
__DEV__: 'readonly',
fetch: false,
},
parser: 'babel-eslint',
parser: '@babel/eslint-parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
requireConfigFile: false,
sourceType: 'module',
},
plugins: ['react', 'prettier'],
Expand Down
9 changes: 9 additions & 0 deletions lib/constant/coreData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default [
'userName',
'userSurname',
'userEmail',
'userId',
'client',
'language',
'currency',
];
18 changes: 9 additions & 9 deletions lib/crashlytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
setAttribute,
setAttributes,
setError,
formatUserData
formatUserData,
} from './utils';
import {validateCoreData} from './utils/helpers';

const setLogAttributes = async (attributes, key) => {
try {
Expand All @@ -25,11 +26,11 @@ const setLogAttributes = async (attributes, key) => {
class Crashlytics {
userData = {};

constructor(){
constructor() {
this.initialize();
}

/**
/**
* @function initialize
* @description initalize data from userData
* @returns {void}
Expand All @@ -43,8 +44,8 @@ class Crashlytics {
async initialize() {
try {
/* istanbul ignore next */
if (!!this.userData && !!Object.keys(this.userData).length) return null;
if (validateCoreData(this.userData)) return null;

const userInfo = await getUserInfo();
this.userData = formatUserData(userInfo);
} catch (error) {
Expand Down Expand Up @@ -92,7 +93,7 @@ class Crashlytics {
*/
async recordError(error, jsErrorName, attributes = {}) {
try {
this.initialize()
this.initialize();

/* istanbul ignore next */
if (!isEnabled) throw new Error('crashlytics is not enabled');
Expand Down Expand Up @@ -142,11 +143,10 @@ class Crashlytics {
* crash.log('this is a pda error', {info: {name: 'Pedro', email: 'pedro@email.com', age: '38'}})
*/
log(message, attributes = {}) {

try {
this.initialize()
this.initialize();
const attributesData = {...attributes, ...this.userData};

/* istanbul ignore next */
if (!isEnabled) throw new Error('crashlytics is not enabled');
if (!message) throw new Error('message is required');
Expand Down
46 changes: 23 additions & 23 deletions lib/utils/formatUserData.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const formatUserData = (data) => {
if(!data || data && !Object.keys(data).length) return {};
const formatUserData = data => {
if (!data || (data && !Object.keys(data).length)) return {};

const {
email = '',
sub = '',
tname = '',
locale = '',
isDev = '',
tcurrency = '',
given_name = '',
family_name = ''
} = data;
const {
email = '',
sub = '',
tcode = '',
locale = '',
isDev = '',
tcurrency = '',
given_name = '',
family_name = '',
} = data;

return {
userName: given_name,
userSurname: family_name,
userEmail: email,
userId: sub,
client: tname,
language: locale,
currency: tcurrency,
isDev,
}
return {
userName: given_name,
userSurname: family_name,
userEmail: email,
userId: sub,
client: tcode,
language: locale,
currency: tcurrency,
isDev,
};
};

export default formatUserData;
export default formatUserData;
58 changes: 58 additions & 0 deletions lib/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import coreData from '../constant/coreData.js';

/**
* @name valueIsValidType
* @description is a utility that returns a boolean true value when the argument is a string or a number, otherwise returns false
* @param {string | number} value is data to validate
* @returns {boolean}
* @example
*
* valueIsValidType('string') => true;
* valueIsValidType(3) => true;
* valueIsValidType(0) => true;
* valueIsValidType('') => false;
* valueIsValidType(null) => false;
*/

export const valueIsValidType = value =>
typeof value === 'number' || (!!value && typeof value === 'string');

/**
* @name includesAllProperties
* @param {Object} data is an object with keys that you want to validate.
* @param {String[]} properties are the keys that you want validate, in addition to those required by default
* @returns {boolean}
* @example
*
* includesAllProperties({name:'janis'}, ['name','address']) => false;
* includesAllProperties({name:'janis',address:'costa rica 4988'}, ['name','address']) => true;
* includesAllProperties({name:'', address: 'costa rica 4988},['name','address']) => false;
*/

export const includesAllProperties = (data, properties = []) => {
if (!data || !Object.keys(data).length) return false;

if (!properties || !Array.isArray(properties)) return true;

const validProperties = properties.filter(prop => typeof prop === 'string');

const dataKeys = Object.keys(data);

return validProperties.every(
value => dataKeys.includes(value) && valueIsValidType(data[value]),
);
};

/**
* @name validateCoreData
* @description This utility validates an object and returns true when all required core data keys are found in it, otherwise, returns false
* @param {*} data object to be validated
* @returns {boolean}
* @example
* validateCoreData({userName:'janis',userSurname:'fizzmod',userEmail:'janis@janis.im',userId:'janis123',client:'janis',language:'en-us,'currency':'currency'}) => true;
* validateCoreData({userName:'',userSurname:'',userEmail:'janis@janis.im',userId:'janis123',client:'janis',language:'en-us,'currency':'currency'}) => false;
* validateCoreData({}) => false;
*/

export const validateCoreData = (data = {}) =>
includesAllProperties(data, coreData);
9 changes: 8 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ import setAttributes from './setAttributes';
import setError from './setError';
import formatUserData from './formatUserData';

export {isEnabled, isDevEnv, setError, setAttribute, setAttributes, formatUserData};
export {
isEnabled,
isDevEnv,
setError,
setAttribute,
setAttributes,
formatUserData,
};
Loading

0 comments on commit d89f9e5

Please sign in to comment.