Skip to content

Commit

Permalink
move env vars to config
Browse files Browse the repository at this point in the history
  • Loading branch information
mitch prewitt committed May 23, 2024
1 parent ad433f3 commit 1f37c88
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 26 deletions.
6 changes: 5 additions & 1 deletion react-example/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initialize } from '@iterable/web-sdk';
import { initialize, config } from '@iterable/web-sdk';
import axios from 'axios';
import ReactDOM from 'react-dom';
import './styles/index.css';
Expand Down Expand Up @@ -38,6 +38,10 @@ const HomeLink = styled(Link)`
`;

((): void => {
config.setConfig({
isEuIterableService: false,
dangerouslyAllowJsPopups: true
});
const { setEmail, logout, refreshJwtToken } = initialize(
process.env.API_KEY || '',
({ email }) => {
Expand Down
12 changes: 5 additions & 7 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import config from './utils/config';

/* number of MS to wait between in-app messages to show the next one */
export const DISPLAY_INTERVAL_DEFAULT = 30000;

/* how many times we try to create a new user when _setUserID_ is invoked */
export const RETRY_USER_ATTEMPTS = 0;

const IS_EU_ITERABLE_SERVICE =
process.env.IS_EU_ITERABLE_SERVICE === 'true' ? true : false;

export const dangerouslyAllowJsPopupExecution =
process.env.DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION === 'true' ? true : false;

const US_ITERABLE_DOMAIN = 'api.iterable.com';

const EU_ITERABLE_DOMAIN = 'api.eu.iterable.com';

const ITERABLE_API_URL = `https://${
IS_EU_ITERABLE_SERVICE ? EU_ITERABLE_DOMAIN : US_ITERABLE_DOMAIN
config.getConfig('isEuIterableService')
? EU_ITERABLE_DOMAIN
: US_ITERABLE_DOMAIN
}/api`;

// Do not set `process.env.BASE_URL` if intending on using the prod or EU APIs.
Expand Down
6 changes: 4 additions & 2 deletions src/inapp/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { by } from '@pabra/sortby';
import {
ANIMATION_DURATION,
dangerouslyAllowJsPopupExecution,
DEFAULT_CLOSE_BUTTON_OFFSET_PERCENTAGE
} from 'src/constants';
import { WebInAppDisplaySettings } from 'src/inapp';
import { srSpeak } from 'src/utils/srSpeak';
import { trackInAppDelivery } from '../events';
import { CloseButtonPosition, InAppMessage } from './types';
import { config } from '..';

interface Breakpoints {
smMatches: boolean;
Expand Down Expand Up @@ -288,7 +288,9 @@ const generateSecuredIFrame = () => {
iframe.setAttribute(
'sandbox',
`allow-same-origin allow-popups allow-top-navigation ${
dangerouslyAllowJsPopupExecution ? 'allow-popups-to-escape-sandbox' : ''
config.getConfig('dangerouslyAllowJsPopups')
? 'allow-popups-to-escape-sandbox'
: ''
}`
);
/*
Expand Down
1 change: 1 addition & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const baseIterableRequest = <T = any>(
abortEarly: false
});
}

return baseAxiosRequest({
...payload,
baseURL: config.getConfig('baseURL') || BASE_URL,
Expand Down
14 changes: 10 additions & 4 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { BASE_URL } from '../constants';

interface Options {
type Options = {
logLevel: 'none' | 'verbose';
baseURL: string;
}
isEuIterableService: boolean;
dangerouslyAllowJsPopups: boolean;
};

const _config = () => {
let options: Options = {
logLevel: 'none',
baseURL: BASE_URL
baseURL: BASE_URL,
isEuIterableService: false,
dangerouslyAllowJsPopups: false
};

const getConfig = <K extends keyof Options>(option: K) => options[option];

return {
getConfig: (option: keyof Options) => options[option],
getConfig,
setConfig: (newOptions: Partial<Options>) => {
options = {
...options,
Expand Down
5 changes: 1 addition & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ function getParsedEnv() {
if (!env.error) {
return {
...env.parsed,
VERSION: version,
IS_EU_ITERABLE_SERVICE: process.env.IS_EU_ITERABLE_SERVICE || false,
DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION:
process.env.DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION || false
VERSION: version
};
}

Expand Down
5 changes: 1 addition & 4 deletions webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ function getParsedEnv() {
if (!env.error) {
return {
...env.parsed,
VERSION: version,
IS_EU_ITERABLE_SERVICE: process.env.IS_EU_ITERABLE_SERVICE || false,
DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION:
process.env.DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION || false
VERSION: version
};
}

Expand Down
5 changes: 1 addition & 4 deletions webpack.node.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ function getParsedEnv() {
if (!env.error) {
return {
...env.parsed,
VERSION: version,
IS_EU_ITERABLE_SERVICE: process.env.IS_EU_ITERABLE_SERVICE || false,
DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION:
process.env.DANGEROUSLY_ALLOW_JS_POPUP_EXECUTION || false
VERSION: version
};
}

Expand Down

0 comments on commit 1f37c88

Please sign in to comment.