@bridgewise/embed-sdk-web 0.0.0-stage → 0.1.0-beta.0
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 +218 -2
- package/dist/index.cjs +508 -0
- package/dist/index.d.cts +326 -0
- package/dist/index.d.ts +326 -0
- package/dist/index.js +474 -0
- package/package.json +51 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,508 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
EmbedClient: () => EmbedClient,
|
|
24
|
+
EmbedError: () => EmbedError,
|
|
25
|
+
SSO_LOGOUT_MESSAGE_TYPE: () => SSO_LOGOUT_MESSAGE_TYPE,
|
|
26
|
+
SSO_READY_MESSAGE_TYPE: () => SSO_READY_MESSAGE_TYPE,
|
|
27
|
+
SSO_TOKEN_MESSAGE_TYPE: () => SSO_TOKEN_MESSAGE_TYPE,
|
|
28
|
+
getApiBaseUrl: () => getApiBaseUrl,
|
|
29
|
+
getEmbeddedOrigin: () => getEmbeddedOrigin,
|
|
30
|
+
isPositionsTradeNow: () => isPositionsTradeNow
|
|
31
|
+
});
|
|
32
|
+
module.exports = __toCommonJS(index_exports);
|
|
33
|
+
|
|
34
|
+
// src/errors.ts
|
|
35
|
+
var EmbedError = class extends Error {
|
|
36
|
+
constructor(reason, message, cause) {
|
|
37
|
+
super(message);
|
|
38
|
+
this.reason = reason;
|
|
39
|
+
this.cause = cause;
|
|
40
|
+
this.name = "EmbedError";
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/internal/Listeners.ts
|
|
45
|
+
var Listeners = class {
|
|
46
|
+
constructor() {
|
|
47
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
48
|
+
}
|
|
49
|
+
/** Adds a listener and returns a function that removes it again. */
|
|
50
|
+
add(listener) {
|
|
51
|
+
this.listeners.add(listener);
|
|
52
|
+
return () => {
|
|
53
|
+
this.listeners.delete(listener);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
emit(value) {
|
|
57
|
+
for (const listener of this.listeners) listener(value);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/auth/tokens.ts
|
|
62
|
+
function parseTokenResponse(json, previous) {
|
|
63
|
+
const raw = json;
|
|
64
|
+
if (typeof raw.access_token !== "string" || typeof raw.expires_in !== "number") {
|
|
65
|
+
throw new Error("Unexpected token response shape");
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
accessToken: raw.access_token,
|
|
69
|
+
// Refresh responses may omit these; keep the ones we already have.
|
|
70
|
+
refreshToken: raw.refresh_token ?? previous?.refreshToken,
|
|
71
|
+
userId: raw.user_id ?? previous?.userId,
|
|
72
|
+
tenantId: raw.tenant_id ?? previous?.tenantId,
|
|
73
|
+
expiresAtEpochSeconds: Math.floor(Date.now() / 1e3) + raw.expires_in
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function isExpired(tokens) {
|
|
77
|
+
return Math.floor(Date.now() / 1e3) >= tokens.expiresAtEpochSeconds;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/auth/AuthApi.ts
|
|
81
|
+
var AuthApi = class {
|
|
82
|
+
constructor(baseUrl, clientId) {
|
|
83
|
+
this.baseUrl = baseUrl;
|
|
84
|
+
this.clientId = clientId;
|
|
85
|
+
}
|
|
86
|
+
/** URL of the hosted login page, which returns the user to `path`. */
|
|
87
|
+
loginUrl(path) {
|
|
88
|
+
const url = new URL(`${this.baseUrl}/bridgewise-auth/login`);
|
|
89
|
+
url.searchParams.set("client_id", this.clientId);
|
|
90
|
+
url.searchParams.set("path", path);
|
|
91
|
+
return url.toString();
|
|
92
|
+
}
|
|
93
|
+
async exchangeCode(code, state) {
|
|
94
|
+
const response = await this.postToken({
|
|
95
|
+
grant_type: "authorization_code",
|
|
96
|
+
code,
|
|
97
|
+
state
|
|
98
|
+
});
|
|
99
|
+
if (!response.ok) {
|
|
100
|
+
throw new Error(`Token exchange failed with status ${response.status}`);
|
|
101
|
+
}
|
|
102
|
+
return parseTokenResponse(await response.json());
|
|
103
|
+
}
|
|
104
|
+
async refresh(refreshToken, previous) {
|
|
105
|
+
const response = await this.postToken(
|
|
106
|
+
{ grant_type: "refresh_token", refresh_token: refreshToken },
|
|
107
|
+
{ credentials: "include" }
|
|
108
|
+
);
|
|
109
|
+
if (!response.ok) {
|
|
110
|
+
throw new Error(`Token refresh failed with status ${response.status}`);
|
|
111
|
+
}
|
|
112
|
+
return parseTokenResponse(await response.json(), previous);
|
|
113
|
+
}
|
|
114
|
+
postToken(params, init) {
|
|
115
|
+
return fetch(`${this.baseUrl}/bridgewise-auth/token`, {
|
|
116
|
+
...init,
|
|
117
|
+
method: "POST",
|
|
118
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
119
|
+
body: new URLSearchParams({ ...params, client_id: this.clientId })
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/auth/callback.ts
|
|
125
|
+
var CALLBACK_PARAMS = ["code", "state", "error", "error_description"];
|
|
126
|
+
function readAuthCallback() {
|
|
127
|
+
const params = new URL(window.location.href).searchParams;
|
|
128
|
+
const error = params.get("error");
|
|
129
|
+
if (error) {
|
|
130
|
+
return {
|
|
131
|
+
status: "error",
|
|
132
|
+
message: params.get("error_description") ?? error
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const code = params.get("code");
|
|
136
|
+
const state = params.get("state");
|
|
137
|
+
return code && state ? { status: "success", code, state } : null;
|
|
138
|
+
}
|
|
139
|
+
function removeAuthCallbackFromUrl() {
|
|
140
|
+
const url = new URL(window.location.href);
|
|
141
|
+
for (const param of CALLBACK_PARAMS) url.searchParams.delete(param);
|
|
142
|
+
window.history.replaceState(window.history.state, "", url.toString());
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/auth/messages.ts
|
|
146
|
+
var SSO_TOKEN_MESSAGE_TYPE = "bridgewise:sso:token";
|
|
147
|
+
var SSO_READY_MESSAGE_TYPE = "bridgewise:sso:ready";
|
|
148
|
+
var SSO_LOGOUT_MESSAGE_TYPE = "bridgewise:sso:logout";
|
|
149
|
+
function toTokenMessage(tokens) {
|
|
150
|
+
return {
|
|
151
|
+
type: SSO_TOKEN_MESSAGE_TYPE,
|
|
152
|
+
accessToken: tokens.accessToken,
|
|
153
|
+
userId: tokens.userId,
|
|
154
|
+
tenantId: tokens.tenantId,
|
|
155
|
+
expiresAtEpochSeconds: tokens.expiresAtEpochSeconds
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function isReadyMessage(data) {
|
|
159
|
+
return typeof data === "object" && data !== null && data.type === SSO_READY_MESSAGE_TYPE;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/auth/SsoAuth.ts
|
|
163
|
+
var SsoAuth = class {
|
|
164
|
+
/** @internal Created by `EmbedClient`. */
|
|
165
|
+
constructor(options, channel) {
|
|
166
|
+
this.options = options;
|
|
167
|
+
this.channel = channel;
|
|
168
|
+
this.tokens = null;
|
|
169
|
+
this.refreshTimer = null;
|
|
170
|
+
this.refreshInFlight = null;
|
|
171
|
+
this.successListeners = new Listeners();
|
|
172
|
+
this.refreshListeners = new Listeners();
|
|
173
|
+
this.errorListeners = new Listeners();
|
|
174
|
+
this.api = new AuthApi(options.apiBaseUrl, options.clientId);
|
|
175
|
+
channel.subscribe((data, source) => {
|
|
176
|
+
if (isReadyMessage(data)) void this.sendTokenTo(source);
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/** Fires after `init()` completes a sign-in. Returns an unsubscribe function. */
|
|
180
|
+
onSuccess(listener) {
|
|
181
|
+
return this.successListeners.add(listener);
|
|
182
|
+
}
|
|
183
|
+
/** Fires after each successful token refresh. Returns an unsubscribe function. */
|
|
184
|
+
onTokenRefresh(listener) {
|
|
185
|
+
return this.refreshListeners.add(listener);
|
|
186
|
+
}
|
|
187
|
+
/** Fires when sign-in or refresh fails. Returns an unsubscribe function. */
|
|
188
|
+
onError(listener) {
|
|
189
|
+
return this.errorListeners.add(listener);
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Call once when the app loads. If the URL carries `code`/`state` from the
|
|
193
|
+
* hosted login page, exchanges them for tokens and removes them from the
|
|
194
|
+
* URL. Otherwise, when `autoLogin` is true, redirects to the hosted login
|
|
195
|
+
* page immediately; when false, resolves to null and leaves starting a
|
|
196
|
+
* sign-in up to an explicit `login()` call.
|
|
197
|
+
*/
|
|
198
|
+
async init() {
|
|
199
|
+
const callback = readAuthCallback();
|
|
200
|
+
if (!callback) {
|
|
201
|
+
if (this.options.autoLogin) this.login();
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
if (callback.status === "error") {
|
|
205
|
+
this.errorListeners.emit(
|
|
206
|
+
new EmbedError("auth_callback_error", callback.message)
|
|
207
|
+
);
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
try {
|
|
211
|
+
const tokens = await this.api.exchangeCode(callback.code, callback.state);
|
|
212
|
+
this.setTokens(tokens);
|
|
213
|
+
removeAuthCallbackFromUrl();
|
|
214
|
+
this.successListeners.emit(tokens);
|
|
215
|
+
return tokens;
|
|
216
|
+
} catch (cause) {
|
|
217
|
+
this.errorListeners.emit(
|
|
218
|
+
new EmbedError(
|
|
219
|
+
"token_exchange_failed",
|
|
220
|
+
"Failed to exchange authorization code for tokens",
|
|
221
|
+
cause
|
|
222
|
+
)
|
|
223
|
+
);
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Redirects the browser to the hosted login page. After sign-in the user
|
|
229
|
+
* returns to `path`, else `redirectPath`, else the current page.
|
|
230
|
+
*/
|
|
231
|
+
login(path) {
|
|
232
|
+
const returnPath = path ?? this.options.redirectPath ?? window.location.pathname + window.location.search;
|
|
233
|
+
window.location.assign(this.api.loginUrl(returnPath));
|
|
234
|
+
}
|
|
235
|
+
/** Drops the in-memory session and tells embedded iframes to drop theirs. */
|
|
236
|
+
logout() {
|
|
237
|
+
this.tokens = null;
|
|
238
|
+
this.clearRefreshTimer();
|
|
239
|
+
this.channel.broadcast({ type: SSO_LOGOUT_MESSAGE_TYPE });
|
|
240
|
+
}
|
|
241
|
+
/** Returns a valid access token, refreshing first if expired. Null if there is no session. */
|
|
242
|
+
async getAccessToken() {
|
|
243
|
+
if (!this.tokens) return null;
|
|
244
|
+
if (isExpired(this.tokens)) await this.refresh();
|
|
245
|
+
return this.tokens?.accessToken ?? null;
|
|
246
|
+
}
|
|
247
|
+
/** Whether the SDK currently holds tokens (they don't survive a reload). */
|
|
248
|
+
hasSession() {
|
|
249
|
+
return this.tokens !== null;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Re-sends the current access token to embedded iframes. Embeds normally
|
|
253
|
+
* ask for it themselves when they load, so this is only needed for an
|
|
254
|
+
* embed that doesn't.
|
|
255
|
+
*/
|
|
256
|
+
broadcastToken() {
|
|
257
|
+
if (this.tokens) this.channel.broadcast(toTokenMessage(this.tokens));
|
|
258
|
+
}
|
|
259
|
+
/** @internal Stops token refresh. Called by `EmbedClient.destroy()`. */
|
|
260
|
+
destroy() {
|
|
261
|
+
this.clearRefreshTimer();
|
|
262
|
+
}
|
|
263
|
+
setTokens(tokens) {
|
|
264
|
+
this.tokens = tokens;
|
|
265
|
+
this.scheduleRefresh(tokens);
|
|
266
|
+
this.broadcastToken();
|
|
267
|
+
}
|
|
268
|
+
async sendTokenTo(frame) {
|
|
269
|
+
await this.getAccessToken();
|
|
270
|
+
if (this.tokens) this.channel.send(frame, toTokenMessage(this.tokens));
|
|
271
|
+
}
|
|
272
|
+
/** Concurrent callers share a single in-flight refresh. */
|
|
273
|
+
refresh() {
|
|
274
|
+
this.refreshInFlight ?? (this.refreshInFlight = this.performRefresh().finally(() => {
|
|
275
|
+
this.refreshInFlight = null;
|
|
276
|
+
}));
|
|
277
|
+
return this.refreshInFlight;
|
|
278
|
+
}
|
|
279
|
+
async performRefresh() {
|
|
280
|
+
const session = this.tokens;
|
|
281
|
+
if (!session?.refreshToken) {
|
|
282
|
+
this.tokens = null;
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
try {
|
|
286
|
+
const tokens = await this.api.refresh(session.refreshToken, session);
|
|
287
|
+
if (this.tokens !== session) return null;
|
|
288
|
+
this.setTokens(tokens);
|
|
289
|
+
this.refreshListeners.emit(tokens);
|
|
290
|
+
return tokens;
|
|
291
|
+
} catch (cause) {
|
|
292
|
+
if (this.tokens !== session) return null;
|
|
293
|
+
this.tokens = null;
|
|
294
|
+
this.clearRefreshTimer();
|
|
295
|
+
this.errorListeners.emit(
|
|
296
|
+
new EmbedError(
|
|
297
|
+
"token_refresh_failed",
|
|
298
|
+
"Failed to refresh access token",
|
|
299
|
+
cause
|
|
300
|
+
)
|
|
301
|
+
);
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
/** Refreshes at the moment the access token expires. */
|
|
306
|
+
scheduleRefresh(tokens) {
|
|
307
|
+
this.clearRefreshTimer();
|
|
308
|
+
if (!tokens.refreshToken) return;
|
|
309
|
+
const delayMs = Math.max(
|
|
310
|
+
0,
|
|
311
|
+
tokens.expiresAtEpochSeconds * 1e3 - Date.now()
|
|
312
|
+
);
|
|
313
|
+
this.refreshTimer = setTimeout(() => void this.refresh(), delayMs);
|
|
314
|
+
}
|
|
315
|
+
clearRefreshTimer() {
|
|
316
|
+
if (this.refreshTimer !== null) {
|
|
317
|
+
clearTimeout(this.refreshTimer);
|
|
318
|
+
this.refreshTimer = null;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
// src/config.ts
|
|
324
|
+
var API_BASE_URLS = {
|
|
325
|
+
dev: "https://rest.dev-bridgewise.com",
|
|
326
|
+
stage: "https://rest.stage-bridgewise.com",
|
|
327
|
+
prod: "https://rest.bridgewise.com"
|
|
328
|
+
};
|
|
329
|
+
var EMBEDDED_ORIGINS = {
|
|
330
|
+
dev: "https://embedded.dev-bridgewise.com",
|
|
331
|
+
stage: "https://embedded.stage-bridgewise.com",
|
|
332
|
+
prod: "https://embedded.bridgewise.com"
|
|
333
|
+
};
|
|
334
|
+
function getApiBaseUrl(environment) {
|
|
335
|
+
return API_BASE_URLS[environment];
|
|
336
|
+
}
|
|
337
|
+
function getEmbeddedOrigin(environment) {
|
|
338
|
+
return EMBEDDED_ORIGINS[environment];
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// src/internal/FrameChannel.ts
|
|
342
|
+
var FrameChannel = class {
|
|
343
|
+
constructor(origin) {
|
|
344
|
+
this.origin = origin;
|
|
345
|
+
this.handlers = /* @__PURE__ */ new Set();
|
|
346
|
+
this.handleMessage = (event) => {
|
|
347
|
+
if (event.origin !== this.origin) return;
|
|
348
|
+
const source = event.source;
|
|
349
|
+
if (!source || !isChildFrame(source)) return;
|
|
350
|
+
for (const handler of this.handlers) handler(event.data, source);
|
|
351
|
+
};
|
|
352
|
+
window.addEventListener("message", this.handleMessage);
|
|
353
|
+
}
|
|
354
|
+
/** Receives every message from an embed. Returns an unsubscribe function. */
|
|
355
|
+
subscribe(handler) {
|
|
356
|
+
this.handlers.add(handler);
|
|
357
|
+
return () => {
|
|
358
|
+
this.handlers.delete(handler);
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Posts a message to every frame on the page. `postMessage`'s
|
|
363
|
+
* `targetOrigin` is enforced by the browser at delivery time, so frames
|
|
364
|
+
* whose actual origin isn't the embed origin simply never receive it —
|
|
365
|
+
* no need to inspect the page to find which frames are embeds.
|
|
366
|
+
*/
|
|
367
|
+
broadcast(message) {
|
|
368
|
+
for (let i = 0; i < window.frames.length; i++) {
|
|
369
|
+
this.send(window.frames[i], message);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
send(frame, message) {
|
|
373
|
+
try {
|
|
374
|
+
frame.postMessage(message, this.origin);
|
|
375
|
+
} catch {
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
destroy() {
|
|
379
|
+
window.removeEventListener("message", this.handleMessage);
|
|
380
|
+
this.handlers.clear();
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
function isChildFrame(source) {
|
|
384
|
+
for (let i = 0; i < window.frames.length; i++) {
|
|
385
|
+
if (window.frames[i] === source) return true;
|
|
386
|
+
}
|
|
387
|
+
return false;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// src/widgets/events.ts
|
|
391
|
+
function isPositionsTradeNow(data, widget) {
|
|
392
|
+
return widget === "portfolio-positions" && data.id === "trade_now" && typeof data.currentAsset === "object" && data.currentAsset !== null && typeof data.targetAsset === "object" && data.targetAsset !== null;
|
|
393
|
+
}
|
|
394
|
+
var WIDGET_CALLBACK_NAMES = {
|
|
395
|
+
load: "onLoad",
|
|
396
|
+
resize: "onResize",
|
|
397
|
+
success: "onSuccess",
|
|
398
|
+
error: "onError",
|
|
399
|
+
"security-select": "onSecuritySelect",
|
|
400
|
+
"bridget-teaser-trigger": "onBridgetTeaserTrigger",
|
|
401
|
+
navigate: "onNavigate",
|
|
402
|
+
"trading-item-id-change": "onTradingItemIdChange",
|
|
403
|
+
"restricted-trigger": "onRestrictedTrigger",
|
|
404
|
+
click: "onClick",
|
|
405
|
+
hover: "onHover",
|
|
406
|
+
trigger: "onTrigger"
|
|
407
|
+
};
|
|
408
|
+
function parseWidgetMessage(raw) {
|
|
409
|
+
if (typeof raw !== "string") return null;
|
|
410
|
+
let msg;
|
|
411
|
+
try {
|
|
412
|
+
msg = JSON.parse(raw);
|
|
413
|
+
} catch {
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
if (typeof msg?.type !== "string" || typeof msg.widget !== "string" || !Object.prototype.hasOwnProperty.call(WIDGET_CALLBACK_NAMES, msg.type)) {
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
return msg;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// src/widgets/WidgetEvents.ts
|
|
423
|
+
var WidgetEvents = class {
|
|
424
|
+
/** @internal Created by `EmbedClient`. */
|
|
425
|
+
constructor(options, channel) {
|
|
426
|
+
this.options = options;
|
|
427
|
+
this.subscriptions = /* @__PURE__ */ new Set();
|
|
428
|
+
channel.subscribe((data, source) => {
|
|
429
|
+
const message = parseWidgetMessage(data);
|
|
430
|
+
if (message) this.handle(message, source);
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Registers callbacks for events posted by embedded widgets (onLoad,
|
|
435
|
+
* onResize, onClick, ...). Returns an unsubscribe function.
|
|
436
|
+
*/
|
|
437
|
+
on(callbacks) {
|
|
438
|
+
this.subscriptions.add(callbacks);
|
|
439
|
+
return () => {
|
|
440
|
+
this.subscriptions.delete(callbacks);
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
handle(message, source) {
|
|
444
|
+
if (this.options.autoResize && isSizeEvent(message)) {
|
|
445
|
+
setIframeHeight(source, message.data.height);
|
|
446
|
+
}
|
|
447
|
+
this.emit(message, source);
|
|
448
|
+
}
|
|
449
|
+
emit(message, source) {
|
|
450
|
+
const name = WIDGET_CALLBACK_NAMES[message.type];
|
|
451
|
+
const context = { widget: message.widget, event: message.event, source };
|
|
452
|
+
for (const callbacks of this.subscriptions) {
|
|
453
|
+
const callback = callbacks[name];
|
|
454
|
+
callback?.(message.data, context);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
function isSizeEvent(message) {
|
|
459
|
+
return message.type === "load" || message.type === "resize" || message.type === "success";
|
|
460
|
+
}
|
|
461
|
+
function setIframeHeight(source, height) {
|
|
462
|
+
if (typeof height !== "number" || !Number.isFinite(height) || height < 0) {
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
for (const iframe of document.querySelectorAll("iframe")) {
|
|
466
|
+
if (iframe.contentWindow === source) {
|
|
467
|
+
iframe.style.height = `${Math.ceil(height)}px`;
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// src/EmbedClient.ts
|
|
474
|
+
var EmbedClient = class {
|
|
475
|
+
constructor(config) {
|
|
476
|
+
this.embeddedOrigin = config.embeddedOrigin ?? getEmbeddedOrigin(config.environment);
|
|
477
|
+
this.channel = new FrameChannel(this.embeddedOrigin);
|
|
478
|
+
this.auth = new SsoAuth(
|
|
479
|
+
{
|
|
480
|
+
clientId: config.clientId,
|
|
481
|
+
apiBaseUrl: getApiBaseUrl(config.environment),
|
|
482
|
+
redirectPath: config.redirectPath,
|
|
483
|
+
autoLogin: config.autoLogin ?? false
|
|
484
|
+
},
|
|
485
|
+
this.channel
|
|
486
|
+
);
|
|
487
|
+
this.widgets = new WidgetEvents(
|
|
488
|
+
{ autoResize: config.autoResize ?? true },
|
|
489
|
+
this.channel
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
/** Stops listening to embeds and stops token refresh. */
|
|
493
|
+
destroy() {
|
|
494
|
+
this.channel.destroy();
|
|
495
|
+
this.auth.destroy();
|
|
496
|
+
}
|
|
497
|
+
};
|
|
498
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
499
|
+
0 && (module.exports = {
|
|
500
|
+
EmbedClient,
|
|
501
|
+
EmbedError,
|
|
502
|
+
SSO_LOGOUT_MESSAGE_TYPE,
|
|
503
|
+
SSO_READY_MESSAGE_TYPE,
|
|
504
|
+
SSO_TOKEN_MESSAGE_TYPE,
|
|
505
|
+
getApiBaseUrl,
|
|
506
|
+
getEmbeddedOrigin,
|
|
507
|
+
isPositionsTradeNow
|
|
508
|
+
});
|