@twilltac/compliance-sdk 0.1.2 → 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 CHANGED
@@ -50,6 +50,39 @@ The promise resolves when the plugin has successfully initialised. If initialisa
50
50
 
51
51
  The plugin instance returned after successful initialisation. Use this to interact with the embedded compliance plugin.
52
52
 
53
+ #### `plugin.on(event, handler)`
54
+
55
+ Subscribe to plugin events. Returns an unsubscribe function.
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 |
63
+
64
+ **Returns:** `() => void` - Call this function to unsubscribe from the event.
65
+
66
+ **Available Events:**
67
+
68
+ | Event | Data | Description |
69
+ | ----------------- | ------ | ----------------------------------------- |
70
+ | `session:expired` | `void` | Fired when the plugin session has expired |
71
+
72
+ **Example:**
73
+
74
+ ```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
+ });
81
+
82
+ // Later, to stop listening:
83
+ unsubscribe();
84
+ ```
85
+
53
86
  ## Framework Examples
54
87
 
55
88
  ### Angular + RxJS
@@ -1 +1 @@
1
- 20267473081-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("plugin:initialisation:success", () => {
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("plugin:initialisation:error", (data) => {
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("plugin:initialisation:success", () => {
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("plugin:initialisation:error", (data) => {
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 { PostMessageBus } from '@twilltac/post-messenger';
3
- interface CtorData {
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
- constructor(opts: CtorData);
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
- ['plugin:initialisation:success']: {
2
+ ['initialisation:success']: {
3
3
  payload: {
4
4
  appInstanceId: string;
5
5
  };
6
6
  };
7
- ['plugin:initialisation:error']: {
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,2 @@
1
+ export * from './lib/typed-event-emitter';
2
+ export * from './lib/typed-event-emitter.interface';
@@ -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
+ }
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twilltac/compliance-sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",