forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.ts
More file actions
62 lines (58 loc) · 1.78 KB
/
Copy pathenvironment.ts
File metadata and controls
62 lines (58 loc) · 1.78 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
type EnvironmentVariables = {
public: Map<string, string>;
private: Map<string, string>;
};
export type EnvironmentConfig = {
get id(): string;
setId(newId: string): EnvironmentConfig;
setEnvVariable(type: keyof EnvironmentVariables, name: string, value: any): EnvironmentConfig;
get publicVariables(): EnvironmentVariables['public'];
get privateVariables(): EnvironmentVariables['private'];
toJson(): { public: Record<string, string>; private: Record<string, string> };
fromJson(json: ReturnType<EnvironmentConfig['toJson']>): EnvironmentConfig;
clone(): EnvironmentConfig;
};
export const environmentConfig = () => {
let id = '';
const envVars: EnvironmentVariables = {
public: new Map<string, string>(),
private: new Map<string, string>(),
};
const self: EnvironmentConfig = {
setId: (newId: string) => {
id = newId;
return self;
},
get id() {
return id;
},
setEnvVariable: (type, name, value) => {
envVars[type].set(name, value);
return self;
},
get publicVariables() {
return envVars.public;
},
get privateVariables() {
return envVars.private;
},
toJson: () => {
return {
public: Object.fromEntries(envVars.public),
private: Object.fromEntries(envVars.private),
};
},
fromJson: json => {
Object.entries(json.public).forEach(([k, v]) => self.setEnvVariable('public', k, v));
Object.entries(json.private).forEach(([k, v]) => self.setEnvVariable('private', k, v));
return self;
},
clone: () => {
const res = environmentConfig();
envVars.private.forEach((v, k) => res.setEnvVariable('private', k, v));
envVars.public.forEach((v, k) => res.setEnvVariable('public', k, v));
return res;
},
};
return self;
};