-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.ts
60 lines (51 loc) · 1.45 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
export function isBackchannel() {
const urlParams = new URLSearchParams(window.location.search)
return urlParams.get("channel") === "back"
}
export function getPollingId() {
const urlParams = new URLSearchParams(window.location.search)
const pollingId = urlParams.get("pollingId")
if (!pollingId) {
throw new Error("Missing pollingId")
}
return pollingId
}
/*
* This function is used to update a polling session with data from the frontchannel.
* It is used to emulate a backchannel response from the frontchannel.
*/
export async function updatePollingSession(baseUrl: string, data: any) {
const body = {
pollingId: getPollingId(),
data,
}
const response = await fetch(baseUrl + "/api/polling-session", {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
})
if (!response.ok) {
throw new Error("Failed to update polling session")
}
// window.close() needs to be called at the end of a backchannel flow
// to support Android devices where the parent FCL instance is unable
// to dismiss the child window. We also have an extra safety check
// to make sure we don't close the window if we're in an iframe.
if (!inIframe()) {
try {
window.close()
} catch (e) {}
}
}
export function inIframe() {
try {
return window.self !== window.top
} catch (e) {
return true
}
}
export function getBaseUrl() {
return window.location.origin
}