@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/plug.d.ts
CHANGED
|
@@ -2,24 +2,37 @@ import { SessionFacade } from '@croct/sdk/facade/sessionFacade';
|
|
|
2
2
|
import { UserFacade } from '@croct/sdk/facade/userFacade';
|
|
3
3
|
import { TrackerFacade } from '@croct/sdk/facade/trackerFacade';
|
|
4
4
|
import { EvaluationOptions, EvaluatorFacade } from '@croct/sdk/facade/evaluatorFacade';
|
|
5
|
-
import { Configuration as
|
|
5
|
+
import { Configuration as Configuration$1 } from '@croct/sdk/facade/sdkFacade';
|
|
6
6
|
import { Optional } from '@croct/sdk/utilityTypes';
|
|
7
|
-
import {
|
|
8
|
-
import { FetchOptions as
|
|
9
|
-
import { PluginFactory } from './plugin';
|
|
10
|
-
import { VersionedSlotId, SlotContent } from './slot';
|
|
11
|
-
import { JsonValue, JsonObject } from '
|
|
12
|
-
|
|
7
|
+
import { ExternalTrackingEventType, ExternalTrackingEventPayload, ExternalTrackingEvent } from '@croct/sdk/trackingEvents';
|
|
8
|
+
import { FetchOptions as FetchOptions$1 } from '@croct/sdk/facade/contentFetcherFacade';
|
|
9
|
+
import { PluginFactory } from './plugin.js';
|
|
10
|
+
import { VersionedSlotId, SlotContent } from './slot.js';
|
|
11
|
+
import { JsonValue, JsonObject } from '@croct/json/mutable';
|
|
12
|
+
import '@croct/sdk/token';
|
|
13
|
+
import './sdk/index.js';
|
|
14
|
+
import '@croct/sdk/eventManager';
|
|
15
|
+
import '@croct/sdk/sdkEvents';
|
|
16
|
+
import '@croct/sdk/logging';
|
|
17
|
+
import '@croct/sdk/tab';
|
|
18
|
+
import '@croct/sdk/cid';
|
|
19
|
+
import '@croct/json';
|
|
20
|
+
import './versioning.js';
|
|
21
|
+
import './component.js';
|
|
22
|
+
|
|
23
|
+
interface PluginConfigurations {
|
|
13
24
|
[key: string]: any;
|
|
14
25
|
}
|
|
15
|
-
|
|
26
|
+
type Configuration = Optional<Configuration$1, 'appId'> & {
|
|
16
27
|
plugins?: PluginConfigurations;
|
|
17
28
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
content: SlotContent<I, C>;
|
|
29
|
+
type FetchOptions<T> = Omit<FetchOptions$1, 'version'> & {
|
|
30
|
+
fallback?: T;
|
|
21
31
|
};
|
|
22
|
-
|
|
32
|
+
type FetchResponse<I extends VersionedSlotId, C extends JsonObject = JsonObject, F = never> = {
|
|
33
|
+
content: SlotContent<I, C> | F;
|
|
34
|
+
};
|
|
35
|
+
interface Plug {
|
|
23
36
|
readonly tracker: TrackerFacade;
|
|
24
37
|
readonly user: UserFacade;
|
|
25
38
|
readonly session: SessionFacade;
|
|
@@ -33,12 +46,13 @@ export interface Plug {
|
|
|
33
46
|
anonymize(): void;
|
|
34
47
|
setToken(token: string): void;
|
|
35
48
|
unsetToken(): void;
|
|
36
|
-
track<T extends
|
|
49
|
+
track<T extends ExternalTrackingEventType>(type: T, payload: ExternalTrackingEventPayload<T>): Promise<ExternalTrackingEvent<T>>;
|
|
37
50
|
evaluate<T extends JsonValue>(expression: string, options?: EvaluationOptions): Promise<T>;
|
|
38
|
-
fetch<
|
|
51
|
+
fetch<I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I>>): Promise<FetchResponse<I>>;
|
|
52
|
+
fetch<F, I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I> | F>): Promise<FetchResponse<I, JsonObject, F>>;
|
|
39
53
|
unplug(): Promise<void>;
|
|
40
54
|
}
|
|
41
|
-
|
|
55
|
+
declare class GlobalPlug implements Plug {
|
|
42
56
|
private pluginFactories;
|
|
43
57
|
private instance?;
|
|
44
58
|
private plugins;
|
|
@@ -61,9 +75,12 @@ export declare class GlobalPlug implements Plug {
|
|
|
61
75
|
anonymize(): void;
|
|
62
76
|
setToken(token: string): void;
|
|
63
77
|
unsetToken(): void;
|
|
64
|
-
track<T extends
|
|
78
|
+
track<T extends ExternalTrackingEventType>(type: T, payload: ExternalTrackingEventPayload<T>): Promise<ExternalTrackingEvent<T>>;
|
|
65
79
|
evaluate<T extends JsonValue>(query: string, options?: EvaluationOptions): Promise<T>;
|
|
66
80
|
test(expression: string, options?: EvaluationOptions): Promise<boolean>;
|
|
67
|
-
fetch<
|
|
81
|
+
fetch<I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I>>): Promise<FetchResponse<I>>;
|
|
82
|
+
fetch<F, I extends VersionedSlotId>(slotId: I, options?: FetchOptions<SlotContent<I> | F>): Promise<FetchResponse<I, JsonObject, F>>;
|
|
68
83
|
unplug(): Promise<void>;
|
|
69
84
|
}
|
|
85
|
+
|
|
86
|
+
export { type Configuration, type FetchOptions, type FetchResponse, GlobalPlug, type Plug, type PluginConfigurations };
|
package/plug.js
CHANGED
|
@@ -1,261 +1,248 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
const preview_1 = require("./preview");
|
|
12
|
-
const PLUGIN_NAMESPACE = 'Plugin';
|
|
1
|
+
import { SdkFacade } from "@croct/sdk/facade/sdkFacade";
|
|
2
|
+
import { formatCause } from "@croct/sdk/error";
|
|
3
|
+
import { describe } from "@croct/sdk/validation";
|
|
4
|
+
import { Token } from "@croct/sdk/token";
|
|
5
|
+
import { VERSION } from "@croct/sdk";
|
|
6
|
+
import { loadSlotContent } from "@croct/content";
|
|
7
|
+
import { CDN_URL } from "./constants.js";
|
|
8
|
+
import { factory as playgroundPluginFactory } from "./playground.js";
|
|
9
|
+
import { factory as previewPluginFactory } from "./preview.js";
|
|
10
|
+
const PLUGIN_NAMESPACE = "Plugin";
|
|
13
11
|
function detectAppId() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
const script = window.document.querySelector(`script[src^='${CDN_URL}']`);
|
|
13
|
+
if (!(script instanceof HTMLScriptElement)) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return new URL(script.src).searchParams.get("appId");
|
|
19
17
|
}
|
|
20
18
|
class GlobalPlug {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
getBrowserStorage: (...namespace) => (sdk.getBrowserStorage(PLUGIN_NAMESPACE, name, ...namespace)),
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
let plugin;
|
|
114
|
-
try {
|
|
115
|
-
plugin = factory(args);
|
|
116
|
-
}
|
|
117
|
-
catch (error) {
|
|
118
|
-
logger.error(`Failed to initialize plugin "${name}": ${(0, error_1.formatCause)(error)}`);
|
|
119
|
-
continue;
|
|
120
|
-
}
|
|
121
|
-
logger.debug(`Plugin "${name}" initialized`);
|
|
122
|
-
if (typeof plugin !== 'object') {
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
this.plugins[name] = plugin;
|
|
126
|
-
const promise = plugin.enable();
|
|
127
|
-
if (!(promise instanceof Promise)) {
|
|
128
|
-
logger.debug(`Plugin "${name}" enabled`);
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
pending.push(promise.then(() => logger.debug(`Plugin "${name}" enabled`))
|
|
132
|
-
.catch(error => logger.error(`Failed to enable plugin "${name}": ${(0, error_1.formatCause)(error)}`)));
|
|
133
|
-
}
|
|
134
|
-
Promise.all(pending)
|
|
135
|
-
.then(() => {
|
|
136
|
-
this.initialize();
|
|
137
|
-
logger.debug('Initialization complete');
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
get initialized() {
|
|
141
|
-
return this.instance !== undefined;
|
|
142
|
-
}
|
|
143
|
-
get plugged() {
|
|
144
|
-
return this.ready.then(() => this);
|
|
145
|
-
}
|
|
146
|
-
get flushed() {
|
|
147
|
-
return this.tracker
|
|
148
|
-
.flushed
|
|
149
|
-
.then(() => this);
|
|
150
|
-
}
|
|
151
|
-
get sdk() {
|
|
152
|
-
if (this.instance === undefined) {
|
|
153
|
-
throw new Error('Croct is not plugged in. For help, see https://croct.help/sdk/javascript/not-plugged-in');
|
|
154
|
-
}
|
|
155
|
-
return this.instance;
|
|
156
|
-
}
|
|
157
|
-
get tracker() {
|
|
158
|
-
return this.sdk.tracker;
|
|
159
|
-
}
|
|
160
|
-
get evaluator() {
|
|
161
|
-
return this.sdk.evaluator;
|
|
162
|
-
}
|
|
163
|
-
get user() {
|
|
164
|
-
return this.sdk.user;
|
|
165
|
-
}
|
|
166
|
-
get session() {
|
|
167
|
-
return this.sdk.session;
|
|
168
|
-
}
|
|
169
|
-
isAnonymous() {
|
|
170
|
-
return this.sdk
|
|
171
|
-
.context
|
|
172
|
-
.isAnonymous();
|
|
173
|
-
}
|
|
174
|
-
getUserId() {
|
|
175
|
-
return this.sdk
|
|
176
|
-
.context
|
|
177
|
-
.getUser();
|
|
178
|
-
}
|
|
179
|
-
identify(userId) {
|
|
180
|
-
if (typeof userId !== 'string') {
|
|
181
|
-
throw new Error('The user ID must be a string. For help, see https://croct.help/sdk/javascript/invalid-user-id');
|
|
19
|
+
constructor() {
|
|
20
|
+
this.pluginFactories = {
|
|
21
|
+
playground: playgroundPluginFactory,
|
|
22
|
+
preview: previewPluginFactory
|
|
23
|
+
};
|
|
24
|
+
this.plugins = {};
|
|
25
|
+
this.ready = new Promise((resolve) => {
|
|
26
|
+
this.initialize = resolve;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
extend(name, plugin) {
|
|
30
|
+
if (this.pluginFactories[name] !== void 0) {
|
|
31
|
+
throw new Error(`Another plugin is already registered with name "${name}".`);
|
|
32
|
+
}
|
|
33
|
+
this.pluginFactories[name] = plugin;
|
|
34
|
+
}
|
|
35
|
+
plug(configuration = {}) {
|
|
36
|
+
if (this.instance !== void 0) {
|
|
37
|
+
const logger2 = this.instance.getLogger();
|
|
38
|
+
logger2.info("Croct is already plugged in.");
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const detectedAppId = detectAppId();
|
|
42
|
+
const configuredAppId = configuration.appId ?? null;
|
|
43
|
+
if (detectedAppId !== null && configuredAppId !== null && detectedAppId !== configuredAppId) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
'The specified app ID and the auto-detected app ID are conflicting. There is no need to specify an app ID when using an application-specific tag. Please try again omitting the "appId" option. For help, see https://croct.help/sdk/javascript/conflicting-app-id'
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const appId = detectedAppId ?? configuredAppId;
|
|
49
|
+
if (appId === null) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
'The app ID must be specified when it cannot be auto-detected. Please try again specifying the "appId" option.For help, see https://croct.help/sdk/javascript/missing-app-id'
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const { plugins, test, ...sdkConfiguration } = configuration;
|
|
55
|
+
const sdk = SdkFacade.init({
|
|
56
|
+
...sdkConfiguration,
|
|
57
|
+
appId,
|
|
58
|
+
test: test ?? (typeof process === "object" && (process.env?.CROCT_TEST_MODE !== void 0 ? process.env.CROCT_TEST_MODE === "true" : process.env?.NODE_ENV === "test"))
|
|
59
|
+
});
|
|
60
|
+
this.instance = sdk;
|
|
61
|
+
const logger = this.instance.getLogger();
|
|
62
|
+
if (detectedAppId === configuredAppId) {
|
|
63
|
+
logger.warn(
|
|
64
|
+
'It is strongly recommended omitting the "appId" option when using the application-specific tag as it is detected automatically.'
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const pending = [];
|
|
68
|
+
const defaultEnabledPlugins = Object.fromEntries(
|
|
69
|
+
Object.keys(this.pluginFactories).map((name) => [name, true])
|
|
70
|
+
);
|
|
71
|
+
for (const [name, options] of Object.entries({ ...defaultEnabledPlugins, ...plugins })) {
|
|
72
|
+
logger.debug(`Initializing plugin "${name}"...`);
|
|
73
|
+
const factory = this.pluginFactories[name];
|
|
74
|
+
if (factory === void 0) {
|
|
75
|
+
logger.error(`Plugin "${name}" is not registered.`);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (typeof options !== "boolean" && (options === null || typeof options !== "object")) {
|
|
79
|
+
logger.error(
|
|
80
|
+
`Invalid options for plugin "${name}", expected either boolean or object but got ${describe(options)}`
|
|
81
|
+
);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (options === false) {
|
|
85
|
+
logger.warn(`Plugin "${name}" is declared but not enabled`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const args = {
|
|
89
|
+
options: options === true ? {} : options,
|
|
90
|
+
sdk: {
|
|
91
|
+
version: VERSION,
|
|
92
|
+
appId,
|
|
93
|
+
tracker: sdk.tracker,
|
|
94
|
+
evaluator: sdk.evaluator,
|
|
95
|
+
user: sdk.user,
|
|
96
|
+
session: sdk.session,
|
|
97
|
+
tab: sdk.context.getTab(),
|
|
98
|
+
userTokenStore: {
|
|
99
|
+
getToken: sdk.getToken.bind(sdk),
|
|
100
|
+
setToken: sdk.setToken.bind(sdk)
|
|
101
|
+
},
|
|
102
|
+
previewTokenStore: sdk.previewTokenStore,
|
|
103
|
+
cidAssigner: sdk.cidAssigner,
|
|
104
|
+
eventManager: sdk.eventManager,
|
|
105
|
+
getLogger: (...namespace) => sdk.getLogger(PLUGIN_NAMESPACE, name, ...namespace),
|
|
106
|
+
getTabStorage: (...namespace) => sdk.getTabStorage(PLUGIN_NAMESPACE, name, ...namespace),
|
|
107
|
+
getBrowserStorage: (...namespace) => sdk.getBrowserStorage(PLUGIN_NAMESPACE, name, ...namespace)
|
|
182
108
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
109
|
+
};
|
|
110
|
+
let plugin;
|
|
111
|
+
try {
|
|
112
|
+
plugin = factory(args);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
logger.error(`Failed to initialize plugin "${name}": ${formatCause(error)}`);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
logger.debug(`Plugin "${name}" initialized`);
|
|
118
|
+
if (typeof plugin !== "object") {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
this.plugins[name] = plugin;
|
|
122
|
+
const promise = plugin.enable();
|
|
123
|
+
if (!(promise instanceof Promise)) {
|
|
124
|
+
logger.debug(`Plugin "${name}" enabled`);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
pending.push(
|
|
128
|
+
promise.then(() => logger.debug(`Plugin "${name}" enabled`)).catch((error) => logger.error(`Failed to enable plugin "${name}": ${formatCause(error)}`))
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
Promise.all(pending).then(() => {
|
|
132
|
+
this.initialize();
|
|
133
|
+
logger.debug("Initialization complete");
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
get initialized() {
|
|
137
|
+
return this.instance !== void 0;
|
|
138
|
+
}
|
|
139
|
+
get plugged() {
|
|
140
|
+
return this.ready.then(() => this);
|
|
141
|
+
}
|
|
142
|
+
get flushed() {
|
|
143
|
+
return this.tracker.flushed.then(() => this);
|
|
144
|
+
}
|
|
145
|
+
get sdk() {
|
|
146
|
+
if (this.instance === void 0) {
|
|
147
|
+
throw new Error("Croct is not plugged in. For help, see https://croct.help/sdk/javascript/not-plugged-in");
|
|
148
|
+
}
|
|
149
|
+
return this.instance;
|
|
150
|
+
}
|
|
151
|
+
get tracker() {
|
|
152
|
+
return this.sdk.tracker;
|
|
153
|
+
}
|
|
154
|
+
get evaluator() {
|
|
155
|
+
return this.sdk.evaluator;
|
|
156
|
+
}
|
|
157
|
+
get user() {
|
|
158
|
+
return this.sdk.user;
|
|
159
|
+
}
|
|
160
|
+
get session() {
|
|
161
|
+
return this.sdk.session;
|
|
162
|
+
}
|
|
163
|
+
isAnonymous() {
|
|
164
|
+
return this.sdk.context.isAnonymous();
|
|
165
|
+
}
|
|
166
|
+
getUserId() {
|
|
167
|
+
return this.sdk.context.getUser();
|
|
168
|
+
}
|
|
169
|
+
identify(userId) {
|
|
170
|
+
if (typeof userId !== "string") {
|
|
171
|
+
throw new Error(
|
|
172
|
+
"The user ID must be a string. For help, see https://croct.help/sdk/javascript/invalid-user-id"
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
this.sdk.identify(userId);
|
|
176
|
+
}
|
|
177
|
+
anonymize() {
|
|
178
|
+
this.sdk.anonymize();
|
|
179
|
+
}
|
|
180
|
+
setToken(token) {
|
|
181
|
+
this.sdk.setToken(Token.parse(token));
|
|
182
|
+
}
|
|
183
|
+
unsetToken() {
|
|
184
|
+
this.sdk.unsetToken();
|
|
185
|
+
}
|
|
186
|
+
track(type, payload) {
|
|
187
|
+
return this.sdk.tracker.track(type, payload);
|
|
188
|
+
}
|
|
189
|
+
evaluate(query, options = {}) {
|
|
190
|
+
return this.sdk.evaluator.evaluate(query, options).catch((error) => {
|
|
191
|
+
const logger = this.sdk.getLogger();
|
|
192
|
+
const reference = query.length > 20 ? `${query.slice(0, 20)}...` : query;
|
|
193
|
+
logger.error(`Failed to evaluate query "${reference}": ${formatCause(error)}`);
|
|
194
|
+
throw error;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
test(expression, options = {}) {
|
|
198
|
+
return this.evaluate(expression, options).then((result) => result === true);
|
|
199
|
+
}
|
|
200
|
+
fetch(slotId, { fallback, ...options } = {}) {
|
|
201
|
+
const [id, version = "latest"] = slotId.split("@");
|
|
202
|
+
const logger = this.sdk.getLogger();
|
|
203
|
+
return this.sdk.contentFetcher.fetch(id, version === "latest" ? options : { ...options, version }).catch(async (error) => {
|
|
204
|
+
logger.error(`Failed to fetch content for slot "${id}@${version}": ${formatCause(error)}`);
|
|
205
|
+
const resolvedFallback = fallback === void 0 ? await loadSlotContent(slotId, options.preferredLocale) ?? void 0 : fallback;
|
|
206
|
+
if (resolvedFallback === void 0) {
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
return { content: resolvedFallback };
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
async unplug() {
|
|
213
|
+
if (this.instance === void 0) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
const { instance, plugins } = this;
|
|
217
|
+
const logger = this.sdk.getLogger();
|
|
218
|
+
const pending = [];
|
|
219
|
+
for (const [pluginName, controller] of Object.entries(plugins)) {
|
|
220
|
+
if (typeof controller.disable !== "function") {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
logger.debug(`Disabling plugin "${pluginName}"...`);
|
|
224
|
+
const promise = controller.disable();
|
|
225
|
+
if (!(promise instanceof Promise)) {
|
|
226
|
+
logger.debug(`Plugin "${pluginName}" disabled`);
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
pending.push(
|
|
230
|
+
promise.then(() => logger.debug(`Plugin "${pluginName}" disabled`)).catch((error) => logger.error(`Failed to disable "${pluginName}": ${formatCause(error)}`))
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
delete this.instance;
|
|
234
|
+
this.plugins = {};
|
|
235
|
+
this.ready = new Promise((resolve) => {
|
|
236
|
+
this.initialize = resolve;
|
|
237
|
+
});
|
|
238
|
+
await Promise.all(pending);
|
|
239
|
+
try {
|
|
240
|
+
await instance.close();
|
|
241
|
+
} finally {
|
|
242
|
+
logger.info("\u{1F50C} Croct has been unplugged.");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
259
245
|
}
|
|
260
|
-
|
|
261
|
-
|
|
246
|
+
export {
|
|
247
|
+
GlobalPlug
|
|
248
|
+
};
|
package/plugin.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var plugin_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(plugin_exports);
|
package/plugin.d.cts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
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.cjs';
|
|
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 {
|
|
14
|
+
readonly version: string;
|
|
15
|
+
readonly appId: string;
|
|
16
|
+
readonly tracker: TrackerFacade;
|
|
17
|
+
readonly evaluator: EvaluatorFacade;
|
|
18
|
+
readonly user: UserFacade;
|
|
19
|
+
readonly session: SessionFacade;
|
|
20
|
+
readonly tab: Tab;
|
|
21
|
+
readonly userTokenStore: TokenStore;
|
|
22
|
+
readonly previewTokenStore: TokenStore;
|
|
23
|
+
readonly cidAssigner: CidAssigner;
|
|
24
|
+
readonly eventManager: SdkEventManager;
|
|
25
|
+
getLogger(...namespace: string[]): Logger;
|
|
26
|
+
getTabStorage(...namespace: string[]): Storage;
|
|
27
|
+
getBrowserStorage(...namespace: string[]): Storage;
|
|
28
|
+
}
|
|
29
|
+
interface PluginArguments<T = any> {
|
|
30
|
+
options: T;
|
|
31
|
+
sdk: PluginSdk;
|
|
32
|
+
}
|
|
33
|
+
interface PluginFactory<T = any> {
|
|
34
|
+
(args: PluginArguments<T>): Plugin;
|
|
35
|
+
}
|
|
36
|
+
interface Plugin {
|
|
37
|
+
enable(): Promise<void> | void;
|
|
38
|
+
disable?(): Promise<void> | void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type { Plugin, PluginArguments, PluginFactory, PluginSdk };
|