@inglorious/web 6.26.0 → 6.26.2

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 CHANGED
@@ -609,45 +609,36 @@ test("requireAuth blocks unauthenticated access", () => {
609
609
  // Compose the guard with the page type
610
610
  const GuardedPage = requireAuth(AdminPage)
611
611
 
612
- // Signed out: the session entity has no user
613
- const mockApi = {
614
- notify: jest.fn(),
615
- getEntity: () => ({ user: null }),
616
- }
617
-
612
+ // Signed out: the guard was told of no user
618
613
  const { events } = trigger(
619
- { type: "GuardedPage", id: "admin" },
614
+ { type: "GuardedPage", id: "admin", user: null },
620
615
  GuardedPage.routeChange,
621
- { route: "adminPage" },
622
- mockApi,
616
+ { route: "admin", path: "/admin" },
623
617
  )
624
618
 
625
619
  // Should redirect to login
626
620
  expect(events).toContainEqual(
627
621
  expect.objectContaining({
628
622
  type: "navigate",
629
- payload: expect.objectContaining({ to: "/login" }),
623
+ payload: expect.objectContaining({ to: "/login?from=%2Fadmin" }),
630
624
  }),
631
625
  )
632
626
  })
633
627
 
634
628
  test("requireAuth allows authenticated access", () => {
635
- // Signed in: the session entity has a user
629
+ // Signed in: the guard was told of a user
636
630
  const GuardedPage = requireAuth(AdminPage)
637
- const mockApi = {
638
- notify: jest.fn(),
639
- getEntity: () => ({ user: { id: 1 } }),
640
- }
641
631
 
642
632
  const { events } = trigger(
643
- { type: "GuardedPage", id: "admin" },
633
+ { type: "GuardedPage", id: "admin", user: { id: 1 } },
644
634
  GuardedPage.routeChange,
645
- { route: "adminPage" },
646
- mockApi,
635
+ { route: "admin", path: "/admin" },
647
636
  )
648
637
 
649
638
  // Should NOT redirect
650
- expect(events).toEqual([])
639
+ expect(events).not.toContainEqual(
640
+ expect.objectContaining({ type: "navigate" }),
641
+ )
651
642
  })
652
643
  ```
653
644
 
@@ -1128,16 +1119,30 @@ Guards are simply behaviors that intercept events (like `routeChange`) and can p
1128
1119
  #### Example: Authentication Guard
1129
1120
 
1130
1121
  ```javascript
1122
+ // session.js: the one place the user lives (kept between visits with
1123
+ // withStorage, if at all), announcing each change
1124
+ export const Session = {
1125
+ signIn(entity, user, api) {
1126
+ entity.user = user
1127
+ api.notify("sessionChange", user)
1128
+ },
1129
+ signOut(entity, payload, api) {
1130
+ entity.user = null
1131
+ api.notify("sessionChange", null)
1132
+ },
1133
+ }
1134
+
1131
1135
  // guards/require-auth.js
