@inglorious/web 6.28.0 → 6.29.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.
Files changed (36) hide show
  1. package/README.md +257 -200
  2. package/package.json +49 -37
  3. package/src/device/biometrics/index.js +86 -0
  4. package/src/device/compass/handlers.js +163 -0
  5. package/src/{sensors → device}/element-size/handlers.js +2 -2
  6. package/src/device/geolocation/index.js +120 -0
  7. package/src/device/local-notifications/index.js +133 -0
  8. package/src/{sensors → device}/media-query/handlers.js +2 -2
  9. package/src/{sensors → device}/network-status/handlers.js +1 -1
  10. package/src/{sensors → device}/page-visibility/handlers.js +1 -1
  11. package/src/{native → device}/share/index.js +1 -1
  12. package/src/device/storage/index.js +61 -0
  13. package/src/native/index.js +0 -32
  14. package/types/device/biometrics.d.ts +51 -0
  15. package/types/{sensors → device}/compass.d.ts +0 -2
  16. package/types/{sensors → device}/geolocation.d.ts +25 -7
  17. package/types/device/local-notifications.d.ts +76 -0
  18. package/types/device/storage.d.ts +30 -0
  19. package/types/native/index.d.ts +0 -19
  20. package/src/sensors/compass/handlers.js +0 -209
  21. package/src/sensors/geolocation/handlers.js +0 -168
  22. package/src/sensors/geolocation/index.js +0 -3
  23. /package/src/{sensors → device}/compass/index.js +0 -0
  24. /package/src/{sensors → device}/element-size/index.js +0 -0
  25. /package/src/{native → device}/haptics/index.js +0 -0
  26. /package/src/{keyboard → device/keyboard}/index.js +0 -0
  27. /package/src/{sensors → device}/media-query/index.js +0 -0
  28. /package/src/{sensors → device}/network-status/index.js +0 -0
  29. /package/src/{sensors → device}/page-visibility/index.js +0 -0
  30. /package/types/{sensors → device}/element-size.d.ts +0 -0
  31. /package/types/{native → device}/haptics.d.ts +0 -0
  32. /package/types/{keyboard.d.ts → device/keyboard.d.ts} +0 -0
  33. /package/types/{sensors → device}/media-query.d.ts +0 -0
  34. /package/types/{sensors → device}/network-status.d.ts +0 -0
  35. /package/types/{sensors → device}/page-visibility.d.ts +0 -0
  36. /package/types/{native → device}/share.d.ts +0 -0
