@inglorious/web 6.13.1 → 6.14.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 CHANGED
@@ -1088,7 +1088,27 @@ export const AdminPage = {
1088
1088
  }
1089
1089
  ```
1090
1090
 
1091
- ### 5. Route Guards (Type Composition)
1091
+ ### 5. Page Entities
1092
+
1093
+ Every entity hears `routeChange`, so a page can load its data when it opens and drop it when it closes. `pageRouteChange({ select, open, close })` writes that handler: `open` runs when the route resolved to the entity, with what `select` picks from the route, `close` when it resolved elsewhere. Each is optional.
1094
+
1095
+ ```javascript
1096
+ import { pageRouteChange } from "@inglorious/web/router"
1097
+
1098
+ export const EventPage = {
1099
+ routeChange: pageRouteChange({
1100
+ select: (payload) => payload.params.eventId,
1101
+ open: (entity, eventId, api) => api.notify(`#${entity.id}:fetch`, eventId),
1102
+ close: (entity) => {
1103
+ entity.event = null
1104
+ },
1105
+ }),
1106
+ }
1107
+ ```
1108
+
1109
+ It reads the route from the payload: during `routeChange`, the router entity still names the route being left.
1110
+
1111
+ ### 6. Route Guards (Type Composition)
1092
1112
 
1093
1113
  Route guards are implemented using **type composition** — a powerful feature of `@inglorious/store` where types can be defined as arrays of behaviors that wrap and extend each other.
1094
1114
 
@@ -1101,15 +1121,14 @@ Guards are simply behaviors that intercept events (like `routeChange`) and can p
1101
1121
  export const requireAuth = (type) => ({
1102
1122
  routeChange(entity, payload, api) {
1103
1123
  // Only act when navigating to this specific route
1104
- if (payload.route !== entity.type) return
1124
+ if (payload.route !== entity.id) return
1105
1125
 
1106
1126
  // Check authentication
1107
1127
  const user = localStorage.getItem("user")
1108
1128
  if (!user) {
1109
1129
  // Redirect to login, preserving the intended destination
1110
1130
  api.notify("navigate", {
1111
- to: "/login",
1112
- redirectTo: window.location.pathname,
1131
+ to: `/login?from=${encodeURIComponent(payload.path)}`,
1113
1132
  replace: true,
1114
1133
  })
1115
1134
  return
@@ -1121,6 +1140,8 @@ export const requireAuth = (type) => ({
1121
1140
  })
1122
1141
  ```
1123
1142
 
1143
+ 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) ?? "/")`.
1144
+
1124
1145
  #### Using Guards with Type Composition
1125
1146
 
1126
1147
  ```javascript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inglorious/web",
3
- "version": "6.13.1",
3
+ "version": "6.14.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": [
@@ -1,6 +1,9 @@
1
1
  import { createHandlers } from "./handlers.js"
2
2
  import { createHelpers } from "./helpers.js"
3
3
 
4
+ export { pageRouteChange } from "./page-route.js"
5
+ export { safePath } from "./safe-path.js"
6
+
4
7
  /**
5
8
  * Route configuration map. Keys are route patterns (e.g. `/users/:id`) and
6
9
  * values are either a string type name or a loader function that returns a
@@ -0,0 +1,20 @@
1
+ /**
2
+ * A `routeChange` handler for a page entity: `open` when the route resolved to
3
+ * it, `close` when it resolved elsewhere. Each step is optional.
4
+ * @param {object} [handlers]
5
+ * @param {(payload: object) => unknown} [handlers.select] - Picks what `open` needs from the route, such as a param.
6
+ * @param {(entity: object, selected: unknown, api: object) => void} [handlers.open]
7
+ * @param {(entity: object, api: object) => void} [handlers.close]
8
+ * @returns {(entity: object, payload: object, api: object) => void}
9
+ */
10
+ export function pageRouteChange({ select, open, close } = {}) {
11
+ return function routeChange(entity, payload, api) {
12
+ // The payload, not the router entity: inside the router's own notify, the
13
+ // entity still names the route being left.
14
+ if (payload.route !== entity.id) {
15
+ close?.(entity, api)
16
+ return
17
+ }
18
+ open?.(entity, select?.(payload), api)
19
+ }
20
+ }
@@ -0,0 +1,19 @@
1
+ const BASE = "http://same.invalid"
2
+
3
+ /**
4
+ * A path taken from a URL, such as where to return after signing in, if it
5
+ * stays on this site; `null` otherwise. Comparing origins catches the forms
6
+ * browsers read as another site: `//evil.com`, `/\evil.com`, `https://evil.com`.
7
+ * @param {unknown} value
8
+ * @returns {string | null}
9
+ */
10
+ export function safePath(value) {
11
+ if (typeof value !== "string" || !value.startsWith("/")) return null
12
+ try {
13
+ const url = new URL(value, BASE)
14
+ if (url.origin !== BASE) return null
15
+ return url.pathname + url.search + url.hash
16
+ } catch {
17
+ return null
18
+ }
19
+ }
package/types/router.d.ts CHANGED
@@ -183,3 +183,41 @@ export function addRoute(
183
183
  * @param path The route path or pattern to remove.
184
184
  */
185
185
  export function removeRoute(path: string): void
186
+
187
+ /** The steps of a page entity's `routeChange`, all optional. */
188
+ export interface PageRouteHandlers<Entity, Selected> {
189
+ /** Picks what `open` needs from the route, such as a param. */
190
+ select?: (payload: RouteChangePayload) => Selected
191
+ /** The route resolved to this entity. */
192
+ open?: (entity: Entity, selected: Selected, api: Api) => void
193
+ /** The route resolved to another entity. */
194
+ close?: (entity: Entity, api: Api) => void
195
+ }
196
+
197
+ /**
198
+ * A `routeChange` handler for a page entity: `open` when the route resolved to
199
+ * it, with what `select` picks from the route, and `close` when it resolved
200
+ * elsewhere. It reads the route from the payload: inside the router's own
201
+ * notify, the router entity still names the route being left.
202
+ */
203
+ export function pageRouteChange<
204
+ Entity extends { id: string },
205
+ Selected = undefined,
206
+ >(
207
+ handlers?: PageRouteHandlers<Entity, Selected>,
208
+ ): (entity: Entity, payload: RouteChangePayload, api: Api) => void
209
+
210
+ /** What the router broadcasts as `routeChange`. */
211
+ export interface RouteChangePayload {
212
+ route: string
213
+ params: RouteParams
214
+ query: QueryParams
215
+ path: string
216
+ }
217
+
218
+ /**
219
+ * A path taken from a URL, such as where to return after signing in, if it
220
+ * stays on this site: with its query and hash. `null` for anything a browser
221
+ * would read as another site, like `//evil.com` or `/\evil.com`.
222
+ */
223
+ export function safePath(value: unknown): string | null