UNPKG

appsonair-react-native-applink

Version:

A self-hosted dynamic link service for app download tracking, deep linking, and engagement analytics.

154 lines (147 loc) 5.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _exportNames = { initializeAppLink: true, createAppLink: true, getReferralInfo: true, getReferralDetails: true, onDeepLinkProcessed: true, onReferralLinkDetected: true }; exports.onReferralLinkDetected = exports.onDeepLinkProcessed = exports.initializeAppLink = exports.getReferralInfo = exports.getReferralDetails = exports.createAppLink = void 0; var _reactNative = require("react-native"); var _types = require("./types.js"); Object.keys(_types).forEach(function (key) { if (key === "default" || key === "__esModule") return; if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; if (key in exports && exports[key] === _types[key]) return; Object.defineProperty(exports, key, { enumerable: true, get: function () { return _types[key]; } }); }); const LINKING_ERROR = `The package 'appsonair-react-native-applink' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n'; const AppsonairReactNativeApplink = _reactNative.NativeModules.AppsonairReactNativeApplink ? _reactNative.NativeModules.AppsonairReactNativeApplink : new Proxy({}, { get() { throw new Error(LINKING_ERROR); } }); const emitter = _reactNative.NativeModules.AppsonairReactNativeApplink ? new _reactNative.NativeEventEmitter(_reactNative.NativeModules.AppsonairReactNativeApplink) : null; /** * Initializes the AppsOnAir React Native AppLink module. * Should be called once during app startup to set up deep link handling. * * @returns {Promise<boolean>} A promise that resolves to `true` if initialization succeeds. */ const initializeAppLink = async () => { return await AppsonairReactNativeApplink.initialize(); }; /** * Creates a new AppLink using the provided parameters. * The resulting AppLink can be used to navigate to specific content within the app. * * @param {AppLinkParams} params - Parameters used to generate the deep link. * @returns {Promise<CreateAppLinkResponse>} A promise that resolves to the generated deep link URL, or `null` if creation fails. */ exports.initializeAppLink = initializeAppLink; const createAppLink = async ({ url, name, urlPrefix, shortId, socialMeta, isOpenInBrowserAndroid = false, isOpenInAndroidApp = true, androidFallbackUrl = '', isOpenInBrowserApple = false, isOpenInIosApp = true, iosFallbackUrl = '' }) => { const result = await AppsonairReactNativeApplink.createAppLink({ url, name, urlPrefix, shortId: shortId === '' ? undefined : shortId, metaTitle: socialMeta?.title || null, metaDescription: socialMeta?.description || null, metaImageUrl: socialMeta?.imageUrl || null, isOpenInBrowserAndroid, isOpenInAndroidApp, androidFallbackUrl, isOpenInBrowserApple, isOpenInIosApp, iosFallbackUrl }); if (typeof result === 'string') { return JSON.parse(result); } return result; }; /** * Fetches referral details from the AppsOnAir deep link service. * This method retrieves any available referral metadata associated with the user's session. * * @returns A Promise that resolves to an object containing referral details, or `null` if no referral data is available. */ exports.createAppLink = createAppLink; const getReferralInfo = async () => { let result = await AppsonairReactNativeApplink.getReferralInfo(); if (typeof result === 'string') { JSON.parse(result); } if (typeof result?.data === 'string') { result.data = JSON.parse(result.data); } return result; }; /** * @deprecated Use `getReferralInfo` instead. This method will be removed in future versions. * Fetches referral details from the AppsOnAir deep link service. * This method retrieves any available referral metadata associated with the user's session. * * @returns A Promise that resolves to an object containing referral details, or `null` if no referral data is available. */ exports.getReferralInfo = getReferralInfo; const getReferralDetails = async () => { let result = await AppsonairReactNativeApplink.getReferralDetails(); if (typeof result === 'string') { JSON.parse(result); } if (typeof result?.data === 'string') { result.data = JSON.parse(result.data); } return result; }; /** * Registers a listener for when a deep link is successfully processed. * The callback receives the processed deep link URL and any associated result data. * * @param {(event: LinkInfo) => void} callback - Function to be called when a deep link is processed. * @returns {EmitterSubscription | null} An event subscription, or `null` if the listener could not be registered. */ exports.getReferralDetails = getReferralDetails; const onDeepLinkProcessed = callback => { return emitter?.addListener('onDeepLinkProcessed', callback) ?? null; }; /** * Subscribes to referral link detection events from the AppsOnAir SDK. * When a referral link is detected, the native module emits an * `onReferralLinkDetected` event. This helper sets up a listener for that * event and forwards the payload to the provided callback. * * @param {(event: LinkInfo) => void} callback - Function to be called when a deep link is processed. * @returns {EmitterSubscription | null} An event subscription, or `null` if the listener could not be registered. */ exports.onDeepLinkProcessed = onDeepLinkProcessed; const onReferralLinkDetected = callback => { return emitter?.addListener('onReferralLinkDetected', callback) ?? null; }; exports.onReferralLinkDetected = onReferralLinkDetected; //# sourceMappingURL=index.js.map