@twilltac/compliance-sdk 0.1.3 → 0.1.5

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
@@ -19,6 +19,7 @@ const plugin = await createCompliancePlugin({
19
19
  containerElement: container,
20
20
  data: {
21
21
  workspace: 'YOUR_WORKSPACE_KEY',
22
+ view: 'journey',
22
23
  customerReference: 'YOUR_CUSTOMER_REFERENCE',
23
24
  authToken: 'YOUR_AUTH_TOKEN'
24
25
  }
@@ -39,6 +40,7 @@ Creates and initialises the compliance plugin within a container element.
39
40
  | -------------------------------- | ------------- | ------------------------------------------------- |
40
41
  | `options.containerElement` | `HTMLElement` | The DOM element where the plugin will be rendered |
41
42
  | `options.data.workspace` | `string` | Your Twilltac workspace identifier |
43
+ | `options.data.view` | `string` | The plugin view to load. Either `journey` or `handover` |
42
44
  | `options.data.customerReference` | `string` | Reference identifier for the customer |
43
45
  | `options.data.authToken` | `string` | Authentication token for the plugin session |
44
46
 
@@ -46,42 +48,53 @@ Creates and initialises the compliance plugin within a container element.
46
48
 
47
49
  The promise resolves when the plugin has successfully initialised. If initialisation fails, the promise rejects with error details.
48
50
 
49
- ### CompliancePlugin
51
+ ### Events
50
52
 
51
- The plugin instance returned after successful initialisation. Use this to interact with the embedded compliance plugin.
53
+ You can subscribe to events via the plugin instance returned after successful initialisation.
52
54
 
53
55
  #### `plugin.on(event, handler)`
54
56
 
55
- Subscribe to plugin events. Returns an unsubscribe function.
57
+ #### Example:
56
58
 
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 |
59
+ ```typescript
60
+ const plugin = await createCompliancePlugin({ ... });
63
61
 
64
- **Returns:** `() => void` - Call this function to unsubscribe from the event.
62
+ plugin.on('session:expired', () => {
63
+ // Handle session expiry - e.g. refresh token and reinitialise
64
+ });
65
+ ```
65
66
 
66
- **Available Events:**
67
+ #### Available events:
67
68
 
68
- | Event | Data | Description |
69
- | ----------------- | ------ | ----------------------------------------- |
70
- | `session:expired` | `void` | Fired when the plugin session has expired |
69
+ <!-- EVENTS_TABLE_START -->
70
+ | Event | Data | Description |
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. |
75
+ | `session:expired` | `void` | Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. |
71
76
 
72
- **Example:**
77
+ #### Type Definitions
73
78
 
74
79
  ```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
- });
80
+ interface SectionStartedEvent {
81
+ readonly name: string;
82
+ readonly reference: string;
83
+ }
81
84
 
82
- // Later, to stop listening:
83
- unsubscribe();
85
+ interface SectionEndedEvent {
86
+ readonly name: string;
87
+ readonly reference: string;
88
+ readonly recommendations: {
89
+ readonly externalReference: string;
90
+ readonly price: number | null;
91
+ readonly regulated: boolean;
92
+ readonly title: string;
93
+ readonly description: string;
94
+ }[];
95
+ }
84
96
  ```
97
+ <!-- EVENTS_TABLE_END -->
85
98
 
86
99
  ## Framework Examples
87
100
 
@@ -99,6 +112,7 @@ import { from, switchMap } from 'rxjs';
99
112
  })
100
113
  export class ComplianceComponent {
101
114
  readonly workspace = input.required<string>();
115
+ readonly view = input.required<string>();
102
116
  readonly customerReference = input.required<string>();
103
117
  readonly authToken = input.required<string>();
104
118
 
@@ -115,6 +129,7 @@ export class ComplianceComponent {
115
129
  containerElement: container.nativeElement,
116
130
  data: {
117
131
  workspace: this.workspace(),
132
+ view: this.view(),
118
133
  customerReference: this.customerReference(),
119
134
  authToken: this.authToken()
120
135
  }
@@ -147,6 +162,7 @@ import { createCompliancePlugin, CompliancePlugin } from '@twilltac/compliance-s
147
162
  })
