@sera4/essentia 1.1.52 → 1.1.53

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sera4/essentia",
3
- "version": "1.1.52",
3
+ "version": "1.1.53",
4
4
  "description": "A library of utilities for Teleporte Web Services",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/package.tar.gz CHANGED
Binary file
@@ -8,23 +8,53 @@ export default {
8
8
  * @param {Object} Model the squelize model to add this plugin to
9
9
  * @param {Object} options various options for serialization. These can be:
10
10
  * • hide - an array of fields to be omitted
11
+ * • remove - an array of fields ot be REMOVED so they cannot be unhidden
11
12
  * • decorators - an array of special decorators to run during serialization
13
+ * • handler - a handler for transforming data using only the values
14
+ * • advhandler - an advanced handler for transforming data using the value, object or options
15
+ * • includeAllFields - show fields that are normally hidden
16
+ * • includeFields[..] - show specific fields that are normally hidden
12
17
  */
13
18
  serialize : (Model, options) => {
14
19
  Model.prototype.toJSON = function(eachOptions={}) {
15
20
  const hide = options && options.hide ? options.hide : [];
21
+ const remove = options && options.remove ? options.remove : [];
22
+ // ignore hide principles; but still do NOT show null
23
+ const includeAllFields = eachOptions["includeAllFields"];
24
+
25
+ // optionally force a normally hidden fields to be seen (by name)
26
+ const includeFields = eachOptions["includeFields"] || []
27
+ const handlerOptions = eachOptions["handlerOptions"] || {}
28
+
16
29
  const values = this.get();
17
30
 
31
+ // for security reasons, some fields may need to be removed, such as
32
+ // password salts and mfa tokens
33
+ remove.forEach((field) => {
34
+ delete values[field];
35
+ });
36
+
18
37
  if (options && options.decorators) {
19
38
  options.decorators.forEach(dec => {
20
39
  const k = dec.name;
21
40
  const v = values[k];
22
- values[k] = dec.handler(v);
41
+ // the advanced handler takes more parameters
42
+ if (dec.advHandler) {
43
+ values[k] = dec.advHandler(v, this, handlerOptions);
44
+ } else {
45
+ values[k] = dec.handler(v);
46
+ }
23
47
  });
24
48
  }
25
49
 
50
+ // hidden fields CAN be resurrected if NOT null, and forced to be
51
+ // included
26
52
  let omitted = _.omitBy(values, (v, k) => {
27
- return hide.includes(k) || v === null;
53
+ if (v === null) return true;
54
+
55
+ let omitted = (includeAllFields ? false : hide.includes(k));
56
+ if (omitted && includeFields[k]) omitted = true ;
57
+ return omitted;
28
58
  });
29
59
 
30
60
  const s = eachOptions["serializer"];