@sera4/essentia 1.0.32 → 1.0.34

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 CHANGED
@@ -9,5 +9,6 @@ module.exports = {
9
9
  formatter : require("./formatter"),
10
10
  healthCheck : require("./health"),
11
11
  queue : require("./queue"),
12
- utils : require("./utils")
12
+ utils : require("./utils"),
13
+ safeProxy : require("./safe_proxy")
13
14
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.0.32",
3
+ "version": "1.0.34",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -27,11 +27,13 @@
27
27
  "sinon": "^5.0.1"
28
28
  },
29
29
  "dependencies": {
30
- "amqplib": "^0.5.5",
30
+ "amqplib": "^0.8.0",
31
31
  "async": "^2.6.1",
32
+ "axios": "^0.21.1",
32
33
  "fast-safe-stringify": "^2.0.6",
33
34
  "git-rev": "^0.2.1",
34
- "url-parse": "^1.4.7",
35
+ "lodash": "^4.17.21",
36
+ "url-parse": "^1.5.1",
35
37
  "uuid": "^3.3.3",
36
38
  "winston": "^3.2.1",
37
39
  "winston-logstash": "^0.4.0"
package/package.tar.gz CHANGED
Binary file
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+
3
+ const axios = require("axios"); // required only until migration is complete
4
+ const { omitBy, isNil } = require("lodash");
5
+
6
+ /* A class which safely wraps an axios connection in order to pass the current user through to
7
+ * the next service. Useful when one of our microservices requires auth as either an admin (sera4tal)
8
+ * or as the end user (example: DELETE /auth/sessions/:id
9
+ *
10
+ * each method requires a res.locals from a pre-authd session
11
+ */
12
+ class SafeProxy {
13
+ constructor() {
14
+ }
15
+
16
+ // a safe post request
17
+ // locals = res.locals - your authentication details
18
+ post(locals, url, options = { headers: {}, data: {}}) {
19
+ let { account, tenant, membership } = locals;
20
+ return this._proxyRequest({ account, tenant, membership }, url, "post", options);
21
+ }
22
+ delete(locals, url, options = { headers: {}, data: {}}) {
23
+ let { account, tenant, membership } = locals;
24
+ return this._proxyRequest({ account, tenant, membership }, url, "delete", options);
25
+ }
26
+
27
+ /* private methods */
28
+ // supports get, delete, post
29
+ // options includes { headers, data } for post / delete payloads
30
+ async _proxyRequest(locals, url, method = "get", options) {
31
+ let request;
32
+ let membership = locals.membership ? JSON.stringify(locals.membership) : null;
33
+ let headers = omitBy({ ...options.headers,
34
+ "tws-account-id": locals.account?.id,
35
+ "tws-tenant-id": locals.tenant?.id,
36
+ "tws-membership-id": locals.membership?.id,
37
+ "tws-membership": membership
38
+ }, isNil);
39
+
40
+ switch(method) {
41
+ case "post":
42
+ request = axios.post(url, { ...options.data }, { headers })
43
+ break;
44
+ case "delete":
45
+ request = axios.delete(url, { ...options.data, headers } )
46
+ break;
47
+ case "get":
48
+ request = axios.get(url, { headers, data } )
49
+ break;
50
+ default:
51
+ throw new Error("uknown type called in proxyRequest");
52
+ }
53
+
54
+ return request;
55
+ }
56
+
57
+ }
58
+
59
+ module.exports = new SafeProxy();