148
163
  export class ComplianceComponent {
149
164
  readonly workspace = input.required<string>();
165
+ readonly view = input.required<string>();
150
166
  readonly customerReference = input.required<string>();
151
167
  readonly authToken = input.required<string>();
152
168
 
@@ -185,11 +201,12 @@ import { createCompliancePlugin, CompliancePlugin } from '@twilltac/compliance-s
185
201
 
186
202
  interface ComplianceWidgetProps {
187
203
  workspace: string;
204
+ view: 'journey' | 'handover';
188
205
  customerReference: string;
189
206
  authToken: string;
190
207
  }
191
208
 
192
- function ComplianceWidget({ workspace, customerReference, authToken }: ComplianceWidgetProps) {
209
+ function ComplianceWidget({ workspace, view, customerReference, authToken }: ComplianceWidgetProps) {
193
210
  const containerRef = useRef<HTMLDivElement>(null);
194
211
  const pluginRef = useRef<CompliancePlugin | null>(null);
195
212
 
@@ -198,13 +215,13 @@ function ComplianceWidget({ workspace, customerReference, authToken }: Complianc
198
215
 
199
216
  createCompliancePlugin({
200
217
  containerElement: containerRef.current,
201
- data: { workspace, customerReference, authToken }
218
+ data: { workspace, view, customerReference, authToken }
202
219
  })
203
220
  .then((plugin) => {
204
221
  pluginRef.current = plugin;
205
222
  })
206
223
  .catch(console.error);
207
- }, [workspace, customerReference, authToken]);
224
+ }, [workspace, view, customerReference, authToken]);
208
225
 
209
226
  return <div ref={containerRef} style={{ width: '100%', height: '600px' }} />;
210
227
  }
@@ -1 +1 @@
1
- 20269584388-1
1
+ 22776693444-1
package/index.cjs CHANGED
@@ -65,21 +65,28 @@ 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);
80
80
  }
81
81
  };
82
82
 
83
+ // libs/twilltac/plugins/compliance/types/src/lib/compliance-plugin-initialisation.data.ts
84
+ var CompliancePluginView = /* @__PURE__ */ ((CompliancePluginView2) => {
85
+ CompliancePluginView2["JOURNEY"] = "journey";
86
+ CompliancePluginView2["HANDOVER"] = "handover";
87
+ return CompliancePluginView2;
88
+ })(CompliancePluginView || {});
89
+
83
90
  // libs/twilltac/plugins/utils/post-messenger/src/lib/post-message.bus.ts