@@ -0,0 +1,61 @@
1
+ import { isNative } from "../../native/index.js"
2
+
3
+ /**
4
+ * A storage for `withStorage` that survives the operating system clearing the
5
+ * WebView's data: in a native app, Capacitor Preferences (`Preferences` from
6
+ * `@capacitor/preferences`, given as `plugin`), answering with promises; in a
7
+ * browser, `localStorage`, looked up at each call. It also serves as Supabase's `auth.storage`,
8
+ * keeping the session there too.
9
+ *
10
+ * @param {{ plugin?: { get(options: { key: string }): Promise<{ value: string | null }>, set(options: { key: string, value: string }): Promise<void>, remove(options: { key: string }): Promise<void> } }} [config]
11
+ *
12
+ * @example
13
+ * import { Preferences } from "@capacitor/preferences"
14
+ * const preferences = createNativeStorage({ plugin: Preferences })
15
+ * const types = {
16
+ * Settings: [Settings, withStorage(["theme"], { storage: () => preferences })],
17
+ * }
18
+ */
19
+ export function createNativeStorage({ plugin } = {}) {
20
+ if (!plugin || !isNative()) {
21
+ return {
22
+ getItem: (key) => globalThis.localStorage?.getItem(key) ?? null,
23
+ setItem: (key, value) => globalThis.localStorage?.setItem(key, value),
24
+ removeItem: (key) => globalThis.localStorage?.removeItem(key),
25
+ }
26
+ }
27
+
28
+ return {
29
+ getItem: async (key) => (await plugin.get({ key })).value,
30
+ setItem: (key, value) => plugin.set({ key, value }),
31
+ removeItem: (key) => plugin.remove({ key }),
32
+ }
33
+ }
34
+
35
+ /**
36
+ * A storage for secrets, such as a session's tokens: in a native app the
37
+ * Keychain (iOS) or Keystore (Android), through `NativeBiometric` from
38
+ * `@capgo/capacitor-native-biometric`, given as `plugin`; in a browser, which
39
+ * has nothing safer, `localStorage`. Use it as `withStorage`'s storage or
40
+ * Supabase's `auth.storage`.
41
+ *
42
+ * @param {{ plugin?: { getData(options: { key: string }): Promise<{ value: string }>, setData(options: { key: string, value: string }): Promise<void>, deleteData(options: { key: string }): Promise<void> } }} [config]
43
+ *
44
+ * @example
45
+ * import { NativeBiometric } from "@capgo/capacitor-native-biometric"
46
+ * const secrets = createSecureStorage({ plugin: NativeBiometric })
47
+ * createClient(url, key, { auth: { storage: secrets } })
48
+ */
49
+ export function createSecureStorage({ plugin } = {}) {
50
+ if (!plugin || !isNative()) return createNativeStorage()
51
+
52
+ return {
53
+ getItem: (key) =>
54
+ plugin.getData({ key }).then(
55
+ ({ value }) => value,
56
+ () => null,
57
+ ),
58
+ setItem: (key, value) => plugin.setData({ key, value }),
59
+ removeItem: (key) => plugin.deleteData({ key }),
60
+ }
61
+ }
@@ -15,35 +15,3 @@ export function isNative() {
15
15
 
16
16
  return capacitor.isNative === true
17
17
  }
18
-
19
- /**
20
- * A storage for `withStorage` that survives the operating system clearing the
21
- * WebView's data: in a native app, Capacitor Preferences (`Preferences` from
22
- * `@capacitor/preferences`, given as `plugin`), answering with promises; in a
23
- * browser, `localStorage`, looked up at each call. It also serves as Supabase's `auth.storage`,
24
- * keeping the session there too.
25
- *
26
- * @param {{ plugin?: { get(options: { key: string }): Promise<{ value: string | null }>, set(options: { key: string, value: string }): Promise<void>, remove(options: { key: string }): Promise<void> } }} [config]
27
- *
28
- * @example
29
- * import { Preferences } from "@capacitor/preferences"
30
- * const preferences = createNativeStorage({ plugin: Preferences })
31
- * const types = {
32
- * Settings: [Settings, withStorage(["theme"], { storage: () => preferences })],
33
- * }
34
- */
35
- export function createNativeStorage({ plugin } = {}) {
36
- if (!plugin || !isNative()) {
37
- return {
38
- getItem: (key) => globalThis.localStorage?.getItem(key) ?? null,
39
- setItem: (key, value) => globalThis.localStorage?.setItem(key, value),
40
- removeItem: (key) => globalThis.localStorage?.removeItem(key),
41
- }
42
- }
43
-
44
- return {
45
- getItem: async (key) => (await plugin.get({ key })).value,
46
- setItem: (key, value) => plugin.set({ key, value }),
47
- removeItem: (key) => plugin.remove({ key }),
48
- }
49
- }
@@ -0,0 +1,51 @@
1
+ import type { AsyncEvents } from "@inglorious/store/async"
2
+
3
+ import { Api } from "../mount"
4
+
5
+ /** What `createBiometrics` needs of `NativeBiometric` from `@capgo/capacitor-native-biometric`. */
6
+ export interface BiometricsPlugin {
7
+ isAvailable(): Promise<{ isAvailable: boolean; biometryType: number }>
8
+ verifyIdentity(options?: BiometricsOptions): Promise<void>
9
+ }
10
+
11
+ /** What the system's prompt says. */
12
+ export interface BiometricsOptions {
13
+ reason?: string
14
+ title?: string
15
+ subtitle?: string
16
+ description?: string
17
+ }
18
+
19
+ export type BiometryType =
20
+ "face" | "fingerprint" | "iris" | "multiple" | "passcode"
21
+
22
+ export interface BiometricsEntity {
23
+ id?: string
24
+ type: string
25
+ isAvailable: boolean
26
+ biometryType: BiometryType | null
27
+ status: "idle" | "loading" | "success" | "error"
28
+ error: "unavailable" | "failed" | null
29
+ result: "verified" | "cancelled" | null
30
+ }
31
+
32
+ export type BiometricsType = {
33
+ create(entity: BiometricsEntity, payload: unknown, api: Api): void
34
+ biometricsAvailable(
35
+ entity: BiometricsEntity,
36
+ answer: { isAvailable: boolean; biometryType: number },
37
+ ): void
38
+ } & AsyncEvents<
39
+ "verify",
40
+ BiometricsEntity,
41
+ BiometricsOptions | undefined,
42
+ BiometricsEntity["result"]
43
+ >
44
+
45
+ /**
46
+ * The type behind "unlock with Face ID": the system's biometric prompt in a
47
+ * native app; never available in a browser.
48
+ */
49
+ export declare function createBiometrics(config?: {
50
+ plugin?: BiometricsPlugin
51
+ }): BiometricsType
@@ -10,8 +10,6 @@ export interface CompassEntity {
10
10
  isLoading: boolean
11
11
  isSupported: boolean
12
12
  manualOffset: number | null
13
- _absoluteListener: ((event: DeviceOrientationEvent) => void) | null
14
- _orientationListener: ((event: DeviceOrientationEvent) => void) | null
15
13
  _isUsingAbsolute: boolean
16
14
  compassTimeout: number | null
17
15
  }
