@inglorious/web 6.13.1 → 6.14.1

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.1",
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": [
@@ -131,7 +131,7 @@ export function createHandlers(routeConfig) {
131
131
  /**
132
132
  * Handles navigation to a new route.
133
133
  * @param {RouterEntity} entity - The router entity.
134
- * @param {string|number|{to: string, params?: object, replace?: boolean, state?: object}} payload - The navigation target or options.
134
+ * @param {string|number|{to: string, params?: object, replace?: boolean, force?: boolean, state?: object}} payload - The navigation target or options.
135
135
  * @param {Api} api - The application API.
136
136
  */
137
137
  async navigate(entity, payload, api) {
@@ -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
@@ -56,6 +56,8 @@ export interface NavigatePayload {
56
56
  params?: RouteParams
57
57
  /** If true, `history.replaceState` will be used instead of `history.pushState`. */
58
58
  replace?: boolean
59
+ /** If true, navigates even when the path is the current one, running the route again. */
60
+ force?: boolean
59
61
  /** Additional state to be stored in the browser's history. */
60
62
  state?: Record<string, any>
61
63
  }
@@ -183,3 +185,41 @@ export function addRoute(
183
185
  * @param path The route path or pattern to remove.
184
186
  */
185
187
  export function removeRoute(path: string): void
188
+
189
+ /** The steps of a page entity's `routeChange`, all optional. */
190
+ export interface PageRouteHandlers<Entity, Selected> {
191
+ /** Picks what `open` needs from the route, such as a param. */
192
+ select?: (payload: RouteChangePayload) => Selected
193
+ /** The route resolved to this entity. */
194
+ open?: (entity: Entity, selected: Selected, api: Api) => void
195
+ /** The route resolved to another entity. */
196
+ close?: (entity: Entity, api: Api) => void
197
+ }
198
+
199
+ /**
200
+ * A `routeChange` handler for a page entity: `open` when the route resolved to
201
+ * it, with what `select` picks from the route, and `close` when it resolved
202
+ * elsewhere. It reads the route from the payload: inside the router's own
203
+ * notify, the router entity still names the route being left.
204
+ */
205
+ export function pageRouteChange<
206
+ Entity extends { id: string },
207
+ Selected = undefined,
208
+ >(
209
+ handlers?: PageRouteHandlers<Entity, Selected>,
210
+ ): (entity: Entity, payload: RouteChangePayload, api: Api) => void
211
+
212
+ /** What the router broadcasts as `routeChange`. */
213
+ export interface RouteChangePayload {
214
+ route: string
215
+ params: RouteParams
216
+ query: QueryParams
217
+ path: string
218
+ }
219
+
220
+ /**
221
+ * A path taken from a URL, such as where to return after signing in, if it
222
+ * stays on this site: with its query and hash. `null` for anything a browser
223
+ * would read as another site, like `//evil.com` or `/\evil.com`.
224
+ */
225
+ export function safePath(value: unknown): string | null