@miso.ai/client-sdk-core 1.5.1 → 1.6.0-beta.1

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.
Files changed (50) hide show
  1. package/dist/cjs/api/base.js +1 -15
  2. package/dist/cjs/api/helpers.js +2 -31
  3. package/dist/cjs/api/index.js +0 -9
  4. package/dist/cjs/api/interactions.js +7 -24
  5. package/dist/cjs/api/recommendation.js +0 -11
  6. package/dist/cjs/api/search.js +0 -8
  7. package/dist/cjs/classes.js +0 -9
  8. package/dist/cjs/client.js +0 -21
  9. package/dist/cjs/cmd.js +4 -8
  10. package/dist/cjs/context.js +0 -4
  11. package/dist/cjs/index.js +4 -8
  12. package/dist/cjs/plugin/anonymous.js +0 -4
  13. package/dist/cjs/plugin/auto-events.js +0 -28
  14. package/dist/cjs/plugin/index.js +0 -12
  15. package/dist/cjs/plugin/page-info.js +0 -12
  16. package/dist/cjs/plugin/plugin-root.js +7 -55
  17. package/dist/cjs/plugin/user.js +0 -26
  18. package/dist/cjs/root.js +4 -13
  19. package/dist/cjs/utils.js +7 -37
  20. package/dist/cjs/version.js +1 -1
  21. package/dist/es/api/base.js +1 -12
  22. package/dist/es/api/helpers.js +2 -27
  23. package/dist/es/api/index.js +0 -1
  24. package/dist/es/api/interactions.js +7 -20
  25. package/dist/es/api/recommendation.js +0 -7
  26. package/dist/es/api/search.js +0 -4
  27. package/dist/es/client.js +0 -13
  28. package/dist/es/cmd.js +4 -7
  29. package/dist/es/context.js +0 -1
  30. package/dist/es/index.js +4 -4
  31. package/dist/es/plugin/anonymous.js +0 -1
  32. package/dist/es/plugin/auto-events.js +0 -24
  33. package/dist/es/plugin/index.js +1 -2
  34. package/dist/es/plugin/page-info.js +0 -8
  35. package/dist/es/plugin/plugin-root.js +7 -48
  36. package/dist/es/plugin/user.js +0 -22
  37. package/dist/es/root.js +5 -7
  38. package/dist/es/utils.js +1 -34
  39. package/dist/es/version.js +1 -1
  40. package/package.json +2 -2
  41. package/dist/cjs/plugin/units/constants.js +0 -10
  42. package/dist/cjs/plugin/units/index.js +0 -41
  43. package/dist/cjs/plugin/units/tracker.js +0 -365
  44. package/dist/cjs/plugin/units/unit.js +0 -63
  45. package/dist/cjs/plugin/units/units.js +0 -70
  46. package/dist/es/plugin/units/constants.js +0 -2
  47. package/dist/es/plugin/units/index.js +0 -27
  48. package/dist/es/plugin/units/tracker.js +0 -353
  49. package/dist/es/plugin/units/unit.js +0 -50
  50. package/dist/es/plugin/units/units.js +0 -56
@@ -4,35 +4,26 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _commons = require("@miso.ai/commons");
9
-
10
8
  const PLUGIN_ID = 'std:auto-events';
