@ninetailed/experience.js-plugin-preview 7.5.2 → 7.5.3-beta.2

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.cjs.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./src/index";
2
+ export { default } from "./src/index";
@@ -1,41 +1,31 @@
1
1
  import { logger, unionBy } from '@ninetailed/experience.js-shared';
2
- import { NinetailedPlugin, OnChangeEmitter, isExperienceMatch, selectDistribution, PROFILE_CHANGE } from '@ninetailed/experience.js';
2
+ import { NinetailedPlugin, OnChangeEmitter, PROFILE_CHANGE, isExperienceMatch, selectDistribution } from '@ninetailed/experience.js';
3
3
 
4
- /******************************************************************************
5
- Copyright (c) Microsoft Corporation.
6
-
7
- Permission to use, copy, modify, and/or distribute this software for any
8
- purpose with or without fee is hereby granted.
9
-
10
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16
- PERFORMANCE OF THIS SOFTWARE.
17
- ***************************************************************************** */
18
-
19
- function __rest(s, e) {
20
- var t = {};
21
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
22
- t[p] = s[p];
23
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
24
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
25
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
26
- t[p[i]] = s[p[i]];
27
- }
28
- return t;
29
- }
30
-
31
- function __awaiter(thisArg, _arguments, P, generator) {
32
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
33
- return new (P || (P = Promise))(function (resolve, reject) {
34
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
35
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
36
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
37
- step((generator = generator.apply(thisArg, _arguments || [])).next());
38
- });
4
+ function _objectWithoutPropertiesLoose(source, excluded) {
5
+ if (source == null) return {};
6
+ var target = {};
7
+ var sourceKeys = Object.keys(source);
8
+ var key, i;
9
+ for (i = 0; i < sourceKeys.length; i++) {
10
+ key = sourceKeys[i];
11
+ if (excluded.indexOf(key) >= 0) continue;
12
+ target[key] = source[key];
13
+ }
14
+ return target;
15
+ }
16
+ function _toPrimitive(input, hint) {
17
+ if (typeof input !== "object" || input === null) return input;
18
+ var prim = input[Symbol.toPrimitive];
19
+ if (prim !== undefined) {
20
+ var res = prim.call(input, hint || "default");
21
+ if (typeof res !== "object") return res;
22
+ throw new TypeError("@@toPrimitive must return a primitive value.");
23
+ }
24
+ return (hint === "string" ? String : Number)(input);
25
+ }
26
+ function _toPropertyKey(arg) {
27
+ var key = _toPrimitive(arg, "string");
28
+ return typeof key === "symbol" ? key : String(key);
39
29
  }
40
30
 
41
31
  const CONTAINER_WIDTH = 432;
@@ -47,7 +37,8 @@ const TRANSFORM_CLOSED_HIDE = `translate(${CONTAINER_WIDTH}px, 0px)`;
47
37
  const TRANSFORM_OPEN = `translate(0px, 0px)`;
48
38
  class WidgetContainer {
49
39
  constructor(options) {
50
- var _a, _b;
40
+ var _options$ui;
41
+ this.container = void 0;
51
42
  this.options = options;
52
43
  this.container = document.createElement('div');
53
44
  this.container.style.position = 'fixed';
@@ -56,7 +47,7 @@ class WidgetContainer {
56
47
  this.container.style.bottom = `${BUTTON_BOTTOM_POSITION}px`;
57
48
  this.container.style.width = `${CONTAINER_WIDTH}px`;
58
49
  this.container.style.height = `${BUTTON_HEIGHT}px`;
59
- if ((_b = (_a = options.ui) === null || _a === void 0 ? void 0 : _a.opener) === null || _b === void 0 ? void 0 : _b.hide) {
50
+ if ((_options$ui = options.ui) != null && (_options$ui = _options$ui.opener) != null && _options$ui.hide) {
60
51
  this.container.style.transform = TRANSFORM_CLOSED_HIDE;
61
52
  } else {
62
53
  this.container.style.transform = TRANSFORM_CLOSED;
@@ -72,8 +63,8 @@ class WidgetContainer {
72
63
  this.container.style.bottom = `0px`;
73
64
  }
74
65
  close() {
75
- var _a, _b;
76
- if ((_b = (_a = this.options.ui) === null || _a === void 0 ? void 0 : _a.opener) === null || _b === void 0 ? void 0 : _b.hide) {
66
+ var _this$options$ui;
67
+ if ((_this$options$ui = this.options.ui) != null && (_this$options$ui = _this$options$ui.opener) != null && _this$options$ui.hide) {
77
68
  this.container.style.transform = TRANSFORM_CLOSED_HIDE;
78
69
  } else {
79
70
  this.container.style.transform = TRANSFORM_CLOSED;
@@ -88,11 +79,11 @@ class WidgetContainer {
88
79
  }
89
80
  }
90
81
 
91
- var _a;
92
82
  class NinetailedPreviewPlugin extends NinetailedPlugin {
93
83
  constructor(options) {
84
+ var _this;
94
85
  super();
95
- this.options = options;
86
+ _this = this;
96
87
  this.name = 'ninetailed:preview' + Math.random();
97
88
  this.isOpen = false;
98
89
  this.experiences = [];
@@ -103,36 +94,38 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
103
94
  this.container = null;
104
95
  this.bridge = null;
105
96
  this.onChangeEmitter = new OnChangeEmitter();
97
+ this.onOpenExperienceEditor = void 0;
98
+ this.onOpenAudienceEditor = void 0;
106
99
  this.clientId = null;
107
100
  this.environment = null;
108
- this.initialize = () => __awaiter(this, void 0, void 0, function* () {
109
- var _b;
101
+ this.initialize = async function () {
110
102
  if (typeof window !== 'undefined') {
103
+ var _window$ninetailed;
111
104
  const {
112
105
  PreviewBridge
113
- } = yield import('@ninetailed/experience.js-preview-bridge');
114
- this.container = new WidgetContainer({
115
- ui: this.options.ui
106
+ } = await import('@ninetailed/experience.js-preview-bridge');
107
+ _this.container = new WidgetContainer({
108
+ ui: _this.options.ui
116
109
  });
117
- this.bridge = PreviewBridge({
118
- url: this.options.url
110
+ _this.bridge = PreviewBridge({
111
+ url: _this.options.url
119
112
  });
120
- this.bridge.render(this.container.element);
113
+ _this.bridge.render(_this.container.element);
121
114
  window.ninetailed = Object.assign({}, window.ninetailed, {
122
- plugins: Object.assign(Object.assign({}, (_b = window.ninetailed) === null || _b === void 0 ? void 0 : _b.plugins), {
123
- preview: this.windowApi
115
+ plugins: Object.assign({}, (_window$ninetailed = window.ninetailed) == null ? void 0 : _window$ninetailed.plugins, {
116
+ preview: _this.windowApi
124
117
  })
125
118
  });
126
- this.bridge.updateProps({
127
- props: this.pluginApi
119
+ _this.bridge.updateProps({
120
+ props: _this.pluginApi
128
121
  });
129
122
  }
130
- });
123
+ };
131
124
  this.loaded = () => true;
132
- this[_a] = ({
125
+ this[PROFILE_CHANGE] = ({
133
126
  payload
134
127
  }) => {
135
- if (payload === null || payload === void 0 ? void 0 : payload.profile) {
128
+ if (payload != null && payload.profile) {
136
129
  this.onProfileChange(payload.profile);
137
130
  }
138
131
  };
@@ -144,8 +137,8 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
144
137
  const experienceIds = Object.keys(this.pluginApi.experienceVariantIndexes);
145
138
  const experience = experiences.find(experience => {
146
139
  const hasActiveAudience = this.pluginApi.activeAudiences.some(activeAudienceId => {
147
- var _b;
148
- return ((_b = experience.audience) === null || _b === void 0 ? void 0 : _b.id) === activeAudienceId;
140
+ var _experience$audience;
141
+ return ((_experience$audience = experience.audience) == null ? void 0 : _experience$audience.id) === activeAudienceId;
149
142
  });
150
143
  return hasActiveAudience && experienceIds.includes(experience.id);
151
144
  });
@@ -189,10 +182,10 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
189
182
  };
190
183
  };
191
184
  this.onChange = () => {
192
- var _b;
185
+ var _window$ninetailed2;
193
186
  logger.debug('Ninetailed Preview Plugin onChange pluginApi:', this.pluginApi);
194
187
  Object.assign({}, window.ninetailed, {
195
- plugins: Object.assign(Object.assign({}, (_b = window.ninetailed) === null || _b === void 0 ? void 0 : _b.plugins), {
188
+ plugins: Object.assign({}, (_window$ninetailed2 = window.ninetailed) == null ? void 0 : _window$ninetailed2.plugins, {
196
189
  preview: this.windowApi
197
190
  })
198
191
  });
@@ -212,20 +205,21 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
212
205
  this.clientId = clientId;
213
206
  this.environment = environment;
214
207
  };
208
+ this.options = options;
215
209
  this.experiences = options.experiences || [];
216
210
  this.audienceDefinitions = options.audiences || [];
217
211
  this.onOpenExperienceEditor = options.onOpenExperienceEditor;
218
212
  this.onOpenAudienceEditor = options.onOpenAudienceEditor;
219
213
  }
220
214
  open() {
221
- var _b;
222
- (_b = this.container) === null || _b === void 0 ? void 0 : _b.open();
215
+ var _this$container;
216
+ (_this$container = this.container) == null || _this$container.open();
223
217
  this.isOpen = true;
224
218
  this.onChange();
225
219
  }
226
220
  close() {
227
- var _b;
228
- (_b = this.container) === null || _b === void 0 ? void 0 : _b.close();
221
+ var _this$container2;
222
+ (_this$container2 = this.container) == null || _this$container2.close();
229
223
  setTimeout(() => {
230
224
  this.isOpen = false;
231
225
  this.onChange();
@@ -243,14 +237,14 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
243
237
  console.log(`You cannot activate an unknown audience (id: ${id}).`);
244
238
  return;
245
239
  }
246
- this.audienceOverwrites = Object.assign(Object.assign({}, this.audienceOverwrites), {
240
+ this.audienceOverwrites = Object.assign({}, this.audienceOverwrites, {
247
241
  [id]: true
248
242
  });
249
- this.experienceVariantIndexOverwrites = Object.assign(Object.assign({}, this.experienceVariantIndexOverwrites), this.experiences.filter(experience => {
250
- var _b;
251
- return ((_b = experience.audience) === null || _b === void 0 ? void 0 : _b.id) === id;
243
+ this.experienceVariantIndexOverwrites = Object.assign({}, this.experienceVariantIndexOverwrites, this.experiences.filter(experience => {
244
+ var _experience$audience2;
245
+ return ((_experience$audience2 = experience.audience) == null ? void 0 : _experience$audience2.id) === id;
252
246
  }).map(experience => experience.id).reduce((acc, curr) => {
253
- return Object.assign(Object.assign({}, acc), {
247
+ return Object.assign({}, acc, {
254
248
  [curr]: this.experienceVariantIndexes[curr] || 0
255
249
  });
256
250
  }, {}));
@@ -263,28 +257,28 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
263
257
  }
264
258
  this.experienceVariantIndexOverwrites = Object.entries(this.experienceVariantIndexOverwrites).filter(([key, _]) => {
265
259
  return !this.experiences.filter(experience => {
266
- var _b;
267
- return ((_b = experience.audience) === null || _b === void 0 ? void 0 : _b.id) === id;
260
+ var _experience$audience3;
261
+ return ((_experience$audience3 = experience.audience) == null ? void 0 : _experience$audience3.id) === id;
268
262
  }).map(experience => experience.id).includes(key);
269
263
  }).reduce((acc, [key, value]) => {
270
- return Object.assign(Object.assign({}, acc), {
264
+ return Object.assign({}, acc, {
271
265
  [key]: value
272
266
  });
273
267
  }, {});
274
- this.audienceOverwrites = Object.assign(Object.assign({}, this.audienceOverwrites), {
268
+ this.audienceOverwrites = Object.assign({}, this.audienceOverwrites, {
275
269
  [id]: false
276
270
  });
277
271
  this.onChange();
278
272
  this.experiences.filter(experience => {
279
- var _b;
280
- return ((_b = experience.audience) === null || _b === void 0 ? void 0 : _b.id) === id;
273
+ var _experience$audience4;
274
+ return ((_experience$audience4 = experience.audience) == null ? void 0 : _experience$audience4.id) === id;
281
275
  }).forEach(experience => {
282
276
  this.setExperienceVariant({
283
277
  experienceId: experience.id,
284
278
  variantIndex: 0
285
279
  });
286
280
  });
287
- this.audienceOverwrites = Object.assign(Object.assign({}, this.audienceOverwrites), {
281
+ this.audienceOverwrites = Object.assign({}, this.audienceOverwrites, {
288
282
  [id]: false
289
283
  });
290
284
  this.onChange();
@@ -294,10 +288,8 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
294
288
  console.log(`You cannot reset an unknown audience (id: ${id}). How did you get it in the first place?`);
295
289
  return;
296
290
  }
297
- const _b = this.audienceOverwrites,
298
- _c = id;
299
- _b[_c];
300
- const audienceOverwrites = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
291
+ const _this$audienceOverwri = this.audienceOverwrites,
292
+ audienceOverwrites = _objectWithoutPropertiesLoose(_this$audienceOverwri, [id].map(_toPropertyKey));
301
293
  this.audienceOverwrites = audienceOverwrites;
302
294
  this.onChange();
303
295
  }
@@ -311,8 +303,8 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
311
303
  return;
312
304
  }
313
305
  if (experience.audience && !this.activeAudiences.some(id => {
314
- var _b;
315
- return id === ((_b = experience.audience) === null || _b === void 0 ? void 0 : _b.id);
306
+ var _experience$audience5;
307
+ return id === ((_experience$audience5 = experience.audience) == null ? void 0 : _experience$audience5.id);
316
308
  })) {
317
309
  console.log(`You cannot active a variant for an experience (id: ${experienceId}), which is not in the active audiences.`);
318
310
  return;
@@ -321,16 +313,14 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
321
313
  if (!isValidIndex) {
322
314
  console.warn(`You activated a variant at index ${variantIndex} for the experience (id: ${experienceId}). Not all components have that many variants, you may see the baseline for some.`);
323
315
  }
324
- this.experienceVariantIndexOverwrites = Object.assign(Object.assign({}, this.experienceVariantIndexOverwrites), {
316
+ this.experienceVariantIndexOverwrites = Object.assign({}, this.experienceVariantIndexOverwrites, {
325
317
  [experienceId]: variantIndex
326
318
  });
327
319
  this.onChange();
328
320
  }
329
321
  resetExperience(experienceId) {
330
- const _b = this.experienceVariantIndexOverwrites,
331
- _c = experienceId;
332
- _b[_c];
333
- const experienceVariantIndexOverwrites = __rest(_b, [typeof _c === "symbol" ? _c : _c + ""]);
322
+ const _this$experienceVaria = this.experienceVariantIndexOverwrites,
323
+ experienceVariantIndexOverwrites = _objectWithoutPropertiesLoose(_this$experienceVaria, [experienceId].map(_toPropertyKey));
334
324
  this.experienceVariantIndexOverwrites = experienceVariantIndexOverwrites;
335
325
  this.onChange();
336
326
  }
@@ -349,7 +339,7 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
349
339
  if (this.onOpenAudienceEditor && typeof this.onOpenAudienceEditor === 'function') return this.onOpenAudienceEditor(audience);
350
340
  }
351
341
  get pluginApi() {
352
- var _b;
342
+ var _this$profile;
353
343
  return {
354
344
  version: '2.0.0',
355
345
  open: this.open.bind(this),
@@ -359,14 +349,14 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
359
349
  activateAudience: this.activateAudience.bind(this),
360
350
  deactivateAudience: this.deactivateAudience.bind(this),
361
351
  resetAudience: this.resetAudience.bind(this),
362
- apiAudiences: ((_b = this.profile) === null || _b === void 0 ? void 0 : _b.audiences) || [],
352
+ apiAudiences: ((_this$profile = this.profile) == null ? void 0 : _this$profile.audiences) || [],
363
353
  audienceOverwrites: this.audienceOverwrites,
364
354
  activeAudiences: this.activeAudiences,
365
355
  audienceDefinitions: this.audienceDefinitions,
366
356
  setExperienceVariant: this.setExperienceVariant.bind(this),
367
357
  resetExperience: this.resetExperience.bind(this),
368
358
  apiExperienceVariantIndexes: this.apiExperienceVariantIndexes,
369
- experienceVariantIndexes: Object.assign(Object.assign({}, this.experienceVariantIndexes), this.experienceVariantIndexOverwrites),
359
+ experienceVariantIndexes: Object.assign({}, this.experienceVariantIndexes, this.experienceVariantIndexOverwrites),
370
360
  experienceVariantIndexOverwrites: this.experienceVariantIndexOverwrites,
371
361
  reset: this.reset.bind(this),
372
362
  experiences: this.experiences,
@@ -387,7 +377,7 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
387
377
  activeAudiences: this.activeAudiences,
388
378
  setExperienceVariant: this.setExperienceVariant.bind(this),
389
379
  resetExperience: this.resetExperience.bind(this),
390
- experienceVariantIndexes: Object.assign(Object.assign({}, this.experienceVariantIndexes), this.experienceVariantIndexOverwrites)
380
+ experienceVariantIndexes: Object.assign({}, this.experienceVariantIndexes, this.experienceVariantIndexOverwrites)
391
381
  };
392
382
  }
393
383
  isKnownAudience(id) {
@@ -398,10 +388,10 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
398
388
  return unionBy(this.audienceDefinitions, audiencesFromExperiences, 'id');
399
389
  }
400
390
  get activeAudiences() {
401
- var _b;
391
+ var _this$profile2;
402
392
  const deactivatedAudiences = Object.entries(this.audienceOverwrites).filter(([id, active]) => !active).map(([id]) => id);
403
393
  const activatedAudiences = Object.entries(this.audienceOverwrites).filter(([id, active]) => active).map(([id]) => id);
404
- return [...(((_b = this.profile) === null || _b === void 0 ? void 0 : _b.audiences) || []), ...activatedAudiences].filter(id => !deactivatedAudiences.includes(id));
394
+ return [...(((_this$profile2 = this.profile) == null ? void 0 : _this$profile2.audiences) || []), ...activatedAudiences].filter(id => !deactivatedAudiences.includes(id));
405
395
  }
406
396
  calculateExperienceVariantIndexes(profile) {
407
397
  const matchedExperiences = this.experiences.filter(experience => isExperienceMatch({
@@ -413,7 +403,7 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
413
403
  experience,
414
404
  profile
415
405
  });
416
- return Object.assign(Object.assign({}, acc), {
406
+ return Object.assign({}, acc, {
417
407
  [experience.id]: distribution.index
418
408
  });
419
409
  }, {});
@@ -430,12 +420,11 @@ class NinetailedPreviewPlugin extends NinetailedPlugin {
430
420
  if (!profile) {
431
421
  return {};
432
422
  }
433
- return this.calculateExperienceVariantIndexes(Object.assign(Object.assign({}, profile), {
423
+ return this.calculateExperienceVariantIndexes(Object.assign({}, profile, {
434
424
  audiences: this.activeAudiences
435
425
  }));
436
426
  }
437
427
  }
438
- _a = PROFILE_CHANGE;
439
428
 
440
429
  if (typeof window === 'object' && !('process' in window)) {
441
430
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-plugin-preview",
3
- "version": "7.5.2",
3
+ "version": "7.5.3-beta.2",
4
4
  "description": "Ninetailed SDK plugin for preview",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -8,14 +8,17 @@
8
8
  "url": "https://github.com/ninetailed-inc/experience.js.git",
9
9
  "directory": "packages/plugins/preview"
10
10
  },
11
- "module": "./index.js",
12
- "main": "./index.cjs",
13
- "type": "module",
14
- "types": "./src/index.d.ts",
11
+ "keywords": [
12
+ "preview",
13
+ "ninetailed",
14
+ "personalization",
15
+ "a/b testing"
16
+ ],
15
17
  "dependencies": {
16
- "@ninetailed/experience.js": "7.5.2",
17
- "@ninetailed/experience.js-preview-bridge": "7.5.2",
18
- "@ninetailed/experience.js-shared": "7.5.2"
18
+ "@ninetailed/experience.js-shared": "*",
19
+ "@ninetailed/experience.js": "*",
20
+ "@ninetailed/experience.js-preview-bridge": "*"
19
21
  },
20
- "peerDependencies": {}
22
+ "module": "./index.esm.js",
23
+ "main": "./index.cjs.js"
21
24
  }
File without changes