godprotocol 2.2.51 → 2.2.55
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/package.json +1 -1
- package/server/Routable/Header.js +18 -15
- package/server/Routable/Services.js +8 -192
package/package.json
CHANGED
|
@@ -146,7 +146,7 @@ class Headers extends Services {
|
|
|
146
146
|
type: "third_party",
|
|
147
147
|
xplatform,
|
|
148
148
|
authorization: authorization || null,
|
|
149
|
-
platform:
|
|
149
|
+
platform: this.platform_uri,
|
|
150
150
|
};
|
|
151
151
|
|
|
152
152
|
// 🔹 1. Try cache
|
|
@@ -165,24 +165,28 @@ class Headers extends Services {
|
|
|
165
165
|
|
|
166
166
|
// 🔹 2. Call remote
|
|
167
167
|
const headers = {
|
|
168
|
-
"x-api-version": "
|
|
169
|
-
"x-
|
|
168
|
+
"x-api-version": "v3",
|
|
169
|
+
"x-platform": this.platform_uri,
|
|
170
|
+
"x-api-key": this.api_key,
|
|
170
171
|
};
|
|
171
172
|
|
|
172
173
|
if (authorization) {
|
|
173
174
|
headers["authorization"] = `Bearer ${authorization}`;
|
|
174
175
|
}
|
|
175
176
|
|
|
176
|
-
let res = await fetch(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
177
|
+
let res = await fetch(
|
|
178
|
+
`${gp_services.profile}/${xplatform === this.platform_uri ? "me" : "third_party_me"}`,
|
|
179
|
+
{
|
|
180
|
+
method: "post",
|
|
181
|
+
headers: {
|
|
182
|
+
"Content-Type": "application/json",
|
|
183
|
+
Accept: "application/json",
|
|
184
|
+
"x-api-version": "v3",
|
|
185
|
+
...headers,
|
|
186
|
+
},
|
|
187
|
+
body: JSON.stringify({ from: xplatform }),
|
|
183
188
|
},
|
|
184
|
-
|
|
185
|
-
});
|
|
189
|
+
);
|
|
186
190
|
res = await res.json();
|
|
187
191
|
|
|
188
192
|
// 🔹 3. Save cache
|
|
@@ -231,9 +235,7 @@ class Headers extends Services {
|
|
|
231
235
|
if (xplatform) {
|
|
232
236
|
val = await this.validate_third_party(xplatform, authorisation);
|
|
233
237
|
} else {
|
|
234
|
-
// debug(api_key, authorisation);
|
|
235
238
|
val = await this.validate_api_key(api_key, authorisation);
|
|
236
|
-
// debug(val, "Uhh");
|
|
237
239
|
}
|
|
238
240
|
|
|
239
241
|
// debug(val);
|
|
@@ -249,8 +251,9 @@ class Headers extends Services {
|
|
|
249
251
|
};
|
|
250
252
|
}
|
|
251
253
|
request.headers.profile = val.profile;
|
|
252
|
-
request.headers.xplatform = val.xplatform;
|
|
253
254
|
request.headers.platform = val.platform;
|
|
255
|
+
if (val.xplatform) request.headers.xplatform = val.xplatform;
|
|
256
|
+
if (val.third_party) request.headers.third_party = val.third_party;
|
|
254
257
|
}
|
|
255
258
|
|
|
256
259
|
return true;
|
|
@@ -27,7 +27,7 @@ class Services {
|
|
|
27
27
|
// debug(service, id, "gettok");
|
|
28
28
|
let db = await this.platform_db();
|
|
29
29
|
token = await db.read_cache(
|
|
30
|
-
{ platform_uri: service.uri, id: id.profile
|
|
30
|
+
{ platform_uri: service.uri, id: id.profile },
|
|
31
31
|
"token",
|
|
32
32
|
);
|
|
33
33
|
|
|
@@ -53,7 +53,7 @@ class Services {
|
|
|
53
53
|
|
|
54
54
|
if (token?.ok) {
|
|
55
55
|
await db.save_cache(
|
|
56
|
-
{ id: id.profile
|
|
56
|
+
{ id: id.profile, platform_uri: service.uri },
|
|
57
57
|
{ token: token.token },
|
|
58
58
|
"token",
|
|
59
59
|
);
|
|
@@ -93,7 +93,7 @@ class Services {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
|
-
if (
|
|
96
|
+
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
97
97
|
headers["x-platform"] = this.platform_uri;
|
|
98
98
|
|
|
99
99
|
try {
|
|
@@ -122,201 +122,17 @@ class Services {
|
|
|
122
122
|
handle_webhook_prior = async (req) => {
|
|
123
123
|
let { platform } = req.headers;
|
|
124
124
|
if (!platform) return false;
|
|
125
|
-
|
|
126
|
-
let conf = await this.get_setting(
|
|
127
|
-
platform,
|
|
128
|
-
{
|
|
129
|
-
category: "webhooks",
|
|
130
|
-
key: ["general", this.platform_uri],
|
|
131
|
-
},
|
|
132
|
-
{
|
|
133
|
-
type: "webhooks",
|
|
134
|
-
key: platform.uri,
|
|
135
|
-
platform: this.platform_uri,
|
|
136
|
-
},
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
if (conf.ok === false) return null;
|
|
140
|
-
|
|
141
|
-
let response = await fetch(conf?.data?.url, {
|
|
142
|
-
method: "POST",
|
|
143
|
-
headers: {
|
|
144
|
-
"Content-Type": "application/json",
|
|
145
|
-
Accept: "application/json",
|
|
146
|
-
"x-platform": req.headers["x-platform"],
|
|
147
|
-
"is-webhook": true,
|
|
148
|
-
authorization: req.headers["authorization"],
|
|
149
|
-
"x-api-version": conf?.data?.api_version || "v1",
|
|
150
|
-
},
|
|
151
|
-
body: JSON.stringify({
|
|
152
|
-
body: req.body,
|
|
153
|
-
from_platform: this.platform_uri,
|
|
154
|
-
endpoint: req.path,
|
|
155
|
-
server: this.gp.server,
|
|
156
|
-
}),
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
let res = await response.json();
|
|
160
|
-
|
|
161
|
-
return res;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
handle_webhook_after = async (req, result) => {
|
|
165
|
-
let { platform } = req.headers;
|
|
166
|
-
if (!platform) return false;
|
|
167
|
-
|
|
168
|
-
let conf = await this.get_setting(
|
|
169
|
-
platform,
|
|
170
|
-
{
|
|
171
|
-
category: "webhooks",
|
|
172
|
-
key: ["general", this.platform_uri],
|
|
173
|
-
},
|
|
174
|
-
{
|
|
175
|
-
type: "webhooks",
|
|
176
|
-
key: platform.uri,
|
|
177
|
-
platform: this.platform_uri,
|
|
178
|
-
},
|
|
179
|
-
);
|
|
180
|
-
|
|
181
|
-
if (conf.ok === false) return null;
|
|
182
|
-
|
|
183
|
-
let response = await fetch(conf?.data?.url, {
|
|
184
|
-
method: "POST",
|
|
185
|
-
headers: {
|
|
186
|
-
"Content-Type": "application/json",
|
|
187
|
-
Accept: "application/json",
|
|
188
|
-
"x-platform": req.headers["x-platform"],
|
|
189
|
-
"is-webhook": true,
|
|
190
|
-
authorization: req.headers["authorization"],
|
|
191
|
-
"x-api-version": conf?.data?.api_version || "v1",
|
|
192
|
-
},
|
|
193
|
-
body: JSON.stringify({
|
|
194
|
-
response: result,
|
|
195
|
-
body: req.body,
|
|
196
|
-
from_platform: this.platform_uri,
|
|
197
|
-
endpoint: req.path,
|
|
198
|
-
server: this.gp.server,
|
|
199
|
-
}),
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
let res = await response.json();
|
|
203
|
-
|
|
204
|
-
return res;
|
|
205
125
|
};
|
|
206
126
|
|
|
207
|
-
|
|
208
|
-
const baseDB = await this.platform_db();
|
|
209
|
-
|
|
210
|
-
// 🔹 1. Try cache
|
|
211
|
-
const cached = await baseDB.read_cache(query, "config");
|
|
212
|
-
|
|
213
|
-
let config;
|
|
214
|
-
|
|
215
|
-
if (cached?.payload) {
|
|
216
|
-
try {
|
|
217
|
-
config = JSON.parse(cached.payload);
|
|
218
|
-
} catch {
|
|
219
|
-
config = null; // corrupted cache → ignore
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (config) return config;
|
|
224
|
-
|
|
225
|
-
const tokenRes = await this.get_token(
|
|
226
|
-
{ uri: "settings.savvyaisolution.com" },
|
|
227
|
-
{ platform: platform._id },
|
|
228
|
-
);
|
|
229
|
-
|
|
230
|
-
if (!tokenRes.token) {
|
|
231
|
-
return {
|
|
232
|
-
ok: false,
|
|
233
|
-
message: "Failed to retrieve platform token",
|
|
234
|
-
status: 403,
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
let ftch = await fetch(`${gp_services.settings}/get_settings`, {
|
|
239
|
-
method: "POST",
|
|
240
|
-
headers: {
|
|
241
|
-
Authorization: `Bearer ${tokenRes.token}`,
|
|
242
|
-
"x-platform": this.platform_uri,
|
|
243
|
-
"x-api-version": "v1",
|
|
244
|
-
Accept: "application/json",
|
|
245
|
-
"Content-Type": "application/json",
|
|
246
|
-
},
|
|
247
|
-
body: JSON.stringify(setting),
|
|
248
|
-
});
|
|
249
|
-
let Res = await ftch.json();
|
|
250
|
-
|
|
251
|
-
if (!Res.ok) {
|
|
252
|
-
return {
|
|
253
|
-
ok: false,
|
|
254
|
-
message: "Failed to retrieve setting",
|
|
255
|
-
status: 500,
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
config =
|
|
260
|
-
Res.data?.[setting.category]?.[`${this.platform_uri}`] ||
|
|
261
|
-
Res.data?.[setting.category]?.["general"];
|
|
262
|
-
|
|
263
|
-
if (!config) {
|
|
264
|
-
return {
|
|
265
|
-
ok: false,
|
|
266
|
-
message: "setting not found",
|
|
267
|
-
status: 404,
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
// 🔹 3. Save cache
|
|
272
|
-
await baseDB.save_cache(query, config, "config");
|
|
273
|
-
|
|
274
|
-
return config;
|
|
275
|
-
};
|
|
127
|
+
handle_webhook_after = async (req, result) => {};
|
|
276
128
|
|
|
277
129
|
resolve_db = async (req) => {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
// debug(req.headers["x-api-key"], process.env.API_KEY);
|
|
281
|
-
// 🔹 Internal system call → use base DB
|
|
282
|
-
if (req.headers["x-api-key"] === process.env.API_KEY) {
|
|
283
|
-
const db = await this.platform_db();
|
|
284
|
-
req.db = db;
|
|
285
|
-
return { ok: true, db };
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
if (!platform?.uri || !platform?._id) {
|
|
289
|
-
return {
|
|
290
|
-
ok: false,
|
|
291
|
-
message: "Platform context missing",
|
|
292
|
-
status: 400,
|
|
293
|
-
};
|
|
294
|
-
}
|
|
130
|
+
let db = await this.platform_db();
|
|
295
131
|
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
platform: this.platform_uri,
|
|
132
|
+
return {
|
|
133
|
+
ok: true,
|
|
134
|
+
db,
|
|
300
135
|
};
|
|
301
|
-
|
|
302
|
-
// 🔹 2. Fetch remote if no valid cache
|
|
303
|
-
|
|
304
|
-
let config = await this.get_setting(
|
|
305
|
-
platform,
|
|
306
|
-
{
|
|
307
|
-
category: "db",
|
|
308
|
-
key: ["general", this.platform_uri],
|
|
309
|
-
},
|
|
310
|
-
query,
|
|
311
|
-
);
|
|
312
|
-
|
|
313
|
-
if (config?.ok === false) return config;
|
|
314
|
-
// 🔹 4. Instantiate scoped DB
|
|
315
|
-
const db = new DB(config, this);
|
|
316
|
-
|
|
317
|
-
req.db = db;
|
|
318
|
-
|
|
319
|
-
return { ok: true, db };
|
|
320
136
|
};
|
|
321
137
|
}
|
|
322
138
|
|