@lemoncloud/chatic-apps-sdk 0.1.0 → 0.2.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/AGENTS.md +56 -0
- package/GUIDE.md +219 -0
- package/README.md +51 -53
- package/chatic-apps-sdk.global.js +1 -1
- package/index.cjs +1 -1
- package/index.d.cts +36 -3
- package/index.d.ts +36 -3
- package/index.js +196 -153
- package/mock.cjs +1 -1
- package/mock.d.cts +22 -0
- package/mock.d.ts +22 -0
- package/mock.js +112 -91
- package/package.json +2 -2
package/index.d.cts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/** Every code the app itself can fail a call with. */
|
|
2
|
-
export declare type AppErrorCode = 'NOT_FOUND' | 'INTERNAL_ERROR' | 'TOO_LARGE' | 'INVALID' | 'INVALID_KIND' | 'NOT_OPEN' | 'LINK_NOT_ALLOWED' | 'OPEN_URL_FAILED' | 'CLIPBOARD_WRITE_ERROR' | 'SHARE_ERROR' | 'UNAVAILABLE' | 'QUOTA_EXCEEDED';
|
|
2
|
+
export declare type AppErrorCode = 'NOT_FOUND' | 'INTERNAL_ERROR' | 'TOO_LARGE' | 'INVALID' | 'INVALID_KIND' | 'NOT_OPEN' | 'LINK_NOT_ALLOWED' | 'OPEN_URL_FAILED' | 'CLIPBOARD_WRITE_ERROR' | 'SHARE_ERROR' | 'UNAVAILABLE' | 'QUOTA_EXCEEDED' | 'SETTINGS_ERROR';
|
|
3
3
|
|
|
4
4
|
/** What the page can do. `supports` answers from the app's handshake; a browser tab has none. */
|
|
5
|
-
export declare type Capability = 'finish' | 'title' | 'haptic' | 'clipboard' | 'share' | 'openUrl' | 'userKey' | 'back' | 'closeGuard' | 'errorReport' | 'storage';
|
|
5
|
+
export declare type Capability = 'finish' | 'title' | 'haptic' | 'clipboard' | 'share' | 'openUrl' | 'userKey' | 'back' | 'closeGuard' | 'errorReport' | 'storage' | 'permissions';
|
|
6
6
|
|
|
7
7
|
export declare interface ChaticApps {
|
|
8
8
|
/** `true` inside the app; `false` in a browser tab, where the bridge calls fall back or fail. */
|
|
@@ -47,6 +47,10 @@ export declare interface ChaticApps {
|
|
|
47
47
|
* every close through. Use it only for something the user would lose.
|
|
48
48
|
*/
|
|
49
49
|
onCloseRequest(handler: ((trigger: CloseTrigger) => boolean | Promise<boolean>) | null): void;
|
|
50
|
+
/** The camera and the microphone. */
|
|
51
|
+
readonly permissions: ChaticAppsPermissions;
|
|
52
|
+
/** Opens the app's page in the OS settings, where a `blocked` permission can be turned back on. */
|
|
53
|
+
openSettings(): Promise<void>;
|
|
50
54
|
/** Hears a theme or language change while the page shows; returns the function that stops it. */
|
|
51
55
|
onContextChange(listener: (context: ChaticAppsContext) => void): () => void;
|
|
52
56
|
/**
|
|
@@ -98,6 +102,23 @@ export declare class ChaticAppsError extends Error {
|
|
|
98
102
|
*/
|
|
99
103
|
export declare type ChaticAppsErrorCode = AppErrorCode | 'NOT_SUPPORTED' | 'TIMEOUT' | 'UNSUPPORTED_PROTOCOL' | 'NOT_IN_APP' | 'DISCONNECTED' | (string & {});
|
|
100
104
|
|
|
105
|
+
/**
|
|
106
|
+
* The camera and the microphone. A page captures with the browser's own `getUserMedia`; the OS asks the
|
|
107
|
+
* user the first time, under the app's name. These calls let a page learn where it stands and ask when
|
|
108
|
+
* the user reaches for the feature, rather than meet the prompt in the middle of a capture.
|
|
109
|
+
*/
|
|
110
|
+
export declare interface ChaticAppsPermissions {
|
|
111
|
+
/** Where the app stands with the OS for `kind`, showing the user nothing. */
|
|
112
|
+
query(kind: DeviceKind): Promise<PermissionState_2>;
|
|
113
|
+
/**
|
|
114
|
+
* Asks the OS for `kind` and resolves once the user has answered: `granted`, `blocked` (it will not
|
|
115
|
+
* ask again; `openSettings()`), or `prompt` when the user refused this time and it may be asked
|
|
116
|
+
* again. Ask when the user reaches for the feature, not at start: an unexplained question is the
|
|
117
|
+
* one most often refused.
|
|
118
|
+
*/
|
|
119
|
+
request(kind: DeviceKind): Promise<PermissionState_2>;
|
|
120
|
+
}
|
|
121
|
+
|
|
101
122
|
/**
|
|
102
123
|
* This page's own storage in the app: kept on the device for the page's origin, shared by no other
|
|
103
124
|
* page, and kept when the user signs out. Values are JSON. At most 1,048,576 characters in all — keys
|
|
@@ -150,6 +171,9 @@ export declare interface ConnectOptions {
|
|
|
150
171
|
reportErrors?: boolean;
|
|
151
172
|
}
|
|
152
173
|
|
|
174
|
+
/** A device a page captures from with `getUserMedia`. */
|
|
175
|
+
export declare type DeviceKind = 'camera' | 'microphone';
|
|
176
|
+
|
|
153
177
|
/** The feel of a haptic: a tick, a tap, or the outcome of something the user did. */
|
|
154
178
|
export declare type HapticKind = 'selection' | 'impact' | 'success' | 'warning' | 'error';
|
|
155
179
|
|
|
@@ -159,8 +183,17 @@ export declare const isInApp: () => boolean;
|
|
|
159
183
|
/** What a link hands the page it opens, as its query: `{ level: 3 }` opens the page at `/?level=3`. */
|
|
160
184
|
export declare type LinkParams = Readonly<Record<string, string | number | boolean>>;
|
|
161
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Where the app stands with the OS for one device:
|
|
188
|
+
* - `granted` — `getUserMedia` for it asks nothing further;
|
|
189
|
+
* - `prompt` — the OS has not been asked, or was refused and may be asked again;
|
|
190
|
+
* - `blocked` — only the OS settings can change it (`openSettings()`).
|
|
191
|
+
*/
|
|
192
|
+
declare type PermissionState_2 = 'granted' | 'prompt' | 'blocked';
|
|
193
|
+
export { PermissionState_2 as PermissionState }
|
|
194
|
+
|
|
162
195
|
/** The bridge version this library is written against, sent as each request's `version`. */
|
|
163
|
-
export declare const SDK_BRIDGE_VERSION = "2.
|
|
196
|
+
export declare const SDK_BRIDGE_VERSION = "2.20.0";
|
|
164
197
|
|
|
165
198
|
export declare interface ShareContent {
|
|
166
199
|
title?: string;
|
package/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/** Every code the app itself can fail a call with. */
|
|
2
|
-
export declare type AppErrorCode = 'NOT_FOUND' | 'INTERNAL_ERROR' | 'TOO_LARGE' | 'INVALID' | 'INVALID_KIND' | 'NOT_OPEN' | 'LINK_NOT_ALLOWED' | 'OPEN_URL_FAILED' | 'CLIPBOARD_WRITE_ERROR' | 'SHARE_ERROR' | 'UNAVAILABLE' | 'QUOTA_EXCEEDED';
|
|
2
|
+
export declare type AppErrorCode = 'NOT_FOUND' | 'INTERNAL_ERROR' | 'TOO_LARGE' | 'INVALID' | 'INVALID_KIND' | 'NOT_OPEN' | 'LINK_NOT_ALLOWED' | 'OPEN_URL_FAILED' | 'CLIPBOARD_WRITE_ERROR' | 'SHARE_ERROR' | 'UNAVAILABLE' | 'QUOTA_EXCEEDED' | 'SETTINGS_ERROR';
|
|
3
3
|
|
|
4
4
|
/** What the page can do. `supports` answers from the app's handshake; a browser tab has none. */
|
|
5
|
-
export declare type Capability = 'finish' | 'title' | 'haptic' | 'clipboard' | 'share' | 'openUrl' | 'userKey' | 'back' | 'closeGuard' | 'errorReport' | 'storage';
|
|
5
|
+
export declare type Capability = 'finish' | 'title' | 'haptic' | 'clipboard' | 'share' | 'openUrl' | 'userKey' | 'back' | 'closeGuard' | 'errorReport' | 'storage' | 'permissions';
|
|
6
6
|
|
|
7
7
|
export declare interface ChaticApps {
|
|
8
8
|
/** `true` inside the app; `false` in a browser tab, where the bridge calls fall back or fail. */
|
|
@@ -47,6 +47,10 @@ export declare interface ChaticApps {
|
|
|
47
47
|
* every close through. Use it only for something the user would lose.
|
|
48
48
|
*/
|
|
49
49
|
onCloseRequest(handler: ((trigger: CloseTrigger) => boolean | Promise<boolean>) | null): void;
|
|
50
|
+
/** The camera and the microphone. */
|
|
51
|
+
readonly permissions: ChaticAppsPermissions;
|
|
52
|
+
/** Opens the app's page in the OS settings, where a `blocked` permission can be turned back on. */
|
|
53
|
+
openSettings(): Promise<void>;
|
|
50
54
|
/** Hears a theme or language change while the page shows; returns the function that stops it. */
|
|
51
55
|
onContextChange(listener: (context: ChaticAppsContext) => void): () => void;
|
|
52
56
|
/**
|
|
@@ -98,6 +102,23 @@ export declare class ChaticAppsError extends Error {
|
|
|
98
102
|
*/
|
|
99
103
|
export declare type ChaticAppsErrorCode = AppErrorCode | 'NOT_SUPPORTED' | 'TIMEOUT' | 'UNSUPPORTED_PROTOCOL' | 'NOT_IN_APP' | 'DISCONNECTED' | (string & {});
|
|
100
104
|
|
|
105
|
+
/**
|
|
106
|
+
* The camera and the microphone. A page captures with the browser's own `getUserMedia`; the OS asks the
|
|
107
|
+
* user the first time, under the app's name. These calls let a page learn where it stands and ask when
|
|
108
|
+
* the user reaches for the feature, rather than meet the prompt in the middle of a capture.
|
|
109
|
+
*/
|
|
110
|
+
export declare interface ChaticAppsPermissions {
|
|
111
|
+
/** Where the app stands with the OS for `kind`, showing the user nothing. */
|
|
112
|
+
query(kind: DeviceKind): Promise<PermissionState_2>;
|
|
113
|
+
/**
|
|
114
|
+
* Asks the OS for `kind` and resolves once the user has answered: `granted`, `blocked` (it will not
|
|
115
|
+
* ask again; `openSettings()`), or `prompt` when the user refused this time and it may be asked
|
|
116
|
+
* again. Ask when the user reaches for the feature, not at start: an unexplained question is the
|
|
117
|
+
* one most often refused.
|
|
118
|
+
*/
|
|
119
|
+
request(kind: DeviceKind): Promise<PermissionState_2>;
|
|
120
|
+
}
|
|
121
|
+
|
|
101
122
|
/**
|
|
102
123
|
* This page's own storage in the app: kept on the device for the page's origin, shared by no other
|
|
103
124
|
* page, and kept when the user signs out. Values are JSON. At most 1,048,576 characters in all — keys
|
|
@@ -150,6 +171,9 @@ export declare interface ConnectOptions {
|
|
|
150
171
|
reportErrors?: boolean;
|
|
151
172
|
}
|
|
152
173
|
|
|
174
|
+
/** A device a page captures from with `getUserMedia`. */
|
|
175
|
+
export declare type DeviceKind = 'camera' | 'microphone';
|
|
176
|
+
|
|
153
177
|
/** The feel of a haptic: a tick, a tap, or the outcome of something the user did. */
|
|
154
178
|
export declare type HapticKind = 'selection' | 'impact' | 'success' | 'warning' | 'error';
|
|
155
179
|
|
|
@@ -159,8 +183,17 @@ export declare const isInApp: () => boolean;
|
|
|
159
183
|
/** What a link hands the page it opens, as its query: `{ level: 3 }` opens the page at `/?level=3`. */
|
|
160
184
|
export declare type LinkParams = Readonly<Record<string, string | number | boolean>>;
|
|
161
185
|
|
|
186
|
+
/**
|
|
187
|
+
* Where the app stands with the OS for one device:
|
|
188
|
+
* - `granted` — `getUserMedia` for it asks nothing further;
|
|
189
|
+
* - `prompt` — the OS has not been asked, or was refused and may be asked again;
|
|
190
|
+
* - `blocked` — only the OS settings can change it (`openSettings()`).
|
|
191
|
+
*/
|
|
192
|
+
declare type PermissionState_2 = 'granted' | 'prompt' | 'blocked';
|
|
193
|
+
export { PermissionState_2 as PermissionState }
|
|
194
|
+
|
|
162
195
|
/** The bridge version this library is written against, sent as each request's `version`. */
|
|
163
|
-
export declare const SDK_BRIDGE_VERSION = "2.
|
|
196
|
+
export declare const SDK_BRIDGE_VERSION = "2.20.0";
|
|
164
197
|
|
|
165
198
|
export declare interface ShareContent {
|
|
166
199
|
title?: string;
|
package/index.js
CHANGED
|
@@ -1,80 +1,80 @@
|
|
|
1
|
-
import { C as
|
|
2
|
-
const
|
|
1
|
+
import { C as u, b as I, s as k } from "./storage-BuvWwLx2.js";
|
|
2
|
+
const V = "2.20.0", x = 1e4, N = 2e3;
|
|
3
3
|
let W = 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
let
|
|
7
|
-
const v = (a,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
},
|
|
11
|
-
},
|
|
12
|
-
for (const [a,
|
|
13
|
-
|
|
4
|
+
const q = () => `cas-${Date.now().toString(36)}-${(++W).toString(36)}`, R = () => typeof document < "u" && document.visibilityState === "hidden", U = (r, t = x) => {
|
|
5
|
+
const n = /* @__PURE__ */ new Map(), s = /* @__PURE__ */ new Map();
|
|
6
|
+
let h = !1;
|
|
7
|
+
const v = (a, o) => {
|
|
8
|
+
o.remaining === null || R() || (o.startedAt = Date.now(), o.timer = setTimeout(() => {
|
|
9
|
+
n.delete(a), o.reject(new u("TIMEOUT", "The app did not answer in time."));
|
|
10
|
+
}, o.remaining));
|
|
11
|
+
}, w = () => {
|
|
12
|
+
for (const [a, o] of n)
|
|
13
|
+
o.remaining !== null && (R() ? (o.timer && clearTimeout(o.timer), o.timer = null, o.remaining = Math.max(0, o.remaining - (Date.now() - o.startedAt))) : o.timer || (o.remaining = Math.max(o.remaining, N), v(a, o)));
|
|
14
14
|
};
|
|
15
|
-
typeof document < "u" && document.addEventListener("visibilitychange",
|
|
16
|
-
const
|
|
17
|
-
let
|
|
15
|
+
typeof document < "u" && document.addEventListener("visibilitychange", w);
|
|
16
|
+
const g = r.listen((a) => {
|
|
17
|
+
let o;
|
|
18
18
|
try {
|
|
19
|
-
|
|
19
|
+
o = JSON.parse(a);
|
|
20
20
|
} catch {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
if (!
|
|
24
|
-
const d = typeof
|
|
25
|
-
if (d && typeof
|
|
26
|
-
if (
|
|
27
|
-
d.resolve(
|
|
23
|
+
if (!o || typeof o != "object" || typeof o.type != "string") return;
|
|
24
|
+
const d = typeof o.refId == "string" ? n.get(o.refId) : void 0;
|
|
25
|
+
if (d && typeof o.refId == "string") {
|
|
26
|
+
if (n.delete(o.refId), d.timer && clearTimeout(d.timer), o.success === !0)
|
|
27
|
+
d.resolve(o.data);
|
|
28
28
|
else {
|
|
29
|
-
const
|
|
30
|
-
d.reject(new
|
|
29
|
+
const p = typeof o.error?.code == "string" ? o.error.code : "INTERNAL_ERROR", m = typeof o.error?.message == "string" ? o.error.message : p;
|
|
30
|
+
d.reject(new u(p, m));
|
|
31
31
|
}
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
|
-
|
|
34
|
+
s.get(o.type)?.forEach((p) => {
|
|
35
35
|
try {
|
|
36
|
-
|
|
36
|
+
p(o.data);
|
|
37
37
|
} catch {
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
40
|
});
|
|
41
41
|
return {
|
|
42
|
-
request: (a,
|
|
43
|
-
if (
|
|
44
|
-
m(new
|
|
42
|
+
request: (a, o = {}, d = {}) => new Promise((p, m) => {
|
|
43
|
+
if (h) {
|
|
44
|
+
m(new u("DISCONNECTED", "The connection to the app was closed."));
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
|
-
const
|
|
48
|
-
resolve:
|
|
47
|
+
const l = q(), e = d.timeoutMs === void 0 ? t : d.timeoutMs, i = {
|
|
48
|
+
resolve: p,
|
|
49
49
|
reject: m,
|
|
50
50
|
remaining: e,
|
|
51
51
|
startedAt: Date.now(),
|
|
52
52
|
timer: null
|
|
53
53
|
};
|
|
54
|
-
|
|
54
|
+
n.set(l, i), v(l, i), r.send(JSON.stringify({ type: a, data: o, refId: l, version: V }));
|
|
55
55
|
}),
|
|
56
|
-
onPush: (a,
|
|
57
|
-
let d =
|
|
58
|
-
return d ||
|
|
56
|
+
onPush: (a, o) => {
|
|
57
|
+
let d = s.get(a);
|
|
58
|
+
return d || s.set(a, d = /* @__PURE__ */ new Set()), d.add(o), () => d?.delete(o);
|
|
59
59
|
},
|
|
60
60
|
close: () => {
|
|
61
|
-
if (!
|
|
62
|
-
|
|
63
|
-
for (const a of
|
|
64
|
-
a.timer && clearTimeout(a.timer), a.reject(new
|
|
65
|
-
|
|
61
|
+
if (!h) {
|
|
62
|
+
h = !0, g(), typeof document < "u" && document.removeEventListener("visibilitychange", w);
|
|
63
|
+
for (const a of n.values())
|
|
64
|
+
a.timer && clearTimeout(a.timer), a.reject(new u("DISCONNECTED", "The connection to the app was closed."));
|
|
65
|
+
n.clear(), s.clear();
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
|
-
},
|
|
70
|
-
send: (
|
|
71
|
-
listen: (
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
typeof
|
|
69
|
+
}, M = () => typeof window < "u" && !!window.ReactNativeWebView, _ = () => ({
|
|
70
|
+
send: (r) => window.ReactNativeWebView?.postMessage(r),
|
|
71
|
+
listen: (r) => {
|
|
72
|
+
const t = (n) => {
|
|
73
|
+
const s = n.data;
|
|
74
|
+
typeof s == "string" && r(s);
|
|
75
75
|
};
|
|
76
|
-
return window.addEventListener("message",
|
|
77
|
-
window.removeEventListener("message",
|
|
76
|
+
return window.addEventListener("message", t), document.addEventListener("message", t), () => {
|
|
77
|
+
window.removeEventListener("message", t), document.removeEventListener("message", t);
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
}), b = {
|
|
@@ -88,111 +88,115 @@ const M = () => `cas-${Date.now().toString(36)}-${(++W).toString(36)}`, E = () =
|
|
|
88
88
|
back: "SetCanGoBack",
|
|
89
89
|
closeGuard: "SetSubWebViewCloseGuard",
|
|
90
90
|
errorReport: "ReportSubWebViewError",
|
|
91
|
-
storage: "GetSubWebViewStorage"
|
|
92
|
-
|
|
91
|
+
storage: "GetSubWebViewStorage",
|
|
92
|
+
permissions: "RequestSubWebViewPermission"
|
|
93
|
+
}, O = 1, D = 1e3, $ = 4e3, A = () => typeof window < "u" && window.ChaticSubWebView || null, j = () => new Promise((r) => {
|
|
93
94
|
if (typeof document > "u" || document.readyState === "complete") {
|
|
94
|
-
|
|
95
|
+
r(A());
|
|
95
96
|
return;
|
|
96
97
|
}
|
|
97
|
-
window.addEventListener("load", () =>
|
|
98
|
-
}), G = (
|
|
99
|
-
let
|
|
98
|
+
window.addEventListener("load", () => r(A()), { once: !0 });
|
|
99
|
+
}), G = (r) => {
|
|
100
|
+
let t, n;
|
|
100
101
|
try {
|
|
101
|
-
|
|
102
|
+
r instanceof Error ? (t = r.message || r.name, n = typeof r.stack == "string" ? r.stack : void 0) : typeof r == "string" ? t = r : t = `A non-Error value was thrown: ${Object.prototype.toString.call(r)}`;
|
|
102
103
|
} catch {
|
|
103
|
-
|
|
104
|
+
t = "An error that could not be read was thrown";
|
|
104
105
|
}
|
|
105
106
|
return {
|
|
106
|
-
message: (
|
|
107
|
-
...
|
|
107
|
+
message: (t || "Unknown error").slice(0, D),
|
|
108
|
+
...n ? { stack: n.slice(0, $) } : {}
|
|
108
109
|
};
|
|
109
|
-
},
|
|
110
|
-
if (
|
|
111
|
-
throw new
|
|
112
|
-
const
|
|
113
|
-
for (const [
|
|
114
|
-
const
|
|
115
|
-
return
|
|
116
|
-
},
|
|
110
|
+
}, E = (r) => new u("NOT_SUPPORTED", `${r} is not available here: an older app, or not granted.`), S = (r) => Promise.reject(new u("NOT_IN_APP", `${r} needs the app; this page is in a browser.`)), L = (r = {}) => {
|
|
111
|
+
if (r === null || typeof r != "object" || Array.isArray(r))
|
|
112
|
+
throw new u("INVALID", "link() takes an object of query params, such as { level: 3 }.");
|
|
113
|
+
const t = new URLSearchParams();
|
|
114
|
+
for (const [s, h] of Object.entries(r)) t.append(s, String(h));
|
|
115
|
+
const n = t.toString();
|
|
116
|
+
return n ? `?${n}` : "";
|
|
117
|
+
}, P = (r) => typeof location > "u" ? `/${r}` : `${location.origin}/${r}`;
|
|
117
118
|
let y = null;
|
|
118
|
-
const
|
|
119
|
-
throw y = null,
|
|
120
|
-
})), y),
|
|
121
|
-
const
|
|
119
|
+
const Q = (r = {}) => (y || (y = (M() ? B(_(), r) : Promise.resolve(H())).catch((t) => {
|
|
120
|
+
throw y = null, t;
|
|
121
|
+
})), y), B = async (r, t = {}) => {
|
|
122
|
+
const n = U(r, t.timeoutMs);
|
|
122
123
|
try {
|
|
123
|
-
const
|
|
124
|
-
return
|
|
125
|
-
} catch (
|
|
126
|
-
throw
|
|
124
|
+
const s = await C(n, !0);
|
|
125
|
+
return K(n, s, t);
|
|
126
|
+
} catch (s) {
|
|
127
|
+
throw n.close(), s;
|
|
127
128
|
}
|
|
128
|
-
},
|
|
129
|
-
const
|
|
130
|
-
if (
|
|
131
|
-
throw new
|
|
129
|
+
}, C = async (r, t) => {
|
|
130
|
+
const n = await r.request("WebAppReady", {}), s = n.subWebView ?? (t ? await j() : null);
|
|
131
|
+
if (s && s.protocolVersion !== O)
|
|
132
|
+
throw new u(
|
|
132
133
|
"UNSUPPORTED_PROTOCOL",
|
|
133
|
-
`The app speaks protocol ${String(
|
|
134
|
+
`The app speaks protocol ${String(s.protocolVersion)}; this library speaks ${O}.`
|
|
134
135
|
);
|
|
135
136
|
return {
|
|
136
|
-
supported: new Set(Array.isArray(
|
|
137
|
-
context:
|
|
137
|
+
supported: new Set(Array.isArray(n.supportedWebMessages) ? n.supportedWebMessages : []),
|
|
138
|
+
context: s
|
|
138
139
|
};
|
|
139
|
-
},
|
|
140
|
-
let
|
|
141
|
-
const v = /* @__PURE__ */ new Set(),
|
|
140
|
+
}, K = (r, t, n) => {
|
|
141
|
+
let s = null, h = null;
|
|
142
|
+
const v = /* @__PURE__ */ new Set(), w = [], g = (e) => t.supported.has(b[e]), a = (e, i, c, f) => {
|
|
143
|
+
const T = f ?? b[e];
|
|
144
|
+
return t.supported.has(T) ? r.request(T, i, c === void 0 ? {} : { timeoutMs: c }) : Promise.reject(E(e));
|
|
145
|
+
}, o = (e, i) => g("storage") ? r.request(e, i) : Promise.reject(E("storage")), d = (e) => v.forEach((i) => {
|
|
142
146
|
try {
|
|
143
|
-
|
|
147
|
+
i(e);
|
|
144
148
|
} catch (c) {
|
|
145
|
-
|
|
149
|
+
l.reportError(c);
|
|
146
150
|
}
|
|
147
|
-
}),
|
|
148
|
-
|
|
149
|
-
}),
|
|
151
|
+
}), p = () => {
|
|
152
|
+
g("back") && r.request(b.back, { canGoBack: !!s }).catch(() => {
|
|
153
|
+
}), g("closeGuard") && r.request(b.closeGuard, { enabled: !!h }).catch(() => {
|
|
150
154
|
});
|
|
151
155
|
}, m = (e) => {
|
|
152
156
|
if (!e.persisted) {
|
|
153
|
-
|
|
157
|
+
p();
|
|
154
158
|
return;
|
|
155
159
|
}
|
|
156
|
-
|
|
157
|
-
const c =
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
160
|
+
C(r, !1).then((i) => {
|
|
161
|
+
const c = t.context;
|
|
162
|
+
t.supported = i.supported, i.context && (t.context = i.context), p();
|
|
163
|
+
const f = t.context;
|
|
164
|
+
f && (f.theme !== c?.theme || f.locale !== c?.locale) && d(f);
|
|
161
165
|
}).catch(() => {
|
|
162
166
|
});
|
|
163
167
|
};
|
|
164
|
-
typeof window < "u" && (window.addEventListener("pageshow", m),
|
|
165
|
-
|
|
168
|
+
typeof window < "u" && (window.addEventListener("pageshow", m), w.push(() => window.removeEventListener("pageshow", m))), w.push(
|
|
169
|
+
r.onPush("OnBackPressed", () => {
|
|
166
170
|
try {
|
|
167
|
-
|
|
171
|
+
s?.();
|
|
168
172
|
} catch (e) {
|
|
169
|
-
|
|
173
|
+
l.reportError(e);
|
|
170
174
|
}
|
|
171
175
|
})
|
|
172
|
-
),
|
|
173
|
-
|
|
174
|
-
const
|
|
175
|
-
if (!
|
|
176
|
+
), w.push(
|
|
177
|
+
r.onPush("OnSubWebViewCloseRequested", (e) => {
|
|
178
|
+
const i = h;
|
|
179
|
+
if (!i) return;
|
|
176
180
|
const c = e?.trigger ?? "control";
|
|
177
|
-
Promise.resolve().then(() =>
|
|
181
|
+
Promise.resolve().then(() => i(c)).then((f) => f ? l.finish() : void 0).catch((f) => l.reportError(f));
|
|
178
182
|
})
|
|
179
|
-
),
|
|
180
|
-
|
|
181
|
-
|
|
183
|
+
), w.push(
|
|
184
|
+
r.onPush("OnSubWebViewContextChanged", (e) => {
|
|
185
|
+
t.context && (t.context = { ...t.context, ...e }, d(t.context));
|
|
182
186
|
})
|
|
183
187
|
);
|
|
184
|
-
const
|
|
188
|
+
const l = {
|
|
185
189
|
inApp: !0,
|
|
186
190
|
get context() {
|
|
187
|
-
return
|
|
191
|
+
return t.context;
|
|
188
192
|
},
|
|
189
|
-
supports:
|
|
193
|
+
supports: g,
|
|
190
194
|
finish: (e) => a("finish", e === void 0 ? {} : { result: e }).then(() => {
|
|
191
195
|
}),
|
|
192
196
|
setTitle: (e) => a("title", { title: e }).then(() => {
|
|
193
197
|
}),
|
|
194
198
|
haptic: (e) => {
|
|
195
|
-
|
|
199
|
+
g("haptic") && r.request(b.haptic, { kind: e }).catch(() => {
|
|
196
200
|
});
|
|
197
201
|
},
|
|
198
202
|
copy: (e) => a("clipboard", { text: e }).then(() => {
|
|
@@ -200,89 +204,128 @@ const H = (t = {}) => (y || (y = (U() ? $(_(), t) : Promise.resolve(K())).catch(
|
|
|
200
204
|
openUrl: (e) => a("openUrl", { url: e }).then(() => {
|
|
201
205
|
}),
|
|
202
206
|
share: (e) => a("share", e, null).then(
|
|
203
|
-
(
|
|
207
|
+
(i) => i?.action === "dismissedAction" ? "dismissed" : "shared"
|
|
204
208
|
),
|
|
205
209
|
getUserKey: () => a("userKey").then((e) => e.key),
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
210
|
+
permissions: {
|
|
211
|
+
query: (e) => a("permissions", { kind: e }, void 0, "GetSubWebViewPermission").then(
|
|
212
|
+
(i) => i.state
|
|
213
|
+
),
|
|
214
|
+
// Waits for the user, however long they take.
|
|
215
|
+
request: (e) => a("permissions", { kind: e }, null).then((i) => i.state)
|
|
216
|
+
},
|
|
217
|
+
openSettings: () => a("permissions", {}, void 0, "OpenSettings").then(() => {
|
|
218
|
+
}),
|
|
219
|
+
reportError: (e) => g("errorReport") ? r.request(b.errorReport, G(e)).then((i) => !!i?.recorded).catch(() => !1) : Promise.resolve(!1),
|
|
220
|
+
storage: k({
|
|
221
|
+
get: (e) => o("GetSubWebViewStorage", { keys: e }),
|
|
222
|
+
set: (e) => o("SetSubWebViewStorage", { entries: e }),
|
|
223
|
+
remove: (e) => o("RemoveSubWebViewStorage", { keys: e }),
|
|
224
|
+
clear: () => o("ClearSubWebViewStorage", {}),
|
|
225
|
+
info: () => o("GetSubWebViewStorageInfo", {})
|
|
213
226
|
}),
|
|
214
227
|
onBack: (e) => {
|
|
215
|
-
|
|
228
|
+
s = e, p();
|
|
216
229
|
},
|
|
217
230
|
onCloseRequest: (e) => {
|
|
218
|
-
|
|
231
|
+
h = e, p();
|
|
219
232
|
},
|
|
220
233
|
onContextChange: (e) => (v.add(e), () => v.delete(e)),
|
|
221
234
|
link: (e) => {
|
|
222
|
-
const
|
|
223
|
-
return c ? `${c}${
|
|
235
|
+
const i = L(e), c = t.context?.appLink;
|
|
236
|
+
return c ? `${c}${i}` : P(i);
|
|
224
237
|
},
|
|
225
238
|
disconnect: () => {
|
|
226
|
-
|
|
239
|
+
w.forEach((e) => e()), r.close(), y = null;
|
|
227
240
|
}
|
|
228
241
|
};
|
|
229
|
-
if (
|
|
242
|
+
if (n.reportErrors !== !1 && typeof window < "u") {
|
|
230
243
|
const e = (c) => {
|
|
231
|
-
|
|
232
|
-
},
|
|
233
|
-
|
|
244
|
+
l.reportError(c.error ?? c.message);
|
|
245
|
+
}, i = (c) => {
|
|
246
|
+
l.reportError(c.reason);
|
|
234
247
|
};
|
|
235
|
-
window.addEventListener("error", e), window.addEventListener("unhandledrejection",
|
|
236
|
-
window.removeEventListener("error", e), window.removeEventListener("unhandledrejection",
|
|
248
|
+
window.addEventListener("error", e), window.addEventListener("unhandledrejection", i), w.push(() => {
|
|
249
|
+
window.removeEventListener("error", e), window.removeEventListener("unhandledrejection", i);
|
|
237
250
|
});
|
|
238
251
|
}
|
|
239
|
-
return
|
|
240
|
-
},
|
|
241
|
-
const
|
|
252
|
+
return l;
|
|
253
|
+
}, F = () => {
|
|
254
|
+
const r = async (t) => {
|
|
255
|
+
try {
|
|
256
|
+
const n = await navigator.permissions.query({ name: t });
|
|
257
|
+
return n.state === "granted" ? "granted" : n.state === "denied" ? "blocked" : "prompt";
|
|
258
|
+
} catch {
|
|
259
|
+
return "prompt";
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
return {
|
|
263
|
+
query: r,
|
|
264
|
+
request: async (t) => {
|
|
265
|
+
if (typeof navigator > "u" || !navigator.mediaDevices?.getUserMedia)
|
|
266
|
+
throw new u("NOT_SUPPORTED", `This browser cannot capture the ${t}.`);
|
|
267
|
+
try {
|
|
268
|
+
return (await navigator.mediaDevices.getUserMedia(
|
|
269
|
+
t === "camera" ? { video: !0 } : { audio: !0 }
|
|
270
|
+
)).getTracks().forEach((s) => s.stop()), "granted";
|
|
271
|
+
} catch (n) {
|
|
272
|
+
const s = n?.name;
|
|
273
|
+
if (s === "NotFoundError" || s === "OverconstrainedError")
|
|
274
|
+
throw new u("UNAVAILABLE", `This device has no ${t}.`);
|
|
275
|
+
if (s === "NotReadableError" || s === "SecurityError" || s === "TypeError" || s === "AbortError")
|
|
276
|
+
throw new u("INTERNAL_ERROR", `The ${t} could not be used: ${s}.`);
|
|
277
|
+
return await r(t) === "blocked" ? "blocked" : "prompt";
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
}, H = () => {
|
|
282
|
+
const r = (t) => typeof navigator < "u" && navigator.clipboard ? navigator.clipboard.writeText(t) : Promise.reject(E("The clipboard"));
|
|
242
283
|
return {
|
|
243
284
|
inApp: !1,
|
|
244
285
|
context: null,
|
|
245
286
|
supports: () => !1,
|
|
246
|
-
finish: () =>
|
|
247
|
-
setTitle: async (
|
|
248
|
-
typeof document < "u" && (document.title =
|
|
287
|
+
finish: () => S("Finishing the view"),
|
|
288
|
+
setTitle: async (t) => {
|
|
289
|
+
typeof document < "u" && (document.title = t);
|
|
249
290
|
},
|
|
250
291
|
haptic: () => {
|
|
251
292
|
typeof navigator < "u" && navigator.vibrate?.(10);
|
|
252
293
|
},
|
|
253
|
-
copy:
|
|
254
|
-
openUrl: async (
|
|
255
|
-
typeof window < "u" && window.open(
|
|
294
|
+
copy: r,
|
|
295
|
+
openUrl: async (t) => {
|
|
296
|
+
typeof window < "u" && window.open(t, "_blank", "noopener,noreferrer");
|
|
256
297
|
},
|
|
257
|
-
share: async (
|
|
258
|
-
const
|
|
259
|
-
if (!
|
|
298
|
+
share: async (t) => {
|
|
299
|
+
const n = [t.message, t.url].filter(Boolean).join(" ");
|
|
300
|
+
if (!n) throw new u("INVALID", "Share a message or a url.");
|
|
260
301
|
if (typeof navigator < "u" && navigator.share)
|
|
261
302
|
try {
|
|
262
|
-
return await navigator.share(
|
|
263
|
-
} catch (
|
|
264
|
-
if (
|
|
303
|
+
return await navigator.share(t), "shared";
|
|
304
|
+
} catch (s) {
|
|
305
|
+
if (s?.name === "AbortError") return "dismissed";
|
|
265
306
|
}
|
|
266
|
-
return await
|
|
307
|
+
return await r(n), "copied";
|
|
267
308
|
},
|
|
268
|
-
getUserKey: () =>
|
|
269
|
-
|
|
270
|
-
|
|
309
|
+
getUserKey: () => S("A user key"),
|
|
310
|
+
permissions: F(),
|
|
311
|
+
openSettings: () => S("Opening the settings"),
|
|
312
|
+
reportError: async (t) => (console.error(t), !1),
|
|
313
|
+
storage: I(),
|
|
271
314
|
onBack: () => {
|
|
272
315
|
},
|
|
273
316
|
onCloseRequest: () => {
|
|
274
317
|
},
|
|
275
318
|
onContextChange: () => () => {
|
|
276
319
|
},
|
|
277
|
-
link: (
|
|
320
|
+
link: (t) => P(L(t)),
|
|
278
321
|
disconnect: () => {
|
|
279
322
|
y = null;
|
|
280
323
|
}
|
|
281
324
|
};
|
|
282
325
|
};
|
|
283
326
|
export {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
327
|
+
u as ChaticAppsError,
|
|
328
|
+
V as SDK_BRIDGE_VERSION,
|
|
329
|
+
Q as connect,
|
|
330
|
+
M as isInApp
|
|
288
331
|
};
|