@twilltac/compliance-sdk 0.0.3
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/README.md +1 -0
- package/index.cjs +233 -0
- package/index.js +206 -0
- package/libs/twilltac/plugins/compliance/sdk/src/index.d.ts +2 -0
- package/libs/twilltac/plugins/compliance/sdk/src/lib/compliance-plugin-sdk.spec.d.ts +0 -0
- package/libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.d.ts +14 -0
- package/libs/twilltac/plugins/compliance/sdk/src/lib/create-compliance.plugin.d.ts +8 -0
- package/libs/twilltac/plugins/compliance/sdk/src/test-setup.d.ts +1 -0
- package/libs/twilltac/plugins/compliance/types/src/index.d.ts +2 -0
- package/libs/twilltac/plugins/compliance/types/src/lib/compliance-plugin-initialisation.data.d.ts +5 -0
- package/libs/twilltac/plugins/compliance/types/src/lib/compliance-plugin-post.messages.d.ts +15 -0
- package/libs/twilltac/plugins/utils/post-messenger/src/index.d.ts +3 -0
- package/libs/twilltac/plugins/utils/post-messenger/src/lib/noop-post-message.bus.d.ts +13 -0
- package/libs/twilltac/plugins/utils/post-messenger/src/lib/post-message-bus.interface.d.ts +40 -0
- package/libs/twilltac/plugins/utils/post-messenger/src/lib/post-message.bus.d.ts +28 -0
- package/package.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# compliance-plugin-sdk
|
package/index.cjs
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// libs/twilltac/plugins/compliance/sdk/src/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
CompliancePlugin: () => CompliancePlugin,
|
|
25
|
+
createCompliancePlugin: () => createCompliancePlugin
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
|
|
29
|
+
// libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
|
|
30
|
+
var CompliancePlugin = class {
|
|
31
|
+
_iframeElement;
|
|
32
|
+
_containerElement;
|
|
33
|
+
_postMessageBus;
|
|
34
|
+
constructor(opts) {
|
|
35
|
+
this._iframeElement = opts.iframeElement;
|
|
36
|
+
this._containerElement = opts.containerElement;
|
|
37
|
+
this._postMessageBus = opts.postMessageBus;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// libs/twilltac/plugins/utils/post-messenger/src/lib/post-message.bus.ts
|
|
42
|
+
var PostMessageBus = class {
|
|
43
|
+
_channel;
|
|
44
|
+
_targetOrigin;
|
|
45
|
+
_targetWindow;
|
|
46
|
+
_defaultTimeout;
|
|
47
|
+
_handlers = /* @__PURE__ */ new Map();
|
|
48
|
+
_requestHandlers = /* @__PURE__ */ new Map();
|
|
49
|
+
_pendingRequests = /* @__PURE__ */ new Map();
|
|
50
|
+
_boundMessageHandler;
|
|
51
|
+
constructor(options) {
|
|
52
|
+
this._channel = options.channel;
|
|
53
|
+
this._targetOrigin = options.targetOrigin ?? "*";
|
|
54
|
+
this._targetWindow = options.targetWindow ?? window.parent;
|
|
55
|
+
this._defaultTimeout = options.defaultTimeout ?? 3e4;
|
|
56
|
+
this._boundMessageHandler = (event) => this.handleMessage(event);
|
|
57
|
+
window.addEventListener("message", this._boundMessageHandler);
|
|
58
|
+
}
|
|
59
|
+
send(type, ...args) {
|
|
60
|
+
const envelope = {
|
|
61
|
+
channel: this._channel,
|
|
62
|
+
type,
|
|
63
|
+
payload: args[0]
|
|
64
|
+
};
|
|
65
|
+
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
66
|
+
}
|
|
67
|
+
on(type, handler) {
|
|
68
|
+
if (!this._handlers.has(type)) {
|
|
69
|
+
this._handlers.set(type, /* @__PURE__ */ new Set());
|
|
70
|
+
}
|
|
71
|
+
this._handlers.get(type).add(handler);
|
|
72
|
+
return () => this.off(type, handler);
|
|
73
|
+
}
|
|
74
|
+
off(type, handler) {
|
|
75
|
+
const handlers = this._handlers.get(type);
|
|
76
|
+
if (handlers) {
|
|
77
|
+
handlers.delete(handler);
|
|
78
|
+
if (handlers.size === 0) {
|
|
79
|
+
this._handlers.delete(type);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
once(type, handler) {
|
|
84
|
+
const wrappedHandler = (payload) => {
|
|
85
|
+
this.off(type, wrappedHandler);
|
|
86
|
+
handler(payload);
|
|
87
|
+
};
|
|
88
|
+
return this.on(type, wrappedHandler);
|
|
89
|
+
}
|
|
90
|
+
request(type, ...args) {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const hasPayload = args.length > 0 && typeof args[0] !== "number";
|
|
93
|
+
const payload = hasPayload ? args[0] : void 0;
|
|
94
|
+
const timeout = hasPayload ? args[1] : args[0];
|
|
95
|
+
const correlationId = this.generateCorrelationId();
|
|
96
|
+
const timeoutMs = timeout ?? this._defaultTimeout;
|
|
97
|
+
const timeoutId = setTimeout(() => {
|
|
98
|
+
this._pendingRequests.delete(correlationId);
|
|
99
|
+
reject(new Error(`Request '${type}' timed out after ${timeoutMs}ms`));
|
|
100
|
+
}, timeoutMs);
|
|
101
|
+
this._pendingRequests.set(correlationId, {
|
|
102
|
+
resolve,
|
|
103
|
+
reject,
|
|
104
|
+
timeoutId
|
|
105
|
+
});
|
|
106
|
+
const envelope = {
|
|
107
|
+
channel: this._channel,
|
|
108
|
+
type,
|
|
109
|
+
payload,
|
|
110
|
+
correlationId
|
|
111
|
+
};
|
|
112
|
+
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
onRequest(type, handler) {
|
|
116
|
+
this._requestHandlers.set(type, handler);
|
|
117
|
+
return () => this._requestHandlers.delete(type);
|
|
118
|
+
}
|
|
119
|
+
destroy() {
|
|
120
|
+
window.removeEventListener("message", this._boundMessageHandler);
|
|
121
|
+
this._handlers.clear();
|
|
122
|
+
this._requestHandlers.clear();
|
|
123
|
+
this._pendingRequests.forEach((pending) => {
|
|
124
|
+
clearTimeout(pending.timeoutId);
|
|
125
|
+
pending.reject(new Error("PostMessageBus destroyed"));
|
|
126
|
+
});
|
|
127
|
+
this._pendingRequests.clear();
|
|
128
|
+
}
|
|
129
|
+
handleMessage(event) {
|
|
130
|
+
const envelope = event.data;
|
|
131
|
+
if (!envelope || envelope.channel !== this._channel) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (envelope.isResponse && envelope.correlationId) {
|
|
135
|
+
this.handleResponse(envelope);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (envelope.correlationId && !envelope.isResponse) {
|
|
139
|
+
this.handleRequest(envelope);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const handlers = this._handlers.get(envelope.type);
|
|
143
|
+
if (handlers) {
|
|
144
|
+
handlers.forEach((handler) => handler(envelope.payload));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
handleResponse(envelope) {
|
|
148
|
+
const pending = this._pendingRequests.get(envelope.correlationId);
|
|
149
|
+
if (!pending) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
clearTimeout(pending.timeoutId);
|
|
153
|
+
this._pendingRequests.delete(envelope.correlationId);
|
|
154
|
+
if (envelope.isError) {
|
|
155
|
+
pending.reject(envelope.payload);
|
|
156
|
+
} else {
|
|
157
|
+
pending.resolve(envelope.payload);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
handleRequest(envelope) {
|
|
161
|
+
const handler = this._requestHandlers.get(envelope.type);
|
|
162
|
+
if (!handler) {
|
|
163
|
+
this.sendResponse(envelope.correlationId, null, true, `No handler registered for '${envelope.type}'`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
Promise.resolve().then(() => handler(envelope.payload)).then((result) => {
|
|
167
|
+
this.sendResponse(envelope.correlationId, result, false);
|
|
168
|
+
}).catch((error) => {
|
|
169
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
170
|
+
this.sendResponse(envelope.correlationId, null, true, errorMessage);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
sendResponse(correlationId, payload, isError, errorMessage) {
|
|
174
|
+
const envelope = {
|
|
175
|
+
channel: this._channel,
|
|
176
|
+
type: "__response__",
|
|
177
|
+
payload: isError ? { error: errorMessage } : payload,
|
|
178
|
+
correlationId,
|
|
179
|
+
isResponse: true,
|
|
180
|
+
isError
|
|
181
|
+
};
|
|
182
|
+
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
183
|
+
}
|
|
184
|
+
generateCorrelationId() {
|
|
185
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// libs/twilltac/plugins/compliance/sdk/src/lib/create-compliance.plugin.ts
|
|
190
|
+
function createCompliancePlugin(options) {
|
|
191
|
+
const production = !window.location.hostname.endsWith(".ngserve");
|
|
192
|
+
const baseUrl = "https://compliance.plugins.twilltac.app";
|
|
193
|
+
const url = production ? baseUrl : `${baseUrl}.ngserve`;
|
|
194
|
+
const iframe = document.createElement("iframe");
|
|
195
|
+
iframe.name = "compliance-plugin-iframe";
|
|
196
|
+
iframe.style.width = "100%";
|
|
197
|
+
iframe.style.height = "100%";
|
|
198
|
+
iframe.style.border = "none";
|
|
199
|
+
const form = document.createElement("form");
|
|
200
|
+
form.method = "POST";
|
|
201
|
+
form.action = `${url}/en-gb`;
|
|
202
|
+
form.target = iframe.name;
|
|
203
|
+
form.style.display = "none";
|
|
204
|
+
const input = document.createElement("input");
|
|
205
|
+
input.type = "hidden";
|
|
206
|
+
input.name = "data";
|
|
207
|
+
input.value = JSON.stringify(options.data);
|
|
208
|
+
form.appendChild(input);
|
|
209
|
+
options.containerElement.innerHTML = "";
|
|
210
|
+
options.containerElement.appendChild(iframe);
|
|
211
|
+
options.containerElement.appendChild(form);
|
|
212
|
+
const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
|
|
213
|
+
return new Promise((resolve, rejects) => {
|
|
214
|
+
postMessageBus.on("plugin:initialisation:success", () => {
|
|
215
|
+
resolve(
|
|
216
|
+
new CompliancePlugin({
|
|
217
|
+
postMessageBus,
|
|
218
|
+
containerElement: options.containerElement,
|
|
219
|
+
iframeElement: iframe
|
|
220
|
+
})
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
postMessageBus.on("plugin:initialisation:error", (data) => {
|
|
224
|
+
rejects(data);
|
|
225
|
+
});
|
|
226
|
+
form.submit();
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
230
|
+
0 && (module.exports = {
|
|
231
|
+
CompliancePlugin,
|
|
232
|
+
createCompliancePlugin
|
|
233
|
+
});
|
package/index.js
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
|
|
4
|
+
var CompliancePlugin = class {
|
|
5
|
+
_iframeElement;
|
|
6
|
+
_containerElement;
|
|
7
|
+
_postMessageBus;
|
|
8
|
+
constructor(opts) {
|
|
9
|
+
this._iframeElement = opts.iframeElement;
|
|
10
|
+
this._containerElement = opts.containerElement;
|
|
11
|
+
this._postMessageBus = opts.postMessageBus;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// libs/twilltac/plugins/utils/post-messenger/src/lib/post-message.bus.ts
|
|
16
|
+
var PostMessageBus = class {
|
|
17
|
+
_channel;
|
|
18
|
+
_targetOrigin;
|
|
19
|
+
_targetWindow;
|
|
20
|
+
_defaultTimeout;
|
|
21
|
+
_handlers = /* @__PURE__ */ new Map();
|
|
22
|
+
_requestHandlers = /* @__PURE__ */ new Map();
|
|
23
|
+
_pendingRequests = /* @__PURE__ */ new Map();
|
|
24
|
+
_boundMessageHandler;
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this._channel = options.channel;
|
|
27
|
+
this._targetOrigin = options.targetOrigin ?? "*";
|
|
28
|
+
this._targetWindow = options.targetWindow ?? window.parent;
|
|
29
|
+
this._defaultTimeout = options.defaultTimeout ?? 3e4;
|
|
30
|
+
this._boundMessageHandler = (event) => this.handleMessage(event);
|
|
31
|
+
window.addEventListener("message", this._boundMessageHandler);
|
|
32
|
+
}
|
|
33
|
+
send(type, ...args) {
|
|
34
|
+
const envelope = {
|
|
35
|
+
channel: this._channel,
|
|
36
|
+
type,
|
|
37
|
+
payload: args[0]
|
|
38
|
+
};
|
|
39
|
+
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
40
|
+
}
|
|
41
|
+
on(type, handler) {
|
|
42
|
+
if (!this._handlers.has(type)) {
|
|
43
|
+
this._handlers.set(type, /* @__PURE__ */ new Set());
|
|
44
|
+
}
|
|
45
|
+
this._handlers.get(type).add(handler);
|
|
46
|
+
return () => this.off(type, handler);
|
|
47
|
+
}
|
|
48
|
+
off(type, handler) {
|
|
49
|
+
const handlers = this._handlers.get(type);
|
|
50
|
+
if (handlers) {
|
|
51
|
+
handlers.delete(handler);
|
|
52
|
+
if (handlers.size === 0) {
|
|
53
|
+
this._handlers.delete(type);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
once(type, handler) {
|
|
58
|
+
const wrappedHandler = (payload) => {
|
|
59
|
+
this.off(type, wrappedHandler);
|
|
60
|
+
handler(payload);
|
|
61
|
+
};
|
|
62
|
+
return this.on(type, wrappedHandler);
|
|
63
|
+
}
|
|
64
|
+
request(type, ...args) {
|
|
65
|
+
return new Promise((resolve, reject) => {
|
|
66
|
+
const hasPayload = args.length > 0 && typeof args[0] !== "number";
|
|
67
|
+
const payload = hasPayload ? args[0] : void 0;
|
|
68
|
+
const timeout = hasPayload ? args[1] : args[0];
|
|
69
|
+
const correlationId = this.generateCorrelationId();
|
|
70
|
+
const timeoutMs = timeout ?? this._defaultTimeout;
|
|
71
|
+
const timeoutId = setTimeout(() => {
|
|
72
|
+
this._pendingRequests.delete(correlationId);
|
|
73
|
+
reject(new Error(`Request '${type}' timed out after ${timeoutMs}ms`));
|
|
74
|
+
}, timeoutMs);
|
|
75
|
+
this._pendingRequests.set(correlationId, {
|
|
76
|
+
resolve,
|
|
77
|
+
reject,
|
|
78
|
+
timeoutId
|
|
79
|
+
});
|
|
80
|
+
const envelope = {
|
|
81
|
+
channel: this._channel,
|
|
82
|
+
type,
|
|
83
|
+
payload,
|
|
84
|
+
correlationId
|
|
85
|
+
};
|
|
86
|
+
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
onRequest(type, handler) {
|
|
90
|
+
this._requestHandlers.set(type, handler);
|
|
91
|
+
return () => this._requestHandlers.delete(type);
|
|
92
|
+
}
|
|
93
|
+
destroy() {
|
|
94
|
+
window.removeEventListener("message", this._boundMessageHandler);
|
|
95
|
+
this._handlers.clear();
|
|
96
|
+
this._requestHandlers.clear();
|
|
97
|
+
this._pendingRequests.forEach((pending) => {
|
|
98
|
+
clearTimeout(pending.timeoutId);
|
|
99
|
+
pending.reject(new Error("PostMessageBus destroyed"));
|
|
100
|
+
});
|
|
101
|
+
this._pendingRequests.clear();
|
|
102
|
+
}
|
|
103
|
+
handleMessage(event) {
|
|
104
|
+
const envelope = event.data;
|
|
105
|
+
if (!envelope || envelope.channel !== this._channel) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
if (envelope.isResponse && envelope.correlationId) {
|
|
109
|
+
this.handleResponse(envelope);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (envelope.correlationId && !envelope.isResponse) {
|
|
113
|
+
this.handleRequest(envelope);
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const handlers = this._handlers.get(envelope.type);
|
|
117
|
+
if (handlers) {
|
|
118
|
+
handlers.forEach((handler) => handler(envelope.payload));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
handleResponse(envelope) {
|
|
122
|
+
const pending = this._pendingRequests.get(envelope.correlationId);
|
|
123
|
+
if (!pending) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
clearTimeout(pending.timeoutId);
|
|
127
|
+
this._pendingRequests.delete(envelope.correlationId);
|
|
128
|
+
if (envelope.isError) {
|
|
129
|
+
pending.reject(envelope.payload);
|
|
130
|
+
} else {
|
|
131
|
+
pending.resolve(envelope.payload);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
handleRequest(envelope) {
|
|
135
|
+
const handler = this._requestHandlers.get(envelope.type);
|
|
136
|
+
if (!handler) {
|
|
137
|
+
this.sendResponse(envelope.correlationId, null, true, `No handler registered for '${envelope.type}'`);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
Promise.resolve().then(() => handler(envelope.payload)).then((result) => {
|
|
141
|
+
this.sendResponse(envelope.correlationId, result, false);
|
|
142
|
+
}).catch((error) => {
|
|
143
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
144
|
+
this.sendResponse(envelope.correlationId, null, true, errorMessage);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
sendResponse(correlationId, payload, isError, errorMessage) {
|
|
148
|
+
const envelope = {
|
|
149
|
+
channel: this._channel,
|
|
150
|
+
type: "__response__",
|
|
151
|
+
payload: isError ? { error: errorMessage } : payload,
|
|
152
|
+
correlationId,
|
|
153
|
+
isResponse: true,
|
|
154
|
+
isError
|
|
155
|
+
};
|
|
156
|
+
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
157
|
+
}
|
|
158
|
+
generateCorrelationId() {
|
|
159
|
+
return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// libs/twilltac/plugins/compliance/sdk/src/lib/create-compliance.plugin.ts
|
|
164
|
+
function createCompliancePlugin(options) {
|
|
165
|
+
const production = !window.location.hostname.endsWith(".ngserve");
|
|
166
|
+
const baseUrl = "https://compliance.plugins.twilltac.app";
|
|
167
|
+
const url = production ? baseUrl : `${baseUrl}.ngserve`;
|
|
168
|
+
const iframe = document.createElement("iframe");
|
|
169
|
+
iframe.name = "compliance-plugin-iframe";
|
|
170
|
+
iframe.style.width = "100%";
|
|
171
|
+
iframe.style.height = "100%";
|
|
172
|
+
iframe.style.border = "none";
|
|
173
|
+
const form = document.createElement("form");
|
|
174
|
+
form.method = "POST";
|
|
175
|
+
form.action = `${url}/en-gb`;
|
|
176
|
+
form.target = iframe.name;
|
|
177
|
+
form.style.display = "none";
|
|
178
|
+
const input = document.createElement("input");
|
|
179
|
+
input.type = "hidden";
|
|
180
|
+
input.name = "data";
|
|
181
|
+
input.value = JSON.stringify(options.data);
|
|
182
|
+
form.appendChild(input);
|
|
183
|
+
options.containerElement.innerHTML = "";
|
|
184
|
+
options.containerElement.appendChild(iframe);
|
|
185
|
+
options.containerElement.appendChild(form);
|
|
186
|
+
const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
|
|
187
|
+
return new Promise((resolve, rejects) => {
|
|
188
|
+
postMessageBus.on("plugin:initialisation:success", () => {
|
|
189
|
+
resolve(
|
|
190
|
+
new CompliancePlugin({
|
|
191
|
+
postMessageBus,
|
|
192
|
+
containerElement: options.containerElement,
|
|
193
|
+
iframeElement: iframe
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
});
|
|
197
|
+
postMessageBus.on("plugin:initialisation:error", (data) => {
|
|
198
|
+
rejects(data);
|
|
199
|
+
});
|
|
200
|
+
form.submit();
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
export {
|
|
204
|
+
CompliancePlugin,
|
|
205
|
+
createCompliancePlugin
|
|
206
|
+
};
|
|
File without changes
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CompliancePluginPostMessages } from '@twilltac/compliance-plugin-types';
|
|
2
|
+
import { PostMessageBus } from '@twilltac/post-messenger';
|
|
3
|
+
interface CtorData {
|
|
4
|
+
postMessageBus: PostMessageBus<CompliancePluginPostMessages>;
|
|
5
|
+
containerElement: HTMLElement;
|
|
6
|
+
iframeElement: HTMLIFrameElement;
|
|
7
|
+
}
|
|
8
|
+
export declare class CompliancePlugin {
|
|
9
|
+
private readonly _iframeElement;
|
|
10
|
+
private readonly _containerElement;
|
|
11
|
+
private readonly _postMessageBus;
|
|
12
|
+
constructor(opts: CtorData);
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CompliancePluginInitialisationData } from '@twilltac/compliance-plugin-types';
|
|
2
|
+
import { CompliancePlugin } from './compliance.plugin';
|
|
3
|
+
interface InitialisationOptions {
|
|
4
|
+
containerElement: HTMLElement;
|
|
5
|
+
data: CompliancePluginInitialisationData;
|
|
6
|
+
}
|
|
7
|
+
export declare function createCompliancePlugin(options: InitialisationOptions): Promise<CompliancePlugin>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CompliancePluginPostMessages {
|
|
2
|
+
['plugin:initialisation:success']: {
|
|
3
|
+
payload: {
|
|
4
|
+
appInstanceId: string;
|
|
5
|
+
};
|
|
6
|
+
};
|
|
7
|
+
['plugin:initialisation:error']: {
|
|
8
|
+
payload: {
|
|
9
|
+
error: Error;
|
|
10
|
+
};
|
|
11
|
+
response: {
|
|
12
|
+
error: Error;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ExtractResponse, IPostMessageBus } from './post-message-bus.interface';
|
|
2
|
+
export declare class NoopPostMessageBus<TMessages = Record<string, {
|
|
3
|
+
payload: unknown;
|
|
4
|
+
response?: unknown;
|
|
5
|
+
}>> implements IPostMessageBus<TMessages> {
|
|
6
|
+
send(): void;
|
|
7
|
+
on(): () => void;
|
|
8
|
+
off(): void;
|
|
9
|
+
once(): () => void;
|
|
10
|
+
request<K extends keyof TMessages & string>(): Promise<ExtractResponse<TMessages[K]>>;
|
|
11
|
+
onRequest(): () => void;
|
|
12
|
+
destroy(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export type MessageHandler<T = unknown> = (payload: T) => void;
|
|
2
|
+
export type RequestHandler<TRequest = unknown, TResponse = unknown> = (payload: TRequest) => TResponse | Promise<TResponse>;
|
|
3
|
+
export type ExtractPayload<T> = T extends {
|
|
4
|
+
payload: infer P;
|
|
5
|
+
} ? P : void;
|
|
6
|
+
export type ExtractResponse<T> = T extends {
|
|
7
|
+
response: infer R;
|
|
8
|
+
} ? R : void;
|
|
9
|
+
export type OptionalIfVoid<T> = T extends void ? [] : [payload: T];
|
|
10
|
+
export interface PostMessageEnvelope<T = unknown> {
|
|
11
|
+
channel: string;
|
|
12
|
+
type: string;
|
|
13
|
+
payload: T;
|
|
14
|
+
correlationId?: string;
|
|
15
|
+
isResponse?: boolean;
|
|
16
|
+
isError?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface PendingRequest<T> {
|
|
19
|
+
resolve: (value: T) => void;
|
|
20
|
+
reject: (reason: unknown) => void;
|
|
21
|
+
timeoutId: ReturnType<typeof setTimeout>;
|
|
22
|
+
}
|
|
23
|
+
export interface PostMessageBusOptions {
|
|
24
|
+
channel: string;
|
|
25
|
+
targetOrigin?: string;
|
|
26
|
+
targetWindow?: Window;
|
|
27
|
+
defaultTimeout?: number;
|
|
28
|
+
}
|
|
29
|
+
export interface IPostMessageBus<TMessages = Record<string, {
|
|
30
|
+
payload: unknown;
|
|
31
|
+
response?: unknown;
|
|
32
|
+
}>> {
|
|
33
|
+
send<K extends keyof TMessages & string>(type: K, ...args: OptionalIfVoid<ExtractPayload<TMessages[K]>>): void;
|
|
34
|
+
on<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
|
|
35
|
+
off<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): void;
|
|
36
|
+
once<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
|
|
37
|
+
request<K extends keyof TMessages & string>(type: K, ...args: [...OptionalIfVoid<ExtractPayload<TMessages[K]>>, timeout?: number]): Promise<ExtractResponse<TMessages[K]>>;
|
|
38
|
+
onRequest<K extends keyof TMessages & string>(type: K, handler: RequestHandler<ExtractPayload<TMessages[K]>, ExtractResponse<TMessages[K]>>): () => void;
|
|
39
|
+
destroy(): void;
|
|
40
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ExtractPayload, ExtractResponse, IPostMessageBus, MessageHandler, OptionalIfVoid, PostMessageBusOptions, RequestHandler } from './post-message-bus.interface';
|
|
2
|
+
/** @internal */
|
|
3
|
+
export declare class PostMessageBus<TMessages = Record<string, {
|
|
4
|
+
payload: unknown;
|
|
5
|
+
response?: unknown;
|
|
6
|
+
}>> implements IPostMessageBus<TMessages> {
|
|
7
|
+
private readonly _channel;
|
|
8
|
+
private readonly _targetOrigin;
|
|
9
|
+
private readonly _targetWindow;
|
|
10
|
+
private readonly _defaultTimeout;
|
|
11
|
+
private readonly _handlers;
|
|
12
|
+
private readonly _requestHandlers;
|
|
13
|
+
private readonly _pendingRequests;
|
|
14
|
+
private readonly _boundMessageHandler;
|
|
15
|
+
constructor(options: PostMessageBusOptions);
|
|
16
|
+
send<K extends keyof TMessages & string>(type: K, ...args: OptionalIfVoid<ExtractPayload<TMessages[K]>>): void;
|
|
17
|
+
on<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
|
|
18
|
+
off<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): void;
|
|
19
|
+
once<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
|
|
20
|
+
request<K extends keyof TMessages & string>(type: K, ...args: [...OptionalIfVoid<ExtractPayload<TMessages[K]>>, timeout?: number]): Promise<ExtractResponse<TMessages[K]>>;
|
|
21
|
+
onRequest<K extends keyof TMessages & string>(type: K, handler: RequestHandler<ExtractPayload<TMessages[K]>, ExtractResponse<TMessages[K]>>): () => void;
|
|
22
|
+
destroy(): void;
|
|
23
|
+
private handleMessage;
|
|
24
|
+
private handleResponse;
|
|
25
|
+
private handleRequest;
|
|
26
|
+
private sendResponse;
|
|
27
|
+
private generateCorrelationId;
|
|
28
|
+
}
|