wingbot 3.67.29 → 3.67.30

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": "wingbot",
3
- "version": "3.67.29",
3
+ "version": "3.67.30",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
@@ -10,7 +10,7 @@
10
10
  "doc": "npm run doc:gql && node ./bin/makeApiDoc.js && cpy ./CHANGELOG.md ./doc && gitbook install ./doc && gitbook build ./doc && rimraf -rf ./docs && rimraf --rf ./doc/CHANGELOG.md && move-cli ./doc/_book ./docs",
11
11
  "test": "npm run test:lint && npm run test:coverage && npm run test:coverage:threshold",
12
12
  "test:coverage": "nyc --reporter=html mocha ./test && nyc report",
13
- "test:coverage:threshold": "nyc check-coverage --lines 89 --functions 89 --branches 80",
13
+ "test:coverage:threshold": "nyc check-coverage --lines 89 --functions 88 --branches 80",
14
14
  "test:backend": "mocha ./test",
15
15
  "test:lint": "eslint --ext .js src test *.js plugins"
16
16
  },
@@ -4,7 +4,9 @@
4
4
  'use strict';
5
5
 
6
6
  const apiAuthorizer = require('./apiAuthorizer');
7
- const { apiTextOutputFilter } = require('../utils/deepMapTools');
7
+ const { apiTextOutputFilter, mapObject, defaultMapperFactory } = require('../utils/deepMapTools');
8
+
9
+ /** @typedef {import('../utils/deepMapTools').Mapper} Mapper */
8
10
 
9
11
  /**
10
12
  * @typedef {object} ConversationsAPI
@@ -47,6 +49,7 @@ const { apiTextOutputFilter } = require('../utils/deepMapTools');
47
49
  * @param {string[]|Function} [acl] - limit api to array of groups or use auth function
48
50
  * @param {object} options
49
51
  * @param {textFilter} [options.stateTextFilter] - optional funcion for filtering data in states
52
+ * @param {Mapper} [options.mapper] - mapper for state values
50
53
  * @returns {ConversationsAPI}
51
54
  * @example
52
55
  * const { GraphApi, conversationsApi } = require('wingbot');
@@ -106,6 +109,13 @@ function conversationsApi (
106
109
  history,
107
110
  subscribtions
108
111
  });
112
+ } else if (options.mapper) {
113
+ mapState = (d) => mapObject({
114
+ ...d,
115
+ lastInteraction: d.lastInteraction || (new Date(0)),
116
+ history,
117
+ subscribtions
118
+ }, defaultMapperFactory(options.mapper));
109
119
  } else {
110
120
  mapState = (d) => ({
111
121
  ...d,
@@ -23,6 +23,58 @@ function deepEqual (left, right) {
23
23
  }
24
24
  }
25
25
 
26
+ /**
27
+ * @callback Mapper
28
+ * @param {string} key
29
+ * @param {any} val
30
+ * @param {object} lastObj
31
+ * @param {string|null} lastKey
32
+ * @returns {any}
33
+ */
34
+
35
+ /**
36
+ *
37
+ * @param {Mapper} [otherMapper]
38
+ * @returns {Mapper}
39
+ */
40
+ function defaultMapperFactory (otherMapper = null) {
41
+ return (k, v, o, lk) => {
42
+ const useV = otherMapper
43
+ ? otherMapper(k, v, o, lk)
44
+ : v;
45
+
46
+ if (useV instanceof Date) {
47
+ return v.toISOString();
48
+ }
49
+ return useV;
50
+ };
51
+ }
52
+
53
+ /**
54
+ *
55
+ * @template T
56
+ * @param {T} obj
57
+ * @param {Mapper} mapFn
58
+ * @returns {T}
59
+ */
60
+ function mapObject (obj, mapFn) {
61
+ let lastKey = null;
62
+ let lastObj = obj;
63
+
64
+ const str = JSON.stringify(obj, (key, val) => {
65
+ const ret = mapFn(key, val, lastObj, lastKey);
66
+
67
+ if (val && typeof val === 'object') {
68
+ lastObj = val;
69
+ lastKey = key || null;
70
+ }
71
+
72
+ return ret;
73
+ });
74
+
75
+ return JSON.parse(str);
76
+ }
77
+
26
78
  function apiTextOutputFilter (obj, callback, prevKey = '') {
27
79
  let useKey;
28
80
  if (Array.isArray(obj)) {
@@ -59,4 +111,6 @@ function apiTextOutputFilter (obj, callback, prevKey = '') {
59
111
  return obj;
60
112
  }
61
113
 
62
- module.exports = { apiTextOutputFilter, deepEqual };
114
+ module.exports = {
115
+ apiTextOutputFilter, deepEqual, mapObject, defaultMapperFactory
116
+ };