Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/core/classes/Iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ import { IterableLogger } from './IterableLogger';

const RNEventEmitter = new NativeEventEmitter(RNIterableAPI);

/**
* Checks if the response is an IterableAuthResponse
*/
const isIterableAuthResponse = (
response: IterableAuthResponse | string | undefined | null
): response is IterableAuthResponse => {
if (typeof response === 'string') return false;
if (
response?.authToken ||
response?.successCallback ||
response?.failureCallback
) {
return true;
}
return false;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with high complexity (count = 5): isIterableAuthResponse [qlty:function-complexity]

};

/* eslint-disable tsdoc/syntax */
/**
* The main class for the Iterable React Native SDK.
Expand Down Expand Up @@ -952,33 +969,31 @@ export class Iterable {
// Promise result can be either just String OR of type AuthResponse.
// If type AuthReponse, authToken will be parsed looking for `authToken` within promised object. Two additional listeners will be registered for success and failure callbacks sent by native bridge layer.
// Else it will be looked for as a String.
if (typeof promiseResult === typeof new IterableAuthResponse()) {
Iterable.authManager.passAlongAuthToken(
(promiseResult as IterableAuthResponse).authToken
);
if (isIterableAuthResponse(promiseResult)) {
Iterable.authManager.passAlongAuthToken(promiseResult.authToken);

setTimeout(() => {
if (
authResponseCallback === IterableAuthResponseResult.SUCCESS
) {
if ((promiseResult as IterableAuthResponse).successCallback) {
(promiseResult as IterableAuthResponse).successCallback?.();
if (promiseResult.successCallback) {
promiseResult.successCallback?.();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deeply nested control flow (level = 4) [qlty:nested-control-flow]

}
} else if (
authResponseCallback === IterableAuthResponseResult.FAILURE
) {
// We are currently only reporting JWT related errors. In
// the future, we should handle other types of errors as well.
if ((promiseResult as IterableAuthResponse).failureCallback) {
(promiseResult as IterableAuthResponse).failureCallback?.();
if (promiseResult.failureCallback) {
promiseResult.failureCallback?.();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deeply nested control flow (level = 4) [qlty:nested-control-flow]

}
} else {
IterableLogger?.log('No callback received from native layer');
}
}, 1000);
} else if (typeof promiseResult === 'string') {
//If promise only returns string
Iterable.authManager.passAlongAuthToken(promiseResult as string);
Iterable.authManager.passAlongAuthToken(promiseResult);
} else {
IterableLogger?.log(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also consider calling failure callback here with appropriate message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeahhh I agree.. just don't want to mix concerns in the PR

'Unexpected promise returned. Auth token expects promise of String or AuthResponse type.'
Expand Down
Loading