Skip to content

Commit

Permalink
[MOB-8649]: move env vars to config (#398)
Browse files Browse the repository at this point in the history
* 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 <mitch.prewitt@iterable.com>
  • Loading branch information
mprew97 and mitch prewitt committed May 23, 2024
1 parent ad433f3 commit 74c1926
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 60 deletions.
168 changes: 132 additions & 36 deletions README.md

Large diffs are not rendered by default.

16 changes: 11 additions & 5 deletions 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 { initializeWithConfig, WithJWTParams } from '@iterable/web-sdk';
import axios from 'axios';
import ReactDOM from 'react-dom';
import './styles/index.css';
Expand Down Expand Up @@ -38,9 +38,13 @@ const HomeLink = styled(Link)`
`;

((): void => {
const { setEmail, logout, refreshJwtToken } = initialize(
process.env.API_KEY || '',
({ email }) => {
const initializeParams: WithJWTParams = {
authToken: process.env.API_KEY || '',
configOptions: {
isEuIterableService: false,
dangerouslyAllowJsPopups: true
},
generateJWT: ({ email }) => {
return axios
.post(
process.env.JWT_GENERATOR || 'http://localhost:5000/generate',
Expand All @@ -59,7 +63,9 @@ const HomeLink = styled(Link)`
return response.data?.token;
});
}
);
};
const { setEmail, logout, refreshJwtToken } =
initializeWithConfig(initializeParams);

ReactDOM.render(
<BrowserRouter>
Expand Down
33 changes: 32 additions & 1 deletion src/authorization/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
validateTokenTime,
isEmail
} from './utils';
import { config } from '../utils/config';
import { Options, config } from '../utils/config';

const MAX_TIMEOUT = ONE_DAY;

Expand Down Expand Up @@ -773,3 +773,34 @@ export function initialize(
}
};
}

export interface WithJWTParams {
authToken: string;
configOptions: Partial<Options>;
generateJWT: (payload: GenerateJWTPayload) => Promise<string>;
}

export interface WithoutJWTParams {
authToken: string;
configOptions: Partial<Options>;
}

export interface InitializeParams {
authToken: string;
configOptions: Partial<Options>;
generateJWT?: (payload: GenerateJWTPayload) => Promise<string>;
}

export function initializeWithConfig(initializeParams: WithJWTParams): WithJWT;

export function initializeWithConfig(
initializeParams: WithoutJWTParams
): WithoutJWT;

export function initializeWithConfig(initializeParams: InitializeParams) {
const { authToken, configOptions, generateJWT } = initializeParams;
config.setConfig(configOptions ?? {});
return generateJWT
? initialize(authToken, generateJWT)
: initialize(authToken);
}
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const ITERABLE_API_URL = `https://${
IS_EU_ITERABLE_SERVICE ? EU_ITERABLE_DOMAIN : US_ITERABLE_DOMAIN
}/api`;

export const EU_ITERABLE_API = `https://${EU_ITERABLE_DOMAIN}/api`;

// Do not set `process.env.BASE_URL` if intending on using the prod or EU APIs.
export const BASE_URL = process.env.BASE_URL || ITERABLE_API_URL;

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 'src/utils/config';

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
9 changes: 7 additions & 2 deletions src/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Axios, { AxiosRequestConfig } from 'axios';
import { BASE_URL, STATIC_HEADERS } from './constants';
import { BASE_URL, STATIC_HEADERS, EU_ITERABLE_API } from './constants';
import { IterablePromise, IterableResponse } from './types';
import { AnySchema, ValidationError } from 'yup';
import { config } from './utils/config';
Expand Down Expand Up @@ -35,9 +35,14 @@ export const baseIterableRequest = <T = any>(
abortEarly: false
});
}

const baseURL = config.getConfig('isEuIterableService')
? EU_ITERABLE_API
: config.getConfig('baseURL');

return baseAxiosRequest({
...payload,
baseURL: config.getConfig('baseURL') || BASE_URL,
baseURL,
headers: {
...payload.headers,
...STATIC_HEADERS
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 {
export 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
8 changes: 4 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ function getParsedEnv() {
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
IS_EU_ITERABLE_SERVICE: process.env.IS_EU_ITERABLE_SERVICE || false
};
}

return { VERSION: version };
return {
VERSION: version
};
}

module.exports = {
Expand Down
4 changes: 1 addition & 3 deletions webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ function getParsedEnv() {
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
IS_EU_ITERABLE_SERVICE: process.env.IS_EU_ITERABLE_SERVICE || false
};
}

Expand Down
4 changes: 1 addition & 3 deletions webpack.node.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ function getParsedEnv() {
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
IS_EU_ITERABLE_SERVICE: process.env.IS_EU_ITERABLE_SERVICE || false
};
}

Expand Down

0 comments on commit 74c1926

Please sign in to comment.