@notabene/javascript-sdk 2.0.0-next.10 → 2.0.0-next.11
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 +256 -80
- package/dist/notabene.cjs +1 -1
- package/dist/notabene.d.ts +525 -169
- package/dist/notabene.js +136 -13
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +24 -20
- package/src/components/EmbeddedComponent.ts +80 -48
- package/src/components/__tests__/EmbeddedComponent.test.ts +40 -12
- package/src/ivms/types.ts +177 -140
- package/src/notabene.ts +126 -14
- package/src/types.ts +281 -117
- package/src/utils/__tests__/MessageEventManager.test.ts +1 -1
package/dist/notabene.js
CHANGED
|
@@ -32,14 +32,17 @@ class m {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
class u {
|
|
35
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Creates an instance of EmbeddedComponent.
|
|
37
|
+
* @param url - The URL of the embedded component
|
|
38
|
+
* @param value - The initial transaction value
|
|
39
|
+
*/
|
|
36
40
|
constructor(e, t) {
|
|
37
41
|
n(this, "_url");
|
|
38
42
|
n(this, "_value");
|
|
39
43
|
n(this, "_errors", []);
|
|
40
44
|
n(this, "iframe");
|
|
41
45
|
n(this, "eventManager");
|
|
42
|
-
n(this, "ready", !1);
|
|
43
46
|
n(this, "modal");
|
|
44
47
|
this._url = e, this._value = t, this.eventManager = new m(), this.on(a.INVALID, (s) => {
|
|
45
48
|
s.type === a.INVALID && (this._errors = s.errors, this._value = s.value);
|
|
@@ -47,57 +50,98 @@ class u {
|
|
|
47
50
|
s.type === a.RESIZE && this.iframe && (this.iframe.style.height = `${s.height}px`);
|
|
48
51
|
});
|
|
49
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets the URL of the embedded component
|
|
55
|
+
* @returns The URL of the embedded component
|
|
56
|
+
*/
|
|
50
57
|
get url() {
|
|
51
58
|
return this._url;
|
|
52
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Gets the current transaction value
|
|
62
|
+
* @returns The current transaction value
|
|
63
|
+
*/
|
|
53
64
|
get value() {
|
|
54
65
|
return this._value;
|
|
55
66
|
}
|
|
56
67
|
get errors() {
|
|
57
68
|
return this._errors;
|
|
58
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Opens the component URL in the current window
|
|
72
|
+
*/
|
|
59
73
|
open() {
|
|
60
74
|
document.location.href = this.url;
|
|
61
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Mounts the component to a parent element
|
|
78
|
+
* @param parentId - The ID of the parent element
|
|
79
|
+
* @throws Will throw an error if the parent element is not found
|
|
80
|
+
*/
|
|
62
81
|
mount(e) {
|
|
63
82
|
const t = document.querySelector(e);
|
|
64
83
|
if (!t) throw new Error(`parentID ${e} not found`);
|
|
65
84
|
this.embed(t);
|
|
66
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Embeds the component into a parent element
|
|
88
|
+
* @param parent - The parent element to embed the component into
|
|
89
|
+
*/
|
|
67
90
|
embed(e) {
|
|
68
91
|
var t, s;
|
|
69
92
|
this.iframe = document.createElement("iframe"), this.iframe.src = this.url, this.iframe.style.width = "100%", this.iframe.style.height = "100px", this.iframe.style.border = "none", this.iframe.style.overflow = "hidden", this.iframe.setAttribute("allowtransparency", "true"), this.iframe.style.backgroundColor = "transparent", e.appendChild(this.iframe), window.addEventListener("message", (o) => {
|
|
70
93
|
var i, h;
|
|
71
|
-
o.source === ((i = this.iframe) == null ? void 0 : i.contentWindow) && (console.log("received message from iframe", o.data), (h = this.eventManager) == null || h.setPort(o.ports[0])
|
|
94
|
+
o.source === ((i = this.iframe) == null ? void 0 : i.contentWindow) && (console.log("received message from iframe", o.data), (h = this.eventManager) == null || h.setPort(o.ports[0]));
|
|
72
95
|
}), (s = (t = this.iframe) == null ? void 0 : t.contentWindow) == null || s.focus();
|
|
73
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* Sends a message to the embedded component
|
|
99
|
+
* @param message - The message to send
|
|
100
|
+
*/
|
|
74
101
|
send(e) {
|
|
75
102
|
this.eventManager.send(e);
|
|
76
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Adds an event listener for a specific message type
|
|
106
|
+
* @param messageType - The type of message to listen for
|
|
107
|
+
* @param callback - The callback function to execute when the message is received
|
|
108
|
+
*/
|
|
77
109
|
on(e, t) {
|
|
78
110
|
this.eventManager.on(e, t);
|
|
79
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Removes an event listener for a specific message type
|
|
114
|
+
* @param messageType - The type of message to stop listening for
|
|
115
|
+
* @param callback - The callback function to remove
|
|
116
|
+
*/
|
|
80
117
|
off(e, t) {
|
|
81
118
|
this.eventManager.off(e, t);
|
|
82
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Updates the transaction value and sends an update message to the component
|
|
122
|
+
* @param value - The new transaction value
|
|
123
|
+
*/
|
|
83
124
|
update(e) {
|
|
84
125
|
this._value = e, this.send({ type: l.UPDATE, value: e });
|
|
85
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Waits for the component to complete and returns the transaction response
|
|
129
|
+
* @returns A promise that resolves with the transaction response
|
|
130
|
+
*/
|
|
86
131
|
completion() {
|
|
87
132
|
return new Promise((e, t) => {
|
|
88
|
-
this.
|
|
133
|
+
this.on(a.COMPLETE, (s) => {
|
|
89
134
|
e(s.response);
|
|
90
135
|
}), this.on("error", (s) => {
|
|
91
136
|
t(new Error(s.message));
|
|
92
137
|
});
|
|
93
138
|
});
|
|
94
139
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// }
|
|
140
|
+
/**
|
|
141
|
+
* Opens the component in a modal dialog
|
|
142
|
+
*/
|
|
99
143
|
openModal() {
|
|
100
|
-
this.modal && this.closeModal(), this.modal = document.createElement("dialog"), document.body.appendChild(this.modal), this.embed(this.modal), this.on(a.CANCEL, () => {
|
|
144
|
+
return this.modal && this.closeModal(), this.modal = document.createElement("dialog"), this.modal.style.border = "none", this.modal.style.backgroundColor = "transparent", this.modal.style.maxWidth = "100vw", this.modal.style.maxHeight = "100vh", this.modal.style.width = "600px", this.modal.style.height = "800px", document.body.appendChild(this.modal), this.embed(this.modal), this.on(a.CANCEL, () => {
|
|
101
145
|
this.closeModal();
|
|
102
146
|
}), this.on(a.CLOSE, () => {
|
|
103
147
|
this.closeModal();
|
|
@@ -105,8 +149,12 @@ class u {
|
|
|
105
149
|
this.closeModal();
|
|
106
150
|
}), this.modal.showModal(), this.modal.addEventListener("click", () => {
|
|
107
151
|
this.closeModal();
|
|
108
|
-
});
|
|
152
|
+
}), this.completion();
|
|
109
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Closes the modal dialog
|
|
156
|
+
*
|
|
157
|
+
*/
|
|
110
158
|
closeModal() {
|
|
111
159
|
var e;
|
|
112
160
|
this.modal && (console.log("closeModal"), (e = this.modal) == null || e.close(), this.modal.remove(), this.modal = void 0);
|
|
@@ -122,6 +170,11 @@ function f(r) {
|
|
|
122
170
|
}).filter((e) => !!e).join("&");
|
|
123
171
|
}
|
|
124
172
|
class E {
|
|
173
|
+
/**
|
|
174
|
+
* Creates a new instance of the Notabene SDK
|
|
175
|
+
*
|
|
176
|
+
* @param config - Configuration options for the Notabene SDK
|
|
177
|
+
*/
|
|
125
178
|
constructor(e) {
|
|
126
179
|
n(this, "nodeUrl");
|
|
127
180
|
n(this, "authToken");
|
|
@@ -130,6 +183,16 @@ class E {
|
|
|
130
183
|
n(this, "locale");
|
|
131
184
|
this.uxUrl = e.uxUrl || "https://connect.notabene.id", this.nodeUrl = e.nodeUrl, this.authToken = e.authToken, this.theme = e.theme, this.locale = e.locale;
|
|
132
185
|
}
|
|
186
|
+
/**
|
|
187
|
+
* Generates a URL for a Notabene component
|
|
188
|
+
*
|
|
189
|
+
* @param path - The path of the component
|
|
190
|
+
* @param value - Transaction data
|
|
191
|
+
* @param configuration - Optional transaction configuration
|
|
192
|
+
* @param callbacks - Optional callback configuration
|
|
193
|
+
* @returns component URL
|
|
194
|
+
* @internal
|
|
195
|
+
*/
|
|
133
196
|
componentUrl(e, t, s, o) {
|
|
134
197
|
const i = new URL(this.uxUrl);
|
|
135
198
|
i.pathname = e;
|
|
@@ -140,24 +203,84 @@ class E {
|
|
|
140
203
|
});
|
|
141
204
|
return i.hash = h, this.nodeUrl && i.searchParams.set("nodeUrl", this.nodeUrl), this.theme && i.searchParams.set("theme", JSON.stringify(this.theme)), this.locale && i.searchParams.set("locale", this.locale), o && (o.callback && i.searchParams.set("callback_url", o.callback), o.redirectUri && i.searchParams.set("redirect_uri", o.redirectUri)), i.toString();
|
|
142
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Creates a new embedded component
|
|
208
|
+
*
|
|
209
|
+
* @param path - The path of the component
|
|
210
|
+
* @param value - Transaction data
|
|
211
|
+
* @param options - Optional transaction options
|
|
212
|
+
* @param callbacks - Optional callback configuration
|
|
213
|
+
* @returns A new EmbeddedComponent instance
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
143
216
|
createComponent(e, t, s, o) {
|
|
144
217
|
return new u(
|
|
145
218
|
this.componentUrl(e, t, s, o),
|
|
146
219
|
t
|
|
147
220
|
);
|
|
148
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Creates a withdrawal assist component
|
|
224
|
+
*
|
|
225
|
+
* @param value - Withdrawal transaction data
|
|
226
|
+
* @param options - Optional transaction options
|
|
227
|
+
* @param callbacks - Optional callback configuration
|
|
228
|
+
* @returns A new EmbeddedComponent instance for withdrawal assistance
|
|
229
|
+
*/
|
|
149
230
|
createWithdrawalAssist(e, t, s) {
|
|
150
231
|
return this.createComponent("withdrawal-assist", e, t, s);
|
|
151
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Creates a deposit assist component
|
|
235
|
+
*
|
|
236
|
+
* @param value - Deposit transaction data
|
|
237
|
+
* @param options - Optional transaction options
|
|
238
|
+
* @param callbacks - Optional callback configuration
|
|
239
|
+
* @returns A new EmbeddedComponent instance for deposit assistance
|
|
240
|
+
* @alpha
|
|
241
|
+
*/
|
|
152
242
|
createDepositAssist(e, t, s) {
|
|
153
|
-
return this.createComponent(
|
|
243
|
+
return this.createComponent(
|
|
244
|
+
"deposit-assist",
|
|
245
|
+
e,
|
|
246
|
+
t,
|
|
247
|
+
s
|
|
248
|
+
);
|
|
154
249
|
}
|
|
155
|
-
|
|
156
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Creates a connect component
|
|
252
|
+
*
|
|
253
|
+
* @param value - Connection request data
|
|
254
|
+
* @param options - Optional transaction options
|
|
255
|
+
* @param callbacks - Optional callback configuration
|
|
256
|
+
* @returns A new EmbeddedComponent instance for connection
|
|
257
|
+
*/
|
|
258
|
+
createConnectWallet(e, t, s) {
|
|
259
|
+
return this.createComponent(
|
|
260
|
+
"connect",
|
|
261
|
+
e,
|
|
262
|
+
t,
|
|
263
|
+
s
|
|
264
|
+
);
|
|
157
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Creates a deposit request component
|
|
268
|
+
*
|
|
269
|
+
* @param value - Deposit request data
|
|
270
|
+
* @param options - Optional transaction options
|
|
271
|
+
* @param callbacks - Optional callback configuration
|
|
272
|
+
* @returns A new EmbeddedComponent instance for deposit requests
|
|
273
|
+
* @alpha
|
|
274
|
+
*/
|
|
158
275
|
createDepositRequest(e, t, s) {
|
|
159
276
|
return this.createComponent("deposit-request", e, t, s);
|
|
160
277
|
}
|
|
278
|
+
/**
|
|
279
|
+
* Decodes a URL fragment into an object
|
|
280
|
+
*
|
|
281
|
+
* @param fragment - The URL fragment to decode
|
|
282
|
+
* @returns An object containing the decoded key-value pairs
|
|
283
|
+
*/
|
|
161
284
|
decodeFragmentToObject(e) {
|
|
162
285
|
return e.slice(1).split("&").reduce(
|
|
163
286
|
(s, o) => {
|
package/dist/tsdoc-metadata.json
CHANGED
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"author": "Notabene <developers@notabene.id>",
|
|
11
11
|
"license": "MIT",
|
|
12
|
-
"version": "2.0.0-next.
|
|
12
|
+
"version": "2.0.0-next.11",
|
|
13
13
|
"source": "src/notabene.ts",
|
|
14
14
|
"main": "dist/notabene.js",
|
|
15
15
|
"types": "dist/notabene.d.ts",
|
|
@@ -21,44 +21,48 @@
|
|
|
21
21
|
"README.md"
|
|
22
22
|
],
|
|
23
23
|
"scripts": {
|
|
24
|
+
"docs": "typedoc --options typedoc.json",
|
|
24
25
|
"prepare": "husky install",
|
|
25
26
|
"dev": "vite",
|
|
26
27
|
"preview": "vite preview",
|
|
27
28
|
"watch": "parcel watch",
|
|
28
29
|
"build:dev": "yarn build && yarn dev",
|
|
29
30
|
"build:clean": "rimraf dist/ ts-out",
|
|
30
|
-
"build": "vite build && tsc && api-extractor run",
|
|
31
|
+
"build": "yarn lint && vite build && tsc && api-extractor run && yarn docs ",
|
|
31
32
|
"lint": "eslint .",
|
|
32
33
|
"test": "vitest",
|
|
33
34
|
"release": "semantic-release",
|
|
34
35
|
"publish": "yarn build && npm publish --tag next"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@commitlint/cli": "^19.
|
|
38
|
-
"@commitlint/config-conventional": "^19.
|
|
39
|
-
"@eslint/js": "^9.
|
|
40
|
-
"@fast-check/vitest": "^0.1.
|
|
41
|
-
"@microsoft/api-extractor": "^7.47.
|
|
38
|
+
"@commitlint/cli": "^19.5.0",
|
|
39
|
+
"@commitlint/config-conventional": "^19.5.0",
|
|
40
|
+
"@eslint/js": "^9.10.0",
|
|
41
|
+
"@fast-check/vitest": "^0.1.3",
|
|
42
|
+
"@microsoft/api-extractor": "^7.47.9",
|
|
42
43
|
"@semantic-release/git": "^10.0.1",
|
|
43
|
-
"@semantic-release/gitlab": "^9.
|
|
44
|
-
"@semantic-release/npm": "^9.0.
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
46
|
-
"@typescript-eslint/parser": "^8.
|
|
47
|
-
"eslint": "^9.
|
|
44
|
+
"@semantic-release/gitlab": "^9.5.1",
|
|
45
|
+
"@semantic-release/npm": "^9.0.2",
|
|
46
|
+
"@typescript-eslint/eslint-plugin": "^8.6.0",
|
|
47
|
+
"@typescript-eslint/parser": "^8.6.0",
|
|
48
|
+
"eslint": "^9.10.0",
|
|
48
49
|
"eslint-config-prettier": "^9.1.0",
|
|
49
50
|
"eslint-plugin-prettier": "^5.2.1",
|
|
50
|
-
"fast-check": "^3.
|
|
51
|
-
"globals": "^15.
|
|
51
|
+
"fast-check": "^3.22.0",
|
|
52
|
+
"globals": "^15.9.0",
|
|
52
53
|
"husky": "^7.0.4",
|
|
53
|
-
"
|
|
54
|
+
"jsdom": "^25.0.0",
|
|
55
|
+
"lint-staged": "^12.5.0",
|
|
54
56
|
"organize-imports-cli": "^0.9.0",
|
|
55
57
|
"prettier": "^3.3.3",
|
|
56
58
|
"rimraf": "^6.0.1",
|
|
57
|
-
"semantic-release": "^24.1.
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
59
|
+
"semantic-release": "^24.1.1",
|
|
60
|
+
"typedoc": "^0.26.7",
|
|
61
|
+
"typedoc-plugin-markdown": "^4.2.7",
|
|
62
|
+
"typescript": "^5.6.2",
|
|
63
|
+
"typescript-eslint": "^8.6.0",
|
|
64
|
+
"vite": "^5.4.6",
|
|
65
|
+
"vitest": "^2.1.1"
|
|
62
66
|
},
|
|
63
67
|
"lint-staged": {
|
|
64
68
|
"*.ts": [
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CMType,
|
|
3
|
+
ComponentRequest,
|
|
3
4
|
HMType,
|
|
4
5
|
HostMessage,
|
|
5
|
-
Transaction,
|
|
6
|
-
TransactionResponse,
|
|
7
6
|
ValidationError,
|
|
8
7
|
} from '../types';
|
|
9
8
|
import {
|
|
@@ -15,25 +14,30 @@ import {
|
|
|
15
14
|
* An embedded Notabene component
|
|
16
15
|
* @public
|
|
17
16
|
*/
|
|
18
|
-
|
|
17
|
+
|
|
18
|
+
export default class EmbeddedComponent<V, R> {
|
|
19
19
|
private _url: string;
|
|
20
|
-
private _value:
|
|
20
|
+
private _value: V;
|
|
21
21
|
private _errors: ValidationError[] = [];
|
|
22
22
|
|
|
23
23
|
private iframe?: HTMLIFrameElement;
|
|
24
24
|
private eventManager: MessageEventManager;
|
|
25
|
-
private ready = false;
|
|
26
25
|
private modal?: HTMLDialogElement;
|
|
27
|
-
// private popup?: HTMLElement;
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
/**
|
|
28
|
+
* Creates an instance of EmbeddedComponent.
|
|
29
|
+
* @param url - The URL of the embedded component
|
|
30
|
+
* @param value - The initial transaction value
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
constructor(url: string, value: V) {
|
|
30
34
|
this._url = url;
|
|
31
35
|
this._value = value;
|
|
32
36
|
this.eventManager = new MessageEventManager();
|
|
33
37
|
this.on(CMType.INVALID, (message) => {
|
|
34
38
|
if (message.type === CMType.INVALID) {
|
|
35
39
|
this._errors = message.errors;
|
|
36
|
-
this._value = message.value as
|
|
40
|
+
this._value = message.value as V;
|
|
37
41
|
}
|
|
38
42
|
});
|
|
39
43
|
this.on(CMType.RESIZE, (message) => {
|
|
@@ -42,12 +46,20 @@ export default class EmbeddedComponent {
|
|
|
42
46
|
}
|
|
43
47
|
});
|
|
44
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Gets the URL of the embedded component
|
|
51
|
+
* @returns The URL of the embedded component
|
|
52
|
+
*/
|
|
45
53
|
|
|
46
54
|
public get url(): string {
|
|
47
55
|
return this._url;
|
|
48
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Gets the current transaction value
|
|
59
|
+
* @returns The current transaction value
|
|
60
|
+
*/
|
|
49
61
|
|
|
50
|
-
public get value():
|
|
62
|
+
public get value(): V {
|
|
51
63
|
return this._value;
|
|
52
64
|
}
|
|
53
65
|
|
|
@@ -55,15 +67,29 @@ export default class EmbeddedComponent {
|
|
|
55
67
|
return this._errors;
|
|
56
68
|
}
|
|
57
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Opens the component URL in the current window
|
|
72
|
+
*/
|
|
73
|
+
|
|
58
74
|
public open() {
|
|
59
75
|
document.location.href = this.url;
|
|
60
76
|
}
|
|
61
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Mounts the component to a parent element
|
|
80
|
+
* @param parentId - The ID of the parent element
|
|
81
|
+
* @throws Will throw an error if the parent element is not found
|
|
82
|
+
*/
|
|
83
|
+
|
|
62
84
|
mount(parentId: string) {
|
|
63
85
|
const parent = document.querySelector(parentId);
|
|
64
86
|
if (!parent) throw new Error(`parentID ${parentId} not found`);
|
|
65
87
|
this.embed(parent);
|
|
66
88
|
}
|
|
89
|
+
/**
|
|
90
|
+
* Embeds the component into a parent element
|
|
91
|
+
* @param parent - The parent element to embed the component into
|
|
92
|
+
*/
|
|
67
93
|
|
|
68
94
|
embed(parent: Element) {
|
|
69
95
|
this.iframe = document.createElement('iframe');
|
|
@@ -82,32 +108,55 @@ export default class EmbeddedComponent {
|
|
|
82
108
|
}
|
|
83
109
|
console.log('received message from iframe', event.data);
|
|
84
110
|
this.eventManager?.setPort(event.ports[0]);
|
|
85
|
-
this.ready = true;
|
|
86
111
|
});
|
|
87
112
|
|
|
88
113
|
this.iframe?.contentWindow?.focus();
|
|
89
114
|
}
|
|
90
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Sends a message to the embedded component
|
|
118
|
+
* @param message - The message to send
|
|
119
|
+
*/
|
|
91
120
|
send(message: HostMessage): void {
|
|
92
121
|
this.eventManager.send(message);
|
|
93
122
|
}
|
|
94
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Adds an event listener for a specific message type
|
|
126
|
+
* @param messageType - The type of message to listen for
|
|
127
|
+
* @param callback - The callback function to execute when the message is received
|
|
128
|
+
*/
|
|
129
|
+
|
|
95
130
|
on(messageType: string, callback: MessageCallback): void {
|
|
96
131
|
this.eventManager.on(messageType, callback);
|
|
97
132
|
}
|
|
98
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Removes an event listener for a specific message type
|
|
136
|
+
* @param messageType - The type of message to stop listening for
|
|
137
|
+
* @param callback - The callback function to remove
|
|
138
|
+
*/
|
|
139
|
+
|
|
99
140
|
off(messageType: string, callback: MessageCallback): void {
|
|
100
141
|
this.eventManager.off(messageType, callback);
|
|
101
142
|
}
|
|
102
143
|
|
|
103
|
-
|
|
144
|
+
/**
|
|
145
|
+
* Updates the transaction value and sends an update message to the component
|
|
146
|
+
* @param value - The new transaction value
|
|
147
|
+
*/
|
|
148
|
+
update(value: V) {
|
|
104
149
|
this._value = value;
|
|
105
|
-
this.send({ type: HMType.UPDATE, value });
|
|
150
|
+
this.send({ type: HMType.UPDATE, value: value as ComponentRequest });
|
|
106
151
|
}
|
|
107
152
|
|
|
108
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Waits for the component to complete and returns the transaction response
|
|
155
|
+
* @returns A promise that resolves with the transaction response
|
|
156
|
+
*/
|
|
157
|
+
|
|
158
|
+
completion(): Promise<R> {
|
|
109
159
|
return new Promise((resolve, reject) => {
|
|
110
|
-
this.closeModal();
|
|
111
160
|
this.on(CMType.COMPLETE, (message) => {
|
|
112
161
|
resolve((message as any).response);
|
|
113
162
|
});
|
|
@@ -117,44 +166,22 @@ export default class EmbeddedComponent {
|
|
|
117
166
|
});
|
|
118
167
|
}
|
|
119
168
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// }
|
|
169
|
+
/**
|
|
170
|
+
* Opens the component in a modal dialog
|
|
171
|
+
*/
|
|
124
172
|
|
|
125
|
-
openModal():
|
|
173
|
+
openModal(): Promise<R> {
|
|
126
174
|
if (this.modal) {
|
|
127
175
|
this.closeModal();
|
|
128
176
|
}
|
|
129
177
|
this.modal = document.createElement('dialog');
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
// close.style.fontSize = '1rem';
|
|
138
|
-
// close.style.padding = '0.25rem 0.5rem';
|
|
139
|
-
// close.style.display = 'block';
|
|
140
|
-
// close.style.background = 'white';
|
|
141
|
-
// close.style.color = '#aaa';
|
|
142
|
-
// close.style.border = '#aaa';
|
|
143
|
-
// close.addEventListener('click', () => this.closeModal());
|
|
144
|
-
// this.modal.appendChild(close);
|
|
145
|
-
// this.modal.css;
|
|
146
|
-
// this.modal.style.position = 'fixed';
|
|
147
|
-
// this.modal.style.bottom = '0';
|
|
148
|
-
// this.modal.style.left = '0';
|
|
149
|
-
// this.modal.style.right = '0';
|
|
150
|
-
// this.modal.style.top = '0';
|
|
151
|
-
// this.modal.style.left = '0';
|
|
152
|
-
// this.modal.style.width = '100%';
|
|
153
|
-
// this.modal.style.height = '100%';
|
|
154
|
-
// this.modal.style.backgroundColor = 'transparent';
|
|
155
|
-
// this.modal.style.display = 'flex';
|
|
156
|
-
// this.modal.style.justifyContent = 'center';
|
|
157
|
-
// this.modal.style.alignItems = 'center';
|
|
178
|
+
this.modal.style.border = 'none';
|
|
179
|
+
this.modal.style.backgroundColor = 'transparent';
|
|
180
|
+
this.modal.style.maxWidth = '100vw';
|
|
181
|
+
this.modal.style.maxHeight = '100vh';
|
|
182
|
+
this.modal.style.width = '600px';
|
|
183
|
+
this.modal.style.height = '800px';
|
|
184
|
+
|
|
158
185
|
document.body.appendChild(this.modal);
|
|
159
186
|
this.embed(this.modal);
|
|
160
187
|
this.on(CMType.CANCEL, () => {
|
|
@@ -172,9 +199,14 @@ export default class EmbeddedComponent {
|
|
|
172
199
|
// console.log('click');
|
|
173
200
|
this.closeModal();
|
|
174
201
|
});
|
|
202
|
+
return this.completion();
|
|
175
203
|
}
|
|
204
|
+
/**
|
|
205
|
+
* Closes the modal dialog
|
|
206
|
+
*
|
|
207
|
+
*/
|
|
176
208
|
|
|
177
|
-
|
|
209
|
+
closeModal(): void {
|
|
178
210
|
if (this.modal) {
|
|
179
211
|
console.log('closeModal');
|
|
180
212
|
this.modal?.close();
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
CMType,
|
|
4
|
+
HMType,
|
|
5
|
+
HostMessage,
|
|
6
|
+
Transaction,
|
|
7
|
+
TransactionResponse,
|
|
8
|
+
Withdrawal,
|
|
9
|
+
} from '../../types';
|
|
3
10
|
import EmbeddedComponent from '../EmbeddedComponent';
|
|
4
11
|
|
|
5
12
|
const url = 'https://example.com';
|
|
@@ -8,9 +15,15 @@ const value: Partial<Transaction> = { amountDecimal: 100 };
|
|
|
8
15
|
|
|
9
16
|
describe('EmbeddedComponent', () => {
|
|
10
17
|
describe('creation', () => {
|
|
11
|
-
let component: EmbeddedComponent
|
|
18
|
+
let component: EmbeddedComponent<
|
|
19
|
+
Partial<Withdrawal>,
|
|
20
|
+
TransactionResponse<Withdrawal>
|
|
21
|
+
>;
|
|
12
22
|
beforeEach(() => {
|
|
13
|
-
component = new EmbeddedComponent
|
|
23
|
+
component = new EmbeddedComponent<
|
|
24
|
+
Partial<Withdrawal>,
|
|
25
|
+
TransactionResponse<Withdrawal>
|
|
26
|
+
>(url, { amountDecimal: 100 });
|
|
14
27
|
});
|
|
15
28
|
|
|
16
29
|
test('sets value', () => {
|
|
@@ -60,14 +73,20 @@ describe('EmbeddedComponent', () => {
|
|
|
60
73
|
});
|
|
61
74
|
|
|
62
75
|
test('mount', () => {
|
|
63
|
-
const component = new EmbeddedComponent
|
|
76
|
+
const component = new EmbeddedComponent<
|
|
77
|
+
Partial<Withdrawal>,
|
|
78
|
+
TransactionResponse<Withdrawal>
|
|
79
|
+
>(url, value);
|
|
64
80
|
component.mount(parentId);
|
|
65
81
|
expect(iframe?.src).toBe(url);
|
|
66
82
|
expect(parentElement.appendChild).toHaveBeenCalledWith(iframe);
|
|
67
83
|
});
|
|
68
84
|
|
|
69
85
|
test('on', () => {
|
|
70
|
-
const component = new EmbeddedComponent
|
|
86
|
+
const component = new EmbeddedComponent<
|
|
87
|
+
Partial<Withdrawal>,
|
|
88
|
+
TransactionResponse<Withdrawal>
|
|
89
|
+
>(url, value);
|
|
71
90
|
const messageType = 'customEvent';
|
|
72
91
|
const callback = vi.fn();
|
|
73
92
|
component.on(messageType, callback);
|
|
@@ -78,7 +97,10 @@ describe('EmbeddedComponent', () => {
|
|
|
78
97
|
});
|
|
79
98
|
|
|
80
99
|
test('off', () => {
|
|
81
|
-
const component = new EmbeddedComponent
|
|
100
|
+
const component = new EmbeddedComponent<
|
|
101
|
+
Partial<Withdrawal>,
|
|
102
|
+
TransactionResponse<Withdrawal>
|
|
103
|
+
>(url, value);
|
|
82
104
|
const messageType = 'customEvent';
|
|
83
105
|
const callback = vi.fn();
|
|
84
106
|
component.on(messageType, callback);
|
|
@@ -96,7 +118,10 @@ describe('EmbeddedComponent', () => {
|
|
|
96
118
|
let messageHandler: (event: MessageEvent) => void;
|
|
97
119
|
let port: MessagePort;
|
|
98
120
|
let handshake: MessageEvent;
|
|
99
|
-
let component: EmbeddedComponent
|
|
121
|
+
let component: EmbeddedComponent<
|
|
122
|
+
Partial<Withdrawal>,
|
|
123
|
+
TransactionResponse<Withdrawal>
|
|
124
|
+
>;
|
|
100
125
|
|
|
101
126
|
beforeEach(() => {
|
|
102
127
|
port = {
|
|
@@ -113,7 +138,10 @@ describe('EmbeddedComponent', () => {
|
|
|
113
138
|
.mockImplementation((event, handler) => {
|
|
114
139
|
messageHandler = handler;
|
|
115
140
|
});
|
|
116
|
-
component = new EmbeddedComponent
|
|
141
|
+
component = new EmbeddedComponent<
|
|
142
|
+
Partial<Withdrawal>,
|
|
143
|
+
TransactionResponse<Withdrawal>
|
|
144
|
+
>(url, value);
|
|
117
145
|
component.mount(parentId);
|
|
118
146
|
|
|
119
147
|
expect(messageHandler).toBeDefined();
|
|
@@ -123,12 +151,12 @@ describe('EmbeddedComponent', () => {
|
|
|
123
151
|
test('send', () => {
|
|
124
152
|
const message: HostMessage = {
|
|
125
153
|
type: HMType.UPDATE,
|
|
126
|
-
value: {
|
|
154
|
+
value: {
|
|
155
|
+
requestId: 'id',
|
|
156
|
+
},
|
|
127
157
|
};
|
|
128
158
|
component.send(message);
|
|
129
|
-
|
|
130
|
-
// Assert that the message is sent to the embedded component
|
|
131
|
-
// and handled correctly.
|
|
159
|
+
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
132
160
|
});
|
|
133
161
|
|
|
134
162
|
test('update', () => {
|