11
-
12
9
  class AutoEventsPlugin extends _commons.Component {
13
10
  static get id() {
14
11
  return PLUGIN_ID;
15
12
  }
16
-
17
13
  constructor() {
18
14
  super('auto-events');
19
15
  this._contexts = new WeakMap();
20
16
  }
21
-
22
17
  install(MisoClient, context) {
23
18
  context.addSubtree(this);
24
19
  MisoClient.on('create', this._injectClient.bind(this));
25
20
  context.addPayloadPass(this._captureApiRequest.bind(this));
26
21
  }
27
-
28
22
  _injectClient(client) {
29
23
  const context = new AutoEventsContext(this);
30
-
31
24
  this._contexts.set(client, context);
32
-
33
25
  client.autoEvents = context.interface;
34
26
  }
35
-
36
27
  _captureApiRequest(_ref) {
37
28
  let {
38
29
  client,
@@ -40,36 +31,28 @@ class AutoEventsPlugin extends _commons.Component {
40
31
  apiName,
41
32
  payload
42
33
  } = _ref;
43
-
44
34
  switch (apiGroup) {
45
35
  case 'search':
46
36
  switch (apiName) {
47
37
  case 'search':
48
38
  this._captureSearchApiRequest(client, payload);
49
-
50
39
  break;
51
40
  }
52
-
53
41
  break;
54
42
  }
55
43
  }
56
-
57
44
  _captureSearchApiRequest(client, _ref2) {
58
45
  let {
59
46
  q: keywords
60
47
  } = _ref2;
61
-
62
48
  if (!keywords || keywords === '*') {
63
49
  // might be a facet search without real keywords
64
50
  return;
65
51
  }
66
-
67
52
  const context = this._contexts.get(client);
68
-
69
53
  if (!context || !context._config.search) {
70
54
  return;
71
55
  }
72
-
73
56
  client.api.interactions.upload({
74
57
  type: 'search',
75
58
  search: {
@@ -82,11 +65,8 @@ class AutoEventsPlugin extends _commons.Component {
82
65
  }
83
66
  });
84
67
  }
85
-
86
68
  }
87
-
88
69
  exports.default = AutoEventsPlugin;
89
-
90
70
  class AutoEventsContext {
91
71
  constructor(plugin) {
92
72
  this._plugin = plugin;
@@ -97,30 +77,22 @@ class AutoEventsContext {
97
77
  config: this.config.bind(this)
98
78
  });
99
79
  }
100
-
101
80
  config() {
102
81
  let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
103
-
104
82
  if (typeof options !== 'object') {
105
83
  throw new Error(`Config options must be an object or undefined: ${options}`);
106
84
  }
107
-
108
85
  for (const key of options) {
109
86
  if (!this._config.hasOwnProperty(key)) {
110
87
  continue;
111
88
  }
112
-
113
89
  const oldValue = this._config[key];
114
90
  const newValue = options[key];
115
-
116
91
  if (newValue !== oldValue) {
117
92
  this._config[key] = oldValue;
118
-
119
93
  this._plugin._events.emit('config', `${key}: ${oldValue} -> ${newValue}`);
120
94
  }
121
95
  }
122
-
123
96
  return Object.freeze(Object.assign({}, this._config));
124
97
  }
125
-
126
98
  }
@@ -15,25 +15,13 @@ Object.defineProperty(exports, "PageInfoPlugin", {
15
15
  return _pageInfo.default;
16
16
  }
17
17
  });
18
- Object.defineProperty(exports, "UnitsPlugin", {
19
- enumerable: true,
20
- get: function () {
21
- return _units.default;
22
- }
23
- });
24
18
  Object.defineProperty(exports, "UserPlugin", {
25
19
  enumerable: true,
26
20
  get: function () {
27
21
  return _user.default;
28
22
  }
29
23
  });
30
-
31
24
  var _user = _interopRequireDefault(require("./user"));
32
-
33
25
  var _pageInfo = _interopRequireDefault(require("./page-info"));
34
-
35
26
  var _autoEvents = _interopRequireDefault(require("./auto-events"));
36
-
37
- var _units = _interopRequireDefault(require("./units"));
38
-
39
27
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -4,25 +4,19 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _commons = require("@miso.ai/commons");
9
-
10
8
  const ID = 'std:page-info';
