@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.
Files changed (116) hide show
  1. package/api/evaluate.cjs +45 -0
  2. package/api/evaluate.d.cts +23 -0
  3. package/api/evaluate.d.ts +9 -7
  4. package/api/evaluate.js +20 -22
  5. package/api/fetchContent.cjs +53 -0
  6. package/api/fetchContent.d.cts +45 -0
  7. package/api/fetchContent.d.ts +31 -11
  8. package/api/fetchContent.js +29 -21
  9. package/api/index.cjs +23 -0
  10. package/api/index.d.cts +27 -0
  11. package/api/index.d.ts +27 -2
  12. package/api/index.js +2 -6
  13. package/component.cjs +15 -0
  14. package/component.d.cts +19 -0
  15. package/component.d.ts +12 -9
  16. package/component.js +0 -3
  17. package/constants.cjs +39 -0
  18. package/constants.d.cts +7 -0
  19. package/constants.d.ts +7 -5
  20. package/constants.js +12 -9
  21. package/index.cjs +24 -0
  22. package/index.d.cts +27 -0
  23. package/index.d.ts +26 -3
  24. package/index.js +5 -6
  25. package/package.json +33 -6
  26. package/playground.cjs +166 -0
  27. package/playground.d.cts +64 -0
  28. package/playground.d.ts +18 -8
  29. package/playground.js +130 -138
  30. package/plug.cjs +271 -0
  31. package/plug.d.cts +86 -0
  32. package/plug.d.ts +34 -17
  33. package/plug.js +243 -256
  34. package/plugin.cjs +15 -0
  35. package/plugin.d.cts +41 -0
  36. package/plugin.d.ts +18 -8
  37. package/plugin.js +0 -3
  38. package/preview.cjs +180 -0
  39. package/preview.d.cts +35 -0
  40. package/preview.d.ts +18 -6
  41. package/preview.js +143 -152
  42. package/sdk/apiKey.cjs +21 -0
  43. package/sdk/apiKey.js +1 -5
  44. package/sdk/evaluation.cjs +34 -0
  45. package/sdk/evaluation.d.cts +2 -0
  46. package/sdk/evaluation.d.ts +1 -1
  47. package/sdk/evaluation.js +8 -10
  48. package/sdk/index.cjs +33 -0
  49. package/sdk/index.d.cts +15 -0
  50. package/sdk/index.d.ts +9 -6
  51. package/sdk/index.js +8 -10
  52. package/sdk/json.cjs +15 -0
  53. package/sdk/json.d.cts +6 -0
  54. package/sdk/json.d.ts +4 -1
  55. package/sdk/json.js +0 -5
  56. package/sdk/sdkEvents.cjs +15 -0
  57. package/sdk/sdkEvents.d.cts +1 -0
  58. package/sdk/sdkEvents.js +0 -3
  59. package/sdk/token.cjs +27 -0
  60. package/sdk/token.d.cts +7 -0
  61. package/sdk/token.d.ts +7 -1
  62. package/sdk/token.js +4 -6
  63. package/sdk/tracking.cjs +27 -0
  64. package/sdk/tracking.d.cts +9 -0
  65. package/sdk/tracking.d.ts +8 -5
  66. package/sdk/tracking.js +4 -6
  67. package/sdk/validation.cjs +23 -0
  68. package/sdk/validation.js +2 -6
  69. package/slot.cjs +15 -0
  70. package/slot.d.cts +42 -0
  71. package/slot.d.ts +14 -12
  72. package/slot.js +0 -3
  73. package/versioning.cjs +15 -0
  74. package/versioning.d.cts +26 -0
  75. package/versioning.d.ts +8 -6
  76. package/versioning.js +0 -3
  77. package/api/evaluate.js.map +0 -1
  78. package/api/fetchContent.js.map +0 -1
  79. package/api/index.js.map +0 -1
  80. package/component.js.map +0 -1
  81. package/constants.js.map +0 -1
  82. package/index.js.map +0 -1
  83. package/playground.js.map +0 -1
  84. package/plug.js.map +0 -1
  85. package/plugin.js.map +0 -1
  86. package/preview.js.map +0 -1
  87. package/sdk/apiKey.js.map +0 -1
  88. package/sdk/evaluation.js.map +0 -1
  89. package/sdk/index.js.map +0 -1
  90. package/sdk/json.js.map +0 -1
  91. package/sdk/sdkEvents.js.map +0 -1
  92. package/sdk/token.js.map +0 -1
  93. package/sdk/tracking.js.map +0 -1
  94. package/sdk/validation.js.map +0 -1
  95. package/slot.js.map +0 -1
  96. package/src/api/evaluate.ts +0 -50
  97. package/src/api/fetchContent.ts +0 -70
  98. package/src/api/index.ts +0 -2
  99. package/src/component.ts +0 -19
  100. package/src/constants.ts +0 -5
  101. package/src/index.ts +0 -6
  102. package/src/playground.ts +0 -247
  103. package/src/plug.ts +0 -429
  104. package/src/plugin.ts +0 -39
  105. package/src/preview.ts +0 -226
  106. package/src/sdk/evaluation.ts +0 -2
  107. package/src/sdk/index.ts +0 -14
  108. package/src/sdk/json.ts +0 -4
  109. package/src/sdk/sdkEvents.ts +0 -1
  110. package/src/sdk/token.ts +0 -1
  111. package/src/sdk/tracking.ts +0 -14
  112. package/src/slot.ts +0 -48
  113. package/src/versioning.ts +0 -38
  114. package/versioning.js.map +0 -1
  115. /package/{src/sdk/apiKey.ts → sdk/apiKey.d.cts} +0 -0
  116. /package/{src/sdk/validation.ts → sdk/validation.d.cts} +0 -0