@@ -18,7 +18,6 @@ export interface GeolocationEntity {
18
18
  isWatching: boolean
19
19
  position: GeolocationPosition | null
20
20
  type: string
21
- watchId: number | null
22
21
  }
23
22
 
24
23
  export interface GeolocationError {
@@ -38,21 +37,40 @@ export interface GeolocationPosition {
38
37
  }
39
38
 
40
39
  export type GeolocationType = {
41
- unwatch(entity: GeolocationEntity): void
42
40
  create(entity: GeolocationEntity): void
43
41
  destroy(entity: GeolocationEntity): void
44
- error(entity: GeolocationEntity, error: GeolocationError): void
45
- request(
42
+ geolocationRequest(
46
43
  entity: GeolocationEntity,
47
44
  options: GeolocationOptions | undefined,
48
45
  api: Api,
49
46
  ): void
50
- success(entity: GeolocationEntity, position: GeolocationPosition): void
51
- watch(
47
+ geolocationWatch(
52
48
  entity: GeolocationEntity,
53
49
  options: GeolocationOptions | undefined,
54
50
  api: Api,
55
51
  ): void
52
+ geolocationUnwatch(entity: GeolocationEntity): void
53
+ geolocationSuccess(
54
+ entity: GeolocationEntity,
55
+ position: GeolocationPosition,
56
+ ): void
57
+ geolocationError(entity: GeolocationEntity, error: GeolocationError): void
58
+ }
59
+
60
+ /** What `createGeolocation` needs of `Geolocation` from `@capacitor/geolocation`. */
61
+ export interface GeolocationPlugin {
62
+ getCurrentPosition(options?: GeolocationOptions): Promise<GeolocationPosition>
63
+ watchPosition(
64
+ options: GeolocationOptions,
65
+ callback: (position: GeolocationPosition | null, error?: unknown) => void,
66
+ ): Promise<string>
67
+ clearWatch(options: { id: string }): Promise<void>
56
68
  }
57
69
 
58
- export declare const Geolocation: GeolocationType
70
+ /**
71
+ * The type behind a position: `navigator.geolocation`, or the Capacitor
72
+ * plugin in a native app.
73
+ */
74
+ export declare function createGeolocation(config?: {
75
+ plugin?: GeolocationPlugin
76
+ }): GeolocationType
@@ -0,0 +1,76 @@
1
+ import { Api } from "../mount"
2
+
3
+ /** What `createLocalNotifications` needs of `LocalNotifications` from `@capacitor/local-notifications`. */
4
+ export interface LocalNotificationsPlugin {
5
+ checkPermissions(): Promise<{ display: string }>
6
+ requestPermissions(): Promise<{ display: string }>
7
+ schedule(options: {
8
+ notifications: {
9
+ id: number
10
+ title: string
11
+ body?: string
12
+ schedule: { at: Date }
13
+ extra?: { url?: string }
14
+ }[]
15
+ }): Promise<unknown>
16
+ cancel(options: { notifications: { id: number }[] }): Promise<void>
17
+ addListener(
18
+ event: "localNotificationActionPerformed",
19
+ listener: (action: { notification: { extra?: { url?: string } } }) => void,
20
+ ): unknown
21
+ }
22
+
23
+ /** A reminder to show at a given time. */
24
+ export interface LocalNotification {
25
+ /** A whole number, to cancel it by. */
26
+ id: number
27
+ title: string
28
+ body?: string
29
+ at: Date
30
+ /** Where a tap goes. */
31
+ url?: string
32
+ }
33
+
34
+ export type LocalNotificationsPermission =
35
+ "granted" | "denied" | "prompt" | "unsupported"
36
+
37
+ export interface LocalNotificationsEntity {
38
+ id?: string
39
+ type: string
40
+ /** `null` until the device says. */
41
+ permission: LocalNotificationsPermission | null
42
+ }
43
+
44
+ export type LocalNotificationsType = {
45
+ create(entity: LocalNotificationsEntity, payload: unknown, api: Api): void
46
+ localNotificationsAsk(
47
+ entity: LocalNotificationsEntity,
48
+ payload: unknown,
49
+ api: Api,
50
+ ): void
51
+ localNotificationsPermission(
52
+ entity: LocalNotificationsEntity,
53
+ permission: LocalNotificationsPermission,
54
+ ): void
55
+ localNotificationSchedule(
56
+ entity: LocalNotificationsEntity,
57
+ notification: LocalNotification,
58
+ ): void
59
+ localNotificationCancel(entity: LocalNotificationsEntity, id: number): void
60
+ localNotificationTap(
61
+ entity: LocalNotificationsEntity,
62
+ url: string | undefined,
63
+ api: Api,
64
+ ): void
65
+ }
66
+
67
+ /**
68
+ * Reminders the device shows at a given time: through the Capacitor plugin in
69
+ * a native app, even with the app closed; through the Notification API in a
70
+ * browser, while the page is open.
71
+ */
72
+ export declare function createLocalNotifications(config?: {
73
+ plugin?: LocalNotificationsPlugin
74
+ /** Where a tap goes. Defaults to `#router:navigate`. */
75
+ onTap?: (url: string, api: Api) => void
76
+ }): LocalNotificationsType
@@ -0,0 +1,30 @@
1
+ /** What `createNativeStorage` needs of `Preferences` from `@capacitor/preferences`. */
2
+ export interface PreferencesPlugin {
3
+ get(options: { key: string }): Promise<{ value: string | null }>
4
+ set(options: { key: string; value: string }): Promise<void>
5
+ remove(options: { key: string }): Promise<void>
6
+ }
7
+
8
+ /** A storage for `withStorage`: answering with promises in a native app. */
9
+ export interface NativeStorage {
10
+ getItem(key: string): string | null | Promise<string | null>
11
+ setItem(key: string, value: string): void | Promise<void>
12
+ removeItem(key: string): void | Promise<void>
13
+ }
14
+
15
+ /** Capacitor Preferences in a native app, `localStorage` in a browser. */
16
+ export declare function createNativeStorage(config?: {
17
+ plugin?: PreferencesPlugin
18
+ }): NativeStorage
19
+
20
+ /** What `createSecureStorage` needs of `NativeBiometric` from `@capgo/capacitor-native-biometric`. */
21
+ export interface SecureStoragePlugin {
22
+ getData(options: { key: string }): Promise<{ value: string }>
23
+ setData(options: { key: string; value: string }): Promise<void>
24
+ deleteData(options: { key: string }): Promise<void>
25
+ }
26
+
27
+ /** The Keychain or Keystore in a native app, `localStorage` in a browser. */
28
+ export declare function createSecureStorage(config?: {
29
+ plugin?: SecureStoragePlugin
30
+ }): NativeStorage
@@ -1,20 +1 @@
1
1
  export declare function isNative(): boolean
2
-
3
- /** What `createNativeStorage` needs of `Preferences` from `@capacitor/preferences`. */
4
- export interface PreferencesPlugin {
5
- get(options: { key: string }): Promise<{ value: string | null }>
6
- set(options: { key: string; value: string }): Promise<void>
7
- remove(options: { key: string }): Promise<void>
8
- }
9
-
10
- /** A storage for `withStorage`: answering with promises in a native app. */
11
- export interface NativeStorage {
12
- getItem(key: string): string | null | Promise<string | null>
13
- setItem(key: string, value: string): void | Promise<void>
14
- removeItem(key: string): void | Promise<void>
15
- }
16
-
17
- /** Capacitor Preferences in a native app, `localStorage` in a browser. */
18
- export declare function createNativeStorage(config?: {
19
- plugin?: PreferencesPlugin
20
- }): NativeStorage
@@ -1,209 +0,0 @@
1
- /**
2
- * @typedef {import("../../../types/sensors/compass.js").CompassEntity} CompassEntity
3
- * @typedef {import("../../../types/sensors/compass.js").CompassError} CompassError
4
- */
5
-
6
- const COMPASS_TIMEOUT = 3000
7
- const NO_TIMEOUT = null
8
-
9
- const FULL_CIRCLE = 360
10
- const DEFAULT_ORIENTATION = 0
11
- const DEFAULT_ERROR_CODE = 0
12
-
13
- export function create(entity) {
14
- entity.error ??= null
15
- entity.heading ??= null
16
- entity.isActive ??= false
17
- entity.isPermissionGranted ??= false
18
- entity.isLoading ??= false
19
- entity.isSupported = isCompassSupported()
20
- entity.manualOffset ??= 0
21
- entity._absoluteListener ??= null
22
- entity._cancelTimeout ??= null
23
- entity._orientationListener ??= null
24
- }
25
-
26
- export function destroy(entity) {
27
- compassUnwatch(entity)
28
- }
29
-
30
- export function compassUnwatch(entity) {
31
- const win = getWindow()
32
-
33
- if (entity._orientationListener && win) {
34
- win.removeEventListener("deviceorientation", entity._orientationListener)
35
- }
36
-
37
- if (entity._absoluteListener && win) {
38
- win.removeEventListener(
39
- "deviceorientationabsolute",
40
- entity._absoluteListener,
41
- )
42
- }
43
-
44
- if (entity._cancelTimeout) {
45
- entity._cancelTimeout()
46
- }
47
-
48
- entity.isLoading = false
49
- entity.isActive = false
50
- entity._orientationListener = null
51
- entity._absoluteListener = null
52
- entity._cancelTimeout = null
53
- }
54
-
55
- export function compassError(entity, error) {
56
- entity.error = normalizeError(error)
57
- entity.isLoading = false
58
- entity.isPermissionGranted = false
59
- }
60
-
61
- export async function compassPermissionsRequest(entity, _, api) {
62
- entity.isSupported = isCompassSupported()
63
- entity.error = null
64
-
65
- if (!entity.isSupported) {
66
- entity.isLoading = false
67
- api.notify("compassError", createUnsupportedError())
68
- return
69
- }
70
-
71
- entity.isLoading = true
72
-
73
- if (
74
- typeof DeviceOrientationEvent !== "undefined" &&
75
- typeof DeviceOrientationEvent.requestPermission === "function"
76
- ) {
77
- try {
78
- const permission = await DeviceOrientationEvent.requestPermission()
79
- if (permission === "granted") {
80
- api.notify("compassPermissionsGrant")
81
- } else {
82
- entity.isLoading = false
83
- api.notify("compassError", createPermissionDeniedError())
84
- }
85
- } catch (error) {
86
- entity.isLoading = false
87
- api.notify("compassError", normalizeError(error))
88
- }
89
- } else {
90
- api.notify("compassPermissionsGrant")
91
- }
92
- }
93
-
94
- export function compassPermissionsGrant(entity) {
95
- entity.isLoading = false
96
- entity.isPermissionGranted = true
97
- }
98
-
99
- export function compassWatch(entity, _, api) {
100
- // Snapshot values from the entity while the proxy is still live.
101
- // Closures must not access `entity` after this handler returns, as
102
- // Mutative revokes the draft proxy at that point.
103
- const manualOffset = entity.manualOffset
104
-
105
- let isUsingAbsolute = false
106
- let timeoutId = NO_TIMEOUT
107
-
108
- const cancelTimeout = () => {
109
- if (timeoutId !== NO_TIMEOUT) {
110
- clearTimeout(timeoutId)
111
- timeoutId = NO_TIMEOUT
112
- }
113
- }
114
-
115
- const orientationListener = createOrientationChangeListener(
116
- cancelTimeout,
117
- manualOffset,
118
- api,
119
- )
120
-
121
- const absoluteListener = (event) => {
122
- if (!isUsingAbsolute) {
123
- isUsingAbsolute = true
124
- getWindow()?.removeEventListener("deviceorientation", orientationListener)
125
- }
126
-
127
- orientationListener(event)
128
- }
129
-
130
- entity._orientationListener = orientationListener
131
- entity._absoluteListener = absoluteListener
132
- entity._cancelTimeout = cancelTimeout
133
-
134
- const win = getWindow()
135
- win?.addEventListener("deviceorientationabsolute", absoluteListener)
136
- win?.addEventListener("deviceorientation", orientationListener)
137
-
138
- timeoutId = globalThis.setTimeout(() => {
139
- timeoutId = NO_TIMEOUT
140
- api.notify("compassActiveChange", false)
141
- }, COMPASS_TIMEOUT)
142
- }
143
-
144
- export function compassActiveChange(entity, value) {
145
- entity.isActive = value
146
- }
147
-
148
- export function compassHeadingChange(entity, value) {
149
- entity.isLoading = false
150
- entity.heading = value
151
- }
152
-
153
- function createOrientationChangeListener(cancelTimeout, manualOffset, api) {
154
- return (event) => {
155
- const heading = event?.alpha
156
- if (typeof heading !== "number") {
157
- return
158
- }
159
-
160
- cancelTimeout()
161
-
162
- const screenAngle = getScreenAngle()
163
- const adjustedHeading =
164
- (((heading - screenAngle + manualOffset) % FULL_CIRCLE) + FULL_CIRCLE) %
165
- FULL_CIRCLE
166
-
167
- api.notify("compassActiveChange", true)
168
- api.notify("compassHeadingChange", Math.round(adjustedHeading))
169
- }
170
- }
171
-
172
- function getWindow() {
173
- return typeof window !== "undefined" ? window : null
174
- }
175
-
176
- function getScreenAngle() {
177
- const win = getWindow()
178
- return (
179
- win?.screen?.orientation?.angle ?? win?.orientation ?? DEFAULT_ORIENTATION
180
- )
181
- }
182
-
183
- function isCompassSupported() {
184
- return (
185
- typeof DeviceOrientationEvent !== "undefined" &&
186
- Boolean(getWindow()?.addEventListener)
187
- )
188
- }
189
-
190
- function createUnsupportedError() {
191
- return {
192
- code: 0,
193
- message: "Compass is not supported by this environment.",
194
- }
195
- }
196
-
197
- function createPermissionDeniedError() {
198
- return {
199
- code: 1,
200
- message: "Compass permission denied.",
201
- }
202
- }
203
-
204
- function normalizeError(error) {
205
- return {
206
- code: error?.code ?? DEFAULT_ERROR_CODE,
207
- message: error?.message ?? String(error),
208
- }
209
- }