@twilltac/compliance-sdk 0.1.3 → 0.1.4

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
@@ -46,42 +46,53 @@ Creates and initialises the compliance plugin within a container element.
46
46
 
47
47
  The promise resolves when the plugin has successfully initialised. If initialisation fails, the promise rejects with error details.
48
48
 
49
- ### CompliancePlugin
49
+ ### Events
50
50
 
51
- The plugin instance returned after successful initialisation. Use this to interact with the embedded compliance plugin.
51
+ You can subscribe to events via the plugin instance returned after successful initialisation.
52
52
 
53
53
  #### `plugin.on(event, handler)`
54
54
 
55
- Subscribe to plugin events. Returns an unsubscribe function.
55
+ #### Example:
56
56
 
57
- **Parameters:**
58
-
59
- | Name | Type | Description |
60
- | --------- | ---------- | ------------------------------------------ |
61
- | `event` | `string` | The event name to listen for |
62
- | `handler` | `function` | Callback function invoked when event fires |
57
+ ```typescript
58
+ const plugin = await createCompliancePlugin({ ... });
63
59
 
64
- **Returns:** `() => void` - Call this function to unsubscribe from the event.
60
+ plugin.on('session:expired', () => {
61
+ // Handle session expiry - e.g. refresh token and reinitialise
62
+ });
63
+ ```
65
64
 
66
- **Available Events:**
65
+ #### Available events:
67
66
 
68
- | Event | Data | Description |
69
- | ----------------- | ------ | ----------------------------------------- |
70
- | `session:expired` | `void` | Fired when the plugin session has expired |
67
+ <!-- EVENTS_TABLE_START -->
68
+ | Event | Data | Description |
69
+ | ----- | ---- | ----------- |
70
+ | `session:expired` | `void` | Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. |
71
+ | `started` | `void` | Emits when the user starts the journey and IDD document is generated. |
72
+ | `section:started` | `SectionStartedEvent` | Emits when the user starts a section. |
73
+ | `section:completed` | `SectionEndedEvent` | Emits when the user starts a completes the question set and selects their recommendations. |
71
74
 
72
- **Example:**
75
+ #### Type Definitions
73
76
 
74
77
  ```typescript
75
- const plugin = await createCompliancePlugin({ ... });
76
-
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
- });
78
+ interface SectionStartedEvent {
79
+ readonly name: string;
80
+ readonly reference: string;
81
+ }
81
82
 
82
- // Later, to stop listening:
83
- unsubscribe();
83
+ interface SectionEndedEvent {
84
+ readonly name: string;
85
+ readonly reference: string;
86
+ readonly recommendations: {
87
+ readonly externalReference: string;
88
+ readonly price: number | null;
89
+ readonly regulated: boolean;
90
+ readonly title: string;
91
+ readonly description: string;
92
+ }[];
93
+ }
84
94
  ```
95
+ <!-- EVENTS_TABLE_END -->
85
96
 
86
97
  ## Framework Examples
87
98
 
