@@ -23,9 +23,69 @@ export function getFHIRResourcePaths(patientId) {
23
23
} ) ;
24
24
}
25
25
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
+ }
30
90
31
91
export const queryPatientIdKey = 'launch_queryPatientId' ;
0 commit comments