11
-
12
9
  class PageInfoPlugin {
13
10
  static get id() {
14
11
  return ID;
15
12
  }
16
-
17
13
  constructor() {}
18
-
19
14
  install(_, _ref) {
20
15
  let {
21
16
  addPayloadPass
22
17
  } = _ref;
23
18
  addPayloadPass(this._modifyPayload.bind(this));
24
19
  }
25
-
26
20
  _modifyPayload(_ref2) {
27
21
  let {
28
22
  apiGroup,
@@ -31,7 +25,6 @@ class PageInfoPlugin {
31
25
  } = _ref2;
32
26
  return apiGroup === 'interactions' && apiName === 'upload' ? this._modifyPayloadForInteractions(payload) : payload;
33
27
  }
34
-
35
28
  _modifyPayloadForInteractions(_ref3) {
36
29
  let {
37
30
  data
@@ -43,18 +36,14 @@ class PageInfoPlugin {
43
36
  data
44
37
  };
45
38
  }
46
-
47
39
  }
48
-
49
40
  exports.default = PageInfoPlugin;
50
-
51
41
  function getPayloadContext() {
52
42
  return (0, _commons.trimObj)({
53
43
  page: readPageInfo(),
54
44
  campaign: readUtm()
55
45
  });
56
46
  }
57
-
58
47
  function readPageInfo() {
59
48
  return (0, _commons.trimObj)({
60
49
  url: window.location.href,
@@ -62,7 +51,6 @@ function readPageInfo() {
62
51
  title: document.title
63
52
  });
64
53
  }
65
-
66
54
  function readUtm() {
67
55
  const params = new URLSearchParams(window.location.search);
68
56
  let utm = (0, _commons.trimObj)({
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _commons = require("@miso.ai/commons");
9
-
10
8
  var _classes = _interopRequireDefault(require("../classes"));
11
-
12
9
  var _anonymous = _interopRequireDefault(require("./anonymous"));
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  class PluginRoot extends _commons.Registry {
17
12
  constructor(root) {
18
13
  super('plugins', root, {
@@ -20,36 +15,29 @@ class PluginRoot extends _commons.Registry {
20
15
  keyName: 'id'
21
16
  });
22
17
  this._root = root;
23
-
24
18
  this._events._replays.add('install').add('subtree');
25
-
26
19
  this._installed = {};
27
20
  this._useRequests = {};
28
21
  this._subtrees = [];
29
22
  this.context = new PluginContext(this, _classes.default);
30
23
  this.plugins = new Plugins(this);
31
24
  }
32
-
33
25
  isInstalled(id) {
34
26
  this._checkKey(id);
35
-
36
27
  return !!this._installed[id];
37
28
  }
38
-
39
29
  get installed() {
40
30
  return Object.values(this._installed);
41
31
  }
42
-
43
32
  get(id) {
44
33
  return this._installed[id];
45
34
  }
46
-
47
35
  getPluginClass(id) {
48
36
  return this._libraries[id];
49
- } // TODO: we should be able to support (Class1, Class2, options2, etc.)
50
- // TODO: refactor use() and _instantiate()
51
-
37
+ }
52
38
 
39
+ // TODO: we should be able to support (Class1, Class2, options2, etc.)
40
+ // TODO: refactor use() and _instantiate()
53
41
  use(plugin, options) {
54
42
  // use(PluginClass) is a shortcut of register() then use()
55
43
  if (typeof plugin === 'function' && plugin.id && typeof plugin.id === 'string') {
@@ -57,97 +45,74 @@ class PluginRoot extends _commons.Registry {
57
45
  this.use(plugin.id, options);
58
46
  return;
59
47
  }
60
-
61
48
  let instance;
62
-
63
49
  if (typeof plugin === 'string' && this.isInstalled(plugin)) {
64
50
  // if already installed, try to run config() instead
65
51
  instance = this.get(plugin);
66
-
67
52
  this._tryConfig(instance, options);
68
-
69
53
  return;
70
54
  }
71
-
72
55
  if (typeof plugin === 'string' && !this.isRegistered(plugin)) {
73
56
  // not registered yet, push a use request
74
57
  (this._useRequests[plugin] || (this._useRequests[plugin] = [])).push(options);
75
58
  return;
76
59
  }
77
-
78
60
  instance = this._instantiate(plugin, options);
79
-
80
61
  this._install(instance);
81
- } // TODO: unuse()
62
+ }
82
63
 
64
+ // TODO: unuse()
83
65
 
84
66
  addSubtree(component) {
85
67
  this._subtrees.push(component);
86
-
87
68
  this._events.emit('subtree', component);
88
69
  }
89
-
90
70
  addPayloadPass(pass) {
91
71
  if (typeof pass !== 'function') {
92
72
  throw new Error(`Expect parameter to be a function: ${pass}`);
93
73
  }
94
-
95
74
  this._root._payloadPasses.push(pass);
96
75
  }
97
-
98
76
  addUrlPass(pass) {
99
77
  if (typeof pass !== 'function') {
100
78
  throw new Error(`Expect parameter to be a function: ${pass}`);
101
79
  }
102
-
103
80
  this._root._urlPasses.push(pass);
104
81
  }
105
-
106
82
  _tryConfig(instance, options) {
107
83
  if (options === undefined) {
108
84
  return;
109
85
  }
110
-
111
86
  if (typeof instance.config === 'function') {
112
87
  instance.config(options);
113
88
  } else {
114
89
  this._warn(`Attempt to pass options to plugin "${instance.id}", which offers no config() method.`);
115
90
  }
116
91
  }
117
-
118
92
  _instantiate(plugin, options) {
119
93
  switch (typeof plugin) {
120
94
  case 'string':
121
95
  this._checkKey(plugin);
122
-
123
96
  const pluginClass = this._libraries[plugin];
124
-
125
97
  if (!pluginClass) {
126
98
  throw new Error(`Plugin class not found: ${plugin}`);
127
99
  }
128
-
129
100
  const instance = new pluginClass();
130
101
  (0, _commons.defineValues)(instance, {
131
102
  id: plugin
132
103
  });
133
-
134
104
  this._tryConfig(instance, options);
135
-
136
105
  return instance;
137
-
138
106
  case 'function':
139
107
  return new _anonymous.default(plugin);
140
108
  }
141
-
142
109
  throw new Error(`Unexpected parameters: ${plugin}`);
143
110
  }
144
-
145
111
  _install(instance) {
146
112
  if (typeof instance.install !== 'function') {
147
113
  // TODO: introduce PluginError
148
114
  throw new Error(`Expect plugin.install to be a function: ${instance}`);
149
115
  }
150
-
151
116
  try {
152
117
  instance.installed = false;
153
118
  instance.install(this.MisoClient, this.context);
@@ -156,24 +121,18 @@ class PluginRoot extends _commons.Registry {
156
121
  // TODO: introduce PluginError
157
122
  throw e;
158
123
  }
159
-
160
124
  if (instance.id) {
161
125
  // we don't track anonymous plugins
162
126
  this._installed[instance.id] = instance;
163
127
  }
164
-
165
128
  this._events.emit('install', instance);
166
129
  }
167
-
168
130
  _register(lib) {
169
131
  super._register(lib);
170
-
171
132
  this._fulfillUseRequests(lib.id);
172
133
  }
173
-
174
134
  _fulfillUseRequests(id) {
175
135
  const requests = this._useRequests[id];
176
-
177
136
  if (requests) {
178
137
  for (const options of requests) {
179
138
  try {
@@ -182,19 +141,15 @@ class PluginRoot extends _commons.Registry {
182
141
  this._error(e);
183
142
  }
184
143
  }
185
-
186
144
  delete this._useRequests[id];
187
145
  }
188
146
  }
189
-
190
147
  }
148
+
191
149
  /**
192
150
  * The interface of extended features only for plugin developers.
193
151
  */
194
-
195
-
196
152
  exports.default = PluginRoot;
197
-
198
153
  class PluginContext {
199
154
  constructor(root, classes) {
200
155
  (0, _commons.delegateGetters)(this, root, ['getPluginClass', 'addSubtree', 'addPayloadPass', 'addUrlPass']);
@@ -202,16 +157,13 @@ class PluginContext {
202
157
  classes
203
158
  });
204
159
  }
205
-
206
160
  }
161
+
207
162
  /**
208
163
  * The interface of plugin features exposed to MisoClient.
209
164
  */
210
-
211
-
212
165
  class Plugins {
213
166
  constructor(root) {
214
167
  (0, _commons.delegateGetters)(this, root, ['installed', 'registered', 'isInstalled', 'isRegistered', 'get', 'register', 'use']);
215
168
  }
216
-
217
169
  }
@@ -4,35 +4,26 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _commons = require("@miso.ai/commons");
9
-
10
8
  const PLUGIN_ID = 'std:user';
11
-
12
9
  class UserPlugin extends _commons.Component {
13
10
  static get id() {
14
11
  return PLUGIN_ID;
15
12
  }
16
-
17
13
  constructor() {
18
14
  super('user');
19
15
  this._contexts = new WeakMap();
20
16
  }
21
-
22
17
  install(MisoClient, context) {
23
18
  context.addSubtree(this);
24
19
  MisoClient.on('create', this._injectClient.bind(this));
25
20
  context.addPayloadPass(this._modifyPayload.bind(this));
26
21
  }
27
-
28
22
  _injectClient(client) {
29
23
  const context = new UserContext(this);
30
-
31
24
  this._contexts.set(client, context);
32
-
33
25
  (0, _commons.delegateProperties)(client.context, context, ['anonymous_id', 'user_id', 'user_hash']);
34
26
  }
35
-
36
27
  _modifyPayload(_ref) {
37
28
  let {
38
29
  client,
@@ -42,7 +33,6 @@ class UserPlugin extends _commons.Component {
42
33
  } = _ref;
43
34
  return apiGroup === 'interactions' && apiName === 'upload' ? this._modifyPayloadForInteractions(client, payload) : this._modifyPayloadForOthers(client, payload);
44
35
  }
45
-
46
36
  _modifyPayloadForOthers(client, payload) {
47
37
  const {
48
38
  user_id,
@@ -57,7 +47,6 @@ class UserPlugin extends _commons.Component {
57
47
  };
58
48
  return Object.assign({}, userInfo, payload);
59
49
  }
60
-
61
50
  _modifyPayloadForInteractions(client, _ref2) {
62
51
  let {
63
52
  data
@@ -75,54 +64,39 @@ class UserPlugin extends _commons.Component {
75
64
  data
76
65
  };
77
66
  }
78
-
79
67
  }
80
-
81
68
  exports.default = UserPlugin;
82
-
83
69
  function getAutoAnonymousId() {
84
70
  return (0, _commons.getOrComputeFromStorage)('miso_anonymous_id', _commons.uuidv4);
85
71
  }
86
-
87
72
  class UserContext {
88
73
  constructor(plugin) {
89
74
  this._plugin = plugin;
90
75
  }
91
-
92
76
  get anonymous_id() {
93
77
  return this._anonymousId || this._autoAnonymousId || (this._autoAnonymousId = getAutoAnonymousId());
94
78
  }
95
-
96
79
  set anonymous_id(value) {
97
80
  value = (0, _commons.asString)(value);
98
81
  this._anonymousId = value;
99
-
100
82
  this._plugin._events.emit('set', `anonymous_id = '${value}'`);
101
83
  }
102
-
103
84
  get user_id() {
104
85
  return this._userId;
105
86
  }
106
-
107
87
  set user_id(value) {
108
88
  value = (0, _commons.asString)(value);
109
89
  this._userId = value;
110
-
111
90
  this._plugin._events.emit('set', `user_id = '${value}'`);
112
91
  }
113
-
114
92
  get user_hash() {
115
93
  return this._userHash;
116
94
  }
117
-
118
95
  set user_hash(value) {
119
96
  if (typeof value !== 'string' && typeof value !== 'undefined') {
120
97
  throw new Error(`User hash must be a string`);
121
98
  }
122
-
123
99
  this._userHash = value;
124
-
125
100
  this._plugin._events.emit('set', `user_hash = '${value}'`);
126
101
  }
127
-
128
102
  }
package/dist/cjs/root.js CHANGED
@@ -6,50 +6,41 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.init = init;
7
7
  exports.register = register;
8
8
  exports.root = void 0;
9
-
10
9
  var _commons = require("@miso.ai/commons");
11
-
12
10
  var _version = _interopRequireDefault(require("./version"));
13
-
14
11
  var _pluginRoot = _interopRequireDefault(require("./plugin/plugin-root"));
15
-
16
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
-
18
13
  /**
19
14
  * The hidden singleton root component that oversees all client components.
20
15
  */
21
16
  class Root extends _commons.Component {
22
17
  constructor() {
23
18
  super();
24
-
25
19
  this._events._replays.add('create');
26
-
27
20
  this._pluginRoot = new _pluginRoot.default(this);
28
21
  this._clients = [];
29
22
  this._payloadPasses = [];
30
23
  this._urlPasses = [];
31
24
  this.version = _version.default || 'dev';
32
25
  }
33
-
34
26
  get instances() {
35
27
  return [...this._clients]; // write protection
36
28
  }
37
29
 
30
+ async any() {
31
+ return this._clients[0] || this._any || (this._any = (await this._events.once('create')).data);
32
+ }
38
33
  }
39
-
40
34
  const root = new Root();
41
35
  exports.root = root;
42
-
43
36
  function init(MisoClient) {
44
37
  const pluginRoot = root._pluginRoot;
45
38
  root.MisoClient = pluginRoot.MisoClient = MisoClient;
46
- (0, _commons.delegateGetters)(MisoClient, root, ['version', 'instances', 'meta', 'on', 'once']);
39
+ (0, _commons.delegateGetters)(MisoClient, root, ['version', 'instances', 'any', 'meta', 'on', 'once']);
47
40
  (0, _commons.delegateGetters)(MisoClient, pluginRoot, ['plugins']);
48
41
  return MisoClient;
49
42
  }
50
-
51
43
  function register(client) {
52
44
  root._clients.push(client);
53
-
54
45
  root._events.emit('create', client);
55
46
  }
package/dist/cjs/utils.js CHANGED
@@ -3,40 +3,10 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.viewable = viewable;
7
-
8
- var _commons = require("@miso.ai/commons");
9
-
10
- async function viewable(element) {
11
- let {
12
- area = 0.5,
13
- duration = 1000,
14
- signal
15
- } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
16
- element = (0, _commons.asElement)(element); // TODO: check element
17
-
18
- return new Promise(resolve => {
19
- const continuity = new _commons.ContinuityObserver(value => {
20
- if (value) {
21
- continuity.disconnect();
22
- intersection.disconnect();
23
- resolve();
24
- }
25
- }, {
26
- onDuration: duration
27
- });
28
- const intersection = new IntersectionObserver(entries => {
29
- continuity.value = entries[0].isIntersecting;
30
- }, {
31
- threshold: area
32
- });
33
- intersection.observe(element);
34
-
35
- if (signal && signal.addEventListener) {
36
- signal.addEventListener('abort', () => {
37
- continuity.disconnect();
38
- intersection.disconnect();
39
- });
40
- }
41
- });
42
- }
6
+ Object.defineProperty(exports, "viewable", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _commons.viewable;
10
+ }
11
+ });
12
+ var _commons = require("@miso.ai/commons");
@@ -4,5 +4,5 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
- var _default = '1.5.1';
7
+ var _default = '1.6.0-beta.1';
8
8
  exports.default = _default;