@fullstory/browser 1.7.1 → 2.0.0-beta.0

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/dist/index.js CHANGED
@@ -1,178 +1,158 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var snippet = require('@fullstory/snippet');
6
4
 
7
- function ownKeys(object, enumerableOnly) {
8
- var keys = Object.keys(object);
9
-
10
- if (Object.getOwnPropertySymbols) {
11
- var symbols = Object.getOwnPropertySymbols(object);
12
- enumerableOnly && (symbols = symbols.filter(function (sym) {
13
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
14
- })), keys.push.apply(keys, symbols);
15
- }
16
-
17
- return keys;
18
- }
19
-
20
- function _objectSpread2(target) {
21
- for (var i = 1; i < arguments.length; i++) {
22
- var source = null != arguments[i] ? arguments[i] : {};
23
- i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
24
- _defineProperty(target, key, source[key]);
25
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
26
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
27
- });
28
- }
29
-
30
- return target;
31
- }
32
-
33
- function _defineProperty(obj, key, value) {
34
- if (key in obj) {
35
- Object.defineProperty(obj, key, {
36
- value: value,
37
- enumerable: true,
38
- configurable: true,
39
- writable: true
40
- });
41
- } else {
42
- obj[key] = value;
43
- }
44
-
45
- return obj;
46
- }
47
-
48
- var fs = function fs() {
49
- return window[window._fs_namespace];
5
+ /******************************************************************************
6
+ Copyright (c) Microsoft Corporation.
7
+
8
+ Permission to use, copy, modify, and/or distribute this software for any
9
+ purpose with or without fee is hereby granted.
10
+
11
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
12
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
14
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
16
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17
+ PERFORMANCE OF THIS SOFTWARE.
18
+ ***************************************************************************** */
19
+
20
+ var __assign = function() {
21
+ __assign = Object.assign || function __assign(t) {
22
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
23
+ s = arguments[i];
24
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
25
+ }
26
+ return t;
27
+ };
28
+ return __assign.apply(this, arguments);
50
29
  };
51
30
 
