Skip to content

Commit 5d81f46

Browse files
Amy ChenAmy Chen
Amy Chen
authored and
Amy Chen
committed
read env variables from env.json if there
1 parent 0e08861 commit 5d81f46

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

public/env.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"test": "HELLO"
3+
}

src/FhirClientProvider.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import FHIR from "fhirclient";
33
import Stack from "@mui/material/Stack";
44
import CircularProgress from "@mui/material/CircularProgress";
55
import { FhirClientContext } from "./FhirClientContext";
6-
import { queryPatientIdKey } from "./util/util";
6+
import { fetchEnvData, getEnvs, queryPatientIdKey } from "./util/util";
77
import ErrorComponent from "./components/ErrorComponent";
88

99
export default function FhirClientProvider(props) {
@@ -27,6 +27,10 @@ export default function FhirClientProvider(props) {
2727
});
2828
}, [client]);
2929

30+
useEffect(() => {
31+
fetchEnvData();
32+
console.log("environment variables ", getEnvs());
33+
}, []);
3034
useEffect(() => {
3135
FHIR.oauth2.ready().then(
3236
(client) => {

src/components/Launch.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import FHIR from "fhirclient";
33
import Stack from "@mui/material/Stack";
44
import CircularProgress from "@mui/material/CircularProgress";
55
import ErrorComponent from "./ErrorComponent";
6-
import { getEnv, queryPatientIdKey } from "../util/util.js";
6+
import { fetchEnvData, getEnv, queryPatientIdKey } from "../util/util.js";
77
import "../style/App.scss";
88

99
export default function Launch() {
1010
const [error, setError] = React.useState("");
1111

12+
React.useEffect(() => fetchEnvData(), []);
13+
1214
React.useEffect(() => {
1315
let authURL = "launch-context.json";
1416
const backendURL = getEnv("REACT_APP_BACKEND_URL");

src/util/util.js

+64-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,69 @@ export function getFHIRResourcePaths(patientId) {
2323
});
2424
}
2525

26-
export const getEnv = (key) => {
27-
if (!process || !process.env) return "";
28-
return process.env[key];
29-
};
26+
export function fetchEnvData() {
27+
if (window["appConfig"] && Object.keys(window["appConfig"]).length) {
28+
console.log("Window config variables added. ");
29+
return;
30+
}
31+
const setConfig = function () {
32+
if (!xhr.readyState === xhr.DONE) {
33+
return;
34+
}
35+
if (xhr.status !== 200) {
36+
console.log("Request failed! ");
37+
return;
38+
}
39+
var envObj = JSON.parse(xhr.responseText);
40+
window["appConfig"] = {};
41+
//assign window process env variables for access by app
42+
//won't be overridden when Node initializing env variables
43+
for (var key in envObj) {
44+
if (!window["appConfig"][key]) {
45+
window["appConfig"][key] = envObj[key];
46+
}
47+
}
48+
};
49+
var xhr = new XMLHttpRequest();
50+
xhr.open("GET", "/env.json", false);
51+
xhr.onreadystatechange = function () {
52+
//in the event of a communication error (such as the server going down),
53+
//or error happens when parsing data
54+
//an exception will be thrown in the onreadystatechange method when accessing the response properties, e.g. status.
55+
try {
56+
setConfig();
57+
} catch (e) {
58+
console.log("Caught exception " + e);
59+
}
60+
};
61+
try {
62+
xhr.send();
63+
} catch (e) {
64+
console.log("Request failed to send. Error: ", e);
65+
}
66+
xhr.ontimeout = function (e) {
67+
// XMLHttpRequest timed out.
68+
console.log("request to fetch env.json file timed out ", e);
69+
};
70+
}
71+
72+
export function getEnv(key) {
73+
//window application global variables
74+
if (window["appConfig"] && window["appConfig"][key])
75+
return window["appConfig"][key];
76+
const envDefined = typeof process !== "undefined" && process.env;
77+
//enviroment variables as defined in Node
78+
if (envDefined && process.env[key]) return process.env[key];
79+
return "";
80+
}
81+
82+
export function getEnvs() {
83+
const appConfig = window["appConfig"] ? window["appConfig"] : {};
84+
const processEnvs = process.env ? process.env : {};
85+
return {
86+
...appConfig,
87+
...processEnvs,
88+
};
89+
}
3090

3191
export const queryPatientIdKey = 'launch_queryPatientId';

0 commit comments

Comments
 (0)