@twilltac/compliance-sdk 0.1.7 → 0.1.9

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 CHANGED
@@ -36,24 +36,47 @@ Creates and initialises the compliance plugin within a container element.
36
36
 
37
37
  **Parameters:**
38
38
 
39
- | Name | Type | Description |
40
- | -------------------------------- | ------------- | ------------------------------------------------------- |
41
- | `options.containerElement` | `HTMLElement` | The DOM element where the plugin will be rendered |
42
- | `options.data.workspace` | `string` | Your Twilltac workspace identifier |
39
+ | Name | Type | Description |
40
+ | -------------------------------- | ------------- | --------------------------------------------------------------------- |
41
+ | `options.containerElement` | `HTMLElement` | The DOM element where the plugin will be rendered |
42
+ | `options.data.workspace` | `string` | Your Twilltac workspace identifier |
43
43
  | `options.data.flow` | `string` | The plugin flow to load. One of `journey`, `handover`, or `documents` |
44
- | `options.data.customerReference` | `string` | Reference identifier for the customer |
45
- | `options.data.authToken` | `string` | Authentication token for the plugin session |
44
+ | `options.data.customerReference` | `string` | Reference identifier for the customer |
45
+ | `options.data.authToken` | `string` | Authentication token for the plugin session |
46
46
 
47
47
  **Returns:** `Promise<CompliancePlugin>`
48
48
 
49
49
  The promise resolves when the plugin has successfully initialised. If initialisation fails, the promise rejects with error details.
50
50
 
51
+ ### `plugin.destroy()`
52
+
53
+ Cleans up all event listeners and internal resources held by the plugin instance. Call this before re-initialising the plugin to prevent listeners from accumulating across multiple initialisations.
54
+
55
+ ```typescript
56
+ const plugin = await createCompliancePlugin({ ... });
57
+
58
+ // Later, when re-initialising or unmounting:
59
+ plugin.destroy();
60
+ ```
61
+
51
62
  ### Events
52
63
 
53
64
  You can subscribe to events via the plugin instance returned after successful initialisation.
54
65
 
55
66
  #### `plugin.on(event, handler)`
56
67
 
68
+ Subscribe to a specific event.
69
+
70
+ #### `plugin.on(handler)`
71
+
72
+ Subscribe to all events. The handler receives the event name as the first argument and the event data as the second.
73
+
74
+ ```typescript
75
+ plugin.on((event, data) => {
76
+ console.log(`Event: ${event}`, data);
77
+ });
78
+ ```
79
+
57
80
  #### Example:
58
81
 
59
82
  ```typescript
@@ -69,9 +92,10 @@ plugin.on('session:expired', () => {
69
92
  <!-- EVENTS_TABLE_START -->
70
93
  | Event | Data | Description |
71
94
  | ----- | ---- | ----------- |
72
- | `started` | `void` | Emits when the user starts the journey and IDD document is generated. |
73
- | `section:started` | `SectionStartedEvent` | Emits when the user starts a section. |
74
- | `section:completed` | `SectionEndedEvent` | Emits when the user starts a completes the question set and selects their recommendations. |
95
+ | `flow.journey.started` | `void` | Emits when the user starts the journey and IDD document is generated. |
96
+ | `flow.journey.section:started` | `SectionStartedEvent` | Emits when the user starts a section. |
97
+ | `flow.journey.section:completed` | `SectionEndedEvent` | Emits when the user starts a completes the question set and selects their recommendations. |
98
+ | `flow.documents:list` | `ComplianceDocument[]` | Emits when the user completes the journey and recommendations are produced. |
75
99
  | `session:expired` | `void` | Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. |
76
100
 
77
101
  #### Type Definitions
@@ -93,6 +117,11 @@ interface SectionEndedEvent {
93
117
  readonly description: string;
94
118
  }[];
95
119
  }
120
+
121
+ interface ComplianceDocument {
122
+ readonly name: string;
123
+ readonly reference: string;
124
+ }
96
125
  ```
97
126
  <!-- EVENTS_TABLE_END -->
98
127
 
