File tree Expand file tree Collapse file tree 10 files changed +204
-60
lines changed Expand file tree Collapse file tree 10 files changed +204
-60
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change 1- import { initialize } from '@iterable/web-sdk' ;
1+ import { initializeWithConfig , WithJWTParams } from '@iterable/web-sdk' ;
22import axios from 'axios' ;
33import ReactDOM from 'react-dom' ;
44import './styles/index.css' ;
@@ -38,9 +38,13 @@ const HomeLink = styled(Link)`
3838` ;
3939
4040( ( ) : void => {
41- const { setEmail, logout, refreshJwtToken } = initialize (
42- process . env . API_KEY || '' ,
43- ( { email } ) => {
41+ const initializeParams : WithJWTParams = {
42+ authToken : process . env . API_KEY || '' ,
43+ configOptions : {
44+ isEuIterableService : false ,
45+ dangerouslyAllowJsPopups : true
46+ } ,
47+ generateJWT : ( { email } ) => {
4448 return axios
4549 . post (
4650 process . env . JWT_GENERATOR || 'http://localhost:5000/generate' ,
@@ -59,7 +63,9 @@ const HomeLink = styled(Link)`
5963 return response . data ?. token ;
6064 } ) ;
6165 }
62- ) ;
66+ } ;
67+ const { setEmail, logout, refreshJwtToken } =
68+ initializeWithConfig ( initializeParams ) ;
6369
6470 ReactDOM . render (
6571 < BrowserRouter >
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ import {
1616 validateTokenTime ,
1717 isEmail
1818} from './utils' ;
19- import { config } from '../utils/config' ;
19+ import { Options , config } from '../utils/config' ;
2020
2121const MAX_TIMEOUT = ONE_DAY ;
2222
@@ -773,3 +773,34 @@ export function initialize(
773773 }
774774 } ;
775775}
776+
777+ export interface WithJWTParams {
778+ authToken : string ;
779+ configOptions : Partial < Options > ;
780+ generateJWT : ( payload : GenerateJWTPayload ) => Promise < string > ;
781+ }
782+
783+ export interface WithoutJWTParams {
784+ authToken : string ;
785+ configOptions : Partial < Options > ;
786+ }
787+
788+ export interface InitializeParams {
789+ authToken : string ;
790+ configOptions : Partial < Options > ;
791+ generateJWT ?: ( payload : GenerateJWTPayload ) => Promise < string > ;
792+ }
793+
794+ export function initializeWithConfig ( initializeParams : WithJWTParams ) : WithJWT ;
795+
796+ export function initializeWithConfig (
797+ initializeParams : WithoutJWTParams
798+ ) : WithoutJWT ;
799+
800+ export function initializeWithConfig ( initializeParams : InitializeParams ) {
801+ const { authToken, configOptions, generateJWT } = initializeParams ;
802+ config . setConfig ( configOptions ?? { } ) ;
803+ return generateJWT
804+ ? initialize ( authToken , generateJWT )
805+ : initialize ( authToken ) ;
806+ }
Original file line number Diff line number Diff line change @@ -18,6 +18,8 @@ const ITERABLE_API_URL = `https://${
1818 IS_EU_ITERABLE_SERVICE ? EU_ITERABLE_DOMAIN : US_ITERABLE_DOMAIN
1919} /api`;
2020
21+ export const EU_ITERABLE_API = `https://${ EU_ITERABLE_DOMAIN } /api` ;
22+
2123// Do not set `process.env.BASE_URL` if intending on using the prod or EU APIs.
2224export const BASE_URL = process . env . BASE_URL || ITERABLE_API_URL ;
2325
Original file line number Diff line number Diff line change 11import { by } from '@pabra/sortby' ;
22import {
33 ANIMATION_DURATION ,
4- dangerouslyAllowJsPopupExecution ,
54 DEFAULT_CLOSE_BUTTON_OFFSET_PERCENTAGE
65} from 'src/constants' ;
76import { WebInAppDisplaySettings } from 'src/inapp' ;
87import { srSpeak } from 'src/utils/srSpeak' ;
98import { trackInAppDelivery } from '../events' ;
109import { CloseButtonPosition , InAppMessage } from './types' ;
10+ import { config } from 'src/utils/config' ;
1111
1212interface Breakpoints {
1313 smMatches : boolean ;
@@ -288,7 +288,9 @@ const generateSecuredIFrame = () => {
288288 iframe . setAttribute (
289289 'sandbox' ,
290290 `allow-same-origin allow-popups allow-top-navigation ${
291- dangerouslyAllowJsPopupExecution ? 'allow-popups-to-escape-sandbox' : ''
291+ config . getConfig ( 'dangerouslyAllowJsPopups' )
292+ ? 'allow-popups-to-escape-sandbox'
293+ : ''
292294 } `
293295 ) ;
294296 /*
Original file line number Diff line number Diff line change 11import Axios , { AxiosRequestConfig } from 'axios' ;
2- import { BASE_URL , STATIC_HEADERS } from './constants' ;
2+ import { BASE_URL , STATIC_HEADERS , EU_ITERABLE_API } from './constants' ;
33import { IterablePromise , IterableResponse } from './types' ;
44import { AnySchema , ValidationError } from 'yup' ;
55import { config } from './utils/config' ;
@@ -35,9 +35,14 @@ export const baseIterableRequest = <T = any>(
3535 abortEarly : false
3636 } ) ;
3737 }
38+
39+ const baseURL = config . getConfig ( 'isEuIterableService' )
40+ ? EU_ITERABLE_API
41+ : config . getConfig ( 'baseURL' ) ;
42+
3843 return baseAxiosRequest ( {
3944 ...payload ,
40- baseURL : config . getConfig ( 'baseURL' ) || BASE_URL ,
45+ baseURL,
4146 headers : {
4247 ...payload . headers ,
4348 ...STATIC_HEADERS
Original file line number Diff line number Diff line change 11import { BASE_URL } from '../constants' ;
22
3- interface Options {
3+ export type Options = {
44 logLevel : 'none' | 'verbose' ;
55 baseURL : string ;
6- }
6+ isEuIterableService : boolean ;
7+ dangerouslyAllowJsPopups : boolean ;
8+ } ;
79
810const _config = ( ) => {
911 let options : Options = {
1012 logLevel : 'none' ,
11- baseURL : BASE_URL
13+ baseURL : BASE_URL ,
14+ isEuIterableService : false ,
15+ dangerouslyAllowJsPopups : false
1216 } ;
1317
18+ const getConfig = < K extends keyof Options > ( option : K ) => options [ option ] ;
19+
1420 return {
15- getConfig : ( option : keyof Options ) => options [ option ] ,
21+ getConfig,
1622 setConfig : ( newOptions : Partial < Options > ) => {
1723 options = {
1824 ...options ,
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ function getParsedEnv() {
88 return {
99 ...env . parsed ,
1010 VERSION : version ,
11- IS_EU_ITERABLE_SERVICE : process . env . IS_EU_ITERABLE_SERVICE || false ,
12- DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION :
13- process . env . DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION || false
11+ IS_EU_ITERABLE_SERVICE : process . env . IS_EU_ITERABLE_SERVICE || false
1412 } ;
1513 }
1614
17- return { VERSION : version } ;
15+ return {
16+ VERSION : version
17+ } ;
1818}
1919
2020module . exports = {
Original file line number Diff line number Diff line change @@ -8,9 +8,7 @@ function getParsedEnv() {
88 return {
99 ...env . parsed ,
1010 VERSION : version ,
11- IS_EU_ITERABLE_SERVICE : process . env . IS_EU_ITERABLE_SERVICE || false ,
12- DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION :
13- process . env . DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION || false
11+ IS_EU_ITERABLE_SERVICE : process . env . IS_EU_ITERABLE_SERVICE || false
1412 } ;
1513 }
1614
Original file line number Diff line number Diff line change @@ -8,9 +8,7 @@ function getParsedEnv() {
88 return {
99 ...env . parsed ,
1010 VERSION : version ,
11- IS_EU_ITERABLE_SERVICE : process . env . IS_EU_ITERABLE_SERVICE || false ,
12- DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION :
13- process . env . DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION || false
11+ IS_EU_ITERABLE_SERVICE : process . env . IS_EU_ITERABLE_SERVICE || false
1412 } ;
1513 }
1614
You can’t perform that action at this time.
0 commit comments