-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathappShell.ts
More file actions
60 lines (54 loc) · 1.77 KB
/
Copy pathappShell.ts
File metadata and controls
60 lines (54 loc) · 1.77 KB
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
/** Packaged desktop shell (Electron `app://`, local `file://` previews). */
export function isDesktopAppShell(): boolean {
if (typeof window === "undefined" || !window.location?.href) return false;
try {
const { protocol } = new URL(window.location.href);
return protocol !== "http:" && protocol !== "https:";
} catch {
return false;
}
}
/** Browser-style welcome OAuth (web tab or desktop shell with fetch). */
export function hasWelcomeBrowserAuthContext(): boolean {
return typeof globalThis !== "undefined" && typeof globalThis.fetch === "function";
}
export type DesktopSwapCoffeeFetchResult =
| { ok: true; status: number; body: string }
| { ok: false; error: string };
export type DesktopOAuthBridge = {
openOAuthUrl: (
authUrl: string,
apiOrigin: string,
) => Promise<{
ok: boolean;
error?: string | null;
sessionToken?: string | null;
}>;
fetchSwapCoffee?: (
url: string,
init?: {
method?: string;
headers?: Record<string, string>;
body?: string;
timeoutMs?: number;
},
) => Promise<DesktopSwapCoffeeFetchResult>;
/** Set by windows/preload.cjs in the Electron shell. */
isElectronShell?: boolean;
};
declare global {
interface Window {
__HSP_DESKTOP__?: DesktopOAuthBridge;
}
}
export function getDesktopOAuthBridge(): DesktopOAuthBridge | null {
if (typeof window === "undefined") return null;
const bridge = window.__HSP_DESKTOP__;
return bridge && typeof bridge.openOAuthUrl === "function" ? bridge : null;
}
/** Packaged Windows/Electron app (not a normal browser tab). */
export function isElectronDesktopShell(): boolean {
if (typeof window === "undefined") return false;
if (window.__HSP_DESKTOP__?.isElectronShell === true) return true;
return isDesktopAppShell();
}