godprotocol 2.3.20 → 2.3.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Godprotocol.js +5 -2
- package/Index.js +0 -10
- package/package.json +1 -1
- package/server/Routable/Header.js +18 -43
- package/server/Routable/Route_table.js +15 -2
- package/server/Routable/Services.js +32 -19
package/Godprotocol.js
CHANGED
|
@@ -48,7 +48,10 @@ class GodProtocol {
|
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
add_router = async (version, routes, opts = {}) => {
|
|
51
|
-
if (opts?.prefix)
|
|
51
|
+
if (opts?.prefix) {
|
|
52
|
+
version = `${opts.prefix}:${version}`;
|
|
53
|
+
if (!opts.db_name) opts.db_name = opts.prefix
|
|
54
|
+
}
|
|
52
55
|
|
|
53
56
|
if (typeof routes === "function" && !opts.is_old) routes = await routes();
|
|
54
57
|
|
|
@@ -58,7 +61,7 @@ class GodProtocol {
|
|
|
58
61
|
return;
|
|
59
62
|
}
|
|
60
63
|
|
|
61
|
-
await this.route_table.init_version(version);
|
|
64
|
+
await this.route_table.init_version(version, {db_name: opts.db_name, db_prefix:opts.db_prefix});
|
|
62
65
|
|
|
63
66
|
await this.route_table.load_routes(routes, opts);
|
|
64
67
|
};
|
package/Index.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
1
|
import GodProtocol from "./Godprotocol.js";
|
|
2
2
|
|
|
3
|
-
// let gp = new GodProtocol({
|
|
4
|
-
// platform_uri: "profile.savvyaisolution.com",
|
|
5
|
-
// api_key: process.env.API_KEY,
|
|
6
|
-
// db_config: {},
|
|
7
|
-
// });
|
|
8
|
-
|
|
9
|
-
// gp.add_router("v2", {});
|
|
10
|
-
// gp.load_services(services);
|
|
11
|
-
|
|
12
|
-
// app.use((req, res) => gp.on_request(req, res));
|
|
13
3
|
export default GodProtocol;
|
package/package.json
CHANGED
|
@@ -4,12 +4,15 @@ import Services, { gp_services } from "./Services.js";
|
|
|
4
4
|
class DB {
|
|
5
5
|
constructor(config, router) {
|
|
6
6
|
this.router = router;
|
|
7
|
+
|
|
8
|
+
this.db_name=config.db_name;
|
|
7
9
|
|
|
8
10
|
let conf = {
|
|
9
11
|
db_url: config.db_url,
|
|
10
12
|
db_name: config.db_name || router?.gp?.platform_uri.replace(/\./g, "-"),
|
|
11
13
|
};
|
|
12
14
|
|
|
15
|
+
|
|
13
16
|
this.db = new Mongo(conf);
|
|
14
17
|
|
|
15
18
|
this.folders = {};
|
|
@@ -132,29 +135,13 @@ class Headers extends Services {
|
|
|
132
135
|
}
|
|
133
136
|
}
|
|
134
137
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
if (authorization) {
|
|
142
|
-
headers["authorization"] = `Bearer ${authorization}`;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
let gp_profile = gp_services().identity;
|
|
146
|
-
let res = await fetch(`${gp_profile.url}/validate`, {
|
|
147
|
-
method: "post",
|
|
148
|
-
headers: {
|
|
149
|
-
"Content-Type": "application/json",
|
|
150
|
-
Accept: "application/json",
|
|
151
|
-
"x-api-version": gp_profile.api_version,
|
|
152
|
-
...headers,
|
|
153
|
-
},
|
|
154
|
-
body: JSON.stringify({}),
|
|
155
|
-
});
|
|
138
|
+
let gp_profile = await this.get_service('identity');
|
|
139
|
+
let res = await gp_profile.call('validate', {}, {
|
|
140
|
+
api_key,
|
|
141
|
+
token: authorization,
|
|
142
|
+
api_version:'v3'
|
|
143
|
+
})
|
|
156
144
|
|
|
157
|
-
res = await res.json();
|
|
158
145
|
// 🔹 3. Save cache if valid
|
|
159
146
|
if (res.ok && res.data) {
|
|
160
147
|
await db.save_cache(query, res.data, "auth");
|
|
@@ -204,30 +191,19 @@ class Headers extends Services {
|
|
|
204
191
|
hst = hst.replace("10.0.2.2", "localhost");
|
|
205
192
|
}
|
|
206
193
|
|
|
207
|
-
let gp_profile =
|
|
194
|
+
let gp_profile = await this.get_service("identity")
|
|
208
195
|
|
|
209
196
|
let third_party =
|
|
210
|
-
xplatform !== this.platform_uri
|
|
197
|
+
xplatform !== this.platform_uri //&& !gp_profile.url.endsWith(hst);
|
|
211
198
|
|
|
212
|
-
if (gp_profile.url.endsWith(hst)) {
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
let res = await fetch(
|
|
217
|
-
`${gp_profile.url}/${third_party ? "third_party_me" : "me"}`,
|
|
218
|
-
{
|
|
219
|
-
method: "post",
|
|
220
|
-
headers: {
|
|
221
|
-
"Content-Type": "application/json",
|
|
222
|
-
Accept: "application/json",
|
|
223
|
-
"x-api-version": gp_profile.api_version,
|
|
224
|
-
...headers,
|
|
225
|
-
},
|
|
226
|
-
body: JSON.stringify(third_party ? { from: xplatform } : {}),
|
|
227
|
-
},
|
|
228
|
-
);
|
|
229
|
-
res = await res.json();
|
|
199
|
+
// if (gp_profile.url.endsWith(hst)) {
|
|
200
|
+
// headers["x-platform"] = xplatform;
|
|
201
|
+
// }
|
|
230
202
|
|
|
203
|
+
let res = await gp_profile.call(`${third_party ? "third_party_me" : "me"}`, (third_party ? { from: xplatform } : {}), {
|
|
204
|
+
token: authorization, xplatform
|
|
205
|
+
})
|
|
206
|
+
|
|
231
207
|
// 🔹 3. Save cache
|
|
232
208
|
if (res.ok && res.data) {
|
|
233
209
|
await db.save_cache(query, res.data, "auth");
|
|
@@ -296,7 +272,6 @@ class Headers extends Services {
|
|
|
296
272
|
val = await this.validate_api_key(api_key, authorisation);
|
|
297
273
|
}
|
|
298
274
|
|
|
299
|
-
// debug(val);
|
|
300
275
|
if (!val?.ok) {
|
|
301
276
|
return val;
|
|
302
277
|
} else {
|
|
@@ -21,8 +21,21 @@ class Route_table extends Headers {
|
|
|
21
21
|
this.validators = {};
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
get_version = (version = this.active_version) => {
|
|
24
|
+
get_version = async (version = this.active_version) => {
|
|
25
25
|
let ver = this.versions[version];
|
|
26
|
+
|
|
27
|
+
if (!ver){
|
|
28
|
+
let versions = Object.keys(this.versions);
|
|
29
|
+
let verr= versions.find(v=>v.startsWith(`${version}:`));
|
|
30
|
+
|
|
31
|
+
if (verr){
|
|
32
|
+
let serv = await this.get_service(version);
|
|
33
|
+
if (serv){
|
|
34
|
+
version = `${version}:${serv.api_version}`
|
|
35
|
+
ver = this.versions[version];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
26
39
|
if (!ver) return;
|
|
27
40
|
|
|
28
41
|
this.active_version = version;
|
|
@@ -67,7 +80,7 @@ class Route_table extends Headers {
|
|
|
67
80
|
}
|
|
68
81
|
|
|
69
82
|
if (opts?.services) {
|
|
70
|
-
let services = await opts.
|
|
83
|
+
let services = await opts.services(opts.services_config);
|
|
71
84
|
await this.load_services(services, this.active_version);
|
|
72
85
|
}
|
|
73
86
|
};
|
|
@@ -85,34 +85,38 @@ class Services {
|
|
|
85
85
|
if (!servic) return;
|
|
86
86
|
|
|
87
87
|
return {
|
|
88
|
+
api_version: servic.api_version,
|
|
88
89
|
uri: servic.uri,
|
|
89
90
|
call: async (path, body, header) => {
|
|
90
91
|
let headers = {
|
|
91
92
|
"Content-Type": "application/json",
|
|
92
93
|
Accept: "application/json",
|
|
93
94
|
"x-api-version": servic.local
|
|
94
|
-
? `${servic.local}:${servic.api_version}`
|
|
95
|
-
: servic.api_version || "v1",
|
|
95
|
+
? `${servic.local}:${servic.api_version || header.api_version}`
|
|
96
|
+
: servic.api_version || header.api_version || "v1",
|
|
96
97
|
};
|
|
97
|
-
|
|
98
|
-
if (
|
|
98
|
+
|
|
99
|
+
if (header?.api_key){
|
|
100
|
+
headers['x-api-key'] = header.api_key;
|
|
101
|
+
} else if (servic.api_key) {
|
|
102
|
+
headers["x-api-key"] = servic.api_key;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (header?.token){
|
|
106
|
+
headers['Authorization'] = `Bearer ${header.token}`
|
|
107
|
+
headers["x-platform"] = this.platform_uri;
|
|
108
|
+
} else if (servic.profile_key) {
|
|
99
109
|
headers["Authorization"] = `${servic.profile_key}`;
|
|
100
|
-
} else if (header?.
|
|
101
|
-
|
|
102
|
-
token = header.token;
|
|
103
|
-
} else if (header.profile) {
|
|
104
|
-
token = await this.get_token(servic, header);
|
|
105
|
-
|
|
106
|
-
if (typeof token !== "string") {
|
|
107
|
-
if (token?.ok === false) return token;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
+
} else if (header?.profile) {
|
|
111
|
+
let token = await this.get_token(servic, header);
|
|
110
112
|
|
|
113
|
+
if (typeof token !== "string") {
|
|
114
|
+
if (token?.ok === false) return token;
|
|
115
|
+
}
|
|
116
|
+
|
|
111
117
|
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
112
118
|
headers["x-platform"] = this.platform_uri;
|
|
113
|
-
}
|
|
114
|
-
headers["x-api-key"] = servic.api_key;
|
|
115
|
-
}
|
|
119
|
+
}
|
|
116
120
|
|
|
117
121
|
if (
|
|
118
122
|
!headers["x-api-key"] &&
|
|
@@ -125,7 +129,10 @@ class Services {
|
|
|
125
129
|
meesage: "Service unauthorised",
|
|
126
130
|
};
|
|
127
131
|
}
|
|
132
|
+
|
|
128
133
|
try {
|
|
134
|
+
let prev_version = this.active_version;
|
|
135
|
+
|
|
129
136
|
let response = await fetch(
|
|
130
137
|
`${servic.local ? this.gp.server.domain : servic.url}/${path}`,
|
|
131
138
|
{
|
|
@@ -135,6 +142,8 @@ class Services {
|
|
|
135
142
|
},
|
|
136
143
|
);
|
|
137
144
|
|
|
145
|
+
this.get_version(prev_version)
|
|
146
|
+
|
|
138
147
|
return await response.json();
|
|
139
148
|
} catch (e) {
|
|
140
149
|
return {
|
|
@@ -165,7 +174,11 @@ class Services {
|
|
|
165
174
|
|
|
166
175
|
let db = this.dbs[name];
|
|
167
176
|
if (!db) {
|
|
168
|
-
|
|
177
|
+
let conf = {...this.db_config};
|
|
178
|
+
if (name){
|
|
179
|
+
conf = {...this.db_config, db_name: name}
|
|
180
|
+
}
|
|
181
|
+
db = new DB(conf, this);
|
|
169
182
|
|
|
170
183
|
this.dbs[name] = db;
|
|
171
184
|
}
|
|
@@ -181,7 +194,7 @@ class Services {
|
|
|
181
194
|
return res && Object.keys(res).hasOwnProperty("ok") ? res : { ok: false };
|
|
182
195
|
}
|
|
183
196
|
return {
|
|
184
|
-
ok: true,
|
|
197
|
+
ok: true,
|
|
185
198
|
};
|
|
186
199
|
};
|
|
187
200
|
|