package/index.d.cts ADDED
@@ -0,0 +1,27 @@
1
+ import { GlobalPlug } from './plug.cjs';
2
+ export { Configuration, Plug } from './plug.cjs';
3
+ import '@croct/sdk/facade/sessionFacade';
4
+ import '@croct/sdk/facade/userFacade';
5
+ import '@croct/sdk/facade/trackerFacade';
6
+ import '@croct/sdk/facade/evaluatorFacade';
7
+ import '@croct/sdk/facade/sdkFacade';
8
+ import '@croct/sdk/utilityTypes';
9
+ import '@croct/sdk/trackingEvents';
10
+ import '@croct/sdk/facade/contentFetcherFacade';
11
+ import './plugin.cjs';
12
+ import '@croct/sdk/token';
13
+ import './sdk/index.cjs';
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 './slot.cjs';
20
+ import '@croct/json';
21
+ import './versioning.cjs';
22
+ import './component.cjs';
23
+ import '@croct/json/mutable';
24
+
25
+ declare const _default: GlobalPlug;
26
+
27
+ export { _default as default };
package/index.d.ts CHANGED
@@ -1,4 +1,27 @@
1
- import { Configuration, Plug, GlobalPlug } from './plug';
2
- export { Configuration, Plug };
1
+ import { GlobalPlug } from './plug.js';
2
+ export { Configuration, Plug } from './plug.js';
3
+ import '@croct/sdk/facade/sessionFacade';
4
+ import '@croct/sdk/facade/userFacade';
5
+ import '@croct/sdk/facade/trackerFacade';
6
+ import '@croct/sdk/facade/evaluatorFacade';
7
+ import '@croct/sdk/facade/sdkFacade';
8
+ import '@croct/sdk/utilityTypes';
9
+ import '@croct/sdk/trackingEvents';
10
+ import '@croct/sdk/facade/contentFetcherFacade';
11
+ import './plugin.js';
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 './slot.js';
20
+ import '@croct/json';
21
+ import './versioning.js';
22
+ import './component.js';
23
+ import '@croct/json/mutable';
24
+
3
25
  declare const _default: GlobalPlug;
4
- export default _default;
26
+
27
+ export { _default as default };
package/index.js CHANGED
@@ -1,6 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const plug_1 = require("./plug");
4
- /* eslint-disable-next-line import/no-default-export -- Should be default export */
5
- exports.default = new plug_1.GlobalPlug();
6
- //# sourceMappingURL=index.js.map
1
+ import { GlobalPlug } from "./plug.js";
2
+ var index_default = new GlobalPlug();
3
+ export {
4
+ index_default as default
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@croct/plug",
3
- "version": "0.16.4",
3
+ "version": "0.17.0",
4
4
  "description": "A fully-featured devkit for building natively personalized applications.",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -15,8 +15,27 @@
15
15
  "javascript",
16
16
  "typescript"
17
17
  ],
18
+ "type": "module",
18
19
  "main": "./index.js",
19
20
  "types": "./index.d.ts",
