Skip to content

Commit

Permalink
correccion 3 - clase crashlytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Morales committed Dec 7, 2023
1 parent 7b04beb commit c17c5c7
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 43 deletions.
88 changes: 52 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,34 @@ npm install @janis-commerce/app-crashlytics
## Functions

<dl>
<dt><a href="#crash">crash()</a> ⇒ <code>void</code></dt>
<dt><a href="#initialize">initialize()</a> ⇒ <code>void</code></dt>
<dd><p>initalize data from userData</p>
</dd>
<dt><a href="#crashThisApp">crashThisApp()</a> ⇒ <code>void</code></dt>
<dd><p>Cause your app to crash for testing purposes</p>
</dd>
<dt><a href="#recordError">recordError(error, jsErrorName, attributes)</a> ⇒ <code>void</code></dt>
<dd><p>Record a JavaScript Error.</p>
</dd>
<dt><a href="#log">log(message, attributes, attributes[attributes)</a> ⇒ <code>void</code></dt>
<dd><p>Log a message that will appear in any subsequent Crash or Non-fatal error reports</p>
</dd>
<dt><a href="#recordError">recordError(error, jsErrorName)</a> ⇒ <code>void</code></dt>
<dd><p>Record a JavaScript Error.</p>
</dd>
</dl>

<a name="initialize"></a>

## initialize() ⇒ <code>void</code>
initalize data from userData

**Kind**: global function
**Example**
```js
import Crashlytics from '@janiscommerce/app-crashlytics'
const crash = new Crashlytics()

// minimum example
crash.initialize()
```
<a name="crashThisApp"></a>

## crashThisApp() ⇒ <code>void</code>
Expand All @@ -148,10 +165,10 @@ const crash = new Crashlytics()
// minimum example
crash.crashThisApp()
```
<a name="log"></a>
<a name="recordError"></a>

## log(message, {attributes}) ⇒ <code>void</code>
Log a message that will appear in any subsequent Crash or Non-fatal error reports
## recordError(error, jsErrorName, attributes) ⇒ <code>void</code>
Record a JavaScript Error.

**Kind**: global function
**Throws**:
Expand All @@ -161,36 +178,27 @@ Log a message that will appear in any subsequent Crash or Non-fatal error report

| Param | Type | Description |
| --- | --- | --- |
| message | <code>string</code> | Context message. |
| attributes | <code>object</code> | Attributes. |
| attributes.userIdAattribute | <code>string</code> | User Id. |
| attributes.error | <code>Error</code> | Attribute to error records. |
| attributes[attributes] | <code>string</code> \| <code>object</code> | Attributes that can be either a string or an object. |
| error | <code>Error</code> | Javascript error |
| jsErrorName | <code>string</code> \| <code>undefined</code> | Error name |
| attributes | <code>object</code> | Attributes that can be either a string or an object. |

**Example**
```js
import Crashlytics from '@janiscommerce/app-crashlytics'
const crash = new Crashlytics()

// minimum example
crash.log('this is a pda error')

// add userId to crashlytics console
crash.log('this is a pda error', {userId: '213213})
// recod an error to crashlytics console
crash.log('this is a pda error', {error: Error})
// add an attribute to crashlytics console
crash.log('this is a pda error', {name: 'Pedro'})
// without params
const error = throw Error('params are required');
crash.recordError(error, 'error-name')

// add attributes to crashlytics console
crash.log('this is a pda error', {info: {name: 'Pedro', email: 'pedro@email.com', age: '38'}})
// with params
const error = throw Error('params are required');
crash.recordError(error, 'error-name', {name: Pepe})
```
<a name="recordError"></a>
<a name="log"></a>

## recordError(error, jsErrorName) ⇒ <code>void</code>
Record a JavaScript Error.
## log(message, attributes, attributes[attributes) ⇒ <code>void</code>
Log a message that will appear in any subsequent Crash or Non-fatal error reports

**Kind**: global function
**Throws**:
Expand All @@ -200,21 +208,29 @@ Record a JavaScript Error.

| Param | Type | Description |
| --- | --- | --- |
| error | <code>Error</code> | Javascript error. |
| jsErrorName | <code>string</code> \| <code>undefined</code> | Error name. |
| message | <code>string</code> | Context message. |
| attributes | <code>object</code> | Attributes. |
| attributes[attributes] | <code>string</code> \| <code>object</code> | Attributes that can be either a string or an object. |
| attributes.userIdAattribute | <code>string</code> | User Id. |
| attributes.error | <code>Error</code> | Attribute to error records. |
| attributes[attributes | <code>string</code> \| <code>object</code> | Attributes that can be either a string or an object. |

**Example**
```js
import Crashlytics from '@janiscommerce/app-crashlytics'
const crash = new Crashlytics()

// minimum example
const error = throw Error('params are required');
recordError(error)
crash.log('this is a pda error')

// with error description
const error = throw Error('params are required');
recordError(error, 'Required params')
// add userId to crashlytics console
crash.log('this is a pda error', {userId: '213213})
// recod an error to crashlytics console
crash.log('this is a pda error', {error: Error})
// add an attribute to crashlytics console
crash.log('this is a pda error', {name: 'Pedro'})
// add attributes to crashlytics console
crash.log('this is a pda error', {info: {name: 'Pedro', email: 'pedro@email.com', age: '38'}})
```
40 changes: 35 additions & 5 deletions lib/crashlytics.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import crashlytics from '@react-native-firebase/crashlytics';
import {getUserInfo} from '@janiscommerce/oauth-native';
import {
isDevEnv,
isEnabled,
setAttribute,
setAttributes,
setError,
formatUserData
} from './utils';

const setLogAttributes = async (attributes, key) => {
Expand All @@ -23,9 +25,33 @@ const setLogAttributes = async (attributes, key) => {
class Crashlytics {
userData = {};

constructor(data) {
if (!!data && Object.keys(data).length) {
this.userData = data;
constructor(){
this.initialize();
}

/**
* @function initialize
* @description initalize data from userData
* @returns {void}
* @example
* import Crashlytics from '@janiscommerce/app-crashlytics'
* const crash = new Crashlytics()
*
* // minimum example
* crash.initialize()
*/
async initialize() {
try {
/* istanbul ignore next */
if(!!Object.keys(this.userData).length) return null;

const userInfo = await getUserInfo();
this.userData = formatUserData(userInfo);
} catch (error) {
/* istanbul ignore next */
if (isDevEnv()) {
console.error(error.message);
}
}
}

Expand Down Expand Up @@ -66,6 +92,8 @@ class Crashlytics {
*/
async recordError(error, jsErrorName, attributes = {}) {
try {
this.initialize()

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


try {
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
27 changes: 27 additions & 0 deletions lib/utils/formatUserData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const formatUserData = (data) => {
if(!data || data && !Object.keys(data).length) return {};

const {
email = '',
sub = '',
tname = '',
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,
}
};

export default formatUserData;
3 changes: 2 additions & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import isDevEnv from './isDevEnv';
import setAttribute from './setAttribute';
import setAttributes from './setAttributes';
import setError from './setError';
import formatUserData from './formatUserData';

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

0 comments on commit c17c5c7

Please sign in to comment.