UNPKG

4.21 kBJavaScriptView Raw
1import { NativeModules, Platform } from 'react-native'
2import BranchEvent from './BranchEvent'
3
4const { RNBranch } = NativeModules
5
6export default async function createBranchUniversalObject(identifier, options = {}) {
7 if (typeof identifier !== 'string') throw new Error('react-native-branch: identifier must be a string')
8
9 const contentMetadata = options.contentMetadata || {}
10
11 if (contentMetadata.customMetadata) {
12 for (const key in contentMetadata.customMetadata) {
13 const valueType = typeof contentMetadata.customMetadata[key]
14 if (valueType == 'string') continue
15 console.warn('[Branch] customMetadata values must be strings. Value for property ' + key + ' has type ' + valueType + '.')
16 // TODO: throw?
17 }
18 }
19
20 const branchUniversalObject = {
21 canonicalIdentifier: identifier,
22 contentMetadata: contentMetadata,
23 ...options
24 }
25
26 // For the benefit of NSDecimalNumber on iOS.
27 const price = contentMetadata.price === undefined ? undefined : '' + contentMetadata.price
28 branchUniversalObject.contentMetadata.price = price
29
30 if (options.automaticallyListOnSpotlight !== undefined) {
31 console.info('[Branch] automaticallyListOnSpotlight is deprecated. Please use locallyIndex instead.')
32 }
33
34 if (options.price !== undefined) {
35 console.info('[Branch] price is deprecated. Please use contentMetadata.price instead.')
36 }
37
38 if (options.currency !== undefined) {
39 console.info('[Branch] currency is deprecated. Please use contentMetadata.price instead.')
40 }
41
42 if (options.metadata !== undefined) {
43 console.info('[Branch] metadata is deprecated. Please use contentMetadata.customMetadata instead.')
44 }
45
46 if (options.contentIndexingMode !== undefined) {
47 console.info('[Branch] contentIndexingMode is deprecated. Please use locallyIndex or publiclyIndex instead.')
48 }
49
50 const { ident } = await RNBranch.createUniversalObject(branchUniversalObject)
51
52 return {
53 ident: ident,
54 showShareSheet(shareOptions = {}, linkProperties = {}, controlParams = {}) {
55 shareOptions = {
56 title: options.title || '',
57 text: options.contentDescription || '',
58 ...shareOptions,
59 }
60
61 linkProperties = {
62 feature: 'share',
63 channel: 'RNApp',
64 ...linkProperties,
65 }
66
67 return this._tryFunction(RNBranch.showShareSheet, shareOptions, linkProperties, controlParams)
68 },
69 // deprecated in favor of userCompletedAction(RegisterViewEvent)
70 registerView() {
71 console.info('[Branch] registerView is deprecated. Please use logEvent(BranchEvent.ViewItem) instead.')
72 return this._tryFunction(RNBranch.registerView)
73 },
74 generateShortUrl(linkProperties = {}, controlParams = {}) {
75 return this._tryFunction(RNBranch.generateShortUrl, linkProperties, controlParams)
76 },
77 listOnSpotlight() {
78 console.info('[Branch] listOnSpotlight is deprecated. Please use locallyIndex instead.')
79 if (Platform.OS !== 'ios') return Promise.resolve()
80 return this._tryFunction(RNBranch.listOnSpotlight)
81 },
82 userCompletedAction(event, state = {}) {
83 console.info('[Branch] userCompletedAction is deprecated. Please use logEvent or the BranchEvent class instead.')
84 if (event == RNBranch.REGISTER_VIEW_EVENT) {
85 return this.logEvent(BranchEvent.ViewItem, { customData: state })
86 }
87 return this._tryFunction(RNBranch.userCompletedActionOnUniversalObject, event, state)
88 },
89 logEvent(eventName, params = {}) {
90 return new BranchEvent(eventName, this, params).logEvent()
91 },
92 release() {
93 return RNBranch.releaseUniversalObject(this.ident)
94 },
95
96 /**
97 * Used by exception handlers when RNBranch::Error::BUONotFound is caught.
98 */
99 _newIdent() {
100 return RNBranch.createUniversalObject(branchUniversalObject).then(({ident}) => {
101 this.ident = ident
102 return ident
103 })
104 },
105
106 _tryFunction(func, ...args) {
107 return func(this.ident, ...args).catch((error) => {
108 if (error.code != 'RNBranch::Error::BUONotFound') {
109 throw error
110 }
111 return this._newIdent().then((ident) => {
112 return func(ident, ...args)
113 })
114 })
115 }
116 }
117}