diff --git a/.gitignore b/.gitignore index 8a6fe18..424d6da 100644 --- a/.gitignore +++ b/.gitignore @@ -26,7 +26,6 @@ DerivedData *.xcuserstate - *.hprof # Android/IntelliJ @@ -44,7 +43,6 @@ local.properties npm-debug.log yarn-error.log - # BUCK buck-out/ \.buckd/ @@ -66,3 +64,4 @@ buck-out/ *.bundle sample/ios/Podfile.lock sample/android/app/google-services.json +typescript/* diff --git a/index.js b/index.js index fc701ae..e63043d 100644 --- a/index.js +++ b/index.js @@ -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} 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} 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 | undefined} eventAttributes - Object containing the attributes of the tracked custom event. + * @returns {Promise} 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} 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} 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} Promise with success or failure boolean. + */ + changeMerchantId(merchantId) { + return RNEmarsysWrapper.changeMerchantId(merchantId); + }, + + /** + * Provides what is the actual applicationCode set in the SDK. + * @returns {Promise} Currently set `applicationCode` string. + */ + getApplicationCode() { + return RNEmarsysWrapper.getApplicationCode(); + }, + + /** + * Provides what is the actual merchantId set in the SDK. + * @returns {Promise} Currently set `merchantId` string. + */ + getMerchantId() { + return RNEmarsysWrapper.getMerchantId(); + }, + + /** + * Provides what is the actual contactFieldId set in the SDK. + * @returns {Promise} Currently set `contactFieldId` number. + */ + getContactFieldId() { + return RNEmarsysWrapper.getContactFieldId(); + }, + + /** + * Provides what is the actual `hardwareId` set in the SDK. + * @returns {Promise} Current `hardwareId` string. + */ + getHardwareId() { + return RNEmarsysWrapper.getHardwareId(); + }, + + /** + * Provides what is the actual `languageCode` set in the SDK. + * @returns {Promise} Current `languageCode` string. + */ + getLanguageCode() { + return RNEmarsysWrapper.getLanguageCode(); + }, + + /** + * Provides what is the actual `sdkVersion` in the SDK. + * @returns {Promise} 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 }); diff --git a/package.json b/package.json index ac93ea8..d443f12 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,29 @@ { - "name": "react-native-emarsys-wrapper", - "version": "1.17.0", - "description": "React Native wrapper for Emarsys SDK", - "main": "index.js", - "type": "module", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "keywords": [ - "react-native", - "Emarsys" - ], - "author": "emarsys", - "license": "MIT", - "peerDependencies": { - "react": ">=17.0.2", - "react-native": ">=0.67.3" - } + "name": "react-native-emarsys-wrapper", + "version": "1.17.0", + "description": "React Native wrapper for Emarsys SDK", + "main": "index.js", + "types": "typescript/index.d.ts", + "type": "module", + "scripts": { + "prepare": "tsc", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "react-native", + "Emarsys" + ], + "author": "emarsys", + "license": "MIT", + "devDependencies": { + "typescript": ">=3.7.2" + }, + "peerDependencies": { + "@types/jest": "*", + "@types/react": "*", + "@tsconfig/react-native": "*", + "@types/react-test-renderer": "*", + "react": ">=17.0.2", + "react-native": ">=0.67.3" + } } diff --git a/src/geofence.js b/src/geofence.js index c0f58f8..94d3ca7 100644 --- a/src/geofence.js +++ b/src/geofence.js @@ -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} 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} Promise with success or failure boolean. + */ + enable() { + return RNEmarsysGeofenceWrapper.geofenceEnable(); + }, + + /** + * Disables Geofence. + * @returns {Promise} Promise with success or failure boolean. + */ + disable() { + return RNEmarsysGeofenceWrapper.geofenceDisable(); + }, + + /** + * Returns whether or not geofencing is currently enabled. + * @returns {Promise} 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} Promise with success or failure boolean. + */ + setInitialEnterTriggerEnabled(enabled) { + return RNEmarsysGeofenceWrapper.geofenceSetInitialEnterTriggerEnabled(enabled); + }, + + /** + * Accesses the registered geofences from the device. + * @returns {Promise} Promise with the array of registered geofences. + */ + registeredGeofences() { + return RNEmarsysGeofenceWrapper.registeredGeofences(); + }, }; export default Geofence; diff --git a/src/inapp.js b/src/inapp.js index 5cf2e69..9b7ed49 100644 --- a/src/inapp.js +++ b/src/inapp.js @@ -1,68 +1,70 @@ -import React from 'react'; -import { NativeModules, requireNativeComponent, UIManager, findNodeHandle } from 'react-native'; -import PropTypes from 'prop-types'; +import React from "react"; +import PropTypes from "prop-types"; +import { NativeModules, requireNativeComponent, UIManager, findNodeHandle } from "react-native"; const { RNEmarsysInAppWrapper } = NativeModules; const InApp = { + /** + * When a critical activity starts and should not be interrupted by InApp, use `pause()` to pause InApp messages. + * @returns {Promise} Promise with success or failure boolean. + */ + pause() { + return RNEmarsysInAppWrapper.pause(); + }, - /** - * @desc When a critical activity starts and should not be interrupted by InApp, use pause to pause InApp messages. - * @return bool - success or failure - */ - pause() { - return RNEmarsysInAppWrapper.pause() - }, - - /** - * @desc In order to show inApp messages after being paused, use the resume method. - * @return bool - success or failure - */ - resume() { - return RNEmarsysInAppWrapper.resume() - }, - + /** + * In order to show inApp messages after being paused, use the `resume()` method. + * @returns {Promise} Promise with success or failure boolean. + */ + resume() { + return RNEmarsysInAppWrapper.resume(); + }, }; -const RNEmarsysInlineInAppView = requireNativeComponent('RNEmarsysInlineInAppView'); +const RNEmarsysInlineInAppView = requireNativeComponent("RNEmarsysInlineInAppView"); class InlineInAppView extends React.Component { - loadInApp(viewId) { - let commandID = UIManager.RNEmarsysInlineInAppView.Commands.loadInApp - UIManager.dispatchViewManagerCommand( - findNodeHandle(this), - Platform.OS === 'ios' ? commandID : commandID.toString(), - [viewId], - ); - } + loadInApp(viewId) { + let commandID = UIManager.RNEmarsysInlineInAppView.Commands.loadInApp; + UIManager.dispatchViewManagerCommand( + findNodeHandle(this), + Platform.OS === "ios" ? commandID : commandID.toString(), + [viewId] + ); + } - _onAppEvent = event => { - if (!this.props.onAppEvent) { return; } - this.props.onAppEvent(event.nativeEvent.eventName, event.nativeEvent.payload); - }; + _onAppEvent = (event) => { + if (!this.props.onAppEvent) return; + this.props.onAppEvent(event.nativeEvent.eventName, event.nativeEvent.payload); + }; - _onCompleted = event => { - if (!this.props.onCompleted) { return; } - this.props.onCompleted(event.nativeEvent.error); - }; + _onCompleted = (event) => { + if (!this.props.onCompleted) return; + this.props.onCompleted(event.nativeEvent.error); + }; - _onClose = event => { - if (!this.props.onClose) { return; } - this.props.onClose(); - }; + _onClose = () => { + if (!this.props.onClose) return; + this.props.onClose(); + }; - render() { - return ; - } + render() { + return ( + + ); + } } InlineInAppView.propTypes = { - onAppEvent: PropTypes.func, - onCompleted: PropTypes.func, - onClose: PropTypes.func, + onAppEvent: PropTypes.func, + onCompleted: PropTypes.func, + onClose: PropTypes.func, }; export default InApp; diff --git a/src/inbox.js b/src/inbox.js index ddc0dbc..29d6013 100644 --- a/src/inbox.js +++ b/src/inbox.js @@ -1,21 +1,42 @@ -import { NativeModules } from 'react-native'; +import { NativeModules } from "react-native"; const { RNEmarsysInboxWrapper } = NativeModules; const Inbox = { + /** + * In order to receive the message Inbox content, you can use the fetchMessages method. + * @returns {Promise} Promise with the Inbox content. + */ + fetchMessages() { + return RNEmarsysInboxWrapper.fetchMessages(); + }, - fetchMessages() { - return RNEmarsysInboxWrapper.fetchMessages() - }, - - addTag(tag, messageId) { - return RNEmarsysInboxWrapper.addTag(tag, messageId) - }, - - removeTag(tag, messageId) { - return RNEmarsysInboxWrapper.removeTag(tag, messageId) - }, + /** + * Tags are to be used to set the status of the inbox message, e.g. `"opened"`, `"seen"`, etc. + * + * There are 6 tags in total: `"high"`, `"cancelled"` and 4 others that are the only ones App developers can add: `"seen"`, `"opened"`, `"pinned"` and `"deleted"`. + * + * It is important to note all the tags though, as they will be included in the message payload in the SDK `tag` field. + * + * Depending on the `tag` included in the message, the message could be handled differently by the app. + * An example would be that messages tagged with `"high"` (for High Priority) could be visible flagged/highlighted by the Contact. + * @param {'seen' | 'opened' | 'pinned' | 'deleted'} tag - Label to use as the new message status. + * @param {string} messageId - Id of the message to update the status of. + * @returns {Promise} Promise with success or failure boolean. + */ + addTag(tag, messageId) { + return RNEmarsysInboxWrapper.addTag(tag, messageId); + }, + /** + * To remove a label from a message, you can use `removeTag()` method. + * @param {'high' | 'cancelled' | 'seen' | 'opened' | 'pinned' | 'deleted'} tag - Current message statues tag to remove. + * @param {string} messageId - Id of the message to update the status of. + * @returns {Promise} Promise with success or failure boolean. + */ + removeTag(tag, messageId) { + return RNEmarsysInboxWrapper.removeTag(tag, messageId); + }, }; export default Inbox; diff --git a/src/predict.js b/src/predict.js index 3780218..c50df04 100644 --- a/src/predict.js +++ b/src/predict.js @@ -1,335 +1,399 @@ -import { NativeModules } from 'react-native'; +import { NativeModules } from "react-native"; const { RNEmarsysPredictWrapper } = NativeModules; const Predict = { + /** + * When you want to track the cart items in the basket, you can call the `trackCart()` method with a list of `CartItems`. + * `CartItem` is an interface that can be used in your application for your own `CartItems` and then simply use the same items with the SDK. + * @param {{itemId: string; price: number; quantity: number}[]} cartItems - Array of `cartItems`. Can be empty, if you want to track an empty cart. + * - `itemId` - cartItem ID + * - `price` - cartItem price + * - `quantity` - cartItem quantity + * + * Empty cart: + * `const cartItems = []` + * + * Cart with Items: + * ``` + * const cartItems = [{ + * itemId: "ID of the Cart Item 1", + * price: 1.66, + * quantity: 26.4, + * }, { + * itemId: "ID of the Cart Item 2", + * price: 2.88, + * quantity: 56.5, + * }] + * ``` + * + * @returns {Promise} Promise with success or failure boolean. + */ + trackCart(cartItems) { + return RNEmarsysPredictWrapper.trackCart(cartItems); + }, - /** - * @desc When you want to track the cart items in the basket, you can call the trackCart method with a list of CartItems. - * CartItem is an interface that can be used in your application for your own CartItems and then simply use the same items with the SDK. - * @param required array - Array of cartItems. Can be empty, if you want to track empty cart. - * - itemId - cartItem ID - * - price - cartItem price - * - quantity - cartItem quantity. - * - * Empty cart: - * let cartItems = [] - * - * Cart with Items: - * let cartItems = [{ - * itemId: "ID of the Cart Item 1", - * price: 1.66, - * quantity: 26.4, - * }, { - * itemId: "ID of the Cart Item 2", - * price: 2.88, - * quantity: 56.5, - * }] - * - * @return bool - success or failure - */ - trackCart(cartItems) { - return RNEmarsysPredictWrapper.trackCart(cartItems) - }, + /** + * To report a purchase event, you should call `trackPurchase()` with the items purchased and an `orderId`. + * @param {string} orderId - The order ID. + * @param {{itemId: string; price: number; quantity: number}[]} cartItems - Array of `cartItems`. Can be empty, if you want to track an empty cart. + * - `itemId` - cartItem ID + * - `price` - cartItem price + * - `quantity` - cartItem quantity + * + * Empty cart: + * `const cartItems = []` + * + * Cart with Items: + * ``` + * const cartItems = [{ + * itemId: "ID of the Cart Item 1", + * price: 1.66, + * quantity: 26.4, + * }, { + * itemId: "ID of the Cart Item 2", + * price: 2.88, + * quantity: 56.5, + * }] + * ``` + * + * @returns {Promise} Promise with success or failure boolean. + */ + trackPurchase(orderId, cartItems) { + return RNEmarsysPredictWrapper.trackPurchase(orderId, cartItems); + }, - /** - * @desc To report a purchase event, you should call trackPurchase with the items purchased and with an orderId. - * @param required string orderId - Order Id. - * @param required array - Array of cartItems. Can be empty, if you want to track empty cart. - * - itemId - cartItem ID - * - price - cartItem price - * - quantity - cartItem quantity. - * - * Empty cart: - * let cartItems = [] - * - * Cart with Items: - * let cartItems = [{ - * itemId: "ID of the Cart Item 1", - * price: 1.66, - * quantity: 26.4, - * }, { - * itemId: "ID of the Cart Item 2", - * price: 2.88, - * quantity: 56.5, - * }] - * - * @return bool - success or failure - */ - trackPurchase(orderId, cartItems) { - return RNEmarsysPredictWrapper.trackPurchase(orderId, cartItems) - }, + /** + * If an item was viewed, use the `trackItemView()` method with an `itemId` as required parameter. + * @param {string} itemId - ID of item that was viewed. + * @returns {Promise} Promise with success or failure boolean. + */ + trackItemView(itemId) { + return RNEmarsysPredictWrapper.trackItemView(itemId); + }, - /** - * @desc If an item was viewed, use the trackItemView method with an itemId as required parameter. - * @param required string itemId - ID of item was viewed. - * @return bool - success or failure - */ - trackItemView(itemId) { - return RNEmarsysPredictWrapper.trackItemView(itemId) - }, + /** + * When the user navigates between the categories, you should call `trackCategoryView()` in every navigation. Be aware to send `categoryPath` in the required format. + * @param {string} categoryPath - Category path that was viewed. + * @returns {Promise} Promise with success or failure boolean. + */ + trackCategoryView(categoryPath) { + return RNEmarsysPredictWrapper.trackCategoryView(categoryPath); + }, - /** - * @desc When the user navigates between the categories, you should call trackCategoryView in every navigation. Be aware to send categoryPath in the required format. - * @param required string categoryPath - Category path was viewed. - * @return bool - success or failure - */ - trackCategoryView(categoryPath) { - return RNEmarsysPredictWrapper.trackCategoryView(categoryPath) - }, + /** + * To report search terms entered by the contact, use `trackSearchTerm()` method. + * @param {string} searchTerm - Term that was searched. + * @returns {Promise} Promise with success or failure boolean. + */ + trackSearchTerm(searchTerm) { + return RNEmarsysPredictWrapper.trackSearchTerm(searchTerm); + }, - /** - * @desc To report search terms entered by the contact, use trackSearchTerm method. - * @param required string searchTerm - Term was searched. - * @return bool - success or failure - */ - trackSearchTerm(searchTerm) { - return RNEmarsysPredictWrapper.trackSearchTerm(searchTerm) - }, + /** + * To track custom tags, use the `trackTag()` method where the `eventName` parameter is required, but the `attributes` are optional. + * @param {string} tagName - Tag name to track. + * @param {Record} tagAttributes - Optional tag attributes. + * + * Eg: + *``` + * const tagAttributes = { + * "tag-key1": "tag-value1", + * "tag-key2": "tag-value2", + * } + *``` + * @returns {Promise} Promise with success or failure boolean. + */ + trackTag(tagName, tagAttributes) { + return RNEmarsysPredictWrapper.trackTag(tagName, tagAttributes || null); + }, - /** - * @desc To track custom tags, use the trackTag method, where, the eventName parameter is required, but the attributes is optional. - * @param required string tagName - Tag name. - * @param optional object tagAttributes - Tag attributes. - * - * let tagAttributes = { - * "tag-key1": "tag-value1", - * "tag-key2": "tag-value2", - * } - * - * @return bool - success or failure - */ - trackTag(tagName, tagAttributes) { - return RNEmarsysPredictWrapper.trackTag(tagName, tagAttributes ? tagAttributes : null) - }, + /** + * With the Emarsys SDK you can ask for product recommendations based on different parameters. + * @param {'SEARCH' | 'CART' | 'RELATED' | 'CATEGORY' | 'ALSO_BOUGHT' | 'POPULAR' | 'PERSONAL' | 'HOME'} logic - For more information of the recommender logics, please visit [the official documentation](https://help.emarsys.com/hc/en-us/articles/115004662189-Web-Recommender-logics "Official documentation"). + * + * The currently supported logics are: + * - [`'SEARCH'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#search1 + * "'SEARCH' documentation") - based on `searchTerm` + * - [`'CART'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#cart "'CART' documentation") - based on `cartItems` + * - [`'RELATED'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#related "'RELATED' documentation") - based on `itemViewId` + * - [`'CATEGORY'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#category "'CATEGORY' documentation") - based on `categoryPath` + * - [`'ALSO_BOUGHT'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#also_bought "'ALSO_BOUGHT' documentation") - based on `itemViewId` + * - [`'POPULAR'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#popular "'POPULAR' documentation") - based on `categoryPath` + * - [`'PERSONAL'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#personal "'PERSONAL' documentation") - based on current browsing and activity + * - [`'HOME'`](https://help.emarsys.com/hc/en-us/articles/115004662189-Overview-Web-Recommender-logics#home "'HOME' documentation") - based on most recent browsing behaviour + * + * @param {Record<'query', string> | Record<'cartItems', {itemId: string; price: number; quantity: number}[]> | + * Record<'variants', string[]>} logicOptions - Options object for the logic. + * + * The object key can either be `'query'`, `'cartItems'`, or `'variants'`: + * - `query` - Query string extends recommended logics. + * Eg: + * ``` + * let logicOptions = { + * query: "Shoes>Pump" + * } + * ``` + * + * - `cartItems` - Array of `cartItems`, can be empty. + * Eg: + * ``` + * let logicOptions = { + * cartItems: [{ + * itemId: "ID of the Cart Item 1", + * price: 1.66, + * quantity: 26.4, + * }, { + * itemId: "ID of the Cart Item 2", + * price: 2.88, + * quantity: 56.5, + * }] + * } + * ``` + * + * - `variants` - Array of variants. + * Eg: + * ``` + * let logicOptions = { + * variants: ["1", "2", "3"] + * } + * ``` + * + * @param {{availabilityZone?: string; limit?: number; filters?: {type: 'exclude' | 'include'; field: string; + * comparison: 'is' | 'in' | 'has' | 'overlaps'; + * expectations: string | string[]}}} recommendationOptions - Options for recommendation. + * - `availabilityZone` - You can personalize the recommendation further by setting the `availabilityZones` parameter of the recommendation, to only recommend the locally available products. This is an optional parameter. + * - `limit` - You can limit the number of recommended products received by defining a limit. This is an optional parameter, by default its value is 5. + * - `filters` - You can filter product recommendations with the SDK by building `RecommendationFilters`. This is an optional parameter. + * - `filters.type` - There are two types of filters: `exclude` or `include`. + * - `filters.field` - String extends `type` of recommended logics. + * - `filters.comparison` - In every case there are four types of comparators you can use to compare your chosen field to expectations: + * - `filters.comparison.is` - checking if the field is matching the value. + * - `filters.comparison.in` - any of the values has a match with the field. + * - `filters.comparison.has` - One of the field values is equal to expectation value (applicable only to fields containing multiple values). + * - `filters.comparison.overlaps` - One or more of the field values are found in expectation values (applicable only to fields containing multiple values). + * - `filters.expectations` - String or array of strings extends `comparison` of recommended logics. + * + * Eg: + * ``` + * let recommendationOptions = { + * availabilityZone: "es", + * limit: 10, + * filters: [{ + * type: "include", + * field: "category", + * comparison: "is", + * expectations: "Shoes>Pump" + * }] + * } + *``` + * @returns {Record[]} Array of objects with recommended `Products`. + */ + recommendProducts(logic, logicOptions, recommendationOptions) { + return RNEmarsysPredictWrapper.recommendProducts(logic, logicOptions, recommendationOptions); + }, - /** - * @desc With the Emarsys SDK you can ask for product recommendations based on different parameters. - * @param required string logic - For more information of the recommender logics, please visit [the official documentation].(https://help.emarsys.com/hc/en-us/articles/115004662189-Web-Recommender-logics "The Official Documentation") - * - * The currently supported logics are: - * - SEARCH - based on searchTerm - * - CART - based on cartItems - * - RELATED - based on itemViewId - * - CATEGORY - based on categoryPath - * - ALSO_BOUGHT - based on itemViewId - * - POPULAR - based on categoryPath - * - PERSONAL - based on current browsing and activity - * - HOME - based on most recent browsing behaviour - * - * @param optional object logicOptions - Option for the logic. - * Either `query`, `cartItems`, or `variants` - * - query - Query string extends recommended logics. - * - cartItems - Array of cartItems, can be empty. - * - variants - Array of variants - * - * let logicOptions = { - * query: "Shoes>Pump" - * } - * - * let logicOptions = { - * cartItems: [{ - * itemId: "ID of the Cart Item 1", - * price: 1.66, - * quantity: 26.4, - * }, { - * itemId: "ID of the Cart Item 2", - * price: 2.88, - * quantity: 56.5, - * }] - * } - * - * let logicOptions = { - * variants: ["1", "2", "3"] - * } - * - * @param optional object recommendationOptions - Option for recommendation. - * - availabilityZone - You can personalize the recommendation further by setting the availabilityZones parameter of the recommendation, to only recommend the locally available products. This is an optional parameter. - * - limit - You can limit the number of recommended products received by defining a limit. This is an optional parameter, by default its value is 5. - * - filters - You can filter product recommendations with the SDK by building RecommendationFilters. This is an optional parameter. - * type - There are two types of filters: exclude or include. - * field - String extends Type of recommended logics. - * comparison - In every case there are four types of comparators you can use to compare your chosen field to expectations: - * is - checking if the field is matching the value. - * in - any of the values has a match with the field. - * has - One of the field values is equal to expectation value (applicable only to fields containing multiple values). - * overlaps - One or more of the field values are found in expectation values (applicable only to fields containing multiple values). - * expectations - String/Array of strings extends Comparison of recommended logics. - * - * let recommendationOptions = { - * availabilityZone: "es", - * limit: 10, - * filters: [{ - * type: "include", - * field: "category", - * comparison: "is", - * expectations: "Shoes>Pump" - * }] - * } - * - * @return array - array of objects with recommended Products - */ - recommendProducts(logic, logicOptions, recommendationOptions) { - return RNEmarsysPredictWrapper.recommendProducts(logic, logicOptions, recommendationOptions) - }, + /** + * @param {never} logic + * @param {never} query + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsQuery(logic, query) { + return RNEmarsysPredictWrapper.recommendProductsQuery(logic, query); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsQuery(logic, query) { - return RNEmarsysPredictWrapper.recommendProductsQuery(logic, query) - }, + /** + * @param {never} logic + * @param {never} cartItems + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsCartItems(logic, cartItems) { + return RNEmarsysPredictWrapper.recommendProductsCartItems(logic, cartItems); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsCartItems(logic, cartItems) { - return RNEmarsysPredictWrapper.recommendProductsCartItems(logic, cartItems) - }, + /** + * @param {never} logic + * @param {never} variants + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsVariants(logic, variants) { + return RNEmarsysPredictWrapper.recommendProductsVariants(logic, variants); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsVariants(logic, variants) { - return RNEmarsysPredictWrapper.recommendProductsVariants(logic, variants) - }, + /** + * @param {never} logic + * @param {never} limit + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsLimit(logic, limit) { + return RNEmarsysPredictWrapper.recommendProductsLimit(logic, limit); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsLimit(logic, limit) { - return RNEmarsysPredictWrapper.recommendProductsLimit(logic, limit) - }, + /** + * @param {never} logic + * @param {never} query + * @param {never} limit + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsQueryLimit(logic, query, limit) { + return RNEmarsysPredictWrapper.recommendProductsQueryLimit(logic, query, limit); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsQueryLimit(logic, query, limit) { - return RNEmarsysPredictWrapper.recommendProductsQueryLimit(logic, query, limit) - }, + /** + * @param {never} logic + * @param {never} cartItems + * @param {never} limit + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsCartItemsLimit(logic, cartItems, limit) { + return RNEmarsysPredictWrapper.recommendProductsCartItemsLimit(logic, cartItems, limit); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsCartItemsLimit(logic, cartItems, limit) { - return RNEmarsysPredictWrapper.recommendProductsCartItemsLimit(logic, cartItems, limit) - }, + /** + * @param {never} logic + * @param {never} variants + * @param {never} limit + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsVariantsLimit(logic, variants, limit) { + return RNEmarsysPredictWrapper.recommendProductsVariantsLimit(logic, variants, limit); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsVariantsLimit(logic, variants, limit) { - return RNEmarsysPredictWrapper.recommendProductsVariantsLimit(logic, variants, limit) - }, + /** + * @param {never} logic + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsFilters(logic, filters) { + return RNEmarsysPredictWrapper.recommendProductsFilters(logic, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsFilters(logic, filters) { - return RNEmarsysPredictWrapper.recommendProductsFilters(logic, filters) - }, + /** + * @param {never} logic + * @param {never} query + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsQueryFilters(logic, query, filters) { + return RNEmarsysPredictWrapper.recommendProductsQueryFilters(logic, query, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsQueryFilters(logic, query, filters) { - return RNEmarsysPredictWrapper.recommendProductsQueryFilters(logic, query, filters) - }, + /** + * @param {never} logic + * @param {never} cartItems + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsCartItemsFilters(logic, cartItems, filters) { + return RNEmarsysPredictWrapper.recommendProductsCartItemsFilters(logic, cartItems, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsCartItemsFilters(logic, cartItems, filters) { - return RNEmarsysPredictWrapper.recommendProductsCartItemsFilters(logic, cartItems, filters) - }, + /** + * @param {never} logic + * @param {never} variants + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsVariantsFilters(logic, variants, filters) { + return RNEmarsysPredictWrapper.recommendProductsVariantsFilters(logic, variants, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsVariantsFilters(logic, variants, filters) { - return RNEmarsysPredictWrapper.recommendProductsVariantsFilters(logic, variants, filters) - }, + /** + * @param {never} logic + * @param {never} limit + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsLimitFilters(logic, limit, filters) { + return RNEmarsysPredictWrapper.recommendProductsLimitFilters(logic, limit, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsLimitFilters(logic, limit, filters) { - return RNEmarsysPredictWrapper.recommendProductsLimitFilters(logic, limit, filters) - }, + /** + * @param {never} logic + * @param {never} query + * @param {never} limit + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsQueryLimitFilters(logic, query, limit, filters) { + return RNEmarsysPredictWrapper.recommendProductsQueryLimitFilters(logic, query, limit, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsQueryLimitFilters(logic, query, limit, filters) { - return RNEmarsysPredictWrapper.recommendProductsQueryLimitFilters(logic, query, limit, filters) - }, + /** + * @param {never} logic + * @param {never} cartItems + * @param {never} limit + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsCartItemsLimitFilters(logic, cartItems, limit, filters) { + return RNEmarsysPredictWrapper.recommendProductsCartItemsLimitFilters(logic, cartItems, limit, filters); + }, - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsCartItemsLimitFilters(logic, cartItems, limit, filters) { - return RNEmarsysPredictWrapper.recommendProductsCartItemsLimitFilters(logic, cartItems, limit, filters) - }, - - /** - * @desc Deprecated and will be removed in later versions. - * Pleaes use `recommendProducts(logic, logicOptions, recommendationOptions)` - */ - recommendProductsVariantsLimitFilters(logic, variants, limit, filters) { - return RNEmarsysPredictWrapper.recommendProductsVariantsLimitFilters(logic, variants, limit, filters) - }, - - /** - * @desc The Emarsys SDK doesn't track automatically recommendationClicks, - * so you have to call manually trackRecommendationClick when an interaction happens with any of the recommended products. - * @param required object product - Recommended product was clicked. - * - * let product = { - * productId: "productId", //String - * title: "title", //String - * linkUrl: "http://linkUrl.com/test", //URL - * feature: "feature", //String - * cohort: "awesome", //String - * imageUrl: "http://productURL.com/imageUrl", //String - * zoomImageUrl: "http://productURL.com/zoomImageUrl", //String - * categoryPath: "productCategoryPath", //String - * productDescription: "productDescription", //String - * album: "productAlbum", //String - * actor: "productActor", //String - * artist: "productArtist", //String - * author: "productAuthor", //String - * brand: "productBrand", //String - * customFields: { - * productTestKey1: "productTestValue1", // - * productTestKey2: "productTestValue2", // - * productTestKey3: "productTestValue3", // - * }, - * available: true, //Boolean - * price: 45.67, //Float - * msrp: 2.45, //Float - * year: 2019, //Integer - * } - * - * @return bool - success or failure - */ - trackRecommendationClick(product) { - return RNEmarsysPredictWrapper.trackRecommendationClick(product) - }, + /** + * @param {never} logic + * @param {never} variants + * @param {never} limit + * @param {never} filters + * @returns {never} + * @deprecated Please use {@link Predict.recommendProducts()} instead. + */ + recommendProductsVariantsLimitFilters(logic, variants, limit, filters) { + return RNEmarsysPredictWrapper.recommendProductsVariantsLimitFilters(logic, variants, limit, filters); + }, + /** + * The Emarsys SDK doesn't track automatically `recommendationClicks`, + * so you have to call manually `trackRecommendationClick()` when an interaction happens with any of the recommended products. + * @param {Record} product - Recommended product that was clicked. + * + * Eg: + * ``` + * let product = { + * productId: "productId", //String + * title: "title", //String + * linkUrl: "http://linkUrl.com/test", //URL + * feature: "feature", //String + * cohort: "awesome", //String + * imageUrl: "http://productURL.com/imageUrl", //String + * zoomImageUrl: "http://productURL.com/zoomImageUrl", //String + * categoryPath: "productCategoryPath", //String + * productDescription: "productDescription", //String + * album: "productAlbum", //String + * actor: "productActor", //String + * artist: "productArtist", //String + * author: "productAuthor", //String + * brand: "productBrand", //String + * customFields: { + * productTestKey1: "productTestValue1", // + * productTestKey2: "productTestValue2", // + * productTestKey3: "productTestValue3", // + * }, + * available: true, //Boolean + * price: 45.67, //Float + * msrp: 2.45, //Float + * year: 2019, //Integer + * } + *``` + * @returns {Promise} Promise with success or failure boolean. + */ + trackRecommendationClick(product) { + return RNEmarsysPredictWrapper.trackRecommendationClick(product); + }, }; export default Predict; diff --git a/src/push.js b/src/push.js index 0b030a4..de5652f 100644 --- a/src/push.js +++ b/src/push.js @@ -1,31 +1,41 @@ -import { NativeModules } from 'react-native'; +import { NativeModules } from "react-native"; const { RNEmarsysPushWrapper } = NativeModules; const Push = { + /** + * The Emarsys SDK automatically handles `setPushToken()` for the device and it is recommended to leave this to the SDK. + * However if you have your custom implementation of `MessagingService`, please use the `setPushToken()` method. + * @param {string} pushToken - Push Token of your `MessagingService`. + * @returns {Promise} Promise with success or failure boolean. + */ + setPushToken(pushToken) { + return RNEmarsysPushWrapper.setPushToken(pushToken); + }, - /** - * @desc The Emarsys SDK automatically handles setPushToken for the device and it is recommended to leave this to the SDK. - * However if you have your custom implementation of MessagingService, please use the setPushToken() method. - * @param required string pushToken - Push Token of your MessagingService - * @return bool - success or failure - */ - setPushToken(pushToken) { - return RNEmarsysPushWrapper.setPushToken(pushToken) - }, + /** + * If you want to remove `pushToken` from the `Contact`, please use `clearPushToken()`. + * @returns {Promise} Promise with success or failure boolean. + */ + clearPushToken() { + return RNEmarsysPushWrapper.clearPushToken(); + }, - /** - * @desc If you want to remove pushToken for the Contact, please use clearPushToken(). - * @return bool - success or failure - */ - clearPushToken() { - return RNEmarsysPushWrapper.clearPushToken() - }, - - pushToken() { - return RNEmarsysPushWrapper.getPushToken() - }, + /** + * If you want to get the `pushToken` of the `Contact`, please use `getPushToken()`. + * @returns {Promise} Promise with the `pushToken` string. + */ + getPushToken() { + return RNEmarsysPushWrapper.getPushToken(); + }, + /** + * @returns {never} + * @deprecated Please use {@link Push.getPushToken()} instead. + */ + pushToken() { + return RNEmarsysPushWrapper.getPushToken(); + }, }; export default Push; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7fc0256 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "@tsconfig/react-native/tsconfig.json", + // Tells TypeScript where to look for files to work with. + "include": ["index.js", "src/**/*"], + "compilerOptions": { + // Tells TypeScript to read JS files, as normally they are ignored as source files. + "allowJs": true, + // Ask Typescript to generate .d.ts declaration files. + "noEmit": false, + "declaration": true, + // This compiler run should only output .d.ts files. + "emitDeclarationOnly": true, + // Types should go into this directory. + // Removing this option will place the .d.ts files next to their .js source files. + "outDir": "typescript", + // Generate declaration files to go to js file when using IDE functions like "Go to Definition". + "declarationMap": true + } +}