@croct/plug 0.16.4 → 0.17.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/api/evaluate.cjs +45 -0
- package/api/evaluate.d.cts +23 -0
- package/api/evaluate.d.ts +9 -7
- package/api/evaluate.js +20 -22
- package/api/fetchContent.cjs +53 -0
- package/api/fetchContent.d.cts +45 -0
- package/api/fetchContent.d.ts +31 -11
- package/api/fetchContent.js +29 -21
- package/api/index.cjs +23 -0
- package/api/index.d.cts +27 -0
- package/api/index.d.ts +27 -2
- package/api/index.js +2 -6
- package/component.cjs +15 -0
- package/component.d.cts +19 -0
- package/component.d.ts +12 -9
- package/component.js +0 -3
- package/constants.cjs +39 -0
- package/constants.d.cts +7 -0
- package/constants.d.ts +7 -5
- package/constants.js +12 -9
- package/index.cjs +24 -0
- package/index.d.cts +27 -0
- package/index.d.ts +26 -3
- package/index.js +5 -6
- package/package.json +33 -6
- package/playground.cjs +166 -0
- package/playground.d.cts +64 -0
- package/playground.d.ts +18 -8
- package/playground.js +130 -138
- package/plug.cjs +271 -0
- package/plug.d.cts +86 -0
- package/plug.d.ts +34 -17
- package/plug.js +243 -256
- package/plugin.cjs +15 -0
- package/plugin.d.cts +41 -0
- package/plugin.d.ts +18 -8
- package/plugin.js +0 -3
- package/preview.cjs +180 -0
- package/preview.d.cts +35 -0
- package/preview.d.ts +18 -6
- package/preview.js +143 -152
- package/sdk/apiKey.cjs +21 -0
- package/sdk/apiKey.js +1 -5
- package/sdk/evaluation.cjs +34 -0
- package/sdk/evaluation.d.cts +2 -0
- package/sdk/evaluation.d.ts +1 -1
- package/sdk/evaluation.js +8 -10
- package/sdk/index.cjs +33 -0
- package/sdk/index.d.cts +15 -0
- package/sdk/index.d.ts +9 -6
- package/sdk/index.js +8 -10
- package/sdk/json.cjs +15 -0
- package/sdk/json.d.cts +6 -0
- package/sdk/json.d.ts +4 -1
- package/sdk/json.js +0 -5
- package/sdk/sdkEvents.cjs +15 -0
- package/sdk/sdkEvents.d.cts +1 -0
- package/sdk/sdkEvents.js +0 -3
- package/sdk/token.cjs +27 -0
- package/sdk/token.d.cts +7 -0
- package/sdk/token.d.ts +7 -1
- package/sdk/token.js +4 -6
- package/sdk/tracking.cjs +27 -0
- package/sdk/tracking.d.cts +9 -0
- package/sdk/tracking.d.ts +8 -5
- package/sdk/tracking.js +4 -6
- package/sdk/validation.cjs +23 -0
- package/sdk/validation.js +2 -6
- package/slot.cjs +15 -0
- package/slot.d.cts +42 -0
- package/slot.d.ts +14 -12
- package/slot.js +0 -3
- package/versioning.cjs +15 -0
- package/versioning.d.cts +26 -0
- package/versioning.d.ts +8 -6
- package/versioning.js +0 -3
- package/api/evaluate.js.map +0 -1
- package/api/fetchContent.js.map +0 -1
- package/api/index.js.map +0 -1
- package/component.js.map +0 -1
- package/constants.js.map +0 -1
- package/index.js.map +0 -1
- package/playground.js.map +0 -1
- package/plug.js.map +0 -1
- package/plugin.js.map +0 -1
- package/preview.js.map +0 -1
- package/sdk/apiKey.js.map +0 -1
- package/sdk/evaluation.js.map +0 -1
- package/sdk/index.js.map +0 -1
- package/sdk/json.js.map +0 -1
- package/sdk/sdkEvents.js.map +0 -1
- package/sdk/token.js.map +0 -1
- package/sdk/tracking.js.map +0 -1
- package/sdk/validation.js.map +0 -1
- package/slot.js.map +0 -1
- package/src/api/evaluate.ts +0 -50
- package/src/api/fetchContent.ts +0 -70
- package/src/api/index.ts +0 -2
- package/src/component.ts +0 -19
- package/src/constants.ts +0 -5
- package/src/index.ts +0 -6
- package/src/playground.ts +0 -247
- package/src/plug.ts +0 -429
- package/src/plugin.ts +0 -39
- package/src/preview.ts +0 -226
- package/src/sdk/evaluation.ts +0 -2
- package/src/sdk/index.ts +0 -14
- package/src/sdk/json.ts +0 -4
- package/src/sdk/sdkEvents.ts +0 -1
- package/src/sdk/token.ts +0 -1
- package/src/sdk/tracking.ts +0 -14
- package/src/slot.ts +0 -48
- package/src/versioning.ts +0 -38
- package/versioning.js.map +0 -1
- /package/{src/sdk/apiKey.ts → sdk/apiKey.d.cts} +0 -0
- /package/{src/sdk/validation.ts → sdk/validation.d.cts} +0 -0
package/plugin.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import { TokenStore } from '
|
|
2
|
-
import { EvaluatorFacade } from '
|
|
3
|
-
import { TrackerFacade } from '
|
|
4
|
-
import {
|
|
5
|
-
|
|
1
|
+
import { TokenStore } from '@croct/sdk/token';
|
|
2
|
+
import { EvaluatorFacade } from '@croct/sdk/facade/evaluatorFacade';
|
|
3
|
+
import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
|
|
4
|
+
import { SdkEventManager } from './sdk/index.js';
|
|
5
|
+
import { UserFacade } from '@croct/sdk/facade/userFacade';
|
|
6
|
+
import { SessionFacade } from '@croct/sdk/facade/sessionFacade';
|
|
7
|
+
import { Tab } from '@croct/sdk/tab';
|
|
8
|
+
import { CidAssigner } from '@croct/sdk/cid';
|
|
9
|
+
import { Logger } from '@croct/sdk/logging';
|
|
10
|
+
import '@croct/sdk/eventManager';
|
|
11
|
+
import '@croct/sdk/sdkEvents';
|
|
12
|
+
|
|
13
|
+
interface PluginSdk {
|
|
6
14
|
readonly version: string;
|
|
7
15
|
readonly appId: string;
|
|
8
16
|
readonly tracker: TrackerFacade;
|
|
@@ -18,14 +26,16 @@ export interface PluginSdk {
|
|
|
18
26
|
getTabStorage(...namespace: string[]): Storage;
|
|
19
27
|
getBrowserStorage(...namespace: string[]): Storage;
|
|
20
28
|
}
|
|
21
|
-
|
|
29
|
+
interface PluginArguments<T = any> {
|
|
22
30
|
options: T;
|
|
23
31
|
sdk: PluginSdk;
|
|
24
32
|
}
|
|
25
|
-
|
|
33
|
+
interface PluginFactory<T = any> {
|
|
26
34
|
(args: PluginArguments<T>): Plugin;
|
|
27
35
|
}
|
|
28
|
-
|
|
36
|
+
interface Plugin {
|
|
29
37
|
enable(): Promise<void> | void;
|
|
30
38
|
disable?(): Promise<void> | void;
|
|
31
39
|
}
|
|
40
|
+
|
|
41
|
+
export type { Plugin, PluginArguments, PluginFactory, PluginSdk };
|
package/plugin.js
CHANGED
package/preview.cjs
ADDED
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var preview_exports = {};
|
|
19
|
+
__export(preview_exports, {
|
|
20
|
+
PreviewPlugin: () => PreviewPlugin,
|
|
21
|
+
factory: () => factory
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(preview_exports);
|
|
24
|
+
var import_error = require("@croct/sdk/error");
|
|
25
|
+
var import_token = require('./sdk/token.cjs');
|
|
26
|
+
var import_constants = require('./constants.cjs');
|
|
27
|
+
const PREVIEW_PARAMETER = "croct-preview";
|
|
28
|
+
const PREVIEW_EXIT = "exit";
|
|
29
|
+
const _PreviewPlugin = class _PreviewPlugin {
|
|
30
|
+
constructor(configuration) {
|
|
31
|
+
this.cleanUp = [];
|
|
32
|
+
this.tokenStore = configuration.tokenStore;
|
|
33
|
+
this.logger = configuration.logger;
|
|
34
|
+
}
|
|
35
|
+
enable() {
|
|
36
|
+
const url = new URL(window.location.href);
|
|
37
|
+
const previewData = url.searchParams.get(PREVIEW_PARAMETER);
|
|
38
|
+
if (previewData !== null) {
|
|
39
|
+
this.updateToken(previewData);
|
|
40
|
+
this.updateUrl();
|
|
41
|
+
setTimeout(this.updateUrl, 500);
|
|
42
|
+
}
|
|
43
|
+
const token = this.tokenStore.getToken();
|
|
44
|
+
if (token !== null) {
|
|
45
|
+
this.insertWidget(this.getWidgetUrl(token));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
disable() {
|
|
49
|
+
const callbacks = this.cleanUp.splice(0);
|
|
50
|
+
callbacks.forEach((cleanUp) => cleanUp());
|
|
51
|
+
}
|
|
52
|
+
updateToken(data) {
|
|
53
|
+
if (data === PREVIEW_EXIT) {
|
|
54
|
+
this.logger.debug("Exiting preview mode.");
|
|
55
|
+
this.tokenStore.setToken(null);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
let token = import_token.Token.parse(data);
|
|
60
|
+
const { exp } = token.getPayload();
|
|
61
|
+
if (exp !== void 0 && exp <= Date.now() / 1e3) {
|
|
62
|
+
this.logger.debug("Preview token expired.");
|
|
63
|
+
token = null;
|
|
64
|
+
}
|
|
65
|
+
this.logger.debug("Preview token updated.");
|
|
66
|
+
this.tokenStore.setToken(token);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
this.tokenStore.setToken(null);
|
|
69
|
+
this.logger.warn(`Invalid preview token: ${(0, import_error.formatCause)(error)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
getWidgetUrl(token) {
|
|
73
|
+
const params = this.getWidgetParams(token);
|
|
74
|
+
let queryString = params.toString();
|
|
75
|
+
if (queryString !== "") {
|
|
76
|
+
queryString = `?${queryString}`;
|
|
77
|
+
}
|
|
78
|
+
return `${import_constants.PREVIEW_WIDGET_URL}${queryString}`;
|
|
79
|
+
}
|
|
80
|
+
getWidgetParams(token) {
|
|
81
|
+
const { metadata = {} } = token.getPayload();
|
|
82
|
+
const params = new URLSearchParams();
|
|
83
|
+
if (metadata === null || typeof metadata !== "object" || Array.isArray(metadata)) {
|
|
84
|
+
return params;
|
|
85
|
+
}
|
|
86
|
+
for (const [key, param] of Object.entries(_PreviewPlugin.PREVIEW_PARAMS)) {
|
|
87
|
+
const value = metadata[key];
|
|
88
|
+
if (typeof value === "string") {
|
|
89
|
+
params.set(param, value);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return params;
|
|
93
|
+
}
|
|
94
|
+
insertWidget(url) {
|
|
95
|
+
const widget = this.createWidget(url);
|
|
96
|
+
const onMessage = (event) => {
|
|
97
|
+
if (event.origin !== import_constants.PREVIEW_WIDGET_ORIGIN) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
switch (event.data.type) {
|
|
101
|
+
case "croct:preview:leave": {
|
|
102
|
+
this.tokenStore.setToken(null);
|
|
103
|
+
const exitUrl = new URL(window.location.href);
|
|
104
|
+
exitUrl.searchParams.set(PREVIEW_PARAMETER, PREVIEW_EXIT);
|
|
105
|
+
window.location.replace(exitUrl.toString());
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
case "croct:preview:resize":
|
|
109
|
+
widget.style.width = `${event.data.width}px`;
|
|
110
|
+
widget.style.height = `${event.data.height}px`;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
window.addEventListener("message", onMessage);
|
|
115
|
+
this.cleanUp.push(() => window.removeEventListener("message", onMessage));
|
|
116
|
+
const insert = () => {
|
|
117
|
+
const container = document.body;
|
|
118
|
+
container.prepend(widget);
|
|
119
|
+
this.cleanUp.push(() => container.removeChild(widget));
|
|
120
|
+
this.logger.debug("Preview widget mounted.");
|
|
121
|
+
const observer = new MutationObserver(() => {
|
|
122
|
+
if (!container.contains(widget)) {
|
|
123
|
+
container.prepend(widget);
|
|
124
|
+
this.logger.debug("Preview widget removed from DOM, reinserting.");
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
observer.observe(container, { childList: true });
|
|
128
|
+
this.cleanUp.unshift(() => observer.disconnect());
|
|
129
|
+
};
|
|
130
|
+
if (document.readyState !== "loading") {
|
|
131
|
+
insert();
|
|
132
|
+
} else {
|
|
133
|
+
this.logger.debug("Waiting for document to be ready.");
|
|
134
|
+
this.cleanUp.push(() => window.removeEventListener("DOMContentLoaded", insert));
|
|
135
|
+
window.addEventListener("DOMContentLoaded", insert);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
updateUrl() {
|
|
139
|
+
const url = new URL(window.location.href);
|
|
140
|
+
if (url.searchParams.has(PREVIEW_PARAMETER)) {
|
|
141
|
+
url.searchParams.delete(PREVIEW_PARAMETER);
|
|
142
|
+
window.history.replaceState({}, "", url.toString());
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
createWidget(url) {
|
|
146
|
+
const widget = document.createElement("iframe");
|
|
147
|
+
widget.setAttribute("src", url);
|
|
148
|
+
widget.setAttribute("sandbox", "allow-scripts allow-same-origin");
|
|
149
|
+
widget.style.position = "fixed";
|
|
150
|
+
widget.style.width = "0px";
|
|
151
|
+
widget.style.height = "0px";
|
|
152
|
+
widget.style.right = "0";
|
|
153
|
+
widget.style.bottom = "0";
|
|
154
|
+
widget.style.border = "0";
|
|
155
|
+
widget.style.overflow = "hidden";
|
|
156
|
+
widget.style.zIndex = "2147483647";
|
|
157
|
+
return widget;
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
_PreviewPlugin.PREVIEW_PARAMS = {
|
|
161
|
+
previewMode: "previewMode",
|
|
162
|
+
experienceName: "experience",
|
|
163
|
+
experimentName: "experiment",
|
|
164
|
+
audienceName: "audience",
|
|
165
|
+
variantName: "variant",
|
|
166
|
+
locale: "locale"
|
|
167
|
+
};
|
|
168
|
+
let PreviewPlugin = _PreviewPlugin;
|
|
169
|
+
const factory = (props) => {
|
|
170
|
+
const { sdk } = props;
|
|
171
|
+
return new PreviewPlugin({
|
|
172
|
+
tokenStore: props.sdk.previewTokenStore,
|
|
173
|
+
logger: sdk.getLogger()
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
177
|
+
0 && (module.exports = {
|
|
178
|
+
PreviewPlugin,
|
|
179
|
+
factory
|
|
180
|
+
});
|
package/preview.d.cts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Logger } from '@croct/sdk/logging';
|
|
2
|
+
import { Plugin, PluginFactory } from './plugin.cjs';
|
|
3
|
+
import { TokenStore } from '@croct/sdk/token';
|
|
4
|
+
import '@croct/sdk/facade/evaluatorFacade';
|
|
5
|
+
import '@croct/sdk/facade/trackerFacade';
|
|
6
|
+
import './sdk/index.cjs';
|
|
7
|
+
import '@croct/sdk/eventManager';
|
|
8
|
+
import '@croct/sdk/sdkEvents';
|
|
9
|
+
import '@croct/sdk/facade/sessionFacade';
|
|
10
|
+
import '@croct/sdk/facade/userFacade';
|
|
11
|
+
import '@croct/sdk/tab';
|
|
12
|
+
import '@croct/sdk/cid';
|
|
13
|
+
|
|
14
|
+
type Configuration = {
|
|
15
|
+
tokenStore: TokenStore;
|
|
16
|
+
logger: Logger;
|
|
17
|
+
};
|
|
18
|
+
declare class PreviewPlugin implements Plugin {
|
|
19
|
+
private static readonly PREVIEW_PARAMS;
|
|
20
|
+
private readonly tokenStore;
|
|
21
|
+
private readonly logger;
|
|
22
|
+
private cleanUp;
|
|
23
|
+
constructor(configuration: Configuration);
|
|
24
|
+
enable(): void;
|
|
25
|
+
disable(): void;
|
|
26
|
+
private updateToken;
|
|
27
|
+
private getWidgetUrl;
|
|
28
|
+
private getWidgetParams;
|
|
29
|
+
private insertWidget;
|
|
30
|
+
private updateUrl;
|
|
31
|
+
private createWidget;
|
|
32
|
+
}
|
|
33
|
+
declare const factory: PluginFactory;
|
|
34
|
+
|
|
35
|
+
export { type Configuration, PreviewPlugin, factory };
|
package/preview.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
-
import { Logger } from '
|
|
2
|
-
import { Plugin, PluginFactory } from './plugin';
|
|
3
|
-
import { TokenStore } from '
|
|
4
|
-
|
|
1
|
+
import { Logger } from '@croct/sdk/logging';
|
|
2
|
+
import { Plugin, PluginFactory } from './plugin.js';
|
|
3
|
+
import { TokenStore } from '@croct/sdk/token';
|
|
4
|
+
import '@croct/sdk/facade/evaluatorFacade';
|
|
5
|
+
import '@croct/sdk/facade/trackerFacade';
|
|
6
|
+
import './sdk/index.js';
|
|
7
|
+
import '@croct/sdk/eventManager';
|
|
8
|
+
import '@croct/sdk/sdkEvents';
|
|
9
|
+
import '@croct/sdk/facade/sessionFacade';
|
|
10
|
+
import '@croct/sdk/facade/userFacade';
|
|
11
|
+
import '@croct/sdk/tab';
|
|
12
|
+
import '@croct/sdk/cid';
|
|
13
|
+
|
|
14
|
+
type Configuration = {
|
|
5
15
|
tokenStore: TokenStore;
|
|
6
16
|
logger: Logger;
|
|
7
17
|
};
|
|
8
|
-
|
|
18
|
+
declare class PreviewPlugin implements Plugin {
|
|
9
19
|
private static readonly PREVIEW_PARAMS;
|
|
10
20
|
private readonly tokenStore;
|
|
11
21
|
private readonly logger;
|
|
@@ -20,4 +30,6 @@ export declare class PreviewPlugin implements Plugin {
|
|
|
20
30
|
private updateUrl;
|
|
21
31
|
private createWidget;
|
|
22
32
|
}
|
|
23
|
-
|
|
33
|
+
declare const factory: PluginFactory;
|
|
34
|
+
|
|
35
|
+
export { type Configuration, PreviewPlugin, factory };
|
package/preview.js
CHANGED
|
@@ -1,165 +1,156 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
import { formatCause } from "@croct/sdk/error";
|
|
2
|
+
import { Token } from "./sdk/token.js";
|
|
3
|
+
import { PREVIEW_WIDGET_ORIGIN, PREVIEW_WIDGET_URL } from "./constants.js";
|
|
4
|
+
const PREVIEW_PARAMETER = "croct-preview";
|
|
5
|
+
const PREVIEW_EXIT = "exit";
|
|
6
|
+
const _PreviewPlugin = class _PreviewPlugin {
|
|
7
|
+
constructor(configuration) {
|
|
8
|
+
this.cleanUp = [];
|
|
9
|
+
this.tokenStore = configuration.tokenStore;
|
|
10
|
+
this.logger = configuration.logger;
|
|
11
|
+
}
|
|
12
|
+
enable() {
|
|
13
|
+
const url = new URL(window.location.href);
|
|
14
|
+
const previewData = url.searchParams.get(PREVIEW_PARAMETER);
|
|
15
|
+
if (previewData !== null) {
|
|
16
|
+
this.updateToken(previewData);
|
|
17
|
+
this.updateUrl();
|
|
18
|
+
setTimeout(this.updateUrl, 500);
|
|
14
19
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if (previewData !== null) {
|
|
19
|
-
this.updateToken(previewData);
|
|
20
|
-
this.updateUrl();
|
|
21
|
-
// Some frameworks (e.g. Next) may revert the URL change
|
|
22
|
-
// after the page is loaded, so ensure the token is removed
|
|
23
|
-
// from the URL after a short delay.
|
|
24
|
-
setTimeout(this.updateUrl, 500);
|
|
25
|
-
}
|
|
26
|
-
const token = this.tokenStore.getToken();
|
|
27
|
-
if (token !== null) {
|
|
28
|
-
this.insertWidget(this.getWidgetUrl(token));
|
|
29
|
-
}
|
|
20
|
+
const token = this.tokenStore.getToken();
|
|
21
|
+
if (token !== null) {
|
|
22
|
+
this.insertWidget(this.getWidgetUrl(token));
|
|
30
23
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
24
|
+
}
|
|
25
|
+
disable() {
|
|
26
|
+
const callbacks = this.cleanUp.splice(0);
|
|
27
|
+
callbacks.forEach((cleanUp) => cleanUp());
|
|
28
|
+
}
|
|
29
|
+
updateToken(data) {
|
|
30
|
+
if (data === PREVIEW_EXIT) {
|
|
31
|
+
this.logger.debug("Exiting preview mode.");
|
|
32
|
+
this.tokenStore.setToken(null);
|
|
33
|
+
return;
|
|
34
34
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
this.logger.debug('Preview token updated.');
|
|
49
|
-
this.tokenStore.setToken(token);
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
this.tokenStore.setToken(null);
|
|
53
|
-
this.logger.warn(`Invalid preview token: ${(0, error_1.formatCause)(error)}`);
|
|
54
|
-
}
|
|
35
|
+
try {
|
|
36
|
+
let token = Token.parse(data);
|
|
37
|
+
const { exp } = token.getPayload();
|
|
38
|
+
if (exp !== void 0 && exp <= Date.now() / 1e3) {
|
|
39
|
+
this.logger.debug("Preview token expired.");
|
|
40
|
+
token = null;
|
|
41
|
+
}
|
|
42
|
+
this.logger.debug("Preview token updated.");
|
|
43
|
+
this.tokenStore.setToken(token);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
this.tokenStore.setToken(null);
|
|
46
|
+
this.logger.warn(`Invalid preview token: ${formatCause(error)}`);
|
|
55
47
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
return `${constants_1.PREVIEW_WIDGET_URL}${queryString}`;
|
|
48
|
+
}
|
|
49
|
+
getWidgetUrl(token) {
|
|
50
|
+
const params = this.getWidgetParams(token);
|
|
51
|
+
let queryString = params.toString();
|
|
52
|
+
if (queryString !== "") {
|
|
53
|
+
queryString = `?${queryString}`;
|
|
63
54
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const value = metadata[key];
|
|
72
|
-
if (typeof value === 'string') {
|
|
73
|
-
params.set(param, value);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return params;
|
|
55
|
+
return `${PREVIEW_WIDGET_URL}${queryString}`;
|
|
56
|
+
}
|
|
57
|
+
getWidgetParams(token) {
|
|
58
|
+
const { metadata = {} } = token.getPayload();
|
|
59
|
+
const params = new URLSearchParams();
|
|
60
|
+
if (metadata === null || typeof metadata !== "object" || Array.isArray(metadata)) {
|
|
61
|
+
return params;
|
|
77
62
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
switch (event.data.type) {
|
|
85
|
-
case 'croct:preview:leave': {
|
|
86
|
-
this.tokenStore.setToken(null);
|
|
87
|
-
const exitUrl = new URL(window.location.href);
|
|
88
|
-
exitUrl.searchParams.set(PREVIEW_PARAMETER, PREVIEW_EXIT);
|
|
89
|
-
window.location.replace(exitUrl.toString());
|
|
90
|
-
break;
|
|
91
|
-
}
|
|
92
|
-
case 'croct:preview:resize':
|
|
93
|
-
widget.style.width = `${event.data.width}px`;
|
|
94
|
-
widget.style.height = `${event.data.height}px`;
|
|
95
|
-
break;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
window.addEventListener('message', onMessage);
|
|
99
|
-
this.cleanUp.push(() => window.removeEventListener('message', onMessage));
|
|
100
|
-
const insert = () => {
|
|
101
|
-
const container = document.body;
|
|
102
|
-
container.prepend(widget);
|
|
103
|
-
this.cleanUp.push(() => container.removeChild(widget));
|
|
104
|
-
this.logger.debug('Preview widget mounted.');
|
|
105
|
-
// Reinsert the widget if it is removed from the DOM (e.g. by a framework)
|
|
106
|
-
const observer = new MutationObserver(() => {
|
|
107
|
-
if (!container.contains(widget)) {
|
|
108
|
-
container.prepend(widget);
|
|
109
|
-
this.logger.debug('Preview widget removed from DOM, reinserting.');
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
observer.observe(container, { childList: true });
|
|
113
|
-
// Make sure the observer is disconnected before the widget is removed.
|
|
114
|
-
// Otherwise, the observer may reinsert the widget after it is removed.
|
|
115
|
-
this.cleanUp.unshift(() => observer.disconnect());
|
|
116
|
-
};
|
|
117
|
-
if (document.readyState !== 'loading') {
|
|
118
|
-
insert();
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
this.logger.debug('Waiting for document to be ready.');
|
|
122
|
-
this.cleanUp.push(() => window.removeEventListener('DOMContentLoaded', insert));
|
|
123
|
-
window.addEventListener('DOMContentLoaded', insert);
|
|
124
|
-
}
|
|
63
|
+
for (const [key, param] of Object.entries(_PreviewPlugin.PREVIEW_PARAMS)) {
|
|
64
|
+
const value = metadata[key];
|
|
65
|
+
if (typeof value === "string") {
|
|
66
|
+
params.set(param, value);
|
|
67
|
+
}
|
|
125
68
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
69
|
+
return params;
|
|
70
|
+
}
|
|
71
|
+
insertWidget(url) {
|
|
72
|
+
const widget = this.createWidget(url);
|
|
73
|
+
const onMessage = (event) => {
|
|
74
|
+
if (event.origin !== PREVIEW_WIDGET_ORIGIN) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
switch (event.data.type) {
|
|
78
|
+
case "croct:preview:leave": {
|
|
79
|
+
this.tokenStore.setToken(null);
|
|
80
|
+
const exitUrl = new URL(window.location.href);
|
|
81
|
+
exitUrl.searchParams.set(PREVIEW_PARAMETER, PREVIEW_EXIT);
|
|
82
|
+
window.location.replace(exitUrl.toString());
|
|
83
|
+
break;
|
|
131
84
|
}
|
|
85
|
+
case "croct:preview:resize":
|
|
86
|
+
widget.style.width = `${event.data.width}px`;
|
|
87
|
+
widget.style.height = `${event.data.height}px`;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
window.addEventListener("message", onMessage);
|
|
92
|
+
this.cleanUp.push(() => window.removeEventListener("message", onMessage));
|
|
93
|
+
const insert = () => {
|
|
94
|
+
const container = document.body;
|
|
95
|
+
container.prepend(widget);
|
|
96
|
+
this.cleanUp.push(() => container.removeChild(widget));
|
|
97
|
+
this.logger.debug("Preview widget mounted.");
|
|
98
|
+
const observer = new MutationObserver(() => {
|
|
99
|
+
if (!container.contains(widget)) {
|
|
100
|
+
container.prepend(widget);
|
|
101
|
+
this.logger.debug("Preview widget removed from DOM, reinserting.");
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
observer.observe(container, { childList: true });
|
|
105
|
+
this.cleanUp.unshift(() => observer.disconnect());
|
|
106
|
+
};
|
|
107
|
+
if (document.readyState !== "loading") {
|
|
108
|
+
insert();
|
|
109
|
+
} else {
|
|
110
|
+
this.logger.debug("Waiting for document to be ready.");
|
|
111
|
+
this.cleanUp.push(() => window.removeEventListener("DOMContentLoaded", insert));
|
|
112
|
+
window.addEventListener("DOMContentLoaded", insert);
|
|
132
113
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
widget.style.height = '0px';
|
|
140
|
-
widget.style.right = '0';
|
|
141
|
-
widget.style.bottom = '0';
|
|
142
|
-
widget.style.border = '0';
|
|
143
|
-
widget.style.overflow = 'hidden';
|
|
144
|
-
widget.style.zIndex = '2147483647';
|
|
145
|
-
return widget;
|
|
114
|
+
}
|
|
115
|
+
updateUrl() {
|
|
116
|
+
const url = new URL(window.location.href);
|
|
117
|
+
if (url.searchParams.has(PREVIEW_PARAMETER)) {
|
|
118
|
+
url.searchParams.delete(PREVIEW_PARAMETER);
|
|
119
|
+
window.history.replaceState({}, "", url.toString());
|
|
146
120
|
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
121
|
+
}
|
|
122
|
+
createWidget(url) {
|
|
123
|
+
const widget = document.createElement("iframe");
|
|
124
|
+
widget.setAttribute("src", url);
|
|
125
|
+
widget.setAttribute("sandbox", "allow-scripts allow-same-origin");
|
|
126
|
+
widget.style.position = "fixed";
|
|
127
|
+
widget.style.width = "0px";
|
|
128
|
+
widget.style.height = "0px";
|
|
129
|
+
widget.style.right = "0";
|
|
130
|
+
widget.style.bottom = "0";
|
|
131
|
+
widget.style.border = "0";
|
|
132
|
+
widget.style.overflow = "hidden";
|
|
133
|
+
widget.style.zIndex = "2147483647";
|
|
134
|
+
return widget;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
_PreviewPlugin.PREVIEW_PARAMS = {
|
|
138
|
+
previewMode: "previewMode",
|
|
139
|
+
experienceName: "experience",
|
|
140
|
+
experimentName: "experiment",
|
|
141
|
+
audienceName: "audience",
|
|
142
|
+
variantName: "variant",
|
|
143
|
+
locale: "locale"
|
|
156
144
|
};
|
|
145
|
+
let PreviewPlugin = _PreviewPlugin;
|
|
157
146
|
const factory = (props) => {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
147
|
+
const { sdk } = props;
|
|
148
|
+
return new PreviewPlugin({
|
|
149
|
+
tokenStore: props.sdk.previewTokenStore,
|
|
150
|
+
logger: sdk.getLogger()
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
export {
|
|
154
|
+
PreviewPlugin,
|
|
155
|
+
factory
|
|
163
156
|
};
|
|
164
|
-
exports.factory = factory;
|
|
165
|
-
//# sourceMappingURL=preview.js.map
|
package/sdk/apiKey.cjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var apiKey_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(apiKey_exports);
|
|
17
|
+
__reExport(apiKey_exports, require("@croct/sdk/apiKey"), module.exports);
|
|
18
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
19
|
+
0 && (module.exports = {
|
|
20
|
+
...require("@croct/sdk/apiKey")
|
|
21
|
+
});
|
package/sdk/apiKey.js
CHANGED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var evaluation_exports = {};
|
|
19
|
+
__export(evaluation_exports, {
|
|
20
|
+
EvaluationError: () => import_evaluator.EvaluationError,
|
|
21
|
+
EvaluationErrorType: () => import_evaluator.EvaluationErrorType,
|
|
22
|
+
EvaluatorFacade: () => import_evaluatorFacade.EvaluatorFacade,
|
|
23
|
+
QueryError: () => import_evaluator.QueryError
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(evaluation_exports);
|
|
26
|
+
var import_evaluatorFacade = require("@croct/sdk/facade/evaluatorFacade");
|
|
27
|
+
var import_evaluator = require("@croct/sdk/evaluator");
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
EvaluationError,
|
|
31
|
+
EvaluationErrorType,
|
|
32
|
+
EvaluatorFacade,
|
|
33
|
+
QueryError
|
|
34
|
+
});
|