52
- var ensureSnippetLoaded = function ensureSnippetLoaded() {
53
- var snippetLoaded = !!fs();
54
-
55
- if (!snippetLoaded) {
56
- throw Error('FullStory is not loaded, please ensure the init function is invoked before calling FullStory API functions');
57
- }
31
+ var getFullStory = function () {
32
+ if (window._fs_namespace) {
33
+ return window[window._fs_namespace];
34
+ }
35
+ return undefined;
58
36
  };
59
-
60
- var hasFullStoryWithFunction = function hasFullStoryWithFunction() {
61
- ensureSnippetLoaded();
62
-
63
- for (var _len = arguments.length, testNames = new Array(_len), _key = 0; _key < _len; _key++) {
64
- testNames[_key] = arguments[_key];
65
- }
66
-
67
- return testNames.every(function (current) {
68
- return fs()[current];
69
- });
37
+ var ensureSnippetLoaded = function () {
38
+ var fs = getFullStory();
39
+ if (!fs) {
40
+ throw Error('FullStory is not loaded, please ensure the init function is invoked before calling FullStory API functions');
41
+ }
42
+ return fs;
70
43
  };
71
-
72
- var guard = function guard(name) {
73
- return function () {
74
- if (window._fs_dev_mode) {
75
- var message = "FullStory is in dev mode and is not recording: ".concat(name, " method not executed");
76
- console.warn(message);
77
- return message;
44
+ var _init = function (inputOptions, readyCallback) {
45
+ // Make a copy so we can modify `options` if desired.
46
+ var options = __assign({}, inputOptions);
47
+ if (getFullStory()) {
48
+ console.warn('The FullStory snippet has already been defined elsewhere (likely in the <head> element)');
49
+ return;
78
50
  }
79
-
80
- if (hasFullStoryWithFunction(name)) {
81
- var _fs;
82
-
83
- return (_fs = fs())[name].apply(_fs, arguments);
51
+ // see README for details on the recordCrossDomainIFrames option
52
+ if (options.recordCrossDomainIFrames) {
53
+ window._fs_run_in_iframe = true;
84
54
  }
85
-
86
- console.warn("FS.".concat(name, " not ready"));
87
- return null;
88
- };
89
- };
90
-
91
- var event = guard('event');
92
- var log = guard('log');
93
- var getCurrentSessionURL = guard('getCurrentSessionURL');
94
- var identify = guard('identify');
95
- var setUserVars = guard('setUserVars');
96
- var consent = guard('consent');
97
- var shutdown = guard('shutdown');
98
- var restart = guard('restart');
99
- var anonymize = guard('anonymize');
100
- var setVars = guard('setVars');
101
-
102
- var _init = function _init(inputOptions, readyCallback) {
103
- var options = _objectSpread2({}, inputOptions);
104
-
105
- if (fs()) {
106
- console.warn('The FullStory snippet has already been defined elsewhere (likely in the <head> element)');
107
- return;
108
- }
109
-
110
- if (options.recordCrossDomainIFrames) {
111
- window._fs_run_in_iframe = true;
112
- }
113
-
114
- if (options.recordOnlyThisIFrame) {
115
- window._fs_is_outer_script = true;
116
- }
117
-
118
- if (options.cookieDomain) {
119
- window._fs_cookie_domain = options.cookieDomain;
120
- }
121
-
122
- if (options.debug === true) {
123
- if (!options.script) {
124
- options.script = 'edge.fullstory.com/s/fs-debug.js';
125
- } else {
126
- console.warn('Ignoring `debug = true` because `script` is set');
55
+ // record the contents of this iFrame when embedded in a parent site
56
+ if (options.recordOnlyThisIFrame) {
57
+ window._fs_is_outer_script = true;
58
+ }
59
+ // Set cookie domain if it was specified.
60
+ if (options.cookieDomain) {
61
+ window._fs_cookie_domain = options.cookieDomain;
62
+ }
63
+ if (options.debug === true) {
64
+ if (!options.script) {
65
+ options.script = 'edge.fullstory.com/s/fs-debug.js';
66
+ }
67
+ else {
68
+ console.warn('Ignoring `debug = true` because `script` is set');
69
+ }
70
+ }
71
+ snippet.initFS(options);
72
+ var fs = getFullStory();
73
+ if (!fs) {
74
+ console.warn('Failed to initialize FS snippet');
75
+ return;
76
+ }
77
+ if (readyCallback) {
78
+ fs('observe', { type: 'start', callback: readyCallback });
79
+ }
80
+ if (options.devMode === true) {
81
+ var message = 'FullStory was initialized in devMode and will stop recording';
82
+ fs('trackEvent', {
83
+ name: 'FullStory Dev Mode',
84
+ properties: {
85
+ message_str: message,
86
+ }
87
+ });
88
+ fs('shutdown');
89
+ window._fs_dev_mode = true;
90
+ console.warn(message);
127
91
  }
128
- }
129
-
130
- snippet.initFS(options);
131
-
132
- if (readyCallback) {
133
- fs()('observe', {
134
- type: 'start',
135
- callback: readyCallback
136
- });
137
- }
138
-
139
- if (options.devMode === true) {
140
- var message = 'FullStory was initialized in devMode and will stop recording';
141
- event('FullStory Dev Mode', {
142
- message_str: message
143
- });
144
- shutdown();
145
- window._fs_dev_mode = true;
146
- console.warn(message);
147
- }
148
92
  };
149
-
150
- var initOnce = function initOnce(fn, message) {
151
- return function () {
93
+ var initOnce = function (message) { return function (inputOptions, readyCallback) {
152
94
  if (window._fs_initialized) {
153
- if (message) console.warn(message);
154
- return;
95
+ if (message)
96
+ console.warn(message);
97
+ return;
155
98
  }
156
-
157
- fn.apply(void 0, arguments);
99
+ _init(inputOptions, readyCallback);
158
100
  window._fs_initialized = true;
159
- };
101
+ }; };
102
+ var init = initOnce('FullStory init has already been called once, additional invocations are ignored');
103
+ // normalize undefined into boolean
104
+ var isInitialized = function () { return !!window._fs_initialized; };
105
+ var hasFullStoryWithFunction = function () {
106
+ var testNames = [];
107
+ for (var _i = 0; _i < arguments.length; _i++) {
108
+ testNames[_i] = arguments[_i];
109
+ }
110
+ var fs = ensureSnippetLoaded();
111
+ return testNames.every(function (current) { return fs[current]; });
160
112
  };
161
-
162
- var init = initOnce(_init, 'FullStory init has already been called once, additional invocations are ignored');
163
- var isInitialized = function isInitialized() {
164
- return !!window._fs_initialized;
113
+ var guard = function (name) { return function () {
114
+ var args = [];
115
+ for (var _i = 0; _i < arguments.length; _i++) {
116
+ args[_i] = arguments[_i];
117
+ }
118
+ if (window._fs_dev_mode) {
119
+ var message = "FullStory is in dev mode and is not recording: ".concat(name, " method not executed");
120
+ console.warn(message);
121
+ return message;
122
+ }
123
+ var fs = getFullStory();
124
+ if (hasFullStoryWithFunction(name) && fs) {
125
+ return fs[name].apply(fs, args);
126
+ }
127
+ console.warn("FS.".concat(name, " not ready"));
128
+ return null;
129
+ }; };
130
+ var buildFullStoryShim = function () {
131
+ var FS = function (operation, options, source) {
132
+ var fs = ensureSnippetLoaded();
133
+ if (window._fs_dev_mode) {
134
+ var message = 'FullStory is in dev mode and is not recording: method not executed';
135
+ console.warn(message);
136
+ return undefined;
137
+ }
138
+ return fs(operation, options, source);
139
+ };
140
+ FS.anonymize = guard('anonymize');
141
+ FS.consent = guard('consent');
142
+ FS.disableConsole = guard('disableConsole');
143
+ FS.enableConsole = guard('enableConsole');
144
+ FS.event = guard('event');
145
+ FS.getCurrentSessionURL = guard('getCurrentSessionURL');
146
+ FS.identify = guard('identify');
147
+ FS.log = guard('log');
148
+ FS.restart = guard('restart');
149
+ FS.setUserVars = guard('setUserVars');
150
+ FS.setVars = guard('setVars');
151
+ FS.shutdown = guard('shutdown');
152
+ return FS;
165
153
  };
154
+ var FullStory = buildFullStoryShim();
166
155
 
167
- exports.anonymize = anonymize;
168
- exports.consent = consent;
169
- exports.event = event;
170
- exports.getCurrentSessionURL = getCurrentSessionURL;
171
- exports.identify = identify;
156
+ exports.FullStory = FullStory;
172
157
  exports.init = init;
173
158
  exports.isInitialized = isInitialized;
174
- exports.log = log;
175
- exports.restart = restart;
176
- exports.setUserVars = setUserVars;
177
- exports.setVars = setVars;
178
- exports.shutdown = shutdown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fullstory/browser",
3
- "version": "1.7.1",
3
+ "version": "2.0.0-beta.0",
4
4
  "description": "The official FullStory browser SDK",
5
5
  "repository": "git://github.com/fullstorydev/fullstory-browser-sdk.git",
6
6
  "homepage": "https://github.com/fullstorydev/fullstory-browser-sdk",
@@ -11,10 +11,10 @@
11
11
  "types": "./dist/index.d.ts",
12
12
  "scripts": {
13
13
  "clean": "rimraf dist",
14
- "test": "npm run lint && npm run test:karma",
15
- "test:karma": "karma start --single-run --browsers ChromeHeadless karma.conf.js",
16
- "lint": "eslint src test",
17
- "build": "tsc src/index.d.ts && rollup -c"
14
+ "pretest": "tsc --project tsconfig.test.json",
15
+ "test": "npm run lint && karma start --single-run --browsers ChromeHeadless karma.conf.js",
16
+ "lint": "eslint src",
17
+ "build": "rollup -c"
18
18
  },
19
19
  "files": [
20
20
  "dist",
@@ -26,14 +26,19 @@
26
26
  "sdk"
27
27
  ],
28
28
  "dependencies": {
29
- "@fullstory/snippet": "1.3.1"
29
+ "@fullstory/snippet": "2.0.0-beta.2"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@babel/core": "^7.8.7",
33
33
  "@babel/eslint-parser": "^7.16.5",
34
34
  "@babel/preset-env": "^7.10.4",
35
35
  "@babel/register": "^7.8.6",
36
- "@rollup/plugin-babel": "^5.3.0",
36
+ "@rollup/plugin-typescript": "^11.1.0",
37
+ "@types/chai": "^4.3.4",
38
+ "@types/mocha": "^10.0.1",
39
+ "@types/node": "^18.15.11",
40
+ "@typescript-eslint/eslint-plugin": "^5.58.0",
41
+ "@typescript-eslint/parser": "^5.58.0",
37
42
  "chai": "^4.1.2",
38
43
  "eslint": "^8.6.0",
39
44
  "eslint-config-airbnb": "^19.0.4",
@@ -48,9 +53,9 @@
48
53
  "karma-webpack": "^5.0.0",
49
54
  "mocha": "^9.2.0",
50
55
  "rimraf": "^2.7.1",
51
- "rollup": "^1.32.1",
52
- "rollup-plugin-copy": "^3.3.0",
53
- "typescript": "^3.8.3",
56
+ "rollup": "^3.21.0",
57
+ "tslib": "^2.5.0",
58
+ "typescript": "^4.2.4",
54
59
  "webpack": "^5.76.1"
55
60
  }
56
61
  }
@@ -0,0 +1,172 @@
1
+ import { assert, expect } from 'chai';
2
+ import {
3
+ FullStory as FS,
4
+ init,
5
+ isInitialized,
6
+ SnippetOptions,
7
+ } from '.';
8
+
9
+ const testOrg = '123';
10
+
11
+ beforeEach(() => {
12
+ if (window[window._fs_namespace ?? '']) {
13
+ delete window._fs_initialized;
14
+ delete window._fs_dev_mode;
15
+ delete window[window._fs_namespace ?? ''];
16
+ delete window._fs_namespace;
17
+ }
18
+ });
19
+
20
+ describe('init', () => {
21
+ it('should throw error if not initialized with an orgId', () => {
22
+ expect(() => { init({} as SnippetOptions); }).to.throw();
23
+ });
24
+
25
+ it('should throw error if API called before init', () => {
26
+ try {
27
+ FS('log', { msg: 'my log' });
28
+ expect(false).to.be('this should have thrown');
29
+ } catch (error) {
30
+ expect((error as Error).message).to.match(/FullStory is not loaded/);
31
+ }
32
+ init({ orgId: testOrg });
33
+ expect(() => { FS('log', { msg: 'my log' }); }).to.not.throw();
34
+ });
35
+
36
+ it('should add _fs_org value to window object', () => {
37
+ init({ orgId: testOrg });
38
+ expect(window._fs_org).to.equal(testOrg);
39
+ });
40
+
41
+ it('should add _fs_run_in_iframe value to window object', () => {
42
+ init({
43
+ orgId: testOrg,
44
+ recordCrossDomainIFrames: true,
45
+ });
46
+ expect(window._fs_run_in_iframe).to.equal(true);
47
+ });
48
+
49
+ it('should add _fs_is_outer_script value to window object', () => {
50
+ init({
51
+ orgId: testOrg,
52
+ recordOnlyThisIFrame: true,
53
+ });
54
+ expect(window._fs_is_outer_script).to.equal(true);
55
+ });
56
+
57
+ it('should add _fs_dev_mode value to window when initialzed with devMode', () => {
58
+ init({
59
+ orgId: testOrg,
60
+ devMode: true,
61
+ });
62
+
63
+ expect(window._fs_dev_mode).to.equal(true);
64
+ });
65
+
66
+ it('should return whether initialized', () => {
67
+ let isInit = isInitialized();
68
+ expect(isInit).to.equal(false);
69
+
70
+ init({
71
+ orgId: testOrg,
72
+ });
73
+
74
+ isInit = isInitialized();
75
+ expect(isInit).to.equal(true);
76
+ });
77
+
78
+ it('should load fs-debug.js when debug is set', () => {
79
+ init({
80
+ orgId: testOrg,
81
+ debug: true,
82
+ });
83
+
84
+ expect(window._fs_script).to.match(/fs-debug.js$/);
85
+ });
86
+ });
87
+
88
+ describe('devMode', () => {
89
+ let consoleWarnedMessage;
90
+ const oldConsoleWarn = console.warn;
91
+ beforeEach(() => {
92
+ console.warn = (msg) => { consoleWarnedMessage = msg; };
93
+ });
94
+
95
+ afterEach(() => {
96
+ console.warn = oldConsoleWarn;
97
+ consoleWarnedMessage = undefined;
98
+ });
99
+
100
+ it('should return a message for functions invoked when in devMode', () => {
101
+ expect(consoleWarnedMessage).to.equal(undefined);
102
+ init({
103
+ orgId: testOrg,
104
+ devMode: true,
105
+ });
106
+
107
+ expect(consoleWarnedMessage).to.match(/FullStory was initialized in devMode/);
108
+
109
+ FS('log', { msg: 'hello world' });
110
+
111
+ expect(consoleWarnedMessage).to.match(/FullStory is in dev mode/);
112
+ });
113
+ });
114
+
115
+ describe('getCurrentSessionURL', () => {
116
+ it('should return null before fs.js is fully bootstrapped', () => {
117
+ init({ orgId: testOrg });
118
+ // in theory, this is a race condition - assuming that fs.js
119
+ // can't load by the time the following statement is executed
120
+ const url = FS('getSession');
121
+ assert.equal(
122
+ url,
123
+ null,
124
+ 'FullStory.getCurrentSessionURL() should return null if executed before fs.js is fully bootstrapped'
125
+ );
126
+ });
127
+ });
128
+
129
+ describe('typescript safety', () => {
130
+ it('provides type assistance matching the api', () => {
131
+ // Just a quick non-exhaustive check that types are working as expected.
132
+ // The "@ts-expect-error" declaration will fail if the types do NOT throw
133
+ // an error.
134
+ init({ orgId: testOrg });
135
+
136
+ // Passes TypeScript check
137
+ FS('getSession', { format: 'url.now' });
138
+
139
+ // Does not pass (improper format)
140
+ // @ts-expect-error (for testing purposes)
141
+ FS('getSession', { format: '😏' });
142
+
143
+ // Does not pass (invalid action)
144
+ // @ts-expect-error (for testing purposes)
145
+ FS('🦄');
146
+
147
+ // Passes TypeScript check
148
+ FS('observe', { type: 'start', callback: () => console.log('STARTED') });
149
+
150
+ // Does not pass (improper type)
151
+ // @ts-expect-error (for testing purposes)
152
+ FS('observe', { type: '🦂', callback: () => console.log('STARTED') });
153
+
154
+ // LEGACY:
155
+ // Passes TypeScript check
156
+ FS.setVars('user', { email: 'e@mail.com' });
157
+
158
+ // Does not pass (improper VarScope)
159
+ // @ts-expect-error (for testing purposes)
160
+ FS.setVars('🤯', { email: 'e@mail.com' });
161
+
162
+ // Passes TypeScript check
163
+ FS.event('Order Complete', { product_id: 'asdf' });
164
+
165
+ // Does not pass (eventName must be string)
166
+ // @ts-expect-error (for testing purposes)
167
+ FS.event(42, { product_id: 'asdf' });
168
+
169
+ // Assertion for posterity's sake...
170
+ expect(true).to.equal(true);
171
+ });
172
+ });