@@ -1 +1 @@
1
- 20269584388-1
1
+ 21181652609-1
package/index.cjs CHANGED
@@ -65,15 +65,15 @@ var TypedEventEmitter = class {
65
65
 
66
66
  // libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
67
67
  var CompliancePlugin = class {
68
- _iframeElement;
69
68
  _containerElement;
70
- _postMessageBus;
71
69
  _events = new TypedEventEmitter();
72
70
  constructor(opts) {
73
- this._iframeElement = opts.iframeElement;
74
71
  this._containerElement = opts.containerElement;
75
- this._postMessageBus = opts.postMessageBus;
76
- opts.postMessageBus.on("session:error", () => this._events.emit("session:expired"));
72
+ opts.postMessageBus.on("window:resize", (data) => this._containerElement.style.minHeight = `${data.height}px`);
73
+ 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
77
  }
78
78
  on(event, handler) {
79
79
  return this._events.on(event, handler);
@@ -99,10 +99,11 @@ var PostMessageBus = class {
99
99
  window.addEventListener("message", this._boundMessageHandler);
100
100
  }
101
101
  send(type, ...args) {
102
+ const payload = args[0];
102
103
  const envelope = {
103
104
  channel: this._channel,
104
105
  type,
105
- payload: args[0]
106
+ payload
106
107
  };
107
108
  this._targetWindow.postMessage(envelope, this._targetOrigin);
108
109
  }
@@ -131,9 +132,9 @@ var PostMessageBus = class {
131
132
  }
132
133
  request(type, ...args) {
133
134
  return new Promise((resolve, reject) => {
134
- const hasPayload = args.length > 0 && typeof args[0] !== "number";
135
- const payload = hasPayload ? args[0] : void 0;
136
- const timeout = hasPayload ? args[1] : args[0];
135
+ const [firstArg, secondArg] = args;
136
+ const payload = typeof firstArg === "number" ? void 0 : firstArg;
137
+ const timeout = typeof firstArg === "number" ? firstArg : secondArg;
137
138
  const correlationId = this.generateCorrelationId();
138
139
  const timeoutMs = timeout ?? this._defaultTimeout;
139
140
  const timeoutId = setTimeout(() => {
@@ -235,9 +236,11 @@ function createCompliancePlugin(options) {
235
236
  const url = production ? baseUrl : `${baseUrl}.ngserve`;
236
237
  const iframe = document.createElement("iframe");
237
238
  iframe.name = "compliance-plugin-iframe";
239
+ iframe.setAttribute("scrolling", "no");
238
240
  iframe.style.width = "100%";
239
241
  iframe.style.height = "100%";
240
242
  iframe.style.border = "none";
243
+ iframe.style.overflow = "hidden";
241
244
  const form = document.createElement("form");
242
245
  form.method = "POST";
243
246
  form.action = `${url}/en-gb`;
package/index.js CHANGED
@@ -39,15 +39,15 @@ var TypedEventEmitter = class {
39
39
 
40
40
  // libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.ts
41
41
  var CompliancePlugin = class {
42
- _iframeElement;
43
42
  _containerElement;
44
- _postMessageBus;
45
43
  _events = new TypedEventEmitter();
46
44
  constructor(opts) {
47
- this._iframeElement = opts.iframeElement;
48
45
  this._containerElement = opts.containerElement;
49
- this._postMessageBus = opts.postMessageBus;
50
- opts.postMessageBus.on("session:error", () => this._events.emit("session:expired"));
46
+ opts.postMessageBus.on("window:resize", (data) => this._containerElement.style.minHeight = `${data.height}px`);
47
+ 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
51
  }
52
52
  on(event, handler) {
53
53
  return this._events.on(event, handler);
@@ -73,10 +73,11 @@ var PostMessageBus = class {
73
73
  window.addEventListener("message", this._boundMessageHandler);
74
74
  }
75
75
  send(type, ...args) {
76
+ const payload = args[0];
76
77
  const envelope = {
77
78
  channel: this._channel,
78
79
  type,
79
- payload: args[0]
80
+ payload
80
81
  };
81
82
  this._targetWindow.postMessage(envelope, this._targetOrigin);
82
83
  }
@@ -105,9 +106,9 @@ var PostMessageBus = class {
105
106
  }
106
107
  request(type, ...args) {
107
108
  return new Promise((resolve, reject) => {
108
- const hasPayload = args.length > 0 && typeof args[0] !== "number";
109
- const payload = hasPayload ? args[0] : void 0;
110
- const timeout = hasPayload ? args[1] : args[0];
109
+ const [firstArg, secondArg] = args;
110
+ const payload = typeof firstArg === "number" ? void 0 : firstArg;
111
+ const timeout = typeof firstArg === "number" ? firstArg : secondArg;
111
112
  const correlationId = this.generateCorrelationId();
112
113
  const timeoutMs = timeout ?? this._defaultTimeout;
113
114
  const timeoutId = setTimeout(() => {
@@ -209,9 +210,11 @@ function createCompliancePlugin(options) {
209
210
  const url = production ? baseUrl : `${baseUrl}.ngserve`;
210
211
  const iframe = document.createElement("iframe");
211
212
  iframe.name = "compliance-plugin-iframe";
213
+ iframe.setAttribute("scrolling", "no");
212
214
  iframe.style.width = "100%";
213
215
  iframe.style.height = "100%";
214
216
  iframe.style.border = "none";
217
+ iframe.style.overflow = "hidden";
215
218
  const form = document.createElement("form");
216
219
  form.method = "POST";
217
220
  form.action = `${url}/en-gb`;
@@ -0,0 +1,26 @@
1
+ interface SectionStartedEvent {
2
+ readonly name: string;
3
+ readonly reference: string;
4
+ }
5
+ interface SectionEndedEvent {
6
+ readonly name: string;
7
+ readonly reference: string;
8
+ readonly recommendations: {
9
+ readonly externalReference: string;
10
+ readonly price: number | null;
11
+ readonly regulated: boolean;
12
+ readonly title: string;
13
+ readonly description: string;
14
+ }[];
15
+ }
16
+ export interface CompliancePluginEvents {
17
+ /** Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. */
18
+ ['session:expired']: void;
19
+ /** Emits when the user starts the journey and IDD document is generated. */
20
+ ['started']: void;
21
+ /** Emits when the user starts a section. */
22
+ ['section:started']: SectionStartedEvent;
23
+ /** Emits when the user starts a completes the question set and selects their recommendations. */
24
+ ['section:completed']: SectionEndedEvent;
25
+ }
26
+ export {};
@@ -1,18 +1,14 @@
1
1
  import { CompliancePluginPostMessages } 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
+ import { CompliancePluginEvents } from './compliance-plugin.events';
4
5
  interface CompliancePluginData {
5
6
  postMessageBus: PostMessageBus<CompliancePluginPostMessages>;
6
7
  containerElement: HTMLElement;
7
8
  iframeElement: HTMLIFrameElement;
8
9
  }
9
- interface CompliancePluginEvents {
10
- ['session:expired']: void;
11
- }
12
10
  export declare class CompliancePlugin {
13
- private readonly _iframeElement;
14
11
  private readonly _containerElement;
15
- private readonly _postMessageBus;
16
12
  private readonly _events;
17
13
  constructor(opts: CompliancePluginData);
18
14
  on<K extends keyof CompliancePluginEvents & string>(event: K, handler: EventHandler<ExtractEventData<CompliancePluginEvents[K]>>): () => void;
@@ -1,4 +1,25 @@
1
+ interface SectionStartedEvent {
2
+ readonly name: string;
3
+ readonly reference: string;
4
+ }
5
+ interface SectionEndedEvent {
6
+ readonly name: string;
7
+ readonly reference: string;
8
+ readonly recommendations: ProductRecommendation[];
9
+ }
10
+ interface ProductRecommendation {
11
+ readonly externalReference: string;
12
+ readonly price: number | null;
13
+ readonly regulated: boolean;
14
+ readonly title: string;
15
+ readonly description: string;
16
+ }
1
17
  export interface CompliancePluginPostMessages {
18
+ ['window:resize']: {
19
+ payload: {
20
+ height: number;
21
+ };
22
+ };
2
23
  ['initialisation:success']: {
3
24
  payload: {
4
25
  appInstanceId: string;
@@ -12,5 +33,14 @@ export interface CompliancePluginPostMessages {
12
33
  error: Error;
13
34
  };
14
35
  };
15
- ['session:error']: void;
36
+ ['session:expired-token']: void;
37
+ ['journey:started']: void;
38
+ ['journey:completed']: void;
39
+ ['section:started']: {
40
+ payload: SectionStartedEvent;
41
+ };
42
+ ['section:completed']: {
43
+ payload: SectionEndedEvent;
44
+ };
16
45
  }
46
+ export {};
@@ -1,3 +1,3 @@
1
1
  export { NoopPostMessageBus } from './lib/noop-post-message.bus';
2
- export { IPostMessageBus } from './lib/post-message-bus.interface';
2
+ export type { ExtractPayload, HasPayload, IPostMessageBus } from './lib/post-message-bus.interface';
3
3
  export { PostMessageBus } from './lib/post-message.bus';
@@ -1,13 +1,13 @@
1
- import { ExtractResponse, IPostMessageBus } from './post-message-bus.interface';
1
+ import { ExtractPayload, ExtractResponse, HasPayload, IPostMessageBus, MessageHandler, SendArgs } from './post-message-bus.interface';
2
2
  export declare class NoopPostMessageBus<TMessages = Record<string, {
3
3
  payload: unknown;
4
4
  response?: unknown;
5
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]>>;
6
+ send<K extends keyof TMessages & string>(_type: K, ..._args: SendArgs<TMessages[K]>): void;
7
+ on<K extends keyof TMessages & string>(_type: K, _handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
8
+ off<K extends keyof TMessages & string>(_type: K, _handler: MessageHandler<ExtractPayload<TMessages[K]>>): void;
9
+ once<K extends keyof TMessages & string>(_type: K, _handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
10
+ request<K extends keyof TMessages & string>(_type: K, ..._args: HasPayload<TMessages[K]> extends true ? [payload: ExtractPayload<TMessages[K]>, timeout?: number] : [timeout?: number]): Promise<ExtractResponse<TMessages[K]>>;
11
11
  onRequest(): () => void;
12
12
  destroy(): void;
13
13
  }
@@ -2,11 +2,16 @@ export type MessageHandler<T = unknown> = (payload: T) => void;
2
2
  export type RequestHandler<TRequest = unknown, TResponse = unknown> = (payload: TRequest) => TResponse | Promise<TResponse>;
3
3
  export type ExtractPayload<T> = T extends {
4
4
  payload: infer P;
5
- } ? P : void;
5
+ } ? P : never;
6
6
  export type ExtractResponse<T> = T extends {
7
7
  response: infer R;
8
- } ? R : void;
9
- export type OptionalIfVoid<T> = T extends void ? [] : [payload: T];
8
+ } ? R : undefined;
9
+ export type HasPayload<T> = T extends {
10
+ payload: unknown;
11
+ } ? true : false;
12
+ export type SendArgs<T> = T extends {
13
+ payload: infer P;
14
+ } ? [payload: P] : [];
10
15
  export interface PostMessageEnvelope<T = unknown> {
11
16
  channel: string;
12
17
  type: string;
@@ -30,11 +35,11 @@ export interface IPostMessageBus<TMessages = Record<string, {
30
35
  payload: unknown;
31
36
  response?: unknown;
32
37
  }>> {
33
- send<K extends keyof TMessages & string>(type: K, ...args: OptionalIfVoid<ExtractPayload<TMessages[K]>>): void;
38
+ send<K extends keyof TMessages & string>(type: K, ...args: SendArgs<TMessages[K]>): void;
34
39
  on<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
35
40
  off<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): void;
36
41
  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]>>;
42
+ request<K extends keyof TMessages & string>(type: K, ...args: HasPayload<TMessages[K]> extends true ? [payload: ExtractPayload<TMessages[K]>, timeout?: number] : [timeout?: number]): Promise<ExtractResponse<TMessages[K]>>;
38
43
  onRequest<K extends keyof TMessages & string>(type: K, handler: RequestHandler<ExtractPayload<TMessages[K]>, ExtractResponse<TMessages[K]>>): () => void;
39
44
  destroy(): void;
40
45
  }
@@ -1,4 +1,4 @@
1
- import { ExtractPayload, ExtractResponse, IPostMessageBus, MessageHandler, OptionalIfVoid, PostMessageBusOptions, RequestHandler } from './post-message-bus.interface';
1
+ import { ExtractPayload, ExtractResponse, HasPayload, IPostMessageBus, MessageHandler, PostMessageBusOptions, RequestHandler, SendArgs } from './post-message-bus.interface';
2
2
  /** @internal */
3
3
  export declare class PostMessageBus<TMessages = Record<string, {
4
4
  payload: unknown;
@@ -13,11 +13,11 @@ export declare class PostMessageBus<TMessages = Record<string, {
13
13
  private readonly _pendingRequests;
14
14
  private readonly _boundMessageHandler;
15
15
  constructor(options: PostMessageBusOptions);
16
- send<K extends keyof TMessages & string>(type: K, ...args: OptionalIfVoid<ExtractPayload<TMessages[K]>>): void;
16
+ send<K extends keyof TMessages & string>(type: K, ...args: SendArgs<TMessages[K]>): void;
17
17
  on<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): () => void;
18
18
  off<K extends keyof TMessages & string>(type: K, handler: MessageHandler<ExtractPayload<TMessages[K]>>): void;
19
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]>>;
20
+ request<K extends keyof TMessages & string>(type: K, ...args: HasPayload<TMessages[K]> extends true ? [payload: ExtractPayload<TMessages[K]>, timeout?: number] : [timeout?: number]): Promise<ExtractResponse<TMessages[K]>>;
21
21
  onRequest<K extends keyof TMessages & string>(type: K, handler: RequestHandler<ExtractPayload<TMessages[K]>, ExtractResponse<TMessages[K]>>): () => void;
22
22
  destroy(): void;
23
23
  private handleMessage;
@@ -1,7 +1,5 @@
1
1
  export type EventHandler<T = unknown> = (data: T) => void;
2
- export type ExtractEventData<T> = T extends {
3
- data: infer D;
4
- } ? D : void;
2
+ export type ExtractEventData<T> = T extends void ? void : T;
5
3
  export type OptionalIfVoid<T> = T extends void ? [] : [data: T];
6
4
  export interface ITypedEventEmitter<TEvents = Record<string, {
7
5
  data: unknown;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twilltac/compliance-sdk",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",