@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/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.19.0";
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.19.0";
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 w, b as L, s as I } from "./storage-BuvWwLx2.js";
2
- const P = "2.19.0", x = 1e4, V = 2e3;
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 M = () => `cas-${Date.now().toString(36)}-${(++W).toString(36)}`, E = () => typeof document < "u" && document.visibilityState === "hidden", N = (t, o = x) => {
5
- const r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
6
- let l = !1;
7
- const v = (a, n) => {
8
- n.remaining === null || E() || (n.startedAt = Date.now(), n.timer = setTimeout(() => {
9
- r.delete(a), n.reject(new w("TIMEOUT", "The app did not answer in time."));
10
- }, n.remaining));
11
- }, f = () => {
12
- for (const [a, n] of r)
13
- n.remaining !== null && (E() ? (n.timer && clearTimeout(n.timer), n.timer = null, n.remaining = Math.max(0, n.remaining - (Date.now() - n.startedAt))) : n.timer || (n.remaining = Math.max(n.remaining, V), v(a, n)));
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", f);
16
- const h = t.listen((a) => {
17
- let n;
15
+ typeof document < "u" && document.addEventListener("visibilitychange", w);
16
+ const g = r.listen((a) => {
17
+ let o;
18
18
  try {
19
- n = JSON.parse(a);
19
+ o = JSON.parse(a);
20
20
  } catch {
21
21
  return;
22
22
  }
23
- if (!n || typeof n != "object" || typeof n.type != "string") return;
24
- const d = typeof n.refId == "string" ? r.get(n.refId) : void 0;
25
- if (d && typeof n.refId == "string") {
26
- if (r.delete(n.refId), d.timer && clearTimeout(d.timer), n.success === !0)
27
- d.resolve(n.data);
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 u = typeof n.error?.code == "string" ? n.error.code : "INTERNAL_ERROR", m = typeof n.error?.message == "string" ? n.error.message : u;
30
- d.reject(new w(u, m));
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
- i.get(n.type)?.forEach((u) => {
34
+ s.get(o.type)?.forEach((p) => {
35
35
  try {
36
- u(n.data);
36
+ p(o.data);
37
37
  } catch {
38
38
  }
39
39
  });
40
40
  });
41
41
  return {
42
- request: (a, n = {}, d = {}) => new Promise((u, m) => {
43
- if (l) {
44
- m(new w("DISCONNECTED", "The connection to the app was closed."));
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 p = M(), e = d.timeoutMs === void 0 ? o : d.timeoutMs, s = {
48
- resolve: u,
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
- r.set(p, s), v(p, s), t.send(JSON.stringify({ type: a, data: n, refId: p, version: P }));
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, n) => {
57
- let d = i.get(a);
58
- return d || i.set(a, d = /* @__PURE__ */ new Set()), d.add(n), () => d?.delete(n);
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 (!l) {
62
- l = !0, h(), typeof document < "u" && document.removeEventListener("visibilitychange", f);
63
- for (const a of r.values())
64
- a.timer && clearTimeout(a.timer), a.reject(new w("DISCONNECTED", "The connection to the app was closed."));
65
- r.clear(), i.clear();
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
- }, U = () => typeof window < "u" && !!window.ReactNativeWebView, _ = () => ({
70
- send: (t) => window.ReactNativeWebView?.postMessage(t),
71
- listen: (t) => {
72
- const o = (r) => {
73
- const i = r.data;
74
- typeof i == "string" && t(i);
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", o), document.addEventListener("message", o), () => {
77
- window.removeEventListener("message", o), document.removeEventListener("message", o);
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
- }, T = 1, j = 1e3, q = 4e3, R = () => typeof window < "u" && window.ChaticSubWebView || null, D = () => new Promise((t) => {
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
- t(R());
95
+ r(A());
95
96
  return;
96
97
  }
97
- window.addEventListener("load", () => t(R()), { once: !0 });
98
- }), G = (t) => {
99
- let o, r;
98
+ window.addEventListener("load", () => r(A()), { once: !0 });
99
+ }), G = (r) => {
100
+ let t, n;
100
101
  try {
101
- t instanceof Error ? (o = t.message || t.name, r = typeof t.stack == "string" ? t.stack : void 0) : typeof t == "string" ? o = t : o = `A non-Error value was thrown: ${Object.prototype.toString.call(t)}`;
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
- o = "An error that could not be read was thrown";
104
+ t = "An error that could not be read was thrown";
104
105
  }
105
106
  return {
106
- message: (o || "Unknown error").slice(0, j),
107
- ...r ? { stack: r.slice(0, q) } : {}
107
+ message: (t || "Unknown error").slice(0, D),
108
+ ...n ? { stack: n.slice(0, $) } : {}
108
109
  };
109
- }, S = (t) => new w("NOT_SUPPORTED", `${t} is not available here: an older app, or not granted.`), O = (t) => Promise.reject(new w("NOT_IN_APP", `${t} needs the app; this page is in a browser.`)), C = (t = {}) => {
110
- if (t === null || typeof t != "object" || Array.isArray(t))
111
- throw new w("INVALID", "link() takes an object of query params, such as { level: 3 }.");
112
- const o = new URLSearchParams();
113
- for (const [i, l] of Object.entries(t)) o.append(i, String(l));
114
- const r = o.toString();
115
- return r ? `?${r}` : "";
116
- }, k = (t) => typeof location > "u" ? `/${t}` : `${location.origin}/${t}`;
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 H = (t = {}) => (y || (y = (U() ? $(_(), t) : Promise.resolve(K())).catch((o) => {
119
- throw y = null, o;
120
- })), y), $ = async (t, o = {}) => {
121
- const r = N(t, o.timeoutMs);
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 i = await A(r, !0);
124
- return B(r, i, o);
125
- } catch (i) {
126
- throw r.close(), i;
124
+ const s = await C(n, !0);
125
+ return K(n, s, t);
126
+ } catch (s) {
127
+ throw n.close(), s;
127
128
  }
128
- }, A = async (t, o) => {
129
- const r = await t.request("WebAppReady", {}), i = r.subWebView ?? (o ? await D() : null);
130
- if (i && i.protocolVersion !== T)
131
- throw new w(
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(i.protocolVersion)}; this library speaks ${T}.`
134
+ `The app speaks protocol ${String(s.protocolVersion)}; this library speaks ${O}.`
134
135
  );
135
136
  return {
136
- supported: new Set(Array.isArray(r.supportedWebMessages) ? r.supportedWebMessages : []),
137
- context: i
137
+ supported: new Set(Array.isArray(n.supportedWebMessages) ? n.supportedWebMessages : []),
138
+ context: s
138
139
  };
139
- }, B = (t, o, r) => {
140
- let i = null, l = null;
141
- const v = /* @__PURE__ */ new Set(), f = [], h = (e) => o.supported.has(b[e]), a = (e, s, c) => h(e) ? t.request(b[e], s, c === void 0 ? {} : { timeoutMs: c }) : Promise.reject(S(e)), n = (e, s) => h("storage") ? t.request(e, s) : Promise.reject(S("storage")), d = (e) => v.forEach((s) => {
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
- s(e);
147
+ i(e);
144
148
  } catch (c) {
145
- p.reportError(c);
149
+ l.reportError(c);
146
150
  }
147
- }), u = () => {
148
- h("back") && t.request(b.back, { canGoBack: !!i }).catch(() => {
149
- }), h("closeGuard") && t.request(b.closeGuard, { enabled: !!l }).catch(() => {
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
- u();
157
+ p();
154
158
  return;
155
159
  }
156
- A(t, !1).then((s) => {
157
- const c = o.context;
158
- o.supported = s.supported, s.context && (o.context = s.context), u();
159
- const g = o.context;
160
- g && (g.theme !== c?.theme || g.locale !== c?.locale) && d(g);
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), f.push(() => window.removeEventListener("pageshow", m))), f.push(
165
- t.onPush("OnBackPressed", () => {
168
+ typeof window < "u" && (window.addEventListener("pageshow", m), w.push(() => window.removeEventListener("pageshow", m))), w.push(
169
+ r.onPush("OnBackPressed", () => {
166
170
  try {
167
- i?.();
171
+ s?.();
168
172
  } catch (e) {
169
- p.reportError(e);
173
+ l.reportError(e);
170
174
  }
171
175
  })
172
- ), f.push(
173
- t.onPush("OnSubWebViewCloseRequested", (e) => {
174
- const s = l;
175
- if (!s) return;
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(() => s(c)).then((g) => g ? p.finish() : void 0).catch((g) => p.reportError(g));
181
+ Promise.resolve().then(() => i(c)).then((f) => f ? l.finish() : void 0).catch((f) => l.reportError(f));
178
182
  })
179
- ), f.push(
180
- t.onPush("OnSubWebViewContextChanged", (e) => {
181
- o.context && (o.context = { ...o.context, ...e }, d(o.context));
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 p = {
188
+ const l = {
185
189
  inApp: !0,
186
190
  get context() {
187
- return o.context;
191
+ return t.context;
188
192
  },
189
- supports: h,
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
- h("haptic") && t.request(b.haptic, { kind: e }).catch(() => {
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
- (s) => s?.action === "dismissedAction" ? "dismissed" : "shared"
207
+ (i) => i?.action === "dismissedAction" ? "dismissed" : "shared"
204
208
  ),
205
209
  getUserKey: () => a("userKey").then((e) => e.key),
206
- reportError: (e) => h("errorReport") ? t.request(b.errorReport, G(e)).then((s) => !!s?.recorded).catch(() => !1) : Promise.resolve(!1),
207
- storage: I({
208
- get: (e) => n("GetSubWebViewStorage", { keys: e }),
209
- set: (e) => n("SetSubWebViewStorage", { entries: e }),
210
- remove: (e) => n("RemoveSubWebViewStorage", { keys: e }),
211
- clear: () => n("ClearSubWebViewStorage", {}),
212
- info: () => n("GetSubWebViewStorageInfo", {})
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
- i = e, u();
228
+ s = e, p();
216
229
  },
217
230
  onCloseRequest: (e) => {
218
- l = e, u();
231
+ h = e, p();
219
232
  },
220
233
  onContextChange: (e) => (v.add(e), () => v.delete(e)),
221
234
  link: (e) => {
222
- const s = C(e), c = o.context?.appLink;
223
- return c ? `${c}${s}` : k(s);
235
+ const i = L(e), c = t.context?.appLink;
236
+ return c ? `${c}${i}` : P(i);
224
237
  },
225
238
  disconnect: () => {
226
- f.forEach((e) => e()), t.close(), y = null;
239
+ w.forEach((e) => e()), r.close(), y = null;
227
240
  }
228
241
  };
229
- if (r.reportErrors !== !1 && typeof window < "u") {
242
+ if (n.reportErrors !== !1 && typeof window < "u") {
230
243
  const e = (c) => {
231
- p.reportError(c.error ?? c.message);
232
- }, s = (c) => {
233
- p.reportError(c.reason);
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", s), f.push(() => {
236
- window.removeEventListener("error", e), window.removeEventListener("unhandledrejection", s);
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 p;
240
- }, K = () => {
241
- const t = (o) => typeof navigator < "u" && navigator.clipboard ? navigator.clipboard.writeText(o) : Promise.reject(S("The clipboard"));
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: () => O("Finishing the view"),
247
- setTitle: async (o) => {
248
- typeof document < "u" && (document.title = o);
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: t,
254
- openUrl: async (o) => {
255
- typeof window < "u" && window.open(o, "_blank", "noopener,noreferrer");
294
+ copy: r,
295
+ openUrl: async (t) => {
296
+ typeof window < "u" && window.open(t, "_blank", "noopener,noreferrer");
256
297
  },
257
- share: async (o) => {
258
- const r = [o.message, o.url].filter(Boolean).join(" ");
259
- if (!r) throw new w("INVALID", "Share a message or a url.");
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(o), "shared";
263
- } catch (i) {
264
- if (i?.name === "AbortError") return "dismissed";
303
+ return await navigator.share(t), "shared";
304
+ } catch (s) {
305
+ if (s?.name === "AbortError") return "dismissed";
265
306
  }
266
- return await t(r), "copied";
307
+ return await r(n), "copied";
267
308
  },
268
- getUserKey: () => O("A user key"),
269
- reportError: async (o) => (console.error(o), !1),
270
- storage: L(),
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: (o) => k(C(o)),
320
+ link: (t) => P(L(t)),
278
321
  disconnect: () => {
279
322
  y = null;
280
323
  }
281
324
  };
282
325
  };
283
326
  export {
284
- w as ChaticAppsError,
285
- P as SDK_BRIDGE_VERSION,
286
- H as connect,
287
- U as isInApp
327
+ u as ChaticAppsError,
328
+ V as SDK_BRIDGE_VERSION,
329
+ Q as connect,
330
+ M as isInApp
288
331
  };