@twilltac/compliance-sdk 0.1.1 → 0.1.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 +53 -76
- package/build-revision.txt +1 -1
- package/index.cjs +44 -2
- package/index.js +44 -2
- package/libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.d.ts +9 -3
- package/libs/twilltac/plugins/compliance/types/src/lib/compliance-plugin-post.messages.d.ts +3 -2
- package/libs/twilltac/plugins/utils/typed-event-emitter/src/index.d.ts +2 -0
- package/libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.d.ts +11 -0
- package/libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.interface.d.ts +13 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,12 +22,14 @@ const plugin = await createCompliancePlugin({
|
|
|
22
22
|
customerReference: 'YOUR_CUSTOMER_REFERENCE',
|
|
23
23
|
authToken: 'YOUR_AUTH_TOKEN'
|
|
24
24
|
}
|
|
25
|
+
}).catch((error) => {
|
|
26
|
+
console.error('The plugin failed to load', error);
|
|
25
27
|
});
|
|
26
28
|
```
|
|
27
29
|
|
|
28
30
|
## API Reference
|
|
29
31
|
|
|
30
|
-
### createCompliancePlugin(options)
|
|
32
|
+
### `createCompliancePlugin(options)`
|
|
31
33
|
|
|
32
34
|
Creates and initialises the compliance plugin within a container element.
|
|
33
35
|
|
|
@@ -48,71 +50,42 @@ The promise resolves when the plugin has successfully initialised. If initialisa
|
|
|
48
50
|
|
|
49
51
|
The plugin instance returned after successful initialisation. Use this to interact with the embedded compliance plugin.
|
|
50
52
|
|
|
51
|
-
|
|
53
|
+
#### `plugin.on(event, handler)`
|
|
52
54
|
|
|
53
|
-
|
|
55
|
+
Subscribe to plugin events. Returns an unsubscribe function.
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
import { createCompliancePlugin } from '@twilltac/compliance-sdk';
|
|
57
|
-
|
|
58
|
-
async function initCompliance() {
|
|
59
|
-
const container = document.getElementById('compliance-container');
|
|
60
|
-
|
|
61
|
-
if (!container) {
|
|
62
|
-
throw new Error('Container element not found');
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
const plugin = await createCompliancePlugin({
|
|
67
|
-
containerElement: container,
|
|
68
|
-
data: {
|
|
69
|
-
workspace: 'YOUR_WORKSPACE_KEY',
|
|
70
|
-
customerReference: 'YOUR_CUSTOMER_REFERENCE',
|
|
71
|
-
authToken: 'YOUR_AUTH_TOKEN'
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
} catch (error) {
|
|
75
|
-
console.error('Failed to initialise compliance plugin:', error);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
57
|
+
**Parameters:**
|
|
78
58
|
|
|
79
|
-
|
|
80
|
-
|
|
59
|
+
| Name | Type | Description |
|
|
60
|
+
| --------- | ---------- | ------------------------------------------ |
|
|
61
|
+
| `event` | `string` | The event name to listen for |
|
|
62
|
+
| `handler` | `function` | Callback function invoked when event fires |
|
|
81
63
|
|
|
82
|
-
|
|
64
|
+
**Returns:** `() => void` - Call this function to unsubscribe from the event.
|
|
83
65
|
|
|
84
|
-
|
|
85
|
-
import { useEffect, useRef } from 'react';
|
|
86
|
-
import { createCompliancePlugin, CompliancePlugin } from '@twilltac/compliance-sdk';
|
|
66
|
+
**Available Events:**
|
|
87
67
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
authToken: string;
|
|
92
|
-
}
|
|
68
|
+
| Event | Data | Description |
|
|
69
|
+
| ----------------- | ------ | ----------------------------------------- |
|
|
70
|
+
| `session:expired` | `void` | Fired when the plugin session has expired |
|
|
93
71
|
|
|
94
|
-
|
|
95
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
|
96
|
-
const pluginRef = useRef<CompliancePlugin | null>(null);
|
|
72
|
+
**Example:**
|
|
97
73
|
|
|
98
|
-
|
|
99
|
-
|
|
74
|
+
```typescript
|
|
75
|
+
const plugin = await createCompliancePlugin({ ... });
|
|
100
76
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
.then((plugin) => {
|
|
106
|
-
pluginRef.current = plugin;
|
|
107
|
-
})
|
|
108
|
-
.catch(console.error);
|
|
109
|
-
}, [workspace, customerReference, authToken]);
|
|
77
|
+
const unsubscribe = plugin.on('session:expired', () => {
|
|
78
|
+
console.log('Session expired, generate a new token and re-initialise the plugin');
|
|
79
|
+
// Handle session expiry - e.g. refresh token and reinitialise
|
|
80
|
+
});
|
|
110
81
|
|
|
111
|
-
|
|
112
|
-
|
|
82
|
+
// Later, to stop listening:
|
|
83
|
+
unsubscribe();
|
|
113
84
|
```
|
|
114
85
|
|
|
115
|
-
|
|
86
|
+
## Framework Examples
|
|
87
|
+
|
|
88
|
+
### Angular + RxJS
|
|
116
89
|
|
|
117
90
|
```typescript
|
|
118
91
|
import { Component, ElementRef, input, viewChild } from '@angular/core';
|
|
@@ -162,7 +135,7 @@ export class ComplianceComponent {
|
|
|
162
135
|
}
|
|
163
136
|
```
|
|
164
137
|
|
|
165
|
-
### Angular
|
|
138
|
+
### Angular + Promises
|
|
166
139
|
|
|
167
140
|
```typescript
|
|
168
141
|
import { Component, ElementRef, afterNextRender, input, viewChild } from '@angular/core';
|
|
@@ -204,31 +177,37 @@ export class ComplianceComponent {
|
|
|
204
177
|
}
|
|
205
178
|
```
|
|
206
179
|
|
|
207
|
-
|
|
180
|
+
### React
|
|
208
181
|
|
|
209
|
-
|
|
182
|
+
```tsx
|
|
183
|
+
import { useEffect, useRef } from 'react';
|
|
184
|
+
import { createCompliancePlugin, CompliancePlugin } from '@twilltac/compliance-sdk';
|
|
210
185
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
min-height: 400px;
|
|
186
|
+
interface ComplianceWidgetProps {
|
|
187
|
+
workspace: string;
|
|
188
|
+
customerReference: string;
|
|
189
|
+
authToken: string;
|
|
216
190
|
}
|
|
217
|
-
```
|
|
218
191
|
|
|
219
|
-
|
|
192
|
+
function ComplianceWidget({ workspace, customerReference, authToken }: ComplianceWidgetProps) {
|
|
193
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
194
|
+
const pluginRef = useRef<CompliancePlugin | null>(null);
|
|
195
|
+
|
|
196
|
+
useEffect(() => {
|
|
197
|
+
if (!containerRef.current) return;
|
|
220
198
|
|
|
221
|
-
|
|
199
|
+
createCompliancePlugin({
|
|
200
|
+
containerElement: containerRef.current,
|
|
201
|
+
data: { workspace, customerReference, authToken }
|
|
202
|
+
})
|
|
203
|
+
.then((plugin) => {
|
|
204
|
+
pluginRef.current = plugin;
|
|
205
|
+
})
|
|
206
|
+
.catch(console.error);
|
|
207
|
+
}, [workspace, customerReference, authToken]);
|
|
222
208
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
.then((plugin) => {
|
|
226
|
-
// Plugin ready
|
|
227
|
-
})
|
|
228
|
-
.catch((error) => {
|
|
229
|
-
// Handle initialisation error
|
|
230
|
-
console.error('Compliance plugin error:', error);
|
|
231
|
-
});
|
|
209
|
+
return <div ref={containerRef} style={{ width: '100%', height: '600px' }} />;
|
|
210
|
+
}
|
|
232
211
|
```
|
|
233
212
|
|
|
234
213
|
## Browser Support
|
|
@@ -236,8 +215,6 @@ createCompliancePlugin(options)
|
|
|
236
215
|
The SDK requires a modern browser with support for:
|
|
237
216
|
|
|
238
217
|
- ES2020+
|
|
239
|
-
- `postMessage` API
|
|
240
|
-
- `iframe` elements
|
|
241
218
|
|
|
242
219
|
## Support
|
|
243
220
|
|
package/build-revision.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
20269584388-1
|
package/index.cjs
CHANGED
|
@@ -26,15 +26,57 @@ __export(index_exports, {
|
|
|
26
26
|
});
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
29
|
+
// libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.ts
|
|
30
|
+
var TypedEventEmitter = class {
|
|
31
|
+
_handlers = /* @__PURE__ */ new Map();
|
|
32
|
+
on(event, handler) {
|
|
33
|
+
if (!this._handlers.has(event)) {
|
|
34
|
+
this._handlers.set(event, /* @__PURE__ */ new Set());
|
|
35
|
+
}
|
|
36
|
+
this._handlers.get(event).add(handler);
|
|
37
|
+
return () => this.off(event, handler);
|
|
38
|
+
}
|
|
39
|
+
off(event, handler) {
|
|
40
|
+
const handlers = this._handlers.get(event);
|
|
41
|
+
if (handlers) {
|
|
42
|
+
handlers.delete(handler);
|
|
43
|
+
if (handlers.size === 0) {
|
|
44
|
+
this._handlers.delete(event);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
once(event, handler) {
|
|
49
|
+
const wrappedHandler = (data) => {
|
|
50
|
+
this.off(event, wrappedHandler);
|
|
51
|
+
handler(data);
|
|
52
|
+
};
|
|
53
|
+
return this.on(event, wrappedHandler);
|
|
54
|
+
}
|
|
55
|
+
emit(event, ...args) {
|
|
56
|
+
const handlers = this._handlers.get(event);
|
|
57
|
+
if (handlers) {
|
|
58
|
+
handlers.forEach((handler) => handler(args[0]));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
clear() {
|
|
62
|
+
this._handlers.clear();
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
29
66
|
// libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
|
|
30
67
|
var CompliancePlugin = class {
|
|
31
68
|
_iframeElement;
|
|
32
69
|
_containerElement;
|
|
33
70
|
_postMessageBus;
|
|
71
|
+
_events = new TypedEventEmitter();
|
|
34
72
|
constructor(opts) {
|
|
35
73
|
this._iframeElement = opts.iframeElement;
|
|
36
74
|
this._containerElement = opts.containerElement;
|
|
37
75
|
this._postMessageBus = opts.postMessageBus;
|
|
76
|
+
opts.postMessageBus.on("session:error", () => this._events.emit("session:expired"));
|
|
77
|
+
}
|
|
78
|
+
on(event, handler) {
|
|
79
|
+
return this._events.on(event, handler);
|
|
38
80
|
}
|
|
39
81
|
};
|
|
40
82
|
|
|
@@ -211,7 +253,7 @@ function createCompliancePlugin(options) {
|
|
|
211
253
|
options.containerElement.appendChild(form);
|
|
212
254
|
const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
|
|
213
255
|
return new Promise((resolve, rejects) => {
|
|
214
|
-
postMessageBus.on("
|
|
256
|
+
postMessageBus.on("initialisation:success", () => {
|
|
215
257
|
resolve(
|
|
216
258
|
new CompliancePlugin({
|
|
217
259
|
postMessageBus,
|
|
@@ -220,7 +262,7 @@ function createCompliancePlugin(options) {
|
|
|
220
262
|
})
|
|
221
263
|
);
|
|
222
264
|
});
|
|
223
|
-
postMessageBus.on("
|
|
265
|
+
postMessageBus.on("initialisation:error", (data) => {
|
|
224
266
|
rejects(data);
|
|
225
267
|
});
|
|
226
268
|
form.submit();
|
package/index.js
CHANGED
|
@@ -1,14 +1,56 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
// libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.ts
|
|
4
|
+
var TypedEventEmitter = class {
|
|
5
|
+
_handlers = /* @__PURE__ */ new Map();
|
|
6
|
+
on(event, handler) {
|
|
7
|
+
if (!this._handlers.has(event)) {
|
|
8
|
+
this._handlers.set(event, /* @__PURE__ */ new Set());
|
|
9
|
+
}
|
|
10
|
+
this._handlers.get(event).add(handler);
|
|
11
|
+
return () => this.off(event, handler);
|
|
12
|
+
}
|
|
13
|
+
off(event, handler) {
|
|
14
|
+
const handlers = this._handlers.get(event);
|
|
15
|
+
if (handlers) {
|
|
16
|
+
handlers.delete(handler);
|
|
17
|
+
if (handlers.size === 0) {
|
|
18
|
+
this._handlers.delete(event);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
once(event, handler) {
|
|
23
|
+
const wrappedHandler = (data) => {
|
|
24
|
+
this.off(event, wrappedHandler);
|
|
25
|
+
handler(data);
|
|
26
|
+
};
|
|
27
|
+
return this.on(event, wrappedHandler);
|
|
28
|
+
}
|
|
29
|
+
emit(event, ...args) {
|
|
30
|
+
const handlers = this._handlers.get(event);
|
|
31
|
+
if (handlers) {
|
|
32
|
+
handlers.forEach((handler) => handler(args[0]));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
clear() {
|
|
36
|
+
this._handlers.clear();
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
3
40
|
// libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
|
|
4
41
|
var CompliancePlugin = class {
|
|
5
42
|
_iframeElement;
|
|
6
43
|
_containerElement;
|
|
7
44
|
_postMessageBus;
|
|
45
|
+
_events = new TypedEventEmitter();
|
|
8
46
|
constructor(opts) {
|
|
9
47
|
this._iframeElement = opts.iframeElement;
|
|
10
48
|
this._containerElement = opts.containerElement;
|
|
11
49
|
this._postMessageBus = opts.postMessageBus;
|
|
50
|
+
opts.postMessageBus.on("session:error", () => this._events.emit("session:expired"));
|
|
51
|
+
}
|
|
52
|
+
on(event, handler) {
|
|
53
|
+
return this._events.on(event, handler);
|
|
12
54
|
}
|
|
13
55
|
};
|
|
14
56
|
|
|
@@ -185,7 +227,7 @@ function createCompliancePlugin(options) {
|
|
|
185
227
|
options.containerElement.appendChild(form);
|
|
186
228
|
const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
|
|
187
229
|
return new Promise((resolve, rejects) => {
|
|
188
|
-
postMessageBus.on("
|
|
230
|
+
postMessageBus.on("initialisation:success", () => {
|
|
189
231
|
resolve(
|
|
190
232
|
new CompliancePlugin({
|
|
191
233
|
postMessageBus,
|
|
@@ -194,7 +236,7 @@ function createCompliancePlugin(options) {
|
|
|
194
236
|
})
|
|
195
237
|
);
|
|
196
238
|
});
|
|
197
|
-
postMessageBus.on("
|
|
239
|
+
postMessageBus.on("initialisation:error", (data) => {
|
|
198
240
|
rejects(data);
|
|
199
241
|
});
|
|
200
242
|
form.submit();
|
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
import { CompliancePluginPostMessages } from '@twilltac/compliance-plugin-types';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { EventHandler, ExtractEventData } from '@twilltac/plugin-skd-event-emitter';
|
|
3
|
+
import { PostMessageBus } from '@twilltac/plugin-skd-post-messenger';
|
|
4
|
+
interface CompliancePluginData {
|
|
4
5
|
postMessageBus: PostMessageBus<CompliancePluginPostMessages>;
|
|
5
6
|
containerElement: HTMLElement;
|
|
6
7
|
iframeElement: HTMLIFrameElement;
|
|
7
8
|
}
|
|
9
|
+
interface CompliancePluginEvents {
|
|
10
|
+
['session:expired']: void;
|
|
11
|
+
}
|
|
8
12
|
export declare class CompliancePlugin {
|
|
9
13
|
private readonly _iframeElement;
|
|
10
14
|
private readonly _containerElement;
|
|
11
15
|
private readonly _postMessageBus;
|
|
12
|
-
|
|
16
|
+
private readonly _events;
|
|
17
|
+
constructor(opts: CompliancePluginData);
|
|
18
|
+
on<K extends keyof CompliancePluginEvents & string>(event: K, handler: EventHandler<ExtractEventData<CompliancePluginEvents[K]>>): () => void;
|
|
13
19
|
}
|
|
14
20
|
export {};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export interface CompliancePluginPostMessages {
|
|
2
|
-
['
|
|
2
|
+
['initialisation:success']: {
|
|
3
3
|
payload: {
|
|
4
4
|
appInstanceId: string;
|
|
5
5
|
};
|
|
6
6
|
};
|
|
7
|
-
['
|
|
7
|
+
['initialisation:error']: {
|
|
8
8
|
payload: {
|
|
9
9
|
error: Error;
|
|
10
10
|
};
|
|
@@ -12,4 +12,5 @@ export interface CompliancePluginPostMessages {
|
|
|
12
12
|
error: Error;
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
|
+
['session:error']: void;
|
|
15
16
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EventHandler, ExtractEventData, ITypedEventEmitter, OptionalIfVoid } from './typed-event-emitter.interface';
|
|
2
|
+
export declare class TypedEventEmitter<TEvents = Record<string, {
|
|
3
|
+
data: unknown;
|
|
4
|
+
}>> implements ITypedEventEmitter<TEvents> {
|
|
5
|
+
private readonly _handlers;
|
|
6
|
+
on<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): () => void;
|
|
7
|
+
off<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): void;
|
|
8
|
+
once<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): () => void;
|
|
9
|
+
emit<K extends keyof TEvents & string>(event: K, ...args: OptionalIfVoid<ExtractEventData<TEvents[K]>>): void;
|
|
10
|
+
clear(): void;
|
|
11
|
+
}
|
package/libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.interface.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type EventHandler<T = unknown> = (data: T) => void;
|
|
2
|
+
export type ExtractEventData<T> = T extends {
|
|
3
|
+
data: infer D;
|
|
4
|
+
} ? D : void;
|
|
5
|
+
export type OptionalIfVoid<T> = T extends void ? [] : [data: T];
|
|
6
|
+
export interface ITypedEventEmitter<TEvents = Record<string, {
|
|
7
|
+
data: unknown;
|
|
8
|
+
}>> {
|
|
9
|
+
on<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): () => void;
|
|
10
|
+
off<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): void;
|
|
11
|
+
once<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): () => void;
|
|
12
|
+
emit<K extends keyof TEvents & string>(event: K, ...args: OptionalIfVoid<ExtractEventData<TEvents[K]>>): void;
|
|
13
|
+
}
|