@notabene/javascript-sdk 2.0.0-next.16 → 2.0.0-next.18
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 +44 -24
- package/dist/notabene.cjs +1 -1
- package/dist/notabene.d.ts +44 -44
- package/dist/notabene.js +52 -30
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +21 -20
- package/src/components/EmbeddedComponent.ts +30 -20
- package/src/components/__tests__/EmbeddedComponent.test.ts +64 -39
- package/src/notabene.ts +29 -23
- package/src/types.ts +26 -28
- package/src/utils/MessageEventManager.ts +7 -7
- package/src/utils/__tests__/MessageEventManager.test.ts +12 -4
package/dist/notabene.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var c = Object.defineProperty;
|
|
2
2
|
var m = (t, e, s) => e in t ? c(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
|
|
3
3
|
var o = (t, e, s) => m(t, typeof e != "symbol" ? e + "" : e, s);
|
|
4
|
-
var u = /* @__PURE__ */ ((t) => (t.PRIVATE = "WALLET", t.VASP = "VASP", t))(u || {}),
|
|
4
|
+
var u = /* @__PURE__ */ ((t) => (t.PRIVATE = "WALLET", t.VASP = "VASP", t))(u || {}), f = /* @__PURE__ */ ((t) => (t.NATURAL = "natural", t.LEGAL = "legal", t.SELF = "self", t))(f || {}), p = /* @__PURE__ */ ((t) => (t.EMPTY = "empty", t.VERIFY = "verify", t.PENDING = "pending", t.VERIFIED = "verified", t.BANNED = "banned", t))(p || {}), a = /* @__PURE__ */ ((t) => (t.COMPLETE = "complete", t.RESIZE = "resize", t.RESULT = "result", t.READY = "ready", t.INVALID = "invalid", t.ERROR = "error", t.CANCEL = "cancel", t))(a || {}), d = /* @__PURE__ */ ((t) => (t.UPDATE = "update", t.REQUEST_RESPONSE = "requestResponse", t))(d || {}), E = /* @__PURE__ */ ((t) => (t.PENDING = "pending", t.FAILED = "rejected", t.FLAGGED = "flagged", t.VERIFIED = "verified", t))(E || {}), g = /* @__PURE__ */ ((t) => (t.SelfDeclaration = "self-declaration", t.PersonalSignEIP191 = "eip-191", t.SIWE = "siwe", t.PersonalSignEIP712 = "eip-712", t.PersonalSignBIP137 = "bip-137", t.PersonalSignXPUB = "xpub", t.MicroTransfer = "microtransfer", t.Screenshot = "screenshot", t))(g || {});
|
|
5
5
|
class v {
|
|
6
6
|
constructor() {
|
|
7
7
|
o(this, "listeners", /* @__PURE__ */ new Map());
|
|
@@ -30,23 +30,24 @@ class v {
|
|
|
30
30
|
this.port && this.port.postMessage(e);
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
|
-
class
|
|
33
|
+
class w {
|
|
34
34
|
/**
|
|
35
35
|
* Creates an instance of EmbeddedComponent.
|
|
36
36
|
* @param url - The URL of the embedded component
|
|
37
37
|
* @param value - The initial transaction value
|
|
38
38
|
*/
|
|
39
|
-
constructor(e, s) {
|
|
39
|
+
constructor(e, s, r) {
|
|
40
40
|
o(this, "_url");
|
|
41
41
|
o(this, "_value");
|
|
42
|
+
o(this, "_options");
|
|
42
43
|
o(this, "_errors", []);
|
|
43
44
|
o(this, "iframe");
|
|
44
45
|
o(this, "eventManager");
|
|
45
46
|
o(this, "modal");
|
|
46
|
-
this._url = e, this._value = s, this.eventManager = new v(), this.on(a.INVALID, (
|
|
47
|
-
|
|
48
|
-
}), this.on(a.RESIZE, (
|
|
49
|
-
|
|
47
|
+
this._url = e, this._value = s, this._options = r, this.eventManager = new v(), this.on(a.INVALID, (i) => {
|
|
48
|
+
i.type === a.INVALID && (this._errors = i.errors, this._value = i.value);
|
|
49
|
+
}), this.on(a.RESIZE, (i) => {
|
|
50
|
+
i.type === a.RESIZE && this.iframe && (this.iframe.style.height = `${i.height}px`);
|
|
50
51
|
});
|
|
51
52
|
}
|
|
52
53
|
/**
|
|
@@ -63,6 +64,9 @@ class I {
|
|
|
63
64
|
get value() {
|
|
64
65
|
return this._value;
|
|
65
66
|
}
|
|
67
|
+
get options() {
|
|
68
|
+
return this._options;
|
|
69
|
+
}
|
|
66
70
|
get errors() {
|
|
67
71
|
return this._errors;
|
|
68
72
|
}
|
|
@@ -88,11 +92,14 @@ class I {
|
|
|
88
92
|
*/
|
|
89
93
|
embed(e, s = !1) {
|
|
90
94
|
var r, i;
|
|
91
|
-
this.iframe = document.createElement("iframe"), this.iframe.src = this.url + (s ? "" : "&embedded=true"), this.iframe.allow = "web-share", this.iframe.style.width = "100%", this.iframe.style.height = "100px", this.iframe.style.border = "none", this.iframe.style.overflow = "hidden", e.appendChild(this.iframe), window.addEventListener("message", (n) => {
|
|
92
|
-
var
|
|
93
|
-
n.source === ((
|
|
95
|
+
this.removeEmbed(), this.iframe = document.createElement("iframe"), this.iframe.src = this.url + (s ? "" : "&embedded=true"), this.iframe.allow = "web-share", this.iframe.style.width = "100%", this.iframe.style.height = "100px", this.iframe.style.border = "none", this.iframe.style.overflow = "hidden", e.appendChild(this.iframe), window.addEventListener("message", (n) => {
|
|
96
|
+
var h, l;
|
|
97
|
+
n.source === ((h = this.iframe) == null ? void 0 : h.contentWindow) && ((l = this.eventManager) == null || l.setPort(n.ports[0]));
|
|
94
98
|
}), (i = (r = this.iframe) == null ? void 0 : r.contentWindow) == null || i.focus();
|
|
95
99
|
}
|
|
100
|
+
removeEmbed() {
|
|
101
|
+
this.iframe && this.iframe.remove();
|
|
102
|
+
}
|
|
96
103
|
/**
|
|
97
104
|
* Sends a message to the embedded component
|
|
98
105
|
* @param message - The message to send
|
|
@@ -120,8 +127,8 @@ class I {
|
|
|
120
127
|
* Updates the transaction value and sends an update message to the component
|
|
121
128
|
* @param value - The new transaction value
|
|
122
129
|
*/
|
|
123
|
-
update(e) {
|
|
124
|
-
this._value = e, this.send({ type: d.UPDATE, value: e });
|
|
130
|
+
update(e, s) {
|
|
131
|
+
this._value = e, s && (this._options = s), this.send({ type: d.UPDATE, value: e, options: this._options });
|
|
125
132
|
}
|
|
126
133
|
/**
|
|
127
134
|
* Waits for the component to complete and returns the transaction response
|
|
@@ -140,9 +147,7 @@ class I {
|
|
|
140
147
|
* Opens the component in a modal dialog
|
|
141
148
|
*/
|
|
142
149
|
openModal() {
|
|
143
|
-
return this.modal && this.closeModal(), this.modal = document.createElement("dialog"), this.modal.style.border = "none", this.modal.style.backgroundColor = "
|
|
144
|
-
this.closeModal();
|
|
145
|
-
}), this.on(a.CLOSE, () => {
|
|
150
|
+
return this.modal && this.closeModal(), this.modal = document.createElement("dialog"), this.modal.style.border = "none", this.modal.style.backgroundColor = "white", this.modal.style.maxWidth = "100vw", this.modal.style.maxHeight = "100vh", this.modal.style.width = "600px", this.modal.style.height = "600px", document.body.appendChild(this.modal), this.embed(this.modal, !0), this.on(a.CANCEL, () => {
|
|
146
151
|
this.closeModal();
|
|
147
152
|
}), this.on(a.COMPLETE, () => {
|
|
148
153
|
this.closeModal();
|
|
@@ -159,7 +164,7 @@ class I {
|
|
|
159
164
|
this.modal && ((e = this.modal) == null || e.close(), this.modal.remove(), this.modal = void 0);
|
|
160
165
|
}
|
|
161
166
|
}
|
|
162
|
-
function
|
|
167
|
+
function I(t) {
|
|
163
168
|
return Object.entries(t).map(([e, s]) => {
|
|
164
169
|
if (!(e && s)) return;
|
|
165
170
|
const r = encodeURIComponent(e), i = encodeURIComponent(
|
|
@@ -168,7 +173,7 @@ function U(t) {
|
|
|
168
173
|
return `${r}=${i}`;
|
|
169
174
|
}).filter((e) => !!e).join("&");
|
|
170
175
|
}
|
|
171
|
-
class
|
|
176
|
+
class b {
|
|
172
177
|
/**
|
|
173
178
|
* Creates a new instance of the Notabene SDK
|
|
174
179
|
*
|
|
@@ -195,12 +200,12 @@ class L {
|
|
|
195
200
|
componentUrl(e, s, r, i) {
|
|
196
201
|
const n = new URL(this.uxUrl);
|
|
197
202
|
n.pathname = e;
|
|
198
|
-
const
|
|
203
|
+
const h = I({
|
|
199
204
|
authToken: this.authToken,
|
|
200
205
|
value: s,
|
|
201
206
|
configuration: r
|
|
202
207
|
});
|
|
203
|
-
return n.hash =
|
|
208
|
+
return n.hash = h, this.nodeUrl && n.searchParams.set("nodeUrl", this.nodeUrl), this.theme && n.searchParams.set("theme", JSON.stringify(this.theme)), this.locale && n.searchParams.set("locale", this.locale), i && (i.callback && n.searchParams.set("callback_url", i.callback), i.redirectUri && n.searchParams.set("redirect_uri", i.redirectUri)), n.toString();
|
|
204
209
|
}
|
|
205
210
|
/**
|
|
206
211
|
* Creates a new embedded component
|
|
@@ -213,9 +218,10 @@ class L {
|
|
|
213
218
|
* @internal
|
|
214
219
|
*/
|
|
215
220
|
createComponent(e, s, r, i) {
|
|
216
|
-
return new
|
|
221
|
+
return new w(
|
|
217
222
|
this.componentUrl(e, s, r, i),
|
|
218
|
-
s
|
|
223
|
+
s,
|
|
224
|
+
r
|
|
219
225
|
);
|
|
220
226
|
}
|
|
221
227
|
/**
|
|
@@ -227,7 +233,12 @@ class L {
|
|
|
227
233
|
* @returns A new EmbeddedComponent instance for withdrawal assistance
|
|
228
234
|
*/
|
|
229
235
|
createWithdrawalAssist(e, s, r) {
|
|
230
|
-
return this.createComponent(
|
|
236
|
+
return this.createComponent(
|
|
237
|
+
"withdrawal-assist",
|
|
238
|
+
e,
|
|
239
|
+
s,
|
|
240
|
+
r
|
|
241
|
+
);
|
|
231
242
|
}
|
|
232
243
|
/**
|
|
233
244
|
* Creates a wallet assist component
|
|
@@ -239,7 +250,12 @@ class L {
|
|
|
239
250
|
* @alpha
|
|
240
251
|
*/
|
|
241
252
|
createWalletAssist(e, s, r) {
|
|
242
|
-
return this.createComponent(
|
|
253
|
+
return this.createComponent(
|
|
254
|
+
"wallet-assist",
|
|
255
|
+
e,
|
|
256
|
+
s,
|
|
257
|
+
r
|
|
258
|
+
);
|
|
243
259
|
}
|
|
244
260
|
/**
|
|
245
261
|
* Creates a deposit assist component
|
|
@@ -285,7 +301,12 @@ class L {
|
|
|
285
301
|
* @public
|
|
286
302
|
*/
|
|
287
303
|
createDepositRequest(e, s, r) {
|
|
288
|
-
return this.createComponent(
|
|
304
|
+
return this.createComponent(
|
|
305
|
+
"deposit-request",
|
|
306
|
+
e,
|
|
307
|
+
s,
|
|
308
|
+
r
|
|
309
|
+
);
|
|
289
310
|
}
|
|
290
311
|
/**
|
|
291
312
|
* Decodes a URL fragment into an object
|
|
@@ -296,8 +317,8 @@ class L {
|
|
|
296
317
|
decodeFragmentToObject(e) {
|
|
297
318
|
return e.slice(1).split("&").reduce(
|
|
298
319
|
(r, i) => {
|
|
299
|
-
const [n,
|
|
300
|
-
return r[decodeURIComponent(n)] = decodeURIComponent(
|
|
320
|
+
const [n, h] = i.split("=");
|
|
321
|
+
return r[decodeURIComponent(n)] = decodeURIComponent(h), r;
|
|
301
322
|
},
|
|
302
323
|
{}
|
|
303
324
|
);
|
|
@@ -306,9 +327,10 @@ class L {
|
|
|
306
327
|
export {
|
|
307
328
|
u as AgentType,
|
|
308
329
|
a as CMType,
|
|
309
|
-
|
|
310
|
-
|
|
330
|
+
w as EmbeddedComponent,
|
|
331
|
+
f as PersonType,
|
|
332
|
+
E as ProofStatus,
|
|
311
333
|
g as ProofTypes,
|
|
312
|
-
|
|
313
|
-
|
|
334
|
+
p as Status,
|
|
335
|
+
b as default
|
|
314
336
|
};
|
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.18",
|
|
13
13
|
"source": "src/notabene.ts",
|
|
14
14
|
"main": "dist/notabene.js",
|
|
15
15
|
"types": "dist/notabene.d.ts",
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"build:clean": "rimraf dist/ ts-out",
|
|
31
31
|
"build": "yarn lint && vite build && tsc && api-extractor run && yarn docs ",
|
|
32
32
|
"lint": "eslint .",
|
|
33
|
+
"format": "prettier --write .",
|
|
33
34
|
"test": "vitest",
|
|
34
35
|
"release": "semantic-release",
|
|
35
36
|
"publish": "yarn build && npm publish --tag next"
|
|
@@ -37,32 +38,32 @@
|
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"@commitlint/cli": "^19.5.0",
|
|
39
40
|
"@commitlint/config-conventional": "^19.5.0",
|
|
40
|
-
"@eslint/js": "^9.
|
|
41
|
+
"@eslint/js": "^9.13.0",
|
|
41
42
|
"@fast-check/vitest": "^0.1.3",
|
|
42
|
-
"@microsoft/api-extractor": "^7.47.
|
|
43
|
+
"@microsoft/api-extractor": "^7.47.11",
|
|
43
44
|
"@semantic-release/git": "^10.0.1",
|
|
44
|
-
"@semantic-release/gitlab": "^
|
|
45
|
-
"@semantic-release/npm": "^
|
|
46
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
47
|
-
"@typescript-eslint/parser": "^8.
|
|
48
|
-
"eslint": "^9.
|
|
45
|
+
"@semantic-release/gitlab": "^13.2.1",
|
|
46
|
+
"@semantic-release/npm": "^12.0.1",
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^8.11.0",
|
|
48
|
+
"@typescript-eslint/parser": "^8.11.0",
|
|
49
|
+
"eslint": "^9.13.0",
|
|
49
50
|
"eslint-config-prettier": "^9.1.0",
|
|
50
51
|
"eslint-plugin-prettier": "^5.2.1",
|
|
51
52
|
"fast-check": "^3.22.0",
|
|
52
|
-
"globals": "^15.
|
|
53
|
-
"husky": "^
|
|
54
|
-
"jsdom": "^25.0.
|
|
55
|
-
"lint-staged": "^
|
|
56
|
-
"organize-imports-cli": "^0.
|
|
53
|
+
"globals": "^15.11.0",
|
|
54
|
+
"husky": "^9.1.6",
|
|
55
|
+
"jsdom": "^25.0.1",
|
|
56
|
+
"lint-staged": "^15.2.10",
|
|
57
|
+
"organize-imports-cli": "^0.10.0",
|
|
57
58
|
"prettier": "^3.3.3",
|
|
58
59
|
"rimraf": "^6.0.1",
|
|
59
|
-
"semantic-release": "^24.1.
|
|
60
|
-
"typedoc": "^0.26.
|
|
61
|
-
"typedoc-plugin-markdown": "^4.2.
|
|
62
|
-
"typescript": "^5.6.
|
|
63
|
-
"typescript-eslint": "^8.
|
|
64
|
-
"vite": "^5.4.
|
|
65
|
-
"vitest": "^2.1.
|
|
60
|
+
"semantic-release": "^24.1.3",
|
|
61
|
+
"typedoc": "^0.26.10",
|
|
62
|
+
"typedoc-plugin-markdown": "^4.2.9",
|
|
63
|
+
"typescript": "^5.6.3",
|
|
64
|
+
"typescript-eslint": "^8.11.0",
|
|
65
|
+
"vite": "^5.4.10",
|
|
66
|
+
"vitest": "^2.1.3"
|
|
66
67
|
},
|
|
67
68
|
"lint-staged": {
|
|
68
69
|
"*.ts": [
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CMType,
|
|
3
|
-
ComponentRequest,
|
|
4
3
|
HMType,
|
|
5
4
|
HostMessage,
|
|
5
|
+
TransactionResponse,
|
|
6
6
|
ValidationError,
|
|
7
7
|
} from '../types';
|
|
8
8
|
import {
|
|
@@ -15,13 +15,14 @@ import {
|
|
|
15
15
|
* @public
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
export default class EmbeddedComponent<V,
|
|
18
|
+
export default class EmbeddedComponent<V, O> {
|
|
19
19
|
private _url: string;
|
|
20
|
-
private _value: V
|
|
20
|
+
private _value: Partial<V>;
|
|
21
|
+
private _options?: O;
|
|
21
22
|
private _errors: ValidationError[] = [];
|
|
22
23
|
|
|
23
24
|
private iframe?: HTMLIFrameElement;
|
|
24
|
-
private eventManager: MessageEventManager
|
|
25
|
+
private eventManager: MessageEventManager<V, O>;
|
|
25
26
|
private modal?: HTMLDialogElement;
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -30,10 +31,11 @@ export default class EmbeddedComponent<V, R> {
|
|
|
30
31
|
* @param value - The initial transaction value
|
|
31
32
|
*/
|
|
32
33
|
|
|
33
|
-
constructor(url: string, value: V) {
|
|
34
|
+
constructor(url: string, value: Partial<V>, options?: O) {
|
|
34
35
|
this._url = url;
|
|
35
36
|
this._value = value;
|
|
36
|
-
this.
|
|
37
|
+
this._options = options;
|
|
38
|
+
this.eventManager = new MessageEventManager<V, O>();
|
|
37
39
|
this.on(CMType.INVALID, (message) => {
|
|
38
40
|
if (message.type === CMType.INVALID) {
|
|
39
41
|
this._errors = message.errors;
|
|
@@ -59,10 +61,14 @@ export default class EmbeddedComponent<V, R> {
|
|
|
59
61
|
* @returns The current transaction value
|
|
60
62
|
*/
|
|
61
63
|
|
|
62
|
-
public get value(): V {
|
|
64
|
+
public get value(): Partial<V> {
|
|
63
65
|
return this._value;
|
|
64
66
|
}
|
|
65
67
|
|
|
68
|
+
public get options(): O | undefined {
|
|
69
|
+
return this._options;
|
|
70
|
+
}
|
|
71
|
+
|
|
66
72
|
public get errors(): ValidationError[] {
|
|
67
73
|
return this._errors;
|
|
68
74
|
}
|
|
@@ -86,13 +92,14 @@ export default class EmbeddedComponent<V, R> {
|
|
|
86
92
|
if (!parent) throw new Error(`parentID ${parentId} not found`);
|
|
87
93
|
this.embed(parent);
|
|
88
94
|
}
|
|
95
|
+
|
|
89
96
|
/**
|
|
90
97
|
* Embeds the component into a parent element
|
|
91
98
|
* @param parent - The parent element to embed the component into
|
|
92
99
|
*/
|
|
93
|
-
|
|
94
100
|
embed(parent: Element, modal: boolean = false) {
|
|
95
101
|
// console.log(this.url);
|
|
102
|
+
this.removeEmbed();
|
|
96
103
|
this.iframe = document.createElement('iframe');
|
|
97
104
|
this.iframe.src = this.url + (!modal ? '&embedded=true' : '');
|
|
98
105
|
this.iframe.allow = 'web-share';
|
|
@@ -115,11 +122,15 @@ export default class EmbeddedComponent<V, R> {
|
|
|
115
122
|
this.iframe?.contentWindow?.focus();
|
|
116
123
|
}
|
|
117
124
|
|
|
125
|
+
removeEmbed() {
|
|
126
|
+
if (this.iframe) this.iframe.remove();
|
|
127
|
+
}
|
|
128
|
+
|
|
118
129
|
/**
|
|
119
130
|
* Sends a message to the embedded component
|
|
120
131
|
* @param message - The message to send
|
|
121
132
|
*/
|
|
122
|
-
send(message: HostMessage): void {
|
|
133
|
+
send(message: HostMessage<V, O>): void {
|
|
123
134
|
this.eventManager.send(message);
|
|
124
135
|
}
|
|
125
136
|
|
|
@@ -129,7 +140,7 @@ export default class EmbeddedComponent<V, R> {
|
|
|
129
140
|
* @param callback - The callback function to execute when the message is received
|
|
130
141
|
*/
|
|
131
142
|
|
|
132
|
-
on(messageType: string, callback: MessageCallback): void {
|
|
143
|
+
on(messageType: string, callback: MessageCallback<V>): void {
|
|
133
144
|
this.eventManager.on(messageType, callback);
|
|
134
145
|
}
|
|
135
146
|
|
|
@@ -139,7 +150,7 @@ export default class EmbeddedComponent<V, R> {
|
|
|
139
150
|
* @param callback - The callback function to remove
|
|
140
151
|
*/
|
|
141
152
|
|
|
142
|
-
off(messageType: string, callback: MessageCallback): void {
|
|
153
|
+
off(messageType: string, callback: MessageCallback<V>): void {
|
|
143
154
|
this.eventManager.off(messageType, callback);
|
|
144
155
|
}
|
|
145
156
|
|
|
@@ -147,9 +158,10 @@ export default class EmbeddedComponent<V, R> {
|
|
|
147
158
|
* Updates the transaction value and sends an update message to the component
|
|
148
159
|
* @param value - The new transaction value
|
|
149
160
|
*/
|
|
150
|
-
update(value: V) {
|
|
161
|
+
update(value: Partial<V>, options?: O) {
|
|
151
162
|
this._value = value;
|
|
152
|
-
|
|
163
|
+
if (options) this._options = options;
|
|
164
|
+
this.send({ type: HMType.UPDATE, value, options: this._options });
|
|
153
165
|
}
|
|
154
166
|
|
|
155
167
|
/**
|
|
@@ -157,7 +169,7 @@ export default class EmbeddedComponent<V, R> {
|
|
|
157
169
|
* @returns A promise that resolves with the transaction response
|
|
158
170
|
*/
|
|
159
171
|
|
|
160
|
-
completion(): Promise<
|
|
172
|
+
completion(): Promise<TransactionResponse<V>> {
|
|
161
173
|
return new Promise((resolve, reject) => {
|
|
162
174
|
this.on(CMType.COMPLETE, (message) => {
|
|
163
175
|
resolve((message as any).response);
|
|
@@ -172,26 +184,24 @@ export default class EmbeddedComponent<V, R> {
|
|
|
172
184
|
* Opens the component in a modal dialog
|
|
173
185
|
*/
|
|
174
186
|
|
|
175
|
-
openModal(): Promise<
|
|
187
|
+
openModal(): Promise<TransactionResponse<V>> {
|
|
176
188
|
if (this.modal) {
|
|
177
189
|
this.closeModal();
|
|
178
190
|
}
|
|
179
191
|
this.modal = document.createElement('dialog');
|
|
180
192
|
this.modal.style.border = 'none';
|
|
181
|
-
this.modal.style.backgroundColor = '
|
|
193
|
+
this.modal.style.backgroundColor = 'white';
|
|
182
194
|
this.modal.style.maxWidth = '100vw';
|
|
183
195
|
this.modal.style.maxHeight = '100vh';
|
|
184
196
|
this.modal.style.width = '600px';
|
|
185
|
-
this.modal.style.height = '
|
|
197
|
+
this.modal.style.height = '600px';
|
|
186
198
|
|
|
187
199
|
document.body.appendChild(this.modal);
|
|
188
200
|
this.embed(this.modal, true);
|
|
189
201
|
this.on(CMType.CANCEL, () => {
|
|
190
202
|
this.closeModal();
|
|
191
203
|
});
|
|
192
|
-
|
|
193
|
-
this.closeModal();
|
|
194
|
-
});
|
|
204
|
+
|
|
195
205
|
this.on(CMType.COMPLETE, () => {
|
|
196
206
|
this.closeModal();
|
|
197
207
|
});
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
|
|
2
2
|
import {
|
|
3
|
+
AgentType,
|
|
3
4
|
CMType,
|
|
4
5
|
HMType,
|
|
5
|
-
HostMessage,
|
|
6
|
-
Transaction,
|
|
7
|
-
|
|
8
|
-
Withdrawal,
|
|
6
|
+
type HostMessage,
|
|
7
|
+
type Transaction,
|
|
8
|
+
type TransactionOptions,
|
|
9
|
+
type Withdrawal,
|
|
9
10
|
} from '../../types';
|
|
10
11
|
import EmbeddedComponent from '../EmbeddedComponent';
|
|
11
12
|
|
|
@@ -15,15 +16,11 @@ const value: Partial<Transaction> = { amountDecimal: 100 };
|
|
|
15
16
|
|
|
16
17
|
describe('EmbeddedComponent', () => {
|
|
17
18
|
describe('creation', () => {
|
|
18
|
-
let component: EmbeddedComponent<
|
|
19
|
-
Partial<Withdrawal>,
|
|
20
|
-
TransactionResponse<Withdrawal>
|
|
21
|
-
>;
|
|
19
|
+
let component: EmbeddedComponent<Withdrawal, TransactionOptions>;
|
|
22
20
|
beforeEach(() => {
|
|
23
|
-
component = new EmbeddedComponent<
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
>(url, { amountDecimal: 100 });
|
|
21
|
+
component = new EmbeddedComponent<Withdrawal, TransactionOptions>(url, {
|
|
22
|
+
amountDecimal: 100,
|
|
23
|
+
});
|
|
27
24
|
});
|
|
28
25
|
|
|
29
26
|
test('sets value', () => {
|
|
@@ -73,20 +70,20 @@ describe('EmbeddedComponent', () => {
|
|
|
73
70
|
});
|
|
74
71
|
|
|
75
72
|
test('mount', () => {
|
|
76
|
-
const component = new EmbeddedComponent<
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
const component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
74
|
+
url,
|
|
75
|
+
value,
|
|
76
|
+
);
|
|
80
77
|
component.mount(parentId);
|
|
81
78
|
expect(iframe?.src).toBe(url + '&embedded=true');
|
|
82
79
|
expect(parentElement.appendChild).toHaveBeenCalledWith(iframe);
|
|
83
80
|
});
|
|
84
81
|
|
|
85
82
|
test('on', () => {
|
|
86
|
-
const component = new EmbeddedComponent<
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
const component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
84
|
+
url,
|
|
85
|
+
value,
|
|
86
|
+
);
|
|
90
87
|
const messageType = 'customEvent';
|
|
91
88
|
const callback = vi.fn();
|
|
92
89
|
component.on(messageType, callback);
|
|
@@ -97,10 +94,10 @@ describe('EmbeddedComponent', () => {
|
|
|
97
94
|
});
|
|
98
95
|
|
|
99
96
|
test('off', () => {
|
|
100
|
-
const component = new EmbeddedComponent<
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
97
|
+
const component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
98
|
+
url,
|
|
99
|
+
value,
|
|
100
|
+
);
|
|
104
101
|
const messageType = 'customEvent';
|
|
105
102
|
const callback = vi.fn();
|
|
106
103
|
component.on(messageType, callback);
|
|
@@ -118,10 +115,8 @@ describe('EmbeddedComponent', () => {
|
|
|
118
115
|
let messageHandler: (event: MessageEvent) => void;
|
|
119
116
|
let port: MessagePort;
|
|
120
117
|
let handshake: MessageEvent;
|
|
121
|
-
let component: EmbeddedComponent<
|
|
122
|
-
|
|
123
|
-
TransactionResponse<Withdrawal>
|
|
124
|
-
>;
|
|
118
|
+
let component: EmbeddedComponent<Withdrawal, TransactionOptions>;
|
|
119
|
+
let options: TransactionOptions;
|
|
125
120
|
|
|
126
121
|
beforeEach(() => {
|
|
127
122
|
port = {
|
|
@@ -138,10 +133,12 @@ describe('EmbeddedComponent', () => {
|
|
|
138
133
|
.mockImplementation((event, handler) => {
|
|
139
134
|
messageHandler = handler;
|
|
140
135
|
});
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
136
|
+
options = { allowedAgentTypes: [AgentType.PRIVATE, AgentType.VASP] };
|
|
137
|
+
component = new EmbeddedComponent<Withdrawal, TransactionOptions>(
|
|
138
|
+
url,
|
|
139
|
+
value,
|
|
140
|
+
options,
|
|
141
|
+
);
|
|
145
142
|
component.mount(parentId);
|
|
146
143
|
|
|
147
144
|
expect(messageHandler).toBeDefined();
|
|
@@ -149,23 +146,51 @@ describe('EmbeddedComponent', () => {
|
|
|
149
146
|
});
|
|
150
147
|
|
|
151
148
|
test('send', () => {
|
|
152
|
-
const message: HostMessage = {
|
|
149
|
+
const message: HostMessage<Withdrawal, TransactionOptions> = {
|
|
153
150
|
type: HMType.UPDATE,
|
|
154
151
|
value: {
|
|
155
152
|
requestId: 'id',
|
|
156
153
|
},
|
|
154
|
+
options: { allowedAgentTypes: [AgentType.PRIVATE] },
|
|
157
155
|
};
|
|
158
156
|
component.send(message);
|
|
159
157
|
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
160
158
|
});
|
|
161
159
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
160
|
+
describe('update', () => {
|
|
161
|
+
test('with out options', () => {
|
|
162
|
+
const newValue: Partial<Transaction> = { amountDecimal: 200 };
|
|
165
163
|
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
const message: HostMessage<Withdrawal, TransactionOptions> = {
|
|
165
|
+
type: HMType.UPDATE,
|
|
166
|
+
value: newValue,
|
|
167
|
+
options,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
component.update(newValue);
|
|
171
|
+
|
|
172
|
+
expect(component.value).toBe(newValue);
|
|
173
|
+
expect(component.options).toBe(options);
|
|
174
|
+
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test('with options', () => {
|
|
178
|
+
const newValue: Partial<Transaction> = { amountDecimal: 200 };
|
|
179
|
+
const newOptions = { allowedAgentTypes: [AgentType.PRIVATE] };
|
|
168
180
|
|
|
181
|
+
const message: HostMessage<Withdrawal, TransactionOptions> = {
|
|
182
|
+
type: HMType.UPDATE,
|
|
183
|
+
value: newValue,
|
|
184
|
+
options: newOptions,
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
component.update(newValue, newOptions);
|
|
188
|
+
|
|
189
|
+
expect(component.value).toBe(newValue);
|
|
190
|
+
expect(component.options).toBe(newOptions);
|
|
191
|
+
expect(port.postMessage).toHaveBeenCalledWith(message);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
169
194
|
// TODO not sure this actually does what I want it to do
|
|
170
195
|
test('completion', async () => {
|
|
171
196
|
const response = { valid: true, value: 'completed' };
|
|
@@ -175,7 +200,7 @@ describe('EmbeddedComponent', () => {
|
|
|
175
200
|
// @ts-expect-error - we've already verified that port exists
|
|
176
201
|
port.onmessage({
|
|
177
202
|
data: { type: CMType.COMPLETE, response },
|
|
178
|
-
}
|
|
203
|
+
});
|
|
179
204
|
|
|
180
205
|
promise.then((result) => {
|
|
181
206
|
expect(result).toBe(response);
|