Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript support #104

Merged
merged 3 commits into from
May 7, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: update methods comments to comply with JSDoc standards
This is a mandatory steps to add support for TypeScript without migrating the entire library.

More details: https://jsdoc.app.
  • Loading branch information
CharlesMangwa committed Apr 19, 2024
commit 364289f31a869fede78bbc347d0d17d0a2472305
267 changes: 139 additions & 128 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,148 @@
import { NativeModules, NativeEventEmitter } from 'react-native';
import { NativeModules, NativeEventEmitter } from "react-native";

const { RNEmarsysWrapper } = NativeModules;

import Push from './src/push';
import InApp, { InlineInAppView } from './src/inapp';
import Inbox from './src/inbox';
import Predict from './src/predict';
import Geofence from './src/geofence';
import Push from "./src/push";
import Inbox from "./src/inbox";
import Predict from "./src/predict";
import Geofence from "./src/geofence";
import InApp, { InlineInAppView } from "./src/inapp";

const Emarsys = {

/* Init ***************************************************************************************************************************************/

/**
* @desc After application setup is finished, you can use setContact method to identify the user with a contactFieldValue.
* Without setContact all events will be tracked as anonymous usage.
* @param required string contactFieldValue - user identification
* @param required integer contactFieldId - field used for identification
* @return bool - success or failure
*/
setContact(contactFieldId, contactFieldValue) {
return RNEmarsysWrapper.setContact(contactFieldId, contactFieldValue)
},

/**
* @desc When the user signs out, we should use the clearContact method.
* The method is going to automatically log in an anonymous user instead of the one leaving.
* @return bool - success or failure
*/
clearContact() {
return RNEmarsysWrapper.clearContact()
},

/**
* @desc If you want to track custom events, the trackCustomEvent method should be used.
* @param required string eventName - Name of tracked custom event.
* @param object eventAttributes - Attributes of tracked custom event.
* @return bool - success or failure
*/
trackCustomEvent(eventName, eventAttributes) {
return RNEmarsysWrapper.trackCustomEvent(eventName, eventAttributes ? eventAttributes : null)
},

/**
* @desc Register an event handler to react to events triggered by Emarsys.
* @param function (eventName, payload) callback function receiving events
* @return bool - success or failure
*/
setEventHandler(callback) {
const eventEmitter = new NativeEventEmitter(RNEmarsysWrapper);
eventEmitter.addListener('handleEvent', function (result) {
callback(result.eventName, result.payload);
});
RNEmarsysWrapper.setEventHandler();
},

/* DeepLink ***********************************************************************************************************************************/

/**
* @desc The Emarsys SDK automatically tracks email link clicks that open the application directly in most use cases, with only one exception: manual tracking is needed.
* @param string url - Track URL
* @return bool - success or failure
*/
trackDeepLink(url) {
return RNEmarsysWrapper.trackDeepLink(url)
},

/* Config ***********************************************************************************************************************************/

/**
* @desc Emarsys SDK provides a solution for applicationCode change in a convenient way without restarting the SDK.
* @param string applicationCode - applicationCode for change
* @return bool - success or failure
*/
changeApplicationCode(applicationCode) {
return RNEmarsysWrapper.changeApplicationCode(applicationCode)
},

/**
* @desc Emarsys SDK provides a solution for merchantId change in a convenient way without restarting the SDK.
* @param string merchantId - merchantId for change
* @return bool - success or failure
*/
changeMerchantId(merchantId) {
return RNEmarsysWrapper.changeMerchantId(merchantId)
},

/**
* @desc Provides what is the actual applicationCode set in the SDK.
* @return string - applicationCode
*/
getApplicationCode() {
return RNEmarsysWrapper.getApplicationCode()
},

/**
* @desc Provides what is the actual merchantId set in the SDK.
* @return string - merchantId
*/
getMerchantId() {
return RNEmarsysWrapper.getMerchantId()
},

/**
* @desc Provides what is the actual contactFieldId set in the SDK.
* @return integer - contactFieldId
*/
getContactFieldId() {
return RNEmarsysWrapper.getContactFieldId()
},

getHardwareId() {
return RNEmarsysWrapper.getHardwareId()
},

getLanguageCode() {
return RNEmarsysWrapper.getLanguageCode()
},

getSdkVersion() {
return RNEmarsysWrapper.getSdkVersion()
},

push: Push,
inApp: InApp,
InlineInAppView,
inbox: Inbox,
predict: Predict,
geofence: Geofence,

/* Init ***************************************************************************************************************************************/

/**
* After application setup is finished, you can use `setContact()` method to identify the user with a `contactFieldValue`.
* Without `setContact()` all events will be tracked as anonymous usage.
* @param {number} contactFieldId - Field used for identification.
* @param {string} contactFieldValue - User identification.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
setContact(contactFieldId, contactFieldValue) {
return RNEmarsysWrapper.setContact(contactFieldId, contactFieldValue);
},

/**
* When the user signs out, we should use the `clearContact()` method.
* The method is going to automatically log in an anonymous user instead of the one leaving.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
clearContact() {
return RNEmarsysWrapper.clearContact();
},

/**
* If you want to track custom events, the `trackCustomEvent()` method should be used.
* @param {string} eventName - Name of the tracked custom event.
* @param {Record<string, unknown> | undefined} eventAttributes - Object containing the attributes of the tracked custom event.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
trackCustomEvent(eventName, eventAttributes = null) {
return RNEmarsysWrapper.trackCustomEvent(eventName, eventAttributes);
},

/**
* Register an event handler to react to events triggered by Emarsys.
* @param {(eventName: string, payload: unknown) => void} callback - Function receiving `eventName` & `payload` for
* each event.
* @returns {void}
*/
setEventHandler(callback) {
const eventEmitter = new NativeEventEmitter(RNEmarsysWrapper);
eventEmitter.addListener("handleEvent", function (result) {
callback(result.eventName, result.payload);
});
RNEmarsysWrapper.setEventHandler();
},

/* DeepLink ***********************************************************************************************************************************/

/**
* The Emarsys SDK automatically tracks email link clicks that open the application directly in most use cases, with only one exception: manual tracking is needed.
* @param {string} url - URL to track.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
trackDeepLink(url) {
return RNEmarsysWrapper.trackDeepLink(url);
},

/* Config ***********************************************************************************************************************************/

/**
* Emarsys SDK provides a solution for `applicationCode` change in a convenient way without restarting the SDK.
* @param {string} applicationCode - New `applicationCode` string to change for.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
changeApplicationCode(applicationCode) {
return RNEmarsysWrapper.changeApplicationCode(applicationCode);
},

/**
* Emarsys SDK provides a solution for `merchantId` change in a convenient way without restarting the SDK.
* @param {string} merchantId - New `merchantId` string to change for.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
changeMerchantId(merchantId) {
return RNEmarsysWrapper.changeMerchantId(merchantId);
},

/**
* Provides what is the actual applicationCode set in the SDK.
* @returns {Promise<string>} Currently set `applicationCode` string.
*/
getApplicationCode() {
return RNEmarsysWrapper.getApplicationCode();
},

/**
* Provides what is the actual merchantId set in the SDK.
* @returns {Promise<string>} Currently set `merchantId` string.
*/
getMerchantId() {
return RNEmarsysWrapper.getMerchantId();
},

/**
* Provides what is the actual contactFieldId set in the SDK.
* @returns {Promise<number>} Currently set `contactFieldId` number.
*/
getContactFieldId() {
return RNEmarsysWrapper.getContactFieldId();
},

/**
* Provides what is the actual `hardwareId` set in the SDK.
* @returns {Promise<string>} Current `hardwareId` string.
*/
getHardwareId() {
return RNEmarsysWrapper.getHardwareId();
},

/**
* Provides what is the actual `languageCode` set in the SDK.
* @returns {Promise<string>} Current `languageCode` string.
*/
getLanguageCode() {
return RNEmarsysWrapper.getLanguageCode();
},

/**
* Provides what is the actual `sdkVersion` in the SDK.
* @returns {Promise<string>} String of the current Emarsys SDK version.
*/
getSdkVersion() {
return RNEmarsysWrapper.getSdkVersion();
},

push: Push,
inApp: InApp,
inbox: Inbox,
InlineInAppView,
predict: Predict,
geofence: Geofence,
};

