@inglorious/web 6.24.1 → 6.26.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/README.md +57 -11
- package/package.json +6 -2
- package/src/analytics/index.js +7 -24
- package/src/auth/index.js +46 -16
- package/src/push/index.js +8 -25
- package/src/share/index.js +109 -0
- package/types/analytics.d.ts +0 -2
- package/types/auth.d.ts +12 -0
- package/types/push.d.ts +2 -2
- package/types/share.d.ts +42 -0
package/README.md
CHANGED
|
@@ -609,9 +609,10 @@ test("requireAuth blocks unauthenticated access", () => {
|
|
|
609
609
|
// Compose the guard with the page type
|
|
610
610
|
const GuardedPage = requireAuth(AdminPage)
|
|
611
611
|
|
|
612
|
-
//
|
|
612
|
+
// Signed out: the session entity has no user
|
|
613
613
|
const mockApi = {
|
|
614
614
|
notify: jest.fn(),
|
|
615
|
+
getEntity: () => ({ user: null }),
|
|
615
616
|
}
|
|
616
617
|
|
|
617
618
|
const { events } = trigger(
|
|
@@ -631,11 +632,12 @@ test("requireAuth blocks unauthenticated access", () => {
|
|
|
631
632
|
})
|
|
632
633
|
|
|
633
634
|
test("requireAuth allows authenticated access", () => {
|
|
634
|
-
//
|
|
635
|
-
localStorage.setItem("user", JSON.stringify({ id: 1 }))
|
|
636
|
-
|
|
635
|
+
// Signed in: the session entity has a user
|
|
637
636
|
const GuardedPage = requireAuth(AdminPage)
|
|
638
|
-
const mockApi = {
|
|
637
|
+
const mockApi = {
|
|
638
|
+
notify: jest.fn(),
|
|
639
|
+
getEntity: () => ({ user: { id: 1 } }),
|
|
640
|
+
}
|
|
639
641
|
|
|
640
642
|
const { events } = trigger(
|
|
641
643
|
{ type: "GuardedPage", id: "admin" },
|
|
@@ -646,9 +648,6 @@ test("requireAuth allows authenticated access", () => {
|
|
|
646
648
|
|
|
647
649
|
// Should NOT redirect
|
|
648
650
|
expect(events).toEqual([])
|
|
649
|
-
|
|
650
|
-
// Cleanup
|
|
651
|
-
localStorage.removeItem("user")
|
|
652
651
|
})
|
|
653
652
|
```
|
|
654
653
|
|
|
@@ -1135,8 +1134,9 @@ export const requireAuth = (type) => ({
|
|
|
1135
1134
|
// Only act when navigating to this specific route
|
|
1136
1135
|
if (payload.route !== entity.id) return
|
|
1137
1136
|
|
|
1138
|
-
// Check authentication
|
|
1139
|
-
|
|
1137
|
+
// Check authentication: the session entity is the one place the user
|
|
1138
|
+
// lives (kept between visits with withStorage, if at all)
|
|
1139
|
+
const { user } = api.getEntity("session") ?? {}
|
|
1140
1140
|
if (!user) {
|
|
1141
1141
|
// Redirect to login, preserving the intended destination
|
|
1142
1142
|
api.notify("navigate", {
|
|
@@ -1594,9 +1594,26 @@ watchAuth(adapter, store) // keeps `user` in step with the session
|
|
|
1594
1594
|
|
|
1595
1595
|
The entity's `step` says what to ask for: `"email"`, `"password"`, `"signUp"`, `"code"` or `"reset"`. Its events take what the person typed: `emailSubmit(email)`, `signInSubmit(password)`, `signUpSubmit({ password, data })`, `codeSubmit(code)`, `resetSubmit({ code, password })`, and move between steps: `signUpShow`, `signInShow`, `codeRequest`, `codeResend`, `resetRequest`, `back`. Signed in, `signOut` and `accountEraseAsk`, then `accountErase` or `accountEraseCancel`. `isReady` turns true once the saved session is read: until then, render nothing rather than the sign-in screen.
|
|
1596
1596
|
|
|
1597
|
+
Whoever signs in, by password, code, link or password reset, the entity gets `signedIn` with `{ user, isNew }`, `isNew` for an account just created. Add a handler to move on:
|
|
1598
|
+
|
|
1599
|
+
```javascript
|
|
1600
|
+
const types = {
|
|
1601
|
+
Auth: [
|
|
1602
|
+
createAuth({ adapter }),
|
|
1603
|
+
{
|
|
1604
|
+
signedIn(entity, { isNew }, api) {
|
|
1605
|
+
api.notify("#router:navigate", isNew ? "/onboarding" : "/")
|
|
1606
|
+
},
|
|
1607
|
+
},
|
|
1608
|
+
],
|
|
1609
|
+
}
|
|
1610
|
+
```
|
|
1611
|
+
|
|
1612
|
+
For a page that a confirmation email's link opens, `linkSubmit({ tokenHash })` signs in with the link instead of the code. Ask for a tap first: mail scanners open links too.
|
|
1613
|
+
|
|
1597
1614
|
`status` is `"loading"` while waiting, and `error` is a key to put into words: `"invalidEmail"`, `"invalidCode"`, `"weakPassword"`, `"invalidCredentials"`, `"accountExists"`, `"rateLimited"`, `"captcha"`, `"network"` or `"unknown"`. An account not confirmed yet goes straight to the code.
|
|
1598
1615
|
|
|
1599
|
-
The adapter talks to the backend. `supabaseAuth` from `@inglorious/supabase` is one; another backend needs `onSessionChange`, `signIn`, `signUp`, `sendCode`, `verifyCode`, `resendCode`, `sendResetCode`, `updatePassword`, `signOut`, `eraseAccount` and `errorCode`, plus `accountExists` to skip straight to the password or the new account.
|
|
1616
|
+
The adapter talks to the backend. `supabaseAuth` from `@inglorious/supabase` is one; another backend needs `onSessionChange`, `signIn`, `signUp`, `sendCode`, `verifyCode`, `verifyLink`, `resendCode`, `sendResetCode`, `updatePassword`, `signOut`, `eraseAccount` and `errorCode`, plus `accountExists` to skip straight to the password or the new account, and `prepare(action)`, called once the next step is known (`"signIn"` or `"signUp"`), such as to solve a CAPTCHA before the person submits.
|
|
1600
1617
|
|
|
1601
1618
|
`create-app --auth` generates the screen, the account actions in the app bar, the Edge Function that erases accounts and the email templates.
|
|
1602
1619
|
|
|
@@ -1672,6 +1689,35 @@ It returns a function that stops following.
|
|
|
1672
1689
|
|
|
1673
1690
|
---
|
|
1674
1691
|
|
|
1692
|
+
## Share
|
|
1693
|
+
|
|
1694
|
+
`@inglorious/web/share` is the type behind a share button. `share` hands a title, a text, a link or a file to whatever the device does best, and the entity says what happened:
|
|
1695
|
+
|
|
1696
|
+
- in a native app, the system's share sheet, through [`@capacitor/share`](https://capacitorjs.com/docs/apis/share) when you hand it over;
|
|
1697
|
+
- in a browser, a file (a `url` with a `fileName`) downloads, since a browser has nowhere better to put it;
|
|
1698
|
+
- otherwise the browser's share sheet where there is one (Web Share API), else the link is copied.
|
|
1699
|
+
|
|
1700
|
+
```javascript
|
|
1701
|
+
import { Share } from "@capacitor/share"
|
|
1702
|
+
import { createShare } from "@inglorious/web/share"
|
|
1703
|
+
|
|
1704
|
+
const types = { Share: createShare({ plugin: Share }) }
|
|
1705
|
+
|
|
1706
|
+
api.notify("#share:share", {
|
|
1707
|
+
title: "My job",
|
|
1708
|
+
url: "https://example.com/jobs/1",
|
|
1709
|
+
})
|
|
1710
|
+
api.notify("#share:share", {
|
|
1711
|
+
title: "Wedding",
|
|
1712
|
+
url: icsUrl,
|
|
1713
|
+
fileName: "wedding.ics",
|
|
1714
|
+
})
|
|
1715
|
+
```
|
|
1716
|
+
|
|
1717
|
+
`result` is `"shared"`, `"downloaded"`, `"copied"` or `"cancelled"`: closing the sheet is not an error. `status` and `error` (`"unsupported"` or `"failed"`) come from `handleRequest`. `canShare` says whether a link can be shared at all, to hide the button where it can't.
|
|
1718
|
+
|
|
1719
|
+
---
|
|
1720
|
+
|
|
1675
1721
|
## Haptics
|
|
1676
1722
|
|
|
1677
1723
|
`@inglorious/web/haptics` plays taps the user can feel. In a native app it uses the system's haptics through [`@capacitor/haptics`](https://capacitorjs.com/docs/apis/haptics), once you hand the plugin over. Elsewhere it vibrates where the browser can (Chrome on Android), once the user has tapped the page; iOS browsers stay silent. Every call returns whether it played.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inglorious/web",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.26.0",
|
|
4
4
|
"description": "A new web framework that leverages the power of the Inglorious Store combined with the performance and simplicity of lit-html.",
|
|
5
5
|
"author": "Matteo Antony Mistretta <antony.mistretta@gmail.com> (https://ingloriouscoderz.it)",
|
|
6
6
|
"contributors": [
|
|
@@ -61,6 +61,10 @@
|
|
|
61
61
|
"types": "./types/haptics.d.ts",
|
|
62
62
|
"import": "./src/haptics/index.js"
|
|
63
63
|
},
|
|
64
|
+
"./share": {
|
|
65
|
+
"types": "./types/share.d.ts",
|
|
66
|
+
"import": "./src/share/index.js"
|
|
67
|
+
},
|
|
64
68
|
"./decorators/with-debounce": {
|
|
65
69
|
"types": "./types/decorators/with-debounce.d.ts",
|
|
66
70
|
"import": "./src/decorators/with-debounce/index.js"
|
|
@@ -158,7 +162,7 @@
|
|
|
158
162
|
"lit-html": "^3.3.3"
|
|
159
163
|
},
|
|
160
164
|
"peerDependencies": {
|
|
161
|
-
"@inglorious/store": "^12.
|
|
165
|
+
"@inglorious/store": "^12.5.0"
|
|
162
166
|
},
|
|
163
167
|
"devDependencies": {
|
|
164
168
|
"@inglorious/eslint-config": "1.2.0",
|
package/src/analytics/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const DEFAULT_CONSENT_KEY = "inglorious-analytics-consent"
|
|
2
1
|
const GRANTED = "granted"
|
|
3
2
|
const DENIED = "denied"
|
|
4
3
|
|
|
@@ -8,7 +7,8 @@ const DENIED = "denied"
|
|
|
8
7
|
* to an analytics library is what sets its cookies and sends its first hit.
|
|
9
8
|
*
|
|
10
9
|
* Its entity's `consent` is `"granted"`, `"denied"` or `null` (not asked
|
|
11
|
-
* yet)
|
|
10
|
+
* yet). Keep it between visits with `withStorage(["consent"])` from
|
|
11
|
+
* `@inglorious/store/storage`. While `isAsking`, the app renders its own
|
|
12
12
|
* consent prompt, whose answer is `analyticsConsent` with `true` or `false`.
|
|
13
13
|
* `analyticsAsk` brings the prompt back, for a settings page where the
|
|
14
14
|
* choice can be changed as easily as it was made.
|
|
@@ -25,35 +25,19 @@ const DENIED = "denied"
|
|
|
25
25
|
*
|
|
26
26
|
* @param {Object} config
|
|
27
27
|
* @param {Object} config.adapter
|
|
28
|
-
* @param {string} [config.consentKey] - The localStorage key of the choice.
|
|
29
28
|
*
|
|
30
29
|
* @example
|
|
31
|
-
* const types = {
|
|
30
|
+
* const types = {
|
|
31
|
+
* Analytics: [createAnalytics({ adapter }), template, withStorage(["consent"])],
|
|
32
|
+
* }
|
|
32
33
|
* const entities = { analytics: { type: "Analytics" } }
|
|
33
34
|
* // later, anywhere
|
|
34
35
|
* api.notify("#analytics:analyticsTrack", { name: "sign_up" })
|
|
35
36
|
*/
|
|
36
|
-
export function createAnalytics({ adapter
|
|
37
|
-
function readConsent() {
|
|
38
|
-
try {
|
|
39
|
-
const stored = window.localStorage.getItem(consentKey)
|
|
40
|
-
return stored === GRANTED || stored === DENIED ? stored : null
|
|
41
|
-
} catch {
|
|
42
|
-
return null
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function writeConsent(consent) {
|
|
47
|
-
try {
|
|
48
|
-
window.localStorage.setItem(consentKey, consent)
|
|
49
|
-
} catch {
|
|
50
|
-
// Without storage the prompt comes back next visit, nothing worse.
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
37
|
+
export function createAnalytics({ adapter }) {
|
|
54
38
|
return {
|
|
55
39
|
create(entity) {
|
|
56
|
-
entity.consent ??=
|
|
40
|
+
entity.consent ??= null
|
|
57
41
|
entity.isAsking ??= entity.consent === null
|
|
58
42
|
if (entity.consent === GRANTED) call(() => adapter.setConsent(true))
|
|
59
43
|
},
|
|
@@ -65,7 +49,6 @@ export function createAnalytics({ adapter, consentKey = DEFAULT_CONSENT_KEY }) {
|
|
|
65
49
|
|
|
66
50
|
analyticsConsent(entity, isGranted, api) {
|
|
67
51
|
const consent = isGranted ? GRANTED : DENIED
|
|
68
|
-
writeConsent(consent)
|
|
69
52
|
entity.consent = consent
|
|
70
53
|
entity.isAsking = false
|
|
71
54
|
call(() => adapter.setConsent(isGranted))
|
package/src/auth/index.js
CHANGED
|
@@ -21,10 +21,20 @@ const CODE = /^\d{6,10}$/
|
|
|
21
21
|
* `"invalidCredentials"`, `"accountExists"`, `"rateLimited"`, `"captcha"`,
|
|
22
22
|
* `"network"` or `"unknown"`.
|
|
23
23
|
*
|
|
24
|
+
* Whoever signs in, by password, code, link or password reset, the entity
|
|
25
|
+
* gets `#id:signedIn` with `{ user, isNew }`, `isNew` for an account just
|
|
26
|
+
* created. Give the type a `signedIn` handler to move on, such as to an
|
|
27
|
+
* onboarding for new accounts.
|
|
28
|
+
*
|
|
29
|
+
* `linkSubmit({ tokenHash, purpose })` signs in with the link of an email
|
|
30
|
+
* instead of its code, for a page the link opens.
|
|
31
|
+
*
|
|
24
32
|
* The `adapter` talks to the backend: `signIn`, `signUp`, `sendCode`,
|
|
25
|
-
* `verifyCode`, `
|
|
26
|
-
* `eraseAccount`, `errorCode`, and optionally
|
|
27
|
-
* straight to the password or the new account
|
|
33
|
+
* `verifyCode`, `verifyLink`, `resendCode`, `sendResetCode`,
|
|
34
|
+
* `updatePassword`, `signOut`, `eraseAccount`, `errorCode`, and optionally
|
|
35
|
+
* `accountExists`, which skips straight to the password or the new account,
|
|
36
|
+
* and `prepare(action)`, called as soon as the next step is known, such as
|
|
37
|
+
* to solve a CAPTCHA before the person submits. `supabaseAuth` from
|
|
28
38
|
* `@inglorious/supabase` is one.
|
|
29
39
|
*
|
|
30
40
|
* @param {Object} config
|
|
@@ -77,7 +87,11 @@ export function createAuth({
|
|
|
77
87
|
|
|
78
88
|
...request("emailCheck", {
|
|
79
89
|
async run(email) {
|
|
80
|
-
|
|
90
|
+
const exists = adapter.accountExists
|
|
91
|
+
? await adapter.accountExists(email)
|
|
92
|
+
: true
|
|
93
|
+
adapter.prepare?.(exists ? "signIn" : "signUp")
|
|
94
|
+
return exists
|
|
81
95
|
},
|
|
82
96
|
success(entity, exists) {
|
|
83
97
|
entity.step = exists ? "password" : "signUp"
|
|
@@ -104,9 +118,8 @@ export function createAuth({
|
|
|
104
118
|
|
|
105
119
|
...request("signIn", {
|
|
106
120
|
run: ({ email, password }) => adapter.signIn(email, password),
|
|
107
|
-
success(entity, user) {
|
|
108
|
-
entity
|
|
109
|
-
reset(entity)
|
|
121
|
+
success(entity, user, api) {
|
|
122
|
+
signedIn(entity, { user, isNew: false }, api)
|
|
110
123
|
},
|
|
111
124
|
error(entity, code) {
|
|
112
125
|
if (code !== "emailNotConfirmed") return
|
|
@@ -129,10 +142,9 @@ export function createAuth({
|
|
|
129
142
|
|
|
130
143
|
...request("signUp", {
|
|
131
144
|
run: ({ email, password, data }) => adapter.signUp(email, password, data),
|
|
132
|
-
success(entity, user) {
|
|
145
|
+
success(entity, user, api) {
|
|
133
146
|
if (user) {
|
|
134
|
-
entity
|
|
135
|
-
reset(entity)
|
|
147
|
+
signedIn(entity, { user, isNew: true }, api)
|
|
136
148
|
return
|
|
137
149
|
}
|
|
138
150
|
showCode(entity, "signUp")
|
|
@@ -167,9 +179,22 @@ export function createAuth({
|
|
|
167
179
|
...request("codeVerify", {
|
|
168
180
|
run: ({ email, code, purpose }) =>
|
|
169
181
|
adapter.verifyCode(email, code, purpose),
|
|
170
|
-
success(entity, user) {
|
|
171
|
-
entity
|
|
172
|
-
|
|
182
|
+
success(entity, user, api) {
|
|
183
|
+
signedIn(entity, { user, isNew: entity.codeFor === "signUp" }, api)
|
|
184
|
+
},
|
|
185
|
+
}),
|
|
186
|
+
|
|
187
|
+
linkSubmit(entity, { tokenHash, purpose = "signUp" } = {}, api) {
|
|
188
|
+
api.notify(`#${entity.id}:linkVerify`, { tokenHash, purpose })
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
...request("linkVerify", {
|
|
192
|
+
async run({ tokenHash, purpose }) {
|
|
193
|
+
const user = await adapter.verifyLink(tokenHash, purpose)
|
|
194
|
+
return { user, isNew: purpose === "signUp" }
|
|
195
|
+
},
|
|
196
|
+
success(entity, signIn, api) {
|
|
197
|
+
signedIn(entity, signIn, api)
|
|
173
198
|
},
|
|
174
199
|
}),
|
|
175
200
|
|
|
@@ -223,9 +248,8 @@ export function createAuth({
|
|
|
223
248
|
await adapter.verifyCode(email, code, "reset")
|
|
224
249
|
return adapter.updatePassword(password)
|
|
225
250
|
},
|
|
226
|
-
success(entity, user) {
|
|
227
|
-
entity
|
|
228
|
-
reset(entity)
|
|
251
|
+
success(entity, user, api) {
|
|
252
|
+
signedIn(entity, { user, isNew: false }, api)
|
|
229
253
|
},
|
|
230
254
|
}),
|
|
231
255
|
|
|
@@ -277,6 +301,12 @@ export function watchAuth(adapter, store, { id = DEFAULT_ID } = {}) {
|
|
|
277
301
|
)
|
|
278
302
|
}
|
|
279
303
|
|
|
304
|
+
function signedIn(entity, { user, isNew }, api) {
|
|
305
|
+
entity.user = user
|
|
306
|
+
reset(entity)
|
|
307
|
+
api.notify(`#${entity.id}:signedIn`, { user, isNew })
|
|
308
|
+
}
|
|
309
|
+
|
|
280
310
|
function reset(entity, email = "") {
|
|
281
311
|
entity.step = "email"
|
|
282
312
|
entity.email = email
|
package/src/push/index.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { handleAsync } from "@inglorious/store/async"
|
|
2
2
|
|
|
3
|
-
const DEFAULT_DECLINE_KEY = "inglorious-push-declined"
|
|
4
|
-
|
|
5
3
|
// The adapters whose taps are already wired: an entity created again (a hot
|
|
6
4
|
// update, a second store) must not open each push twice.
|
|
7
5
|
const tappedAdapters = new WeakSet()
|
|
@@ -12,9 +10,10 @@ const tappedAdapters = new WeakSet()
|
|
|
12
10
|
* A device shows its permission dialog only so many times, and a refusal
|
|
13
11
|
* there may be for good. So the type asks in the app's own words first: an
|
|
14
12
|
* entity with `isOpen` the app renders as a prompt, whose yes brings up the
|
|
15
|
-
* system's dialog (`pushAsk`) and whose no (`pushDecline`)
|
|
16
|
-
*
|
|
17
|
-
*
|
|
13
|
+
* system's dialog (`pushAsk`) and whose no (`pushDecline`) sets
|
|
14
|
+
* `isDeclined`, so it never asks again. Keep it between visits with
|
|
15
|
+
* `withStorage(["isDeclined"])` from `@inglorious/store/storage`. Allowed
|
|
16
|
+
* later from the system's settings, pushes still register.
|
|
18
17
|
*
|
|
19
18
|
* The app tells it when there is someone to register for: `pushStart` (at
|
|
20
19
|
* sign-in) checks the permission, registers the device if it is granted, and
|
|
@@ -37,7 +36,6 @@ const tappedAdapters = new WeakSet()
|
|
|
37
36
|
*
|
|
38
37
|
* @param {Object} config
|
|
39
38
|
* @param {Object} config.adapter
|
|
40
|
-
* @param {string} [config.declineKey] - The localStorage key of a decline.
|
|
41
39
|
* @param {(url: string, api: Object) => void} [config.onTap]
|
|
42
40
|
*
|
|
43
41
|
* @example
|
|
@@ -51,21 +49,10 @@ const tappedAdapters = new WeakSet()
|
|
|
51
49
|
* },
|
|
52
50
|
* }
|
|
53
51
|
*/
|
|
54
|
-
export function createPushPrompt({
|
|
55
|
-
adapter,
|
|
56
|
-
declineKey = DEFAULT_DECLINE_KEY,
|
|
57
|
-
onTap = navigate,
|
|
58
|
-
}) {
|
|
59
|
-
function isDeclined() {
|
|
60
|
-
try {
|
|
61
|
-
return window.localStorage.getItem(declineKey) !== null
|
|
62
|
-
} catch {
|
|
63
|
-
return false
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
52
|
+
export function createPushPrompt({ adapter, onTap = navigate }) {
|
|
67
53
|
return {
|
|
68
54
|
create(entity, _, api) {
|
|
55
|
+
entity.isDeclined ??= false
|
|
69
56
|
entity.isOpen ??= false
|
|
70
57
|
entity.isActive ??= false
|
|
71
58
|
if (adapter.onTap && !tappedAdapters.has(adapter)) {
|
|
@@ -95,7 +82,7 @@ export function createPushPrompt({
|
|
|
95
82
|
api.notify(`#${entity.id}:pushRegister`)
|
|
96
83
|
return
|
|
97
84
|
}
|
|
98
|
-
entity.isOpen = permission === "prompt" && !isDeclined
|
|
85
|
+
entity.isOpen = permission === "prompt" && !entity.isDeclined
|
|
99
86
|
},
|
|
100
87
|
error() {},
|
|
101
88
|
}),
|
|
@@ -114,11 +101,7 @@ export function createPushPrompt({
|
|
|
114
101
|
}),
|
|
115
102
|
|
|
116
103
|
pushDecline(entity) {
|
|
117
|
-
|
|
118
|
-
window.localStorage.setItem(declineKey, "true")
|
|
119
|
-
} catch {
|
|
120
|
-
// Without storage the prompt comes back next start, nothing worse.
|
|
121
|
-
}
|
|
104
|
+
entity.isDeclined = true
|
|
122
105
|
entity.isOpen = false
|
|
123
106
|
},
|
|
124
107
|
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { handleRequest } from "@inglorious/store/async"
|
|
2
|
+
|
|
3
|
+
import { isNative } from "../mobile/index.js"
|
|
4
|
+
|
|
5
|
+
const UNSUPPORTED = "unsupported"
|
|
6
|
+
const FAILED = "failed"
|
|
7
|
+
// Capacitor's Share rejects with this when the person closes the sheet.
|
|
8
|
+
const NATIVE_CANCEL = /cancel/i
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The type behind a share button. `share` hands a title, text, link or file
|
|
12
|
+
* to whatever the device does best, and the entity says what happened:
|
|
13
|
+
*
|
|
14
|
+
* - in a native app, the system's share sheet, through `Share` from
|
|
15
|
+
* `@capacitor/share` when given as `plugin`;
|
|
16
|
+
* - in a browser, a file (a `url` with a `fileName`) downloads, since a
|
|
17
|
+
* browser has nowhere better to put it;
|
|
18
|
+
* - otherwise the browser's share sheet where there is one (Web Share API),
|
|
19
|
+
* else the link is copied.
|
|
20
|
+
*
|
|
21
|
+
* `result` is `"shared"`, `"downloaded"`, `"copied"` or `"cancelled"`: closing
|
|
22
|
+
* the sheet is not an error. `status` and `error` (`"unsupported"` or
|
|
23
|
+
* `"failed"`) are those of `handleRequest`. `canShare` says whether a link can
|
|
24
|
+
* be shared at all, to hide the button otherwise; a file can always download.
|
|
25
|
+
*
|
|
26
|
+
* @param {{ plugin?: { share(options: object): Promise<unknown> } }} [config]
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* import { Share } from "@capacitor/share"
|
|
30
|
+
* const types = { Share: createShare({ plugin: Share }) }
|
|
31
|
+
* api.notify("#share:share", { title: "My job", url })
|
|
32
|
+
*/
|
|
33
|
+
export function createShare({ plugin } = {}) {
|
|
34
|
+
return {
|
|
35
|
+
create(entity) {
|
|
36
|
+
entity.status ??= "idle"
|
|
37
|
+
entity.error ??= null
|
|
38
|
+
entity.result ??= null
|
|
39
|
+
entity.canShare = canShare(plugin)
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
...handleRequest(
|
|
43
|
+
"share",
|
|
44
|
+
{
|
|
45
|
+
start(entity) {
|
|
46
|
+
entity.result = null
|
|
47
|
+
},
|
|
48
|
+
run: (content) => share(content, plugin),
|
|
49
|
+
success(entity, result) {
|
|
50
|
+
entity.result = result
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
toError: (error) =>
|
|
55
|
+
error?.message === UNSUPPORTED ? UNSUPPORTED : FAILED,
|
|
56
|
+
},
|
|
57
|
+
),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async function share({ title, text, url, fileName } = {}, plugin) {
|
|
62
|
+
if (plugin && isNative()) {
|
|
63
|
+
try {
|
|
64
|
+
await plugin.share({ title, text, url, dialogTitle: title })
|
|
65
|
+
return "shared"
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (NATIVE_CANCEL.test(error?.message ?? "")) return "cancelled"
|
|
68
|
+
throw error
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (url && fileName) {
|
|
73
|
+
download(url, fileName)
|
|
74
|
+
return "downloaded"
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const navigator = globalThis.navigator
|
|
78
|
+
if (navigator?.share) {
|
|
79
|
+
try {
|
|
80
|
+
await navigator.share({ title, text, url })
|
|
81
|
+
return "shared"
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (error?.name === "AbortError") return "cancelled"
|
|
84
|
+
throw error
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (url && navigator?.clipboard) {
|
|
89
|
+
await navigator.clipboard.writeText(url)
|
|
90
|
+
return "copied"
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
throw new Error(UNSUPPORTED)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function canShare(plugin) {
|
|
97
|
+
if (plugin && isNative()) return true
|
|
98
|
+
const navigator = globalThis.navigator
|
|
99
|
+
return Boolean(navigator?.share || navigator?.clipboard)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function download(url, fileName) {
|
|
103
|
+
const link = document.createElement("a")
|
|
104
|
+
link.href = url
|
|
105
|
+
link.download = fileName
|
|
106
|
+
document.body.append(link)
|
|
107
|
+
link.click()
|
|
108
|
+
link.remove()
|
|
109
|
+
}
|
package/types/analytics.d.ts
CHANGED
|
@@ -12,8 +12,6 @@ export interface AnalyticsAdapter {
|
|
|
12
12
|
|
|
13
13
|
export interface AnalyticsConfig {
|
|
14
14
|
adapter: AnalyticsAdapter
|
|
15
|
-
/** The localStorage key of the choice. Defaults to `"inglorious-analytics-consent"`. */
|
|
16
|
-
consentKey?: string
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
export interface AnalyticsEntity {
|
package/types/auth.d.ts
CHANGED
|
@@ -46,6 +46,8 @@ export interface AuthAdapter {
|
|
|
46
46
|
code: string,
|
|
47
47
|
purpose: AuthCodePurpose,
|
|
48
48
|
): Promise<AuthUser>
|
|
49
|
+
/** Signs in with the token of an email's link instead of its code. */
|
|
50
|
+
verifyLink(tokenHash: string, purpose: AuthCodePurpose): Promise<AuthUser>
|
|
49
51
|
resendCode(email: string, purpose: AuthCodePurpose): Promise<void>
|
|
50
52
|
sendResetCode(email: string): Promise<void>
|
|
51
53
|
/** Sets the signed-in user's new password. */
|
|
@@ -55,6 +57,15 @@ export interface AuthAdapter {
|
|
|
55
57
|
eraseAccount(): Promise<void>
|
|
56
58
|
/** The key for a failure. */
|
|
57
59
|
errorCode?(error: unknown): AuthError
|
|
60
|
+
/** Called as soon as the next step is known, such as to solve a CAPTCHA early. */
|
|
61
|
+
prepare?(action: "signIn" | "signUp"): void
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What `#id:signedIn` carries, whoever signed in and however. */
|
|
65
|
+
export interface AuthSignIn {
|
|
66
|
+
user: AuthUser
|
|
67
|
+
/** The account was just created. */
|
|
68
|
+
isNew: boolean
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
export interface AuthConfig {
|
|
@@ -103,6 +114,7 @@ export type AuthType = {
|
|
|
103
114
|
codeRequest: Handler
|
|
104
115
|
codeSubmit: Handler<string>
|
|
105
116
|
codeResend: Handler
|
|
117
|
+
linkSubmit: Handler<{ tokenHash: string; purpose?: AuthCodePurpose }>
|
|
106
118
|
resetRequest: Handler
|
|
107
119
|
resetSubmit: Handler<{ code: string; password: string }>
|
|
108
120
|
back: Handler
|
package/types/push.d.ts
CHANGED
|
@@ -20,8 +20,6 @@ export interface PushAdapter {
|
|
|
20
20
|
|
|
21
21
|
export interface PushPromptConfig {
|
|
22
22
|
adapter: PushAdapter
|
|
23
|
-
/** The localStorage key of a decline. Defaults to `"inglorious-push-declined"`. */
|
|
24
|
-
declineKey?: string
|
|
25
23
|
/** Opens a tapped push's `url`. Defaults to `#router:navigate`. */
|
|
26
24
|
onTap?: (url: string, api: Api) => void
|
|
27
25
|
}
|
|
@@ -31,6 +29,8 @@ export interface PushPromptEntity {
|
|
|
31
29
|
type: string
|
|
32
30
|
/** The app's own prompt is showing. */
|
|
33
31
|
isOpen: boolean
|
|
32
|
+
/** The prompt was declined: it never opens again. Keep it with `withStorage`. */
|
|
33
|
+
isDeclined: boolean
|
|
34
34
|
/** Between `pushStart` and `pushStop`: someone to register for. */
|
|
35
35
|
isActive: boolean
|
|
36
36
|
}
|
package/types/share.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** What `createShare` needs of `Share` from `@capacitor/share`. */
|
|
2
|
+
export interface SharePlugin {
|
|
3
|
+
share(options: {
|
|
4
|
+
title?: string
|
|
5
|
+
text?: string
|
|
6
|
+
url?: string
|
|
7
|
+
dialogTitle?: string
|
|
8
|
+
}): Promise<unknown>
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** What to share: a link, a text, or a file to download in a browser. */
|
|
12
|
+
export interface ShareContent {
|
|
13
|
+
title?: string
|
|
14
|
+
text?: string
|
|
15
|
+
url?: string
|
|
16
|
+
/** Makes `url` a file: a browser downloads it under this name. */
|
|
17
|
+
fileName?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ShareEntity {
|
|
21
|
+
id?: string
|
|
22
|
+
type: string
|
|
23
|
+
status: "idle" | "loading" | "success" | "error"
|
|
24
|
+
error: "unsupported" | "failed" | null
|
|
25
|
+
result: "shared" | "downloaded" | "copied" | "cancelled" | null
|
|
26
|
+
/** A link can be shared, by sheet or by copying it. */
|
|
27
|
+
canShare: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type ShareType = {
|
|
31
|
+
create(entity: ShareEntity): void
|
|
32
|
+
share(entity: ShareEntity, content: ShareContent): void
|
|
33
|
+
[handler: string]: unknown
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The type behind a share button: the system's sheet in a native app, a
|
|
38
|
+
* download for files in a browser, else the browser's sheet, else a copy.
|
|
39
|
+
*/
|
|
40
|
+
export declare function createShare(config?: {
|
|
41
|
+
plugin?: SharePlugin
|
|
42
|
+
}): ShareType
|