@@ -189,62 +189,23 @@ class Iterable {
189189 static initialize ( apiKey : string , config : IterableConfig = new IterableConfig ( ) ) {
190190 console . log ( "initialize: " + apiKey ) ;
191191
192- if ( config . urlHandler ) {
193- RNEventEmitter . addListener (
194- EventName . handleUrlCalled ,
195- ( dict ) => {
196- const url = dict [ "url" ]
197- const context = IterableActionContext . fromDict ( dict [ "context" ] )
198- if ( config . urlHandler ! ( url , context ) == false ) {
199- Linking . canOpenURL ( url )
200- . then ( canOpen => {
201- if ( canOpen ) { Linking . openURL ( url ) }
202- } )
203- . catch ( reason => { console . log ( "could not open url: " + reason ) } )
204- }
205- }
206- )
207- }
208-
209- if ( config . customActionHandler ) {
210- RNEventEmitter . addListener (
211- EventName . handleCustomActionCalled ,
212- ( dict ) => {
213- const action = IterableAction . fromDict ( dict [ "action" ] )
214- const context = IterableActionContext . fromDict ( dict [ "context" ] )
215- config . customActionHandler ! ( action , context )
216- }
217- )
218- }
192+ this . setupEventHandlers ( config )
193+ const version = this . getVersionFromPackageJson ( )
219194
220- if ( config . inAppHandler ) {
221- RNEventEmitter . addListener (
222- EventName . handleInAppCalled ,
223- ( messageDict ) => {
224- const message = IterableInAppMessage . fromDict ( messageDict )
225- const result = config . inAppHandler ! ( message )
226- RNIterableAPI . setInAppShowResponse ( result )
227- }
228- )
229- }
195+ RNIterableAPI . initializeWithApiKey ( apiKey , config . toDict ( ) , version )
196+ }
230197
231- if ( config . authHandler ) {
232- RNEventEmitter . addListener (
233- EventName . handleAuthCalled ,
234- ( ) => {
235- config . authHandler ! ( )
236- . then ( authToken => {
237- RNIterableAPI . passAlongAuthToken ( authToken )
238- } )
239- }
240- )
241- }
198+ /**
199+ * DO NOT CALL THIS METHOD.
200+ * This method is used internally to connect to staging environment.
201+ */
202+ static initialize2 ( apiKey : string , config : IterableConfig = new IterableConfig ( ) , apiEndPoint : string , linksEndPoint : string ) {
203+ console . log ( "initialize2: " + apiKey ) ;
242204
243- // Set version from package.json
244- const json = require ( '../package.json' )
245- const version = json [ "version" ] as string
205+ this . setupEventHandlers ( config )
206+ const version = this . getVersionFromPackageJson ( )
246207
247- RNIterableAPI . initializeWithApiKey ( apiKey , config . toDict ( ) , version )
208+ RNIterableAPI . initialize2WithApiKey ( apiKey , config . toDict ( ) , version , apiEndPoint , linksEndPoint )
248209 }
249210
250211 /**
@@ -436,6 +397,66 @@ class Iterable {
436397 console . log ( "updateSubscriptions" )
437398 RNIterableAPI . updateSubscriptions ( emailListIds , unsubscribedChannelIds , unsubscribedMessageTypeIds , subscribedMessageTypeIds , campaignId , templateId )
438399 }
400+
401+ // PRIVATE
402+ private static setupEventHandlers ( config : IterableConfig ) {
403+ if ( config . urlHandler ) {
404+ RNEventEmitter . addListener (
405+ EventName . handleUrlCalled ,
406+ ( dict ) => {
407+ const url = dict [ "url" ]
408+ const context = IterableActionContext . fromDict ( dict [ "context" ] )
409+ if ( config . urlHandler ! ( url , context ) == false ) {
410+ Linking . canOpenURL ( url )
411+ . then ( canOpen => {
412+ if ( canOpen ) { Linking . openURL ( url ) }
413+ } )
414+ . catch ( reason => { console . log ( "could not open url: " + reason ) } )
415+ }
416+ }
417+ )
418+ }
419+
420+ if ( config . customActionHandler ) {
421+ RNEventEmitter . addListener (
422+ EventName . handleCustomActionCalled ,
423+ ( dict ) => {
424+ const action = IterableAction . fromDict ( dict [ "action" ] )
425+ const context = IterableActionContext . fromDict ( dict [ "context" ] )
426+ config . customActionHandler ! ( action , context )
427+ }
428+ )
429+ }
430+
431+ if ( config . inAppHandler ) {
432+ RNEventEmitter . addListener (
433+ EventName . handleInAppCalled ,
434+ ( messageDict ) => {
435+ const message = IterableInAppMessage . fromDict ( messageDict )
436+ const result = config . inAppHandler ! ( message )
437+ RNIterableAPI . setInAppShowResponse ( result )
438+ }
439+ )
440+ }
441+
442+ if ( config . authHandler ) {
443+ RNEventEmitter . addListener (
444+ EventName . handleAuthCalled ,
445+ ( ) => {
446+ config . authHandler ! ( )
447+ . then ( authToken => {
448+ RNIterableAPI . passAlongAuthToken ( authToken )
449+ } )
450+ }
451+ )
452+ }
453+ }
454+
455+ private static getVersionFromPackageJson ( ) : string {
456+ const json = require ( '../package.json' )
457+ const version = json [ "version" ] as string
458+ return version
459+ }
439460}
440461
441462export {
0 commit comments