export default Emarsys;

import { version } from './package.json';
RNEmarsysWrapper.trackCustomEvent('wrapper:init', { type: 'react-native', version })
import { version } from "./package.json";
RNEmarsysWrapper.trackCustomEvent("wrapper:init", { type: "react-native", version });
108 changes: 53 additions & 55 deletions src/geofence.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,60 @@
import { NativeModules } from 'react-native';
import { NativeModules } from "react-native";

const { RNEmarsysGeofenceWrapper } = NativeModules;

const Geofence = {

/**
* @desc The requestAlwaysAuthorization method is responsible for asking the required permissions from the user.
* Only available on iOS.
* @return bool - success or failure
*/
requestAlwaysAuthorization() {
return Platform.OS === 'ios' ? RNEmarsysGeofenceWrapper.requestAlwaysAuthorization() : "Not supported on Android"
},

/**
* @desc Activate Geofence
* @return bool - success or failure
*/
enable() {
return RNEmarsysGeofenceWrapper.geofenceEnable()
},

/**
* @desc Disable Geofence
* @return bool - success or failure
*/
disable() {
return RNEmarsysGeofenceWrapper.geofenceDisable()
},

/**
* @desc Return if the geofencing is currently enabled or not
* @return bool - geofencing is currently enabled or not
*/
isEnabled() {
return RNEmarsysGeofenceWrapper.geofenceIsEnabled()
},

/**
* @desc When initialEnterTriggerEnabled is true,
* Emarsys SDK will trigger all the affected geofences with Enter type triggers at the moment
* when the geofence is enabled if the device is already inside that geofence.
* By default, this value is set to false.
* @param bool enabled - initialEnterTriggerEnabled value for change
* @return bool - success or failure
*/
setInitialEnterTriggerEnabled(enabled) {
return RNEmarsysGeofenceWrapper.geofenceSetInitialEnterTriggerEnabled(enabled)
},

/**
* @desc Access the registered geofences from the device
* @return array - array of registered geofences
*/
registeredGeofences() {
return RNEmarsysGeofenceWrapper.registeredGeofences()
},

/**
* The `requestAlwaysAuthorization()` method is responsible for asking the required permissions from the user. Only available on iOS.
* @returns {Promise<boolean | "Not supported on Android">} Promise with success/failure boolean or error string on Android.
*/
requestAlwaysAuthorization() {
return Platform.OS === "ios" ? RNEmarsysGeofenceWrapper.requestAlwaysAuthorization() : "Not supported on Android";
},

/**
* Enables Geofence.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
enable() {
return RNEmarsysGeofenceWrapper.geofenceEnable();
},

/**
* Disables Geofence.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
disable() {
return RNEmarsysGeofenceWrapper.geofenceDisable();
},

/**
* Returns whether or not geofencing is currently enabled.
* @returns {Promise<boolean>} Promise with a boolean indicating the geofencing status.
*/
isEnabled() {
return RNEmarsysGeofenceWrapper.geofenceIsEnabled();
},

/**
* When `initialEnterTriggerEnabled` is `true`,
* Emarsys SDK will trigger all the affected geofences with `Enter` type triggers at the moment
* when the geofence is enabled if the device is already inside that geofence.
*
* By default, this value is set to `false`.
* @param {boolean} enabled - New `initialEnterTriggerEnabled` value to change for.
* @returns {Promise<boolean>} Promise with success or failure boolean.
*/
setInitialEnterTriggerEnabled(enabled) {
return RNEmarsysGeofenceWrapper.geofenceSetInitialEnterTriggerEnabled(enabled);
},

/**
* Accesses the registered geofences from the device.
* @returns {Promise<unknown[]>} Promise with the array of registered geofences.
*/
registeredGeofences() {
return RNEmarsysGeofenceWrapper.registeredGeofences();
},
};

export default Geofence;
Loading