84
91
  var PostMessageBus = class {
85
92
  _channel;
@@ -99,10 +106,11 @@ var PostMessageBus = class {
99
106
  window.addEventListener("message", this._boundMessageHandler);
100
107
  }
101
108
  send(type, ...args) {
109
+ const payload = args[0];
102
110
  const envelope = {
103
111
  channel: this._channel,
104
112
  type,
105
- payload: args[0]
113
+ payload
106
114
  };
107
115
  this._targetWindow.postMessage(envelope, this._targetOrigin);
108
116
  }
@@ -131,9 +139,9 @@ var PostMessageBus = class {
131
139
  }
132
140
  request(type, ...args) {
133
141
  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];
142
+ const [firstArg, secondArg] = args;
143
+ const payload = typeof firstArg === "number" ? void 0 : firstArg;
144
+ const timeout = typeof firstArg === "number" ? firstArg : secondArg;
137
145
  const correlationId = this.generateCorrelationId();
138
146
  const timeoutMs = timeout ?? this._defaultTimeout;
139
147
  const timeoutId = setTimeout(() => {
@@ -229,15 +237,42 @@ var PostMessageBus = class {
229
237
  };
230
238
 
231
239
  // libs/twilltac/plugins/compliance/sdk/src/lib/create-compliance.plugin.ts
240
+ function validateOptions(options) {
241
+ if (!options.containerElement) {
242
+ throw new Error("[CompliancePlugin] containerElement is required.");
243
+ }
244
+ if (!(options.containerElement instanceof HTMLElement)) {
245
+ throw new Error("[CompliancePlugin] containerElement must be an HTMLElement.");
246
+ }
247
+ if (!options.data) {
248
+ throw new Error("[CompliancePlugin] data is required.");
249
+ }
250
+ if (!options.data.workspace) {
251
+ throw new Error("[CompliancePlugin] data.workspace is required.");
252
+ }
253
+ const validViews = Object.values(CompliancePluginView);
254
+ if (!options.data.view || !validViews.includes(options.data.view)) {
255
+ throw new Error(`[CompliancePlugin] data.view is required and must be one of: ${validViews.join(", ")}.`);
256
+ }
257
+ if (!options.data.customerReference) {
258
+ throw new Error("[CompliancePlugin] data.customerReference is required.");
259
+ }
260
+ if (!options.data.authToken) {
261
+ throw new Error("[CompliancePlugin] data.authToken is required.");
262
+ }
263
+ }
232
264
  function createCompliancePlugin(options) {
265
+ validateOptions(options);
233
266
  const production = !window.location.hostname.endsWith(".ngserve");
234
267
  const baseUrl = "https://compliance.plugins.twilltac.app";
235
268
  const url = production ? baseUrl : `${baseUrl}.ngserve`;
236
269
  const iframe = document.createElement("iframe");
237
270
  iframe.name = "compliance-plugin-iframe";
271
+ iframe.setAttribute("scrolling", "no");
238
272
  iframe.style.width = "100%";
239
273
  iframe.style.height = "100%";
240
274
  iframe.style.border = "none";
275
+ iframe.style.overflow = "hidden";
241
276
  const form = document.createElement("form");
242
277
  form.method = "POST";
243
278
  form.action = `${url}/en-gb`;
package/index.js CHANGED
@@ -39,21 +39,28 @@ 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);
54
54
  }
55
55
  };
56
56
 
57
+ // libs/twilltac/plugins/compliance/types/src/lib/compliance-plugin-initialisation.data.ts
58
+ var CompliancePluginView = /* @__PURE__ */ ((CompliancePluginView2) => {
59
+ CompliancePluginView2["JOURNEY"] = "journey";
60
+ CompliancePluginView2["HANDOVER"] = "handover";
61
+ return CompliancePluginView2;
62
+ })(CompliancePluginView || {});
63
+
57
64
  // libs/twilltac/plugins/utils/post-messenger/src/lib/post-message.bus.ts
58
65
  var PostMessageBus = class {
59
66
  _channel;
@@ -73,10 +80,11 @@ var PostMessageBus = class {
73
80
  window.addEventListener("message", this._boundMessageHandler);
74
81
  }
75
82
  send(type, ...args) {
83
+ const payload = args[0];
76
84
  const envelope = {
77
85
  channel: this._channel,
78
86
  type,
79
- payload: args[0]
87
+ payload
80
88
  };
81
89
  this._targetWindow.postMessage(envelope, this._targetOrigin);
82
90
  }
@@ -105,9 +113,9 @@ var PostMessageBus = class {
105
113
  }
106
114
  request(type, ...args) {
107
115
  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];
116
+ const [firstArg, secondArg] = args;
117
+ const payload = typeof firstArg === "number" ? void 0 : firstArg;
118
+ const timeout = typeof firstArg === "number" ? firstArg : secondArg;
111
119
  const correlationId = this.generateCorrelationId();
112
120
  const timeoutMs = timeout ?? this._defaultTimeout;
113
121
  const timeoutId = setTimeout(() => {
@@ -203,15 +211,42 @@ var PostMessageBus = class {
203
211
  };
204
212
 
205
213
  // libs/twilltac/plugins/compliance/sdk/src/lib/create-compliance.plugin.ts
214
+ function validateOptions(options) {
215
+ if (!options.containerElement) {
216
+ throw new Error("[CompliancePlugin] containerElement is required.");
217
+ }
218
+ if (!(options.containerElement instanceof HTMLElement)) {
219
+ throw new Error("[CompliancePlugin] containerElement must be an HTMLElement.");
220
+ }
221
+ if (!options.data) {
222
+ throw new Error("[CompliancePlugin] data is required.");
223
+ }
224
+ if (!options.data.workspace) {
225
+ throw new Error("[CompliancePlugin] data.workspace is required.");
226
+ }
227
+ const validViews = Object.values(CompliancePluginView);
228
+ if (!options.data.view || !validViews.includes(options.data.view)) {
229
+ throw new Error(`[CompliancePlugin] data.view is required and must be one of: ${validViews.join(", ")}.`);
230
+ }
231
+ if (!options.data.customerReference) {
232
+ throw new Error("[CompliancePlugin] data.customerReference is required.");
233
+ }
234
+ if (!options.data.authToken) {
235
+ throw new Error("[CompliancePlugin] data.authToken is required.");
236
+ }
237
+ }
206
238
  function createCompliancePlugin(options) {
239
+ validateOptions(options);
207
240
  const production = !window.location.hostname.endsWith(".ngserve");
208
241
  const baseUrl = "https://compliance.plugins.twilltac.app";
209
242
  const url = production ? baseUrl : `${baseUrl}.ngserve`;
210
243
  const iframe = document.createElement("iframe");
211
244
  iframe.name = "compliance-plugin-iframe";
245
+ iframe.setAttribute("scrolling", "no");
212
246
  iframe.style.width = "100%";
213
247
  iframe.style.height = "100%";
214
248
  iframe.style.border = "none";
249
+ iframe.style.overflow = "hidden";
215
250
  const form = document.createElement("form");
216
251
  form.method = "POST";
217
252
  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 user starts the journey and IDD document is generated. */
18
+ ['started']: void;
19
+ /** Emits when the user starts a section. */
20
+ ['section:started']: SectionStartedEvent;
21
+ /** Emits when the user starts a completes the question set and selects their recommendations. */
22
+ ['section:completed']: SectionEndedEvent;
23
+ /** Emits when the session has expired. This would typically trigger your auth flow followed by re-initialising the plugin. */
24
+ ['session:expired']: void;
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,5 +1,10 @@
1
+ export declare enum CompliancePluginView {
2
+ JOURNEY = "journey",
3
+ HANDOVER = "handover"
4
+ }
1
5
  export interface CompliancePluginInitialisationData {
2
6
  readonly workspace: string;
7
+ readonly view: CompliancePluginView;
3
8
  readonly customerReference: string;
4
9
  readonly authToken: string;
5
10
  }
@@ -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.5",
4
4
  "type": "module",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",