@sera4/essentia 1.1.5 → 1.1.8
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/index.js +2 -13
- package/package.json +3 -1
- package/package.tar.gz +0 -0
- package/queue/publisher.js +1 -0
- package/safe_proxy/index.js +62 -0
- package/utils/index.js +1 -3
package/index.js
CHANGED
|
@@ -8,16 +8,5 @@ export { default as halDecorator } from "./hal/index.js";
|
|
|
8
8
|
export { default as formatter } from "./formatter/index.js";
|
|
9
9
|
export { HealthCheck as healthCheck } from "./health/index.js";
|
|
10
10
|
export { default as queue } from "./queue/index.js";
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
//export default {
|
|
14
|
-
// paginator : require("./paginator/s4-pagination"),
|
|
15
|
-
// sqlPaginator : require("./paginator/sql-pagination"),
|
|
16
|
-
// logger : require("./logger/s4-logger"),
|
|
17
|
-
// lastCommit : require("./last_commit"),
|
|
18
|
-
// halDecorator : require("./hal"),
|
|
19
|
-
// formatter : require("./formatter"),
|
|
20
|
-
// healthCheck : require("./health"),
|
|
21
|
-
// queue : require("./queue"),
|
|
22
|
-
// utils : require("./utils")
|
|
23
|
-
//}
|
|
11
|
+
export * from "./utils/index.js";
|
|
12
|
+
export * from "./safe_proxy/index.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sera4/essentia",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.8",
|
|
4
4
|
"description": "A library of utilities for Teleporte Web Services",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -30,8 +30,10 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"amqplib": "^0.7.1",
|
|
32
32
|
"async": "^2.6.3",
|
|
33
|
+
"axios": "^0.21.1",
|
|
33
34
|
"fast-safe-stringify": "^2.0.7",
|
|
34
35
|
"git-rev": "^0.2.1",
|
|
36
|
+
"lodash": "^4.17.21",
|
|
35
37
|
"url-parse": "^1.5.1",
|
|
36
38
|
"uuid": "^3.4.0",
|
|
37
39
|
"winston": "^3.3.3",
|
package/package.tar.gz
CHANGED
|
Binary file
|
package/queue/publisher.js
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
import lodash from 'lodash';
|
|
5
|
+
const { omitBy, isNil } = lodash;
|
|
6
|
+
|
|
7
|
+
/* A class which safely wraps an axios connection in order to pass the current user through to
|
|
8
|
+
* the next service. Useful when one of our microservices requires auth as either an admin (sera4tal)
|
|
9
|
+
* or as the end user (example: DELETE /auth/sessions/:id
|
|
10
|
+
*
|
|
11
|
+
* similar to proxy however this isn't a proxy, it's a full out new call that we can manage
|
|
12
|
+
*
|
|
13
|
+
* each method requires a res.locals from a pre-authd session
|
|
14
|
+
*/
|
|
15
|
+
class SafeProxy {
|
|
16
|
+
constructor() {
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// a safe post request
|
|
20
|
+
// locals = res.locals - your authentication details
|
|
21
|
+
post(locals, url, options = { headers: {}, data: {}}) {
|
|
22
|
+
let { account, tenant, membership } = locals;
|
|
23
|
+
return this._proxyRequest({ account, tenant, membership }, url, "post", options);
|
|
24
|
+
}
|
|
25
|
+
delete(locals, url, options = { headers: {}, data: {}}) {
|
|
26
|
+
let { account, tenant, membership } = locals;
|
|
27
|
+
return this._proxyRequest({ account, tenant, membership }, url, "delete", options);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/* private methods */
|
|
31
|
+
// supports get, delete, post
|
|
32
|
+
// options includes { headers, data } for post / delete payloads
|
|
33
|
+
async _proxyRequest(locals, url, method = "get", options) {
|
|
34
|
+
let request;
|
|
35
|
+
let membership = locals.membership ? JSON.stringify(locals.membership) : null;
|
|
36
|
+
let headers = omitBy({ ...options.headers,
|
|
37
|
+
"tws-account-id": locals.account?.id,
|
|
38
|
+
"tws-tenant-id": locals.tenant?.id,
|
|
39
|
+
"tws-membership-id": locals.membership?.id,
|
|
40
|
+
"tws-membership": membership
|
|
41
|
+
}, isNil);
|
|
42
|
+
|
|
43
|
+
switch(method) {
|
|
44
|
+
case "post":
|
|
45
|
+
request = axios.post(url, { ...options.data }, { headers })
|
|
46
|
+
break;
|
|
47
|
+
case "delete":
|
|
48
|
+
request = axios.delete(url, { ...options.data, headers } )
|
|
49
|
+
break;
|
|
50
|
+
case "get":
|
|
51
|
+
request = axios.get(url, { headers, data } )
|
|
52
|
+
break;
|
|
53
|
+
default:
|
|
54
|
+
throw new Error("uknown type called in proxyRequest");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return request;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const safeProxy = new SafeProxy();
|