@twilltac/compliance-sdk 0.1.2 → 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 +46 -2
- package/build-revision.txt +1 -1
- package/index.cjs +55 -10
- package/index.js +55 -10
- package/libs/twilltac/plugins/compliance/sdk/src/lib/compliance-plugin.events.d.ts +26 -0
- package/libs/twilltac/plugins/compliance/sdk/src/lib/compliance.plugin.d.ts +7 -5
- package/libs/twilltac/plugins/compliance/types/src/lib/compliance-plugin-post.messages.d.ts +33 -2
- package/libs/twilltac/plugins/utils/post-messenger/src/index.d.ts +1 -1
- package/libs/twilltac/plugins/utils/post-messenger/src/lib/noop-post-message.bus.d.ts +6 -6
- package/libs/twilltac/plugins/utils/post-messenger/src/lib/post-message-bus.interface.d.ts +10 -5
- package/libs/twilltac/plugins/utils/post-messenger/src/lib/post-message.bus.d.ts +3 -3
- package/libs/twilltac/plugins/utils/typed-event-emitter/src/index.d.ts +2 -0
- package/libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.d.ts +11 -0
- package/libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.interface.d.ts +11 -0
- package/package.json +1 -1
- package/libs/twilltac/plugins/compliance/sdk/src/lib/compliance-plugin-sdk.spec.d.ts +0 -0
package/README.md
CHANGED
|
@@ -46,9 +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
|
-
###
|
|
49
|
+
### Events
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
You can subscribe to events via the plugin instance returned after successful initialisation.
|
|
52
|
+
|
|
53
|
+
#### `plugin.on(event, handler)`
|
|
54
|
+
|
|
55
|
+
#### Example:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
const plugin = await createCompliancePlugin({ ... });
|
|
59
|
+
|
|
60
|
+
plugin.on('session:expired', () => {
|
|
61
|
+
// Handle session expiry - e.g. refresh token and reinitialise
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### Available events:
|
|
66
|
+
|
|
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. |
|
|
74
|
+
|
|
75
|
+
#### Type Definitions
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
interface SectionStartedEvent {
|
|
79
|
+
readonly name: string;
|
|
80
|
+
readonly reference: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
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
|
+
}
|
|
94
|
+
```
|
|
95
|
+
<!-- EVENTS_TABLE_END -->
|
|
52
96
|
|
|
53
97
|
## Framework Examples
|
|
54
98
|
|
package/build-revision.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
21181652609-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
|
-
_iframeElement;
|
|
32
68
|
_containerElement;
|
|
33
|
-
|
|
69
|
+
_events = new TypedEventEmitter();
|
|
34
70
|
constructor(opts) {
|
|
35
|
-
this._iframeElement = opts.iframeElement;
|
|
36
71
|
this._containerElement = opts.containerElement;
|
|
37
|
-
this.
|
|
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
|
+
}
|
|
78
|
+
on(event, handler) {
|
|
79
|
+
return this._events.on(event, handler);
|
|
38
80
|
}
|
|
39
81
|
};
|
|
40
82
|
|
|
@@ -57,10 +99,11 @@ var PostMessageBus = class {
|
|
|
57
99
|
window.addEventListener("message", this._boundMessageHandler);
|
|
58
100
|
}
|
|
59
101
|
send(type, ...args) {
|
|
102
|
+
const payload = args[0];
|
|
60
103
|
const envelope = {
|
|
61
104
|
channel: this._channel,
|
|
62
105
|
type,
|
|
63
|
-
payload
|
|
106
|
+
payload
|
|
64
107
|
};
|
|
65
108
|
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
66
109
|
}
|
|
@@ -89,9 +132,9 @@ var PostMessageBus = class {
|
|
|
89
132
|
}
|
|
90
133
|
request(type, ...args) {
|
|
91
134
|
return new Promise((resolve, reject) => {
|
|
92
|
-
const
|
|
93
|
-
const payload =
|
|
94
|
-
const timeout =
|
|
135
|
+
const [firstArg, secondArg] = args;
|
|
136
|
+
const payload = typeof firstArg === "number" ? void 0 : firstArg;
|
|
137
|
+
const timeout = typeof firstArg === "number" ? firstArg : secondArg;
|
|
95
138
|
const correlationId = this.generateCorrelationId();
|
|
96
139
|
const timeoutMs = timeout ?? this._defaultTimeout;
|
|
97
140
|
const timeoutId = setTimeout(() => {
|
|
@@ -193,9 +236,11 @@ function createCompliancePlugin(options) {
|
|
|
193
236
|
const url = production ? baseUrl : `${baseUrl}.ngserve`;
|
|
194
237
|
const iframe = document.createElement("iframe");
|
|
195
238
|
iframe.name = "compliance-plugin-iframe";
|
|
239
|
+
iframe.setAttribute("scrolling", "no");
|
|
196
240
|
iframe.style.width = "100%";
|
|
197
241
|
iframe.style.height = "100%";
|
|
198
242
|
iframe.style.border = "none";
|
|
243
|
+
iframe.style.overflow = "hidden";
|
|
199
244
|
const form = document.createElement("form");
|
|
200
245
|
form.method = "POST";
|
|
201
246
|
form.action = `${url}/en-gb`;
|
|
@@ -211,7 +256,7 @@ function createCompliancePlugin(options) {
|
|
|
211
256
|
options.containerElement.appendChild(form);
|
|
212
257
|
const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
|
|
213
258
|
return new Promise((resolve, rejects) => {
|
|
214
|
-
postMessageBus.on("
|
|
259
|
+
postMessageBus.on("initialisation:success", () => {
|
|
215
260
|
resolve(
|
|
216
261
|
new CompliancePlugin({
|
|
217
262
|
postMessageBus,
|
|
@@ -220,7 +265,7 @@ function createCompliancePlugin(options) {
|
|
|
220
265
|
})
|
|
221
266
|
);
|
|
222
267
|
});
|
|
223
|
-
postMessageBus.on("
|
|
268
|
+
postMessageBus.on("initialisation:error", (data) => {
|
|
224
269
|
rejects(data);
|
|
225
270
|
});
|
|
226
271
|
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
|
-
_iframeElement;
|
|
6
42
|
_containerElement;
|
|
7
|
-
|
|
43
|
+
_events = new TypedEventEmitter();
|
|
8
44
|
constructor(opts) {
|
|
9
|
-
this._iframeElement = opts.iframeElement;
|
|
10
45
|
this._containerElement = opts.containerElement;
|
|
11
|
-
this.
|
|
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
|
+
}
|
|
52
|
+
on(event, handler) {
|
|
53
|
+
return this._events.on(event, handler);
|
|
12
54
|
}
|
|
13
55
|
};
|
|
14
56
|
|
|
@@ -31,10 +73,11 @@ var PostMessageBus = class {
|
|
|
31
73
|
window.addEventListener("message", this._boundMessageHandler);
|
|
32
74
|
}
|
|
33
75
|
send(type, ...args) {
|
|
76
|
+
const payload = args[0];
|
|
34
77
|
const envelope = {
|
|
35
78
|
channel: this._channel,
|
|
36
79
|
type,
|
|
37
|
-
payload
|
|
80
|
+
payload
|
|
38
81
|
};
|
|
39
82
|
this._targetWindow.postMessage(envelope, this._targetOrigin);
|
|
40
83
|
}
|
|
@@ -63,9 +106,9 @@ var PostMessageBus = class {
|
|
|
63
106
|
}
|
|
64
107
|
request(type, ...args) {
|
|
65
108
|
return new Promise((resolve, reject) => {
|
|
66
|
-
const
|
|
67
|
-
const payload =
|
|
68
|
-
const timeout =
|
|
109
|
+
const [firstArg, secondArg] = args;
|
|
110
|
+
const payload = typeof firstArg === "number" ? void 0 : firstArg;
|
|
111
|
+
const timeout = typeof firstArg === "number" ? firstArg : secondArg;
|
|
69
112
|
const correlationId = this.generateCorrelationId();
|
|
70
113
|
const timeoutMs = timeout ?? this._defaultTimeout;
|
|
71
114
|
const timeoutId = setTimeout(() => {
|
|
@@ -167,9 +210,11 @@ function createCompliancePlugin(options) {
|
|
|
167
210
|
const url = production ? baseUrl : `${baseUrl}.ngserve`;
|
|
168
211
|
const iframe = document.createElement("iframe");
|
|
169
212
|
iframe.name = "compliance-plugin-iframe";
|
|
213
|
+
iframe.setAttribute("scrolling", "no");
|
|
170
214
|
iframe.style.width = "100%";
|
|
171
215
|
iframe.style.height = "100%";
|
|
172
216
|
iframe.style.border = "none";
|
|
217
|
+
iframe.style.overflow = "hidden";
|
|
173
218
|
const form = document.createElement("form");
|
|
174
219
|
form.method = "POST";
|
|
175
220
|
form.action = `${url}/en-gb`;
|
|
@@ -185,7 +230,7 @@ function createCompliancePlugin(options) {
|
|
|
185
230
|
options.containerElement.appendChild(form);
|
|
186
231
|
const postMessageBus = new PostMessageBus({ channel: "compliance-plugin", targetWindow: iframe.contentWindow });
|
|
187
232
|
return new Promise((resolve, rejects) => {
|
|
188
|
-
postMessageBus.on("
|
|
233
|
+
postMessageBus.on("initialisation:success", () => {
|
|
189
234
|
resolve(
|
|
190
235
|
new CompliancePlugin({
|
|
191
236
|
postMessageBus,
|
|
@@ -194,7 +239,7 @@ function createCompliancePlugin(options) {
|
|
|
194
239
|
})
|
|
195
240
|
);
|
|
196
241
|
});
|
|
197
|
-
postMessageBus.on("
|
|
242
|
+
postMessageBus.on("initialisation:error", (data) => {
|
|
198
243
|
rejects(data);
|
|
199
244
|
});
|
|
200
245
|
form.submit();
|
|
@@ -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,14 +1,16 @@
|
|
|
1
1
|
import { CompliancePluginPostMessages } from '@twilltac/compliance-plugin-types';
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { EventHandler, ExtractEventData } from '@twilltac/plugin-skd-event-emitter';
|
|
3
|
+
import { PostMessageBus } from '@twilltac/plugin-skd-post-messenger';
|
|
4
|
+
import { CompliancePluginEvents } from './compliance-plugin.events';
|
|
5
|
+
interface CompliancePluginData {
|
|
4
6
|
postMessageBus: PostMessageBus<CompliancePluginPostMessages>;
|
|
5
7
|
containerElement: HTMLElement;
|
|
6
8
|
iframeElement: HTMLIFrameElement;
|
|
7
9
|
}
|
|
8
10
|
export declare class CompliancePlugin {
|
|
9
|
-
private readonly _iframeElement;
|
|
10
11
|
private readonly _containerElement;
|
|
11
|
-
private readonly
|
|
12
|
-
constructor(opts:
|
|
12
|
+
private readonly _events;
|
|
13
|
+
constructor(opts: CompliancePluginData);
|
|
14
|
+
on<K extends keyof CompliancePluginEvents & string>(event: K, handler: EventHandler<ExtractEventData<CompliancePluginEvents[K]>>): () => void;
|
|
13
15
|
}
|
|
14
16
|
export {};
|
|
@@ -1,10 +1,31 @@
|
|
|
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 {
|
|
2
|
-
['
|
|
18
|
+
['window:resize']: {
|
|
19
|
+
payload: {
|
|
20
|
+
height: number;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
['initialisation:success']: {
|
|
3
24
|
payload: {
|
|
4
25
|
appInstanceId: string;
|
|
5
26
|
};
|
|
6
27
|
};
|
|
7
|
-
['
|
|
28
|
+
['initialisation:error']: {
|
|
8
29
|
payload: {
|
|
9
30
|
error: Error;
|
|
10
31
|
};
|
|
@@ -12,4 +33,14 @@ export interface CompliancePluginPostMessages {
|
|
|
12
33
|
error: Error;
|
|
13
34
|
};
|
|
14
35
|
};
|
|
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
|
+
};
|
|
15
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 :
|
|
5
|
+
} ? P : never;
|
|
6
6
|
export type ExtractResponse<T> = T extends {
|
|
7
7
|
response: infer R;
|
|
8
|
-
} ? R :
|
|
9
|
-
export type
|
|
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:
|
|
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: [
|
|
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,
|
|
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:
|
|
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: [
|
|
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;
|
|
@@ -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
|
+
}
|
package/libs/twilltac/plugins/utils/typed-event-emitter/src/lib/typed-event-emitter.interface.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type EventHandler<T = unknown> = (data: T) => void;
|
|
2
|
+
export type ExtractEventData<T> = T extends void ? void : T;
|
|
3
|
+
export type OptionalIfVoid<T> = T extends void ? [] : [data: T];
|
|
4
|
+
export interface ITypedEventEmitter<TEvents = Record<string, {
|
|
5
|
+
data: unknown;
|
|
6
|
+
}>> {
|
|
7
|
+
on<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): () => void;
|
|
8
|
+
off<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): void;
|
|
9
|
+
once<K extends keyof TEvents & string>(event: K, handler: EventHandler<ExtractEventData<TEvents[K]>>): () => void;
|
|
10
|
+
emit<K extends keyof TEvents & string>(event: K, ...args: OptionalIfVoid<ExtractEventData<TEvents[K]>>): void;
|
|
11
|
+
}
|
package/package.json
CHANGED
|
File without changes
|