1132
1136
  export const requireAuth = (type) => ({
1137
+ sessionChange(entity, user, api) {
1138
+ entity.user = user
1139
+ type.sessionChange?.(entity, user, api)
1140
+ },
1133
1141
  routeChange(entity, payload, api) {
1134
1142
  // Only act when navigating to this specific route
1135
1143
  if (payload.route !== entity.id) return
1136
1144
 
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
- if (!user) {
1145
+ if (!entity.user) {
1141
1146
  // Redirect to login, preserving the intended destination
1142
1147
  api.notify("navigate", {
1143
1148
  to: `/login?from=${encodeURIComponent(payload.path)}`,
@@ -1152,6 +1157,8 @@ export const requireAuth = (type) => ({
1152
1157
  })
1153
1158
  ```
1154
1159
 
1160
+ The guard keeps what `sessionChange` told it rather than asking `api.getEntity("session")`, which during an update returns the state from before it: after a sign-in followed by navigation in the same chain of events, it would still have no user.
1161
+
1155
1162
  After signing in, `safePath(query.from)` gives the destination back only if it stays on the site, so a crafted `/login?from=//evil.com` leads nowhere: `api.notify("navigate", safePath(query.from) ?? "/")`.
1156
1163
 
1157
1164
  #### Using Guards with Type Composition
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.26.0",
3
+ "version": "6.26.2",
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": [
package/types/auth.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { AsyncEvents } from "@inglorious/store/async"
2
+
1
3
  import { Api } from "./mount"
2
4
 
3
5
  /** The signed-in user, as the backend describes them. */
@@ -122,8 +124,47 @@ export type AuthType = {
122
124
  accountEraseAsk: Handler
123
125
  accountEraseCancel: Handler
124
126
  accountErase: Handler
125
- [handler: string]: unknown
126
- }
127
+ } & AsyncEvents<"emailCheck", AuthEntity, string, boolean> &
128
+ AsyncEvents<
129
+ "signIn",
130
+ AuthEntity,
131
+ { email: string; password: string },
132
+ AuthUser
133
+ > &
134
+ AsyncEvents<
135
+ "signUp",
136
+ AuthEntity,
137
+ { email: string; password: string; data?: Record<string, unknown> },
138
+ AuthUser | null
139
+ > &
140
+ AsyncEvents<"codeSend", AuthEntity, string, void> &
141
+ AsyncEvents<
142
+ "codeVerify",
143
+ AuthEntity,
144
+ { email: string; code: string; purpose: AuthCodePurpose },
145
+ AuthUser
146
+ > &
147
+ AsyncEvents<
148
+ "linkVerify",
149
+ AuthEntity,
150
+ { tokenHash: string; purpose: AuthCodePurpose },
151
+ AuthSignIn
152
+ > &
153
+ AsyncEvents<
154
+ "resend",
155
+ AuthEntity,
156
+ { email: string; purpose: AuthCodePurpose },
157
+ void
158
+ > &
159
+ AsyncEvents<"resetSend", AuthEntity, string, void> &
160
+ AsyncEvents<
161
+ "passwordReset",
162
+ AuthEntity,
163
+ { email: string; code: string; password: string },
164
+ AuthUser
165
+ > &
166
+ AsyncEvents<"signOut", AuthEntity, void, void> &
167
+ AsyncEvents<"accountErase", AuthEntity, void, void>
127
168
 
128
169
  /** The type behind a sign-in screen, holding the signed-in `user`. */
129
170
  export declare function createAuth(config: AuthConfig): AuthType
@@ -1,3 +1,5 @@
1
+ import type { AsyncEvents } from "@inglorious/store/async"
2
+
1
3
  import { Api } from "./mount"
2
4
 
3
5
  export interface NativeUpdateRequirement {
@@ -42,8 +44,12 @@ export type NativeUpdateType = {
42
44
  ): void
43
45
  /** Opens the store. */
44
46
  nativeUpdateApply(entity: NativeUpdateEntity): void
45
- [handler: string]: unknown
46
- }
47
+ } & AsyncEvents<
48
+ "nativeUpdateCheck",
49
+ NativeUpdateEntity,
50
+ NativeUpdateCheckPayload,
51
+ { build: number; requirement: NativeUpdateRequirement | null }
52
+ >
47
53
 
48
54
  /** The type that blocks a native build older than the minimum allowed. */
49
55
  export declare function createNativeUpdate(
package/types/push.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { AsyncEvents } from "@inglorious/store/async"
2
+
1
3
  import { Api } from "./mount"
2
4
 
3
5
  export type PushPermission = "granted" | "denied" | "prompt"
@@ -45,8 +47,9 @@ export type PushPromptType = {
45
47
  pushAsk(entity: PushPromptEntity, payload?: unknown, api?: Api): void
46
48
  /** The prompt's no, remembered on this device. */
47
49
  pushDecline(entity: PushPromptEntity): void
48
- [handler: string]: unknown
49
- }
50
+ } & AsyncEvents<"pushCheck", PushPromptEntity, void, PushPermission | null> &
51
+ AsyncEvents<"pushAsk", PushPromptEntity, void, PushPermission> &
52
+ AsyncEvents<"pushRegister", PushPromptEntity, void, void>
50
53
 
51
54
  /** The type that asks a device for pushes and keeps it registered. */
52
55
  export declare function createPushPrompt(
package/types/share.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { AsyncEvents } from "@inglorious/store/async"
2
+
1
3
  /** What `createShare` needs of `Share` from `@capacitor/share`. */
2
4
  export interface SharePlugin {
3
5
  share(options: {
@@ -29,9 +31,7 @@ export interface ShareEntity {
29
31
 
30
32
  export type ShareType = {
31
33
  create(entity: ShareEntity): void
32
- share(entity: ShareEntity, content: ShareContent): void
33
- [handler: string]: unknown
34
- }
34
+ } & AsyncEvents<"share", ShareEntity, ShareContent, ShareEntity["result"]>
35
35
 
36
36
  /**
37
37
  * The type behind a share button: the system's sheet in a native app, a