Skip to content

Commit 74c1926

Browse files
authored
[MOB-8649]: move env vars to config (#398)
* move env vars to config * add back env for eu * update readme * update again * another comment change * rework some * read me and tests * huh * leftover * update readme and add new wrapper method * fix test * strange * circular dep and cleanup * change requests --------- Co-authored-by: mitch prewitt <[email protected]>
1 parent ad433f3 commit 74c1926

File tree

10 files changed

+204
-60
lines changed

10 files changed

+204
-60
lines changed

README.md

+132-36
Large diffs are not rendered by default.

react-example/src/index.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { initialize } from '@iterable/web-sdk';
1+
import { initializeWithConfig, WithJWTParams } from '@iterable/web-sdk';
22
import axios from 'axios';
33
import ReactDOM from 'react-dom';
44
import './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>

src/authorization/authorization.ts

+32-1
Original file line numberDiff line numberDiff 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

2121
const 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+
}

src/constants.ts

+2
Original file line numberDiff line numberDiff 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.
2224
export const BASE_URL = process.env.BASE_URL || ITERABLE_API_URL;
2325

src/inapp/utils.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { by } from '@pabra/sortby';
22
import {
33
ANIMATION_DURATION,
4-
dangerouslyAllowJsPopupExecution,
54
DEFAULT_CLOSE_BUTTON_OFFSET_PERCENTAGE
65
} from 'src/constants';
76
import { WebInAppDisplaySettings } from 'src/inapp';
87
import { srSpeak } from 'src/utils/srSpeak';
98
import { trackInAppDelivery } from '../events';
109
import { CloseButtonPosition, InAppMessage } from './types';
10+
import { config } from 'src/utils/config';
1111

1212
interface 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
/*

src/request.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Axios, { AxiosRequestConfig } from 'axios';
2-
import { BASE_URL, STATIC_HEADERS } from './constants';
2+
import { BASE_URL, STATIC_HEADERS, EU_ITERABLE_API } from './constants';
33
import { IterablePromise, IterableResponse } from './types';
44
import { AnySchema, ValidationError } from 'yup';
55
import { 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

src/utils/config.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import { 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

810
const _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,

webpack.config.js

+4-4
Original file line numberDiff line numberDiff 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

2020
module.exports = {

webpack.dev.config.js

+1-3
Original file line numberDiff line numberDiff 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

webpack.node.config.js

+1-3
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)