21
+ "exports": {
22
+ "./*": {
23
+ "import": "./*.js",
24
+ "require": "./*.cjs"
25
+ },
26
+ "./api": {
27
+ "import": "./api/index.js",
28
+ "require": "./api/index.cjs"
29
+ },
30
+ ".": {
31
+ "import": "./index.js",
32
+ "require": "./index.cjs"
33
+ },
34
+ "./sdk": {
35
+ "import": "./sdk/index.js",
36
+ "require": "./sdk/index.cjs"
37
+ }
38
+ },
20
39
  "repository": {
21
40
  "type": "git",
22
41
  "url": "git+https://github.com/croct-tech/plug-js.git"
@@ -27,14 +46,16 @@
27
46
  "homepage": "https://github.com/croct-tech/plug-js",
28
47
  "scripts": {
29
48
  "lint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
30
- "test": "jest -c jest.config.js --coverage",
49
+ "test": "jest -c jest.config.mjs --coverage",
31
50
  "validate": "tsc --noEmit",
32
- "build": "tsc -p tsconfig.build.json",
51
+ "build": "tsup",
52
+ "postbuild": "./post-build.mjs",
33
53
  "bundle": "rollup -c"
34
54
  },
35
55
  "dependencies": {
56
+ "@croct/content": "^1.1.0",
36
57
  "@croct/json": "^2.1.0",
37
- "@croct/sdk": "^0.17.6",
58
+ "@croct/sdk": "^0.18.0",
38
59
  "tslib": "^2.7.0"
39
60
  },
40
61
  "devDependencies": {
@@ -45,13 +66,15 @@
45
66
  "@rollup/plugin-terser": "^0.4.4",
46
67
  "@rollup/plugin-typescript": "^12.0.0",
47
68
  "@types/jest": "^29.5.12",
48
- "@typescript-eslint/parser": "^7.0.0",
69
+ "@typescript-eslint/parser": "^6.21.0",
70
+ "esbuild-fix-imports-plugin": "^1.0.19",
49
71
  "eslint": "^8.27.0",
50
72
  "jest": "^29.3.1",
51
73
  "jest-environment-jsdom": "^29.3.1",
52
74
  "rollup": "^4.0.0",
53
75
  "ts-jest": "^29.0.3",
54
76
  "ts-node": "^10.9.1",
77
+ "tsup": "^8.4.0",
55
78
  "typescript": "^5.0.0"
56
79
  },
57
80
  "browserslist": [
@@ -59,7 +82,11 @@
59
82
  ],
60
83
  "files": [
61
84
  "**/*.js",
85
+ "**/*.cjs",
86
+ "**/*.mjs",
62
87
  "**/*.ts",
88
+ "**/*.mts",
89
+ "**/*.cts",
63
90
  "**/*.map"
64
91
  ]
65
- }
92
+ }
package/playground.cjs ADDED
@@ -0,0 +1,166 @@
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 playground_exports = {};
19
+ __export(playground_exports, {
20
+ PlaygroundPlugin: () => PlaygroundPlugin,
21
+ factory: () => factory
22
+ });
23
+ module.exports = __toCommonJS(playground_exports);
24
+ var import_error = require("@croct/sdk/error");
25
+ var import_evaluatorFacade = require("@croct/sdk/facade/evaluatorFacade");
26
+ var import_constants = require('./constants.cjs');
27
+ const CONNECTION_PARAMETER = "__cplay";
28
+ class PlaygroundPlugin {
29
+ constructor(configuration) {
30
+ this.sdkVersion = configuration.sdkVersion;
31
+ this.appId = configuration.appId;
32
+ this.connectionId = configuration.connectionId;
33
+ this.tab = configuration.tab;
34
+ this.contextFactory = configuration.contextFactory;
35
+ this.storage = configuration.storage;
36
+ this.eventSubscriber = configuration.eventSubscriber;
37
+ this.cidAssigner = configuration.cidAssigner;
38
+ this.tokenProvider = configuration.tokenProvider;
39
+ this.logger = configuration.logger;
40
+ }
41
+ enable() {
42
+ const connectionId = this.resolveConnectionId();
43
+ if (connectionId === null) {
44
+ return;
45
+ }
46
+ this.syncListener = () => this.cidAssigner.assignCid().then((cid) => {
47
+ this.syncToken(connectionId, cid);
48
+ }).catch((error) => {
49
+ this.logger.warn(`Sync failed: ${(0, import_error.formatCause)(error)}`);
50
+ });
51
+ this.eventSubscriber.addListener("tokenChanged", this.syncListener);
52
+ this.tab.addListener("urlChange", this.syncListener);
53
+ return this.syncListener();
54
+ }
55
+ resolveConnectionId() {
56
+ if (this.connectionId !== void 0) {
57
+ this.logger.debug("Connection ID passed in configuration");
58
+ return this.connectionId;
59
+ }
60
+ const url = new URL(this.tab.url);
61
+ let connectionId = url.searchParams.get(CONNECTION_PARAMETER);
62
+ if (connectionId === null || connectionId === "") {
63
+ this.logger.debug("No connection ID found in URL");
64
+ connectionId = this.storage.getItem("connectionId");
65
+ this.logger.debug(
66
+ connectionId !== null ? "Previous connection ID found" : "No previous connection ID found"
67
+ );
68
+ return connectionId;
69
+ }
70
+ this.logger.debug("Connection ID found in URL");
71
+ this.storage.setItem("connectionId", connectionId);
72
+ return connectionId;
73
+ }
74
+ disable() {
75
+ if (this.syncListener !== void 0) {
76
+ this.eventSubscriber.removeListener("tokenChanged", this.syncListener);
77
+ this.tab.removeListener("urlChange", this.syncListener);
78
+ delete this.syncListener;
79
+ }
80
+ }
81
+ syncToken(connectionId, cid) {
82
+ const iframe = document.createElement("iframe");
83
+ iframe.setAttribute("src", import_constants.PLAYGROUND_CONNECT_URL);
84
+ iframe.setAttribute("sandbox", "allow-scripts allow-same-origin");
85
+ iframe.style.visibility = "hidden";
86
+ iframe.style.opacity = "0";
87
+ iframe.style.border = "0";
88
+ iframe.style.width = "0";
89
+ iframe.style.height = "0";
90
+ const context = this.createContext();
91
+ iframe.onload = () => {
92
+ if (iframe.contentWindow === null) {
93
+ if (document.body.contains(iframe)) {
94
+ document.body.removeChild(iframe);
95
+ }
96
+ this.logger.warn("Sync handshake failed");
97
+ return;
98
+ }
99
+ const listener = (event) => {
100
+ if (event.origin !== import_constants.PLAYGROUND_ORIGIN || event.data !== connectionId) {
101
+ return;
102
+ }
103
+ window.removeEventListener("message", listener);
104
+ if (document.body.contains(iframe)) {
105
+ document.body.removeChild(iframe);
106
+ }
107
+ this.logger.debug("Sync completed");
108
+ };
109
+ window.addEventListener("message", listener);
110
+ const payload = {
111
+ appId: this.appId,
112
+ connectionId,
113
+ sdkVersion: this.sdkVersion,
114
+ tabId: this.tab.id,
115
+ cid,
116
+ token: this.tokenProvider.getToken()?.toString() ?? null,
117
+ context
118
+ };
119
+ iframe.contentWindow.postMessage(payload, import_constants.PLAYGROUND_ORIGIN);
120
+ this.logger.debug("Waiting for sync acknowledgment...");
121
+ };
122
+ this.logger.debug("Sync started");
123
+ const connect = () => {
124
+ document.body.appendChild(iframe);
125
+ };
126
+ if (document.body === null) {
127
+ document.addEventListener("DOMContentLoaded", connect);
128
+ } else {
129
+ connect();
130
+ }
131
+ }
132
+ createContext() {
133
+ const { page, campaign, timeZone } = this.contextFactory.createContext();
134
+ const context = {};
135
+ if (page !== void 0) {
136
+ context.page = page;
137
+ }
138
+ if (campaign !== void 0) {
139
+ context.campaign = campaign;
140
+ }
141
+ if (timeZone !== void 0) {
142
+ context.timeZone = timeZone;
143
+ }
144
+ return context;
145
+ }
146
+ }
147
+ const factory = (props) => {
148
+ const { sdk, options } = props;
149
+ return new PlaygroundPlugin({
150
+ sdkVersion: sdk.version,
151
+ appId: sdk.appId,
152
+ connectionId: options.connectionId,
153
+ tab: sdk.tab,
154
+ storage: sdk.getTabStorage(),
155
+ tokenProvider: sdk.userTokenStore,
156
+ cidAssigner: sdk.cidAssigner,
157
+ contextFactory: new import_evaluatorFacade.TabContextFactory(sdk.tab),
158
+ eventSubscriber: sdk.eventManager,
159
+ logger: sdk.getLogger()
160
+ });
161
+ };
162
+ // Annotate the CommonJS export names for ESM import in node:
163
+ 0 && (module.exports = {
164
+ PlaygroundPlugin,
165
+ factory
166
+ });
@@ -0,0 +1,64 @@
1
+ import { CidAssigner } from '@croct/sdk/cid';
2
+ import { ContextFactory } from '@croct/sdk/facade/evaluatorFacade';
3
+ import { Campaign, Page } from '@croct/sdk/evaluator';
4
+ import { Plugin, PluginFactory } from './plugin.cjs';
5
+ import { SdkEventSubscriber } from './sdk/index.cjs';
6
+ import { TokenProvider } from '@croct/sdk/token';
7
+ import { Tab } from '@croct/sdk/tab';
8
+ import { Logger } from '@croct/sdk/logging';
9
+ import '@croct/sdk/facade/trackerFacade';
10
+ import '@croct/sdk/facade/userFacade';
11
+ import '@croct/sdk/facade/sessionFacade';
12
+ import '@croct/sdk/eventManager';
13
+ import '@croct/sdk/sdkEvents';
14
+
15
+ type Configuration = {
16
+ appId: string;
17
+ connectionId?: string;
18
+ sdkVersion: string;
19
+ tab: Tab;
20
+ contextFactory: ContextFactory;
21
+ storage: Storage;
22
+ eventSubscriber: SdkEventSubscriber;
23
+ cidAssigner: CidAssigner;
24
+ tokenProvider: TokenProvider;
25
+ logger: Logger;
26
+ };
27
+ type SyncPayload = {
28
+ appId: string;
29
+ connectionId: string;
30
+ sdkVersion: string;
31
+ cid: string;
32
+ tabId: string;
33
+ token: string | null;
34
+ context: {
35
+ campaign?: Campaign;
36
+ page?: Page;
37
+ timezone?: string;
38
+ };
39
+ };
40
+ declare class PlaygroundPlugin implements Plugin {
41
+ private readonly sdkVersion;
42
+ private readonly appId;
43
+ private readonly connectionId?;
44
+ private readonly tab;
45
+ private readonly contextFactory;
46
+ private readonly storage;
47
+ private readonly eventSubscriber;
48
+ private readonly cidAssigner;
49
+ private readonly tokenProvider;
50
+ private readonly logger;
51
+ private syncListener?;
52
+ constructor(configuration: Configuration);
53
+ enable(): Promise<void> | void;
54
+ private resolveConnectionId;
55
+ disable(): Promise<void> | void;
56
+ private syncToken;
57
+ private createContext;
58
+ }
59
+ type Options = {
60
+ connectionId?: string;
61
+ };
62
+ declare const factory: PluginFactory<Options>;
63
+
64
+ export { type Configuration, type Options, PlaygroundPlugin, type SyncPayload, factory };
package/playground.d.ts CHANGED
@@ -1,10 +1,18 @@
1
1
  import { CidAssigner } from '@croct/sdk/cid';
