@twilltac/compliance-sdk 0.1.7 → 0.1.8

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,13 +36,13 @@ 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
 
@@ -69,9 +69,10 @@ plugin.on('session:expired', () => {
69
69
  <!-- EVENTS_TABLE_START -->
70
70
  | Event | Data | Description |
71
71
  | ----- | ---- | ----------- |
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. |
72
+ | `flow.journey.started` | `void` | Emits when the user starts the journey and IDD document is generated. |
73
+ | `flow.journey.section:started` | `SectionStartedEvent` | Emits when the user starts a section. |
74
+ | `flow.journey.section:completed` | `SectionEndedEvent` | Emits when the user starts a completes the question set and selects their recommendations. |
75
+ | `flow.documents:list` | `ComplianceDocument[]` | Emits when the user completes the journey and recommendations are produced. |
75
76
  | `session:expired` | `void` | Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. |
76
77
 
77
78
  #### Type Definitions
@@ -93,6 +94,11 @@ interface SectionEndedEvent {
93
94
  readonly description: string;
94
95
  }[];
95
96
  }
97
+
98
+ interface ComplianceDocument {
99
+ readonly name: string;
100
+ readonly reference: string;
101
+ }
96
102
  ```
97
103
  <!-- EVENTS_TABLE_END -->
98
104
 
@@ -1 +1 @@
1
- 22864006029-1
1
+ 22873654428-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.8",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",