-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.js
130 lines (106 loc) · 3.03 KB
/
config.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
const defaultConfig = {
base: {
endpoint: null,
cookieSource: null,
link: {
credentials: 'same-origin'
}
},
auth: {
tokenType: 'Bearer',
defaultToken: null,
login: {
variables: null,
updateStore: null,
update: async (apolloClient, data, updateStore) => {
await apolloClient.resetStore();
if (updateStore) await apolloClient.writeQuery(updateStore(data));
},
next: null,
authToken: null
},
logout: {
updateStore: null,
update: async (apolloClient, data, updateStore) => {
await apolloClient.resetStore();
if (updateStore) await apolloClient.writeQuery(updateStore(data));
},
next: null
}
},
guards: []
};
class Config {
constructor() {
this.config = null;
this.reset();
}
reset() {
this.config = defaultConfig;
}
static assign(config1, config2) {
if (config1 && config1.base && config2 && config2.base) {
config1.base.link = Object.assign(config1.base.link || {}, config2.base.link || {});
delete config2.link;
}
if (config1 && config1.auth && config2 && config2.auth) {
config1.auth.login = Object.assign(config1.auth.login || {}, config2.auth.login || {});
delete config2.auth.login;
config1.auth.logout = Object.assign(config1.auth.logout || {}, config2.auth.logout || {});
delete config2.auth.logout;
}
if (config1 && config1.auth && config1.auth.guards && config2 && config2.auth && config2.auth.guards) {
config1.auth.guards.forEach((guard1, index1) => {
config2.auth.guards.forEach((guard2, index2) => {
if (guard1.name === guard2.name) {
config1.auth.guards[index1] = Object.assign(guard1, guard2);
delete config2.auth.guards[index2];
}
});
});
if (config2.auth.guards.length) {
config2.auth.guards.forEach(guard => {
if (guard.name) config1.auth.guards.push(guard || []);
});
}
delete config2.auth.guards;
}
if (config1 && config2) {
config1.base = Object.assign(config1.base || {}, config2.base || {});
delete config2.base;
config1.auth = Object.assign(config1.auth || {}, config2.auth || {});
delete config2.auth;
}
config1 = Object.assign(config1, config2);
return config1;
}
add(config) {
this.config = Config.assign(this.config, { base: config });
}
addAuth(config) {
this.config = Config.assign(this.config, { auth: config });
}
addGuard(config) {
this.config = Config.assign(this.config, { guards: [config] });
}
addGuards(config) {
this.config = Config.assign(this.config, { guards: config });
}
get() {
return this.config.base;
}
getAuth() {
return this.config.auth;
}
getGuard(name) {
return this.config.guards.find(guard => name === guard.name);
}
getGuards() {
return this.config.guards;
}
}
exports.default = new Config();