@@ -102,7 +131,7 @@ interface SectionEndedEvent {
102
131
 
103
132
  ```typescript
104
133
  import { Component, ElementRef, input, viewChild } from '@angular/core';
105
- import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
134
+ import { takeUntilDestroyed, toObservable } from '@anlar/core/rxjs-interop';
106
135
  import { createCompliancePlugin, CompliancePlugin } from '@twilltac/compliance-sdk';
107
136
  import { from, switchMap } from 'rxjs';
108
137
 
@@ -123,8 +152,11 @@ export class ComplianceComponent {
123
152
  constructor() {
124
153
  toObservable(this._container)
125
154
  .pipe(
126
- switchMap((container) =>
127
- from(
155
+ switchMap((container) => {
156
+ this._plugin?.destroy();
157
+ this._plugin = null;
158
+
159
+ return from(
128
160
  createCompliancePlugin({
129
161
  containerElement: container.nativeElement,
130
162
  data: {
@@ -134,8 +166,8 @@ export class ComplianceComponent {
134
166
  authToken: this.authToken()
135
167
  }
136
168
  })
137
- )
138
- ),
169
+ );
170
+ }),
139
171
  takeUntilDestroyed()
140
172
  )
141
173
  .subscribe({
@@ -177,6 +209,9 @@ export class ComplianceComponent {
177
209
  }
178
210
 
179
211
  private async _initialisePlugin(): Promise<void> {
212
+ this._plugin?.destroy();
213
+ this._plugin = null;
214
+
180
215
  try {
181
216
  this._plugin = await createCompliancePlugin({
182
217
  containerElement: this._container().nativeElement,
@@ -214,6 +249,9 @@ function ComplianceWidget({ workspace, flow, customerReference, authToken }: Com
214
249
  useEffect(() => {
215
250
  if (!containerRef.current) return;
216
251
 
252
+ pluginRef.current?.destroy();
253
+ pluginRef.current = null;
254
+
217
255
  createCompliancePlugin({
218
256
  containerElement: containerRef.current,
219
257
  data: { workspace, flow, customerReference, authToken }
@@ -222,6 +260,11 @@ function ComplianceWidget({ workspace, flow, customerReference, authToken }: Com
222
260
  pluginRef.current = plugin;
223
261
  })
224
262
  .catch(console.error);
263
+
264
+ return () => {
265
+ pluginRef.current?.destroy();
266
+ pluginRef.current = null;
267
+ };
225
268
  }, [workspace, flow, customerReference, authToken]);
226
269
 
227
270
  return <div ref={containerRef} style={{ width: '100%', height: '600px' }} />;
@@ -1 +1 @@
1
- 22864006029-1
1
+ 22873884653-1
package/index.cjs CHANGED
@@ -65,18 +65,43 @@ var TypedEventEmitter = class {
65
65
 
66
66
  // libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
67
67
  var CompliancePlugin = class {
68
- _containerElement;
69
68
  _events = new TypedEventEmitter();
69
+ _wildcardHandlers = /* @__PURE__ */ new Set();
70
+ _postMessageBus;
70
71
  constructor(opts) {
71
- this._containerElement = opts.containerElement;
72
- opts.postMessageBus.on("window:resize", (data) => this._containerElement.style.minHeight = `${data.height}px`);
72
+ this._postMessageBus = opts.postMessageBus;
73
+ opts.iframeElement.style.visibility = "visible";
74
+ opts.iframeElement.style.minHeight = `${opts.initialisationData.documentHeight}px`;
75
+ opts.postMessageBus.on("window:resize", (data) => opts.iframeElement.style.minHeight = `${data.height}px`);
73
76
  opts.postMessageBus.on("session:expired-token", () => this._events.emit("session:expired"));
74
- opts.postMessageBus.on("journey:started", () => this._events.emit("started"));
75
- opts.postMessageBus.on("section:started", (data) => this._events.emit("section:started", data));
76
- opts.postMessageBus.on("section:completed", (data) => this._events.emit("section:completed", data));
77
+ opts.postMessageBus.on("journey:started", () => this._events.emit("flow.journey.started"));
78
+ opts.postMessageBus.on("journey.section:started", (data) => this._events.emit("flow.journey.section:started", data));
79
+ opts.postMessageBus.on("journey.section:completed", (data) => this._events.emit("flow.journey.section:completed", data));
80
+ opts.postMessageBus.on("documents:loaded", (data) => this._events.emit("flow.documents:list", data));
81
+ const eventNames = [
82
+ "session:expired",
83
+ "flow.journey.started",
84
+ "flow.journey.section:started",
85
+ "flow.journey.section:completed",
86
+ "flow.documents:list"
87
+ ];
88
+ for (const eventName of eventNames) {
89
+ this._events.on(eventName, ((...args) => {
90
+ this._wildcardHandlers.forEach((handler) => handler(eventName, args[0]));
91
+ }));
92
+ }
77
93
  }
78
- on(event, handler) {
79
- return this._events.on(event, handler);
94
+ on(eventOrHandler, handler) {
95
+ if (typeof eventOrHandler === "function") {
96
+ this._wildcardHandlers.add(eventOrHandler);
97
+ return () => this._wildcardHandlers.delete(eventOrHandler);
98
+ }
99
+ return this._events.on(eventOrHandler, handler);
100
+ }
101
+ destroy() {
102
+ this._events.clear();
103
+ this._wildcardHandlers.clear();
104
+ this._postMessageBus.destroy();
80
105
  }
81
106
  };
82
107
 
@@ -274,6 +299,7 @@ function createCompliancePlugin(options) {
274
299
  iframe.style.height = "100%";
275
300
  iframe.style.border = "none";
276
301
  iframe.style.overflow = "hidden";
302
+ iframe.style.visibility = "hidden";
277
303
  const form = document.createElement("form");
278
304
  form.method = "POST";
279
305
  form.action = `${url}/en-gb`;
@@ -289,17 +315,17 @@ function createCompliancePlugin(options) {
289
315
  options.containerElement.appendChild(form);
290
316
  const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
291
317
  return new Promise((resolve, rejects) => {
292
- postMessageBus.on("initialisation:success", () => {
318
+ postMessageBus.on("initialisation:success", (data) => {
293
319
  resolve(
294
320
  new CompliancePlugin({
321
+ initialisationData: data,
295
322
  postMessageBus,
296
- containerElement: options.containerElement,
297
323
  iframeElement: iframe
298
324
  })
299
325
  );
300
326
  });
301
327
  postMessageBus.on("initialisation:error", (data) => {
302
- rejects(data);
328
+ rejects(data.error);
303
329
  });
304
330
  form.submit();
305
331
  });
package/index.js CHANGED
@@ -39,18 +39,43 @@ var TypedEventEmitter = class {
39
39
 
40
40
  // libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
41
41
  var CompliancePlugin = class {
42
- _containerElement;
43
42
  _events = new TypedEventEmitter();
43
+ _wildcardHandlers = /* @__PURE__ */ new Set();
44
+ _postMessageBus;
44
45
  constructor(opts) {
45
- this._containerElement = opts.containerElement;
46
- opts.postMessageBus.on("window:resize", (data) => this._containerElement.style.minHeight = `${data.height}px`);
46
+ this._postMessageBus = opts.postMessageBus;
47
+ opts.iframeElement.style.visibility = "visible";
48
+ opts.iframeElement.style.minHeight = `${opts.initialisationData.documentHeight}px`;
49
+ opts.postMessageBus.on("window:resize", (data) => opts.iframeElement.style.minHeight = `${data.height}px`);
47
50
  opts.postMessageBus.on("session:expired-token", () => this._events.emit("session:expired"));
48
- opts.postMessageBus.on("journey:started", () => this._events.emit("started"));
49
- opts.postMessageBus.on("section:started", (data) => this._events.emit("section:started", data));
50
- opts.postMessageBus.on("section:completed", (data) => this._events.emit("section:completed", data));
51
+ opts.postMessageBus.on("journey:started", () => this._events.emit("flow.journey.started"));
52
+ opts.postMessageBus.on("journey.section:started", (data) => this._events.emit("flow.journey.section:started", data));
53
+ opts.postMessageBus.on("journey.section:completed", (data) => this._events.emit("flow.journey.section:completed", data));
54
+ opts.postMessageBus.on("documents:loaded", (data) => this._events.emit("flow.documents:list", data));
55
+ const eventNames = [
56
+ "session:expired",
57
+ "flow.journey.started",
58
+ "flow.journey.section:started",
59
+ "flow.journey.section:completed",
60
+ "flow.documents:list"
61
+ ];
62
+ for (const eventName of eventNames) {
63
+ this._events.on(eventName, ((...args) => {
64
+ this._wildcardHandlers.forEach((handler) => handler(eventName, args[0]));
65
+ }));
66
+ }
51
67
  }
52
- on(event, handler) {
53
- return this._events.on(event, handler);
68
+ on(eventOrHandler, handler) {
69
+ if (typeof eventOrHandler === "function") {
70
+ this._wildcardHandlers.add(eventOrHandler);
71
+ return () => this._wildcardHandlers.delete(eventOrHandler);
72
+ }
73
+ return this._events.on(eventOrHandler, handler);
74
+ }
75
+ destroy() {
76
+ this._events.clear();
77
+ this._wildcardHandlers.clear();
78
+ this._postMessageBus.destroy();
54
79
  }
55
80
  };
56
81
 
@@ -248,6 +273,7 @@ function createCompliancePlugin(options) {
248
273
  iframe.style.height = "100%";
249
274
  iframe.style.border = "none";
250
275
  iframe.style.overflow = "hidden";
276
+ iframe.style.visibility = "hidden";
251
277
  const form = document.createElement("form");
252
278
  form.method = "POST";
253
279
  form.action = `${url}/en-gb`;
@@ -263,17 +289,17 @@ function createCompliancePlugin(options) {
263
289
  options.containerElement.appendChild(form);
264
290
  const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
265
291
  return new Promise((resolve, rejects) => {
266
- postMessageBus.on("initialisation:success", () => {
292
+ postMessageBus.on("initialisation:success", (data) => {
267
293
  resolve(
268
294
  new CompliancePlugin({
295
+ initialisationData: data,
269
296
  postMessageBus,
270
- containerElement: options.containerElement,
271
297
  iframeElement: iframe
272
298
  })
273
299
  );
274
300
  });
275
301
  postMessageBus.on("initialisation:error", (data) => {
276
- rejects(data);
302
+ rejects(data.error);
277
303
  });
278
304
  form.submit();
279
305
  });
@@ -13,13 +13,19 @@ interface SectionEndedEvent {
13
13
  readonly description: string;
14
14
  }[];
15
15
  }
16
+ interface ComplianceDocument {
17
+ readonly name: string;
18
+ readonly reference: string;
19
+ }
16
20
  export interface CompliancePluginEvents {
17
21
  /** Emits when the user starts the journey and IDD document is generated. */
18
- ['started']: void;
22
+ ['flow.journey.started']: void;
19
23
  /** Emits when the user starts a section. */
20
- ['section:started']: SectionStartedEvent;
24
+ ['flow.journey.section:started']: SectionStartedEvent;
21
25
  /** Emits when the user starts a completes the question set and selects their recommendations. */
22
- ['section:completed']: SectionEndedEvent;
26
+ ['flow.journey.section:completed']: SectionEndedEvent;
27
+ /** Emits when the user completes the journey and recommendations are produced. */
28
+ ['flow.documents:list']: ComplianceDocument[];
23
29
  /** Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. */
24
30
  ['session:expired']: void;
25
31
  }
@@ -1,16 +1,20 @@
1
- import { CompliancePluginPostMessages } from '@twilltac/compliance-plugin-types';
1
+ import { CompliancePluginPostMessages, InitialisationData } from '@twilltac/compliance-plugin-types';
2
2
  import { EventHandler, ExtractEventData } from '@twilltac/plugin-skd-event-emitter';
3
3
  import { PostMessageBus } from '@twilltac/plugin-skd-post-messenger';
4
4
  import { CompliancePluginEvents } from './compliance-plugin.events';
5
+ type WildcardHandler = (event: keyof CompliancePluginEvents & string, data: ExtractEventData<CompliancePluginEvents[keyof CompliancePluginEvents]>) => void;
5
6
  interface CompliancePluginData {
6
7
  postMessageBus: PostMessageBus<CompliancePluginPostMessages>;
7
- containerElement: HTMLElement;
8
+ initialisationData: InitialisationData;
8
9
  iframeElement: HTMLIFrameElement;
9
10
  }
10
11
  export declare class CompliancePlugin {
11
- private readonly _containerElement;
12
12
  private readonly _events;
13
+ private readonly _wildcardHandlers;
14
+ private readonly _postMessageBus;
13
15
  constructor(opts: CompliancePluginData);
16
+ on(handler: WildcardHandler): () => void;
14
17
  on<K extends keyof CompliancePluginEvents & string>(event: K, handler: EventHandler<ExtractEventData<CompliancePluginEvents[K]>>): () => void;
18
+ destroy(): void;
15
19
  }
16
20
  export {};
@@ -14,15 +14,25 @@ interface ProductRecommendation {
14
14
  readonly title: string;
15
15
  readonly description: string;
16
16
  }
17
+ interface ComplianceDocument {
18
+ readonly name: string;
19
+ readonly reference: string;
20
+ }
21
+ export interface InitialisationData {
22
+ readonly appInstanceId: string;
23
+ readonly documentHeight: number;
24
+ }
17
25
  export interface CompliancePluginPostMessages {
18
26
  ['window:resize']: {
19
27
  payload: {
20
28
  height: number;
21
29
  };
22
30
  };
31
+ ['session:expired-token']: void;
23
32
  ['initialisation:success']: {
24
33
  payload: {
25
34
  appInstanceId: string;
35
+ documentHeight: number;
26
36
  };
27
37
  };
28
38
  ['initialisation:error']: {
@@ -33,14 +43,16 @@ export interface CompliancePluginPostMessages {
33
43
  error: Error;
34
44
  };
35
45
  };
36
- ['session:expired-token']: void;
37
46
  ['journey:started']: void;
38
47
  ['journey:completed']: void;
39
- ['section:started']: {
48
+ ['journey.section:started']: {
40
49
  payload: SectionStartedEvent;
41
50
  };
42
- ['section:completed']: {
51
+ ['journey.section:completed']: {
43
52
  payload: SectionEndedEvent;
44
53
  };
54
+ ['documents:loaded']: {
55
+ payload: ComplianceDocument[];
56
+ };
45
57
  }
46
58
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twilltac/compliance-sdk",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",