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