-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigService.js
More file actions
112 lines (94 loc) · 3.35 KB
/
ConfigService.js
File metadata and controls
112 lines (94 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import ls from 'local-storage'
/**
* Encapsulation of both dotenv (.env) configuration settings and local storage.
*/
var ConfigService = (function()
{
var instance;
var togglKeys;
/**
* Dotenv is a zero-dependency module that loads environment
* variables from a '.env' file into process.env. Storing configuration
* in the environment separate from code is based
* on The Twelve-Factor App methodology.
* https://www.npmjs.com/package/dotenv
*/
const readDotenvKeys = () => {
var keys = {
apiKey: process.env.REACT_APP_TOGGL_API_TOKEN,
showDebugOption:
(process.env.REACT_APP_SHOW_DEBUG_OPTION)
? true: false,
};
//console.log(keys);
return keys;
};
function createSingleton() {
togglKeys = readDotenvKeys();
return {
/**
* Retrieves constants from dotenv '.env' file.
* @param {string} defaultProjectId The string representation of an id.
* @return {object} Containing several properties.
*/
getTogglKeys: function() {
return togglKeys;
},
/**
* Retrieves values from local storage.
* @param {string} defaultProjectId The string representation of a project id.
* @return {object} Containing several properties.
*/
getLocalStorageDefaultValues: function() {
let result=this.createLocalStorageDefaultValues(
ls.get("defaultProjectId") || "",
ls.get("defaultWorkspaceId") || "",
ls.get("debugMode") || ""
);
/*Override debugMode if .env says not
to show debug option at all.*/
if(!this.getTogglKeys().showDebugOption){
result.debugMode=false;
}
return result;
},
/**
* Saves values to local storage.
* @param {string} values Containing several properties.
*/
setLocalStorageDefaultValues: function(values){
//console.log(defaultProjectId);
ls.set("defaultProjectId",values.defaultProjectId);
ls.set("defaultWorkspaceId",values.defaultWorkspaceId);
ls.set("debugMode",values.debugMode);
},
/**
* Creates a new default values object.
* @return {object} Containing several properties.
*/
createLocalStorageDefaultValues(
projectId,
workspaceId,
debugMode){
return {
defaultProjectId: projectId,
defaultWorkspaceId: workspaceId,
debugMode: debugMode,
getUserAgent: () => {return `togglStat_user@gmail.com`;}
};
},
}
}
return {
/**
* @return {object} The single instance of this service.
*/
getSingleton: function () {
if (!instance) {
instance = createSingleton();
}
return instance;
}
};
})();
export default ConfigService;