sparse-intern-71089
11/29/2020, 3:24 PMeager-lion-7694
11/29/2020, 4:04 PMtall-rose-87315
11/30/2020, 1:08 AM// index.html checked in, with replaceable params
// - (when developer runs locally, these are not replaced, so they look like this)
<meta property="env:API_HOST" content="${API_HOST}">
<meta property="env:API_PORT" content="${API_PORT}">
<meta property="env:PAYMENTS_KEY_PUBLIC" content="${PAYMENTS_KEY_PUBLIC}">
// static index.html saved to blob storage with ENV values
<meta property="env:API_HOST" content="<https://someapi.com>">
<meta property="env:API_PORT" content="3000">
<meta property="env:PAYMENTS_KEY_PUBLIC" content="Z8JIkfmNSYsPwQIxW7Hf00VBLhHjMq">
// example configuration object
const config: {
host?: string;
port?: string;
paymentsKeyPublic?: string;
} = {
host: undefined,
port: undefined,
paymentsKeyPublic: undefined
};
// map of ENV vars to config
const environmentConfigMap = new Map<string, string>([
['apiHost', 'API_HOST'],
['apiPort', 'API_PORT'],
['paymentsKeyPublic', 'PAYMENTS_KEY_PUBLIC'],
]);
// call load config
loadConfig(config, environmentConfigMap);
export const loadConfig = <TKeys extends { [key: string]: string }>(
config: { [key in keyof TKeys]: string },
map: Map<string, string>,
document: Document = window.document
) => {
Object.keys(config).forEach(key => {
const envKey = map.get(key);
if (envKey) {
const value = getMetaEnvironmentVariable(envKey, config[key], document);
config[key as keyof TKeys] = value;
}
});
};
export const getMetaEnvironmentVariable = (
name: string,
fallback: string,
document: Document
) => {
`const selector = document.querySelector(meta[property="env:${name}"]
);`
if (selector) {
const metaValue = selector.getAttribute('content');
`if (metaValue && metaValue !== $\{${name}}
) {`
return metaValue;
}
}
const reactValue =
`process.env[REACT_APP_CONFIG_${name}
] || process.env[name] || undefined;`
return reactValue?.trim() ?? fallback;
};
tall-rose-87315
11/30/2020, 1:12 AMeager-lion-7694
11/30/2020, 4:24 AMeager-lion-7694
11/30/2020, 4:27 AM