2
2
  import { ContextFactory } from '@croct/sdk/facade/evaluatorFacade';
3
3
  import { Campaign, Page } from '@croct/sdk/evaluator';
4
- import { Plugin, PluginFactory } from './plugin';
5
- import { Logger, SdkEventSubscriber, Tab } from './sdk';
6
- import { TokenProvider } from './sdk/token';
7
- export type Configuration = {
4
+ import { Plugin, PluginFactory } from './plugin.js';
5
+ import { SdkEventSubscriber } from './sdk/index.js';
6
+ import { TokenProvider } from '@croct/sdk/token';
7
+ import { Tab } from '@croct/sdk/tab';
8
+ import { Logger } from '@croct/sdk/logging';
9
+ import '@croct/sdk/facade/trackerFacade';
10
+ import '@croct/sdk/facade/userFacade';
11
+ import '@croct/sdk/facade/sessionFacade';
12
+ import '@croct/sdk/eventManager';
13
+ import '@croct/sdk/sdkEvents';
14
+
15
+ type Configuration = {
8
16
  appId: string;
9
17
  connectionId?: string;
10
18
  sdkVersion: string;
@@ -16,7 +24,7 @@ export type Configuration = {
16
24
  tokenProvider: TokenProvider;
17
25
  logger: Logger;
18
26
  };
19
- export type SyncPayload = {
27
+ type SyncPayload = {
20
28
  appId: string;
21
29
  connectionId: string;
22
30
  sdkVersion: string;
@@ -29,7 +37,7 @@ export type SyncPayload = {
29
37
  timezone?: string;
30
38
  };
31
39
  };
32
- export declare class PlaygroundPlugin implements Plugin {
40
+ declare class PlaygroundPlugin implements Plugin {
33
41
  private readonly sdkVersion;
34
42
  private readonly appId;
35
43
  private readonly connectionId?;
@@ -48,7 +56,9 @@ export declare class PlaygroundPlugin implements Plugin {
48
56
  private syncToken;
49
57
  private createContext;
50
58
  }
51
- export type Options = {
59
+ type Options = {
52
60
  connectionId?: string;
53
61
  };
54
- export declare const factory: PluginFactory<Options>;
62
+ declare const factory: PluginFactory<Options>;
63
+
64
+ export { type Configuration, type Options, PlaygroundPlugin, type SyncPayload, factory };