@alecsibilia/ui-kit 0.0.0-stage → 0.1.0-alpha.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/CHANGELOG.md +7 -0
- package/LICENSE +201 -0
- package/README.md +233 -2
- package/bin/ui-kit.js +12 -0
- package/dist/alchemy/app.d.ts +34 -0
- package/dist/alchemy/app.js +88 -0
- package/dist/alchemy/index.d.ts +11 -0
- package/dist/alchemy/index.js +11 -0
- package/dist/alchemy/live-rooms.d.ts +21 -0
- package/dist/alchemy/live-rooms.js +31 -0
- package/dist/alchemy/migrations.d.ts +28 -0
- package/dist/alchemy/migrations.js +56 -0
- package/dist/alchemy/stage.d.ts +20 -0
- package/dist/alchemy/stage.js +20 -0
- package/dist/alchemy/wrangler-config.d.ts +76 -0
- package/dist/alchemy/wrangler-config.js +82 -0
- package/dist/build/app-modules.d.ts +11 -0
- package/dist/build/app-modules.js +40 -0
- package/dist/build/check.d.ts +4 -0
- package/dist/build/check.js +170 -0
- package/dist/build/cli.d.ts +1 -0
- package/dist/build/cli.js +79 -0
- package/dist/build/feature.d.ts +8 -0
- package/dist/build/feature.js +24 -0
- package/dist/build/index.d.ts +13 -0
- package/dist/build/index.js +13 -0
- package/dist/build/measure-cpu.d.ts +2 -0
- package/dist/build/measure-cpu.js +196 -0
- package/dist/build/prerender.d.ts +5 -0
- package/dist/build/prerender.js +109 -0
- package/dist/build/preview.d.ts +7 -0
- package/dist/build/preview.js +41 -0
- package/dist/build/seo-files.d.ts +27 -0
- package/dist/build/seo-files.js +24 -0
- package/dist/client/head-swap.d.ts +45 -0
- package/dist/client/head-swap.js +183 -0
- package/dist/client/hydration-pass.d.ts +56 -0
- package/dist/client/hydration-pass.js +186 -0
- package/dist/client/hydration-reset.d.ts +82 -0
- package/dist/client/hydration-reset.js +154 -0
- package/dist/client/index.d.ts +37 -0
- package/dist/client/index.js +65 -0
- package/dist/client/nav-client.d.ts +35 -0
- package/dist/client/nav-client.js +371 -0
- package/dist/live/collab/doc-provider.d.ts +74 -0
- package/dist/live/collab/doc-provider.js +412 -0
- package/dist/live/collab/index.d.ts +51 -0
- package/dist/live/collab/index.jsx +142 -0
- package/dist/live/collab-status.d.ts +25 -0
- package/dist/live/collab-status.js +17 -0
- package/dist/live/cursors/cursor-engine.d.ts +57 -0
- package/dist/live/cursors/cursor-engine.js +243 -0
- package/dist/live/cursors/index.d.ts +46 -0
- package/dist/live/cursors/index.jsx +110 -0
- package/dist/live/declaration.d.ts +113 -0
- package/dist/live/declaration.js +94 -0
- package/dist/live/index.d.ts +8 -0
- package/dist/live/index.js +8 -0
- package/dist/live/protocol.d.ts +224 -0
- package/dist/live/protocol.js +272 -0
- package/dist/live/server/doc-store.d.ts +68 -0
- package/dist/live/server/doc-store.js +187 -0
- package/dist/live/server/index.d.ts +60 -0
- package/dist/live/server/index.js +77 -0
- package/dist/live/server/room-presence.d.ts +24 -0
- package/dist/live/server/room-presence.js +123 -0
- package/dist/live/server/room-sockets.d.ts +65 -0
- package/dist/live/server/room-sockets.js +97 -0
- package/dist/live/server/room.d.ts +20 -0
- package/dist/live/server/room.js +348 -0
- package/dist/live/server/route.d.ts +56 -0
- package/dist/live/server/route.js +159 -0
- package/dist/live/server/types.d.ts +163 -0
- package/dist/live/server/types.js +2 -0
- package/dist/logic/agent-actions.d.ts +133 -0
- package/dist/logic/agent-actions.js +150 -0
- package/dist/logic/command-types.d.ts +53 -0
- package/dist/logic/command-types.js +0 -0
- package/dist/logic/dev.d.ts +9 -0
- package/dist/logic/dev.js +10 -0
- package/dist/logic/draft-rebase.d.ts +59 -0
- package/dist/logic/draft-rebase.js +71 -0
- package/dist/logic/form-state.d.ts +105 -0
- package/dist/logic/form-state.js +93 -0
- package/dist/logic/head.d.ts +145 -0
- package/dist/logic/head.js +140 -0
- package/dist/logic/index.d.ts +27 -0
- package/dist/logic/index.js +25 -0
- package/dist/logic/island-def.d.ts +89 -0
- package/dist/logic/island-def.js +59 -0
- package/dist/logic/kit-commands.d.ts +104 -0
- package/dist/logic/kit-commands.js +72 -0
- package/dist/logic/kit-errors.d.ts +25 -0
- package/dist/logic/kit-errors.js +6 -0
- package/dist/logic/listen.d.ts +99 -0
- package/dist/logic/listen.js +92 -0
- package/dist/logic/mutation-def.d.ts +65 -0
- package/dist/logic/mutation-def.js +43 -0
- package/dist/logic/navigation-data.d.ts +50 -0
- package/dist/logic/navigation-data.js +55 -0
- package/dist/logic/navigation-policy.d.ts +74 -0
- package/dist/logic/navigation-policy.js +127 -0
- package/dist/logic/page-model-def.d.ts +108 -0
- package/dist/logic/page-model-def.js +31 -0
- package/dist/logic/path.d.ts +31 -0
- package/dist/logic/path.js +48 -0
- package/dist/logic/query-def.d.ts +56 -0
- package/dist/logic/query-def.js +37 -0
- package/dist/logic/query-key.d.ts +24 -0
- package/dist/logic/query-key.js +35 -0
- package/dist/logic/query-types.d.ts +28 -0
- package/dist/logic/query-types.js +2 -0
- package/dist/logic/resources.d.ts +163 -0
- package/dist/logic/resources.js +62 -0
- package/dist/logic/result.d.ts +30 -0
- package/dist/logic/result.js +23 -0
- package/dist/logic/route-model.d.ts +139 -0
- package/dist/logic/route-model.js +207 -0
- package/dist/logic/route-params.d.ts +80 -0
- package/dist/logic/route-params.js +114 -0
- package/dist/logic/route-table.d.ts +308 -0
- package/dist/logic/route-table.js +206 -0
- package/dist/logic/seo.d.ts +61 -0
- package/dist/logic/seo.js +101 -0
- package/dist/logic/site.d.ts +12 -0
- package/dist/logic/site.js +0 -0
- package/dist/logic/subscriptions.d.ts +177 -0
- package/dist/logic/subscriptions.js +152 -0
- package/dist/render/index.d.ts +32 -0
- package/dist/render/index.js +52 -0
- package/dist/runtime/island-wake.d.ts +38 -0
- package/dist/runtime/island-wake.js +147 -0
- package/dist/runtime/query-scope.d.ts +224 -0
- package/dist/runtime/query-scope.js +222 -0
- package/dist/runtime/room-hub.d.ts +107 -0
- package/dist/runtime/room-hub.js +307 -0
- package/dist/runtime/subscription-runner.d.ts +71 -0
- package/dist/runtime/subscription-runner.js +240 -0
- package/dist/server/bindings.d.ts +41 -0
- package/dist/server/bindings.js +74 -0
- package/dist/server/browser.d.ts +9 -0
- package/dist/server/browser.js +9 -0
- package/dist/server/edge-cache.d.ts +28 -0
- package/dist/server/edge-cache.js +35 -0
- package/dist/server/errors.d.ts +45 -0
- package/dist/server/errors.js +36 -0
- package/dist/server/form-actions-setup.d.ts +1 -0
- package/dist/server/form-actions-setup.js +15 -0
- package/dist/server/form-actions.d.ts +121 -0
- package/dist/server/form-actions.js +211 -0
- package/dist/server/index.d.ts +36 -0
- package/dist/server/index.js +35 -0
- package/dist/server/inputs.d.ts +45 -0
- package/dist/server/inputs.js +77 -0
- package/dist/server/rate-limit.d.ts +29 -0
- package/dist/server/rate-limit.js +32 -0
- package/dist/server/read-cache.d.ts +74 -0
- package/dist/server/read-cache.js +58 -0
- package/dist/server/request-info.d.ts +17 -0
- package/dist/server/request-info.js +11 -0
- package/dist/server/request.d.ts +52 -0
- package/dist/server/request.js +73 -0
- package/dist/server/response-cookies.d.ts +23 -0
- package/dist/server/response-cookies.js +24 -0
- package/dist/server/run-effect.d.ts +42 -0
- package/dist/server/run-effect.js +92 -0
- package/dist/server/run.d.ts +63 -0
- package/dist/server/run.js +66 -0
- package/dist/server/session.d.ts +125 -0
- package/dist/server/session.js +247 -0
- package/dist/test/fake-bindings.d.ts +32 -0
- package/dist/test/fake-bindings.js +108 -0
- package/dist/test/fake-room.d.ts +66 -0
- package/dist/test/fake-room.js +216 -0
- package/dist/test/index.d.ts +19 -0
- package/dist/test/index.js +44 -0
- package/dist/test/probe.d.ts +11 -0
- package/dist/test/probe.js +11 -0
- package/dist/test/solid-jsx.d.ts +23 -0
- package/dist/test/solid-jsx.js +28 -0
- package/dist/test/solid-preload.d.ts +1 -0
- package/dist/test/solid-preload.js +29 -0
- package/dist/ui/code-editor/index.d.ts +21 -0
- package/dist/ui/code-editor/index.jsx +286 -0
- package/dist/ui/collab-text/index.d.ts +29 -0
- package/dist/ui/collab-text/index.jsx +96 -0
- package/dist/ui/components/controls.d.ts +69 -0
- package/dist/ui/components/controls.jsx +81 -0
- package/dist/ui/components/feedback.d.ts +26 -0
- package/dist/ui/components/feedback.jsx +80 -0
- package/dist/ui/components/layout.d.ts +74 -0
- package/dist/ui/components/layout.jsx +146 -0
- package/dist/ui/components/lists.d.ts +69 -0
- package/dist/ui/components/lists.jsx +133 -0
- package/dist/ui/components/navigation.d.ts +73 -0
- package/dist/ui/components/navigation.jsx +112 -0
- package/dist/ui/components/text.d.ts +34 -0
- package/dist/ui/components/text.jsx +163 -0
- package/dist/ui/index.d.ts +7 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/jsx-runtime.d.ts +19 -0
- package/dist/ui/jsx-runtime.js +0 -0
- package/dist/ui/list-keys.d.ts +31 -0
- package/dist/ui/list-keys.js +45 -0
- package/dist/ui/styles/base.css +38 -0
- package/dist/ui/styles/components.css +608 -0
- package/dist/ui/styles/tokens.css +27 -0
- package/dist/ui/styles/transitions.css +23 -0
- package/dist/ui/styles/workbench.css +428 -0
- package/dist/ui/styles.css +9 -0
- package/dist/view/agent-actions-view.d.ts +30 -0
- package/dist/view/agent-actions-view.js +27 -0
- package/dist/view/agent-surface.d.ts +42 -0
- package/dist/view/agent-surface.js +123 -0
- package/dist/view/app-scope.d.ts +51 -0
- package/dist/view/app-scope.js +75 -0
- package/dist/view/boundary.d.ts +18 -0
- package/dist/view/boundary.jsx +26 -0
- package/dist/view/command-runner.d.ts +16 -0
- package/dist/view/command-runner.js +245 -0
- package/dist/view/form.d.ts +82 -0
- package/dist/view/form.jsx +109 -0
- package/dist/view/head-view.d.ts +31 -0
- package/dist/view/head-view.js +46 -0
- package/dist/view/index.d.ts +41 -0
- package/dist/view/index.js +40 -0
- package/dist/view/island-root.d.ts +30 -0
- package/dist/view/island-root.js +36 -0
- package/dist/view/island.d.ts +102 -0
- package/dist/view/island.jsx +154 -0
- package/dist/view/match.d.ts +41 -0
- package/dist/view/match.js +111 -0
- package/dist/view/model.d.ts +92 -0
- package/dist/view/model.js +162 -0
- package/dist/view/navigation-head.d.ts +29 -0
- package/dist/view/navigation-head.jsx +38 -0
- package/dist/view/page-model.d.ts +46 -0
- package/dist/view/page-model.js +146 -0
- package/dist/view/query-effects.d.ts +87 -0
- package/dist/view/query-effects.js +147 -0
- package/dist/view/query-ok.d.ts +45 -0
- package/dist/view/query-ok.js +96 -0
- package/dist/view/query.d.ts +115 -0
- package/dist/view/query.jsx +179 -0
- package/dist/view/result-view.d.ts +14 -0
- package/dist/view/result-view.jsx +15 -0
- package/dist/view/route-pages.d.ts +43 -0
- package/dist/view/route-pages.js +59 -0
- package/dist/view/router.d.ts +44 -0
- package/dist/view/router.js +123 -0
- package/dist/vite/dev-middleware.d.ts +12 -0
- package/dist/vite/dev-middleware.js +39 -0
- package/dist/vite/dev-objects.d.ts +25 -0
- package/dist/vite/dev-objects.js +127 -0
- package/dist/vite/dev-platform.d.ts +19 -0
- package/dist/vite/dev-platform.js +77 -0
- package/dist/vite/dev-seo-files.d.ts +11 -0
- package/dist/vite/dev-seo-files.js +40 -0
- package/dist/vite/index.d.ts +53 -0
- package/dist/vite/index.js +148 -0
- package/dist/vite/local-config.d.ts +34 -0
- package/dist/vite/local-config.js +72 -0
- package/dist/vite/worker-build.d.ts +24 -0
- package/dist/vite/worker-build.js +84 -0
- package/dist/worker/create-worker.d.ts +52 -0
- package/dist/worker/create-worker.js +90 -0
- package/dist/worker/index.d.ts +12 -0
- package/dist/worker/index.js +12 -0
- package/dist/worker/page-routes.d.ts +88 -0
- package/dist/worker/page-routes.js +100 -0
- package/dist/worker/page-status.d.ts +41 -0
- package/dist/worker/page-status.js +23 -0
- package/dist/worker/request-prelude.d.ts +69 -0
- package/dist/worker/request-prelude.js +107 -0
- package/dist/worker/security.d.ts +76 -0
- package/dist/worker/security.js +137 -0
- package/dist/worker/server-app.d.ts +54 -0
- package/dist/worker/server-app.js +76 -0
- package/guards/browser-only.js +5 -0
- package/guards/node-only.js +6 -0
- package/guards/server-only.js +5 -0
- package/migrations/0001-sessions.sql +22 -0
- package/package.json +233 -5
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The head's client navigation data block (`<PageNavigation>` writes it,
|
|
3
|
+
* the island client entry and the navigation runtime read it). Tiny and
|
|
4
|
+
* import-free on purpose: the island entry of every page imports it, so it
|
|
5
|
+
* must not pull the route table into the shared client chunks.
|
|
6
|
+
*/
|
|
7
|
+
/** The `type` of the head's client navigation data block. */
|
|
8
|
+
export const CLIENT_NAV_DATA_TYPE = 'application/kit-nav+json';
|
|
9
|
+
/**
|
|
10
|
+
* Reads the head's client navigation data block (the runtime's side).
|
|
11
|
+
* Anything malformed reads as `undefined` (no client navigation).
|
|
12
|
+
*/
|
|
13
|
+
export function parseClientNavigationData(text) {
|
|
14
|
+
if (!text)
|
|
15
|
+
return undefined;
|
|
16
|
+
try {
|
|
17
|
+
const value = JSON.parse(text);
|
|
18
|
+
if (typeof value.section !== 'string' || value.section === '')
|
|
19
|
+
return undefined;
|
|
20
|
+
if (!Array.isArray(value.client) ||
|
|
21
|
+
!value.client.every((p) => typeof p === 'string'))
|
|
22
|
+
return undefined;
|
|
23
|
+
return { section: value.section, client: value.client };
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Whether a fetched page may be swapped into this document (pure; the
|
|
31
|
+
* runtime's last check before the swap). Only a page of the **same named
|
|
32
|
+
* section** is swapped: anything else is a full load, which is what
|
|
33
|
+
* resets persisted islands, page models and held room sockets across
|
|
34
|
+
* sections.
|
|
35
|
+
*
|
|
36
|
+
* @param here - This document's data block.
|
|
37
|
+
* @param finalPath - The fetched page's normalized pathname (after redirects).
|
|
38
|
+
* @param next - The fetched page's data block (`undefined`: none).
|
|
39
|
+
* @param matches - `matchPattern`-style test of a pattern against a path.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* navigationDecision({ section: "app", client: ["/"] }, "/", { section: "app", client: ["/"] }, m); // { swap: true }
|
|
44
|
+
* navigationDecision({ section: "app", client: ["/"] }, "/account", undefined, m); // { swap: false, reason: "outside the section" }
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export function navigationDecision(here, finalPath, next, matches) {
|
|
48
|
+
if (!here.client.some((pattern) => matches(pattern, finalPath)))
|
|
49
|
+
return { swap: false, reason: 'outside the section' };
|
|
50
|
+
if (!next)
|
|
51
|
+
return { swap: false, reason: 'the page has no client navigation' };
|
|
52
|
+
if (next.section !== here.section)
|
|
53
|
+
return { swap: false, reason: 'another section' };
|
|
54
|
+
return { swap: true };
|
|
55
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ClientNavigationData } from './navigation-data.ts';
|
|
2
|
+
import { type RouteTable } from './route-table.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Page transitions, as data from the route table (`docs/guides/ui-kit-page-transitions.md`).
|
|
5
|
+
* Pure and import-free (no Solid, no DOM), so the server head, tests and
|
|
6
|
+
* the client navigation runtime share it.
|
|
7
|
+
*
|
|
8
|
+
* - Layer 1 (every page, zero JS): `speculationRulesFor(table)` is the
|
|
9
|
+
* page's `<script type="speculationrules">`: hover prefetch for public
|
|
10
|
+
* GET page links, prerender where a route opts in, never `auth:
|
|
11
|
+
* "required"` routes or `/_server`.
|
|
12
|
+
* - Layer 2 (opt-in, `navigation: { client: "<section>" }`):
|
|
13
|
+
* `clientNavigationFor(table, url)` says whether the page at `url`
|
|
14
|
+
* installs the kit's client navigation runtime, its named section, and
|
|
15
|
+
* that section's route patterns (the only ones it may follow without a
|
|
16
|
+
* full load; `application/kit-nav+json` data block in the head).
|
|
17
|
+
*/
|
|
18
|
+
/** Links the rules never cover: the kit's opt-out, `nofollow`, downloads, new tabs. */
|
|
19
|
+
export declare const SPECULATION_EXCLUDED_LINKS = "[data-kit-reload], [rel~=\"nofollow\"], [download], [target=\"_blank\"]";
|
|
20
|
+
/** Chrome's `moderate`: on 200 ms hover or pointerdown, at most 2 at once. */
|
|
21
|
+
export declare const SPECULATION_EAGERNESS = "moderate";
|
|
22
|
+
type SpeculationRule = {
|
|
23
|
+
readonly where: unknown;
|
|
24
|
+
readonly eagerness: typeof SPECULATION_EAGERNESS;
|
|
25
|
+
};
|
|
26
|
+
/** A `speculationrules` document (only the keys the kit writes). */
|
|
27
|
+
export interface SpeculationRules {
|
|
28
|
+
readonly prefetch?: readonly SpeculationRule[];
|
|
29
|
+
readonly prerender?: readonly SpeculationRule[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Set on `<html>` by the client navigation runtime once it is installed,
|
|
33
|
+
* with the page's section as its value (`data-kit-client-nav="app"`):
|
|
34
|
+
* links to that section's routes are then fetched by the runtime itself,
|
|
35
|
+
* so the browser's speculative prefetch of the same URL (a second,
|
|
36
|
+
* concurrent server render) is switched off for them. Links into other
|
|
37
|
+
* sections (full loads) keep theirs, and so does every link with JS off.
|
|
38
|
+
*/
|
|
39
|
+
export declare const CLIENT_NAV_ACTIVE_ATTR = "data-kit-client-nav";
|
|
40
|
+
/**
|
|
41
|
+
* The page's speculation rules from the route table, or `undefined` when
|
|
42
|
+
* no route speculates. Patterns are the routes' own paths (`/plans/:planId`
|
|
43
|
+
* is a URL Pattern too), matched on this origin with any query string.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* speculationRulesFor(routes);
|
|
48
|
+
* // { prefetch: [{ where: { and: [{ or: [{ href_matches: { pathname: "/" } }, ...] }, ...] }, eagerness: "moderate" }],
|
|
49
|
+
* // prerender: [{ ... "/architecture" ... }] }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export declare function speculationRulesFor(table: RouteTable): SpeculationRules | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* JSON text for an inline `<script>` body: `<` is escaped so no value can
|
|
55
|
+
* close the element (route paths are literals, but the escape is free).
|
|
56
|
+
*/
|
|
57
|
+
export declare function inlineJson(value: unknown): string;
|
|
58
|
+
/**
|
|
59
|
+
* Whether the page at `url` installs the client navigation runtime: its
|
|
60
|
+
* named section and that section's route patterns (no other section's).
|
|
61
|
+
* Only a route in a section rendered per request (`render: "ssr"`, nonce
|
|
62
|
+
* CSP) gets it: a prerendered page has a hash CSP and can't run the next
|
|
63
|
+
* page's inline scripts, so links from it are full loads. `undefined` = a
|
|
64
|
+
* plain document (every link is a full load).
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* clientNavigationFor(routes, "/plans/voltar-main"); // { section: "app", client: ["/", "/plans/:planId"] }
|
|
69
|
+
* clientNavigationFor(routes, "/account"); // { section: "account", client: ["/account", "/sign-in"] }
|
|
70
|
+
* clientNavigationFor(routes, "/architecture"); // undefined (prerendered, a document route)
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function clientNavigationFor(table: RouteTable, url: string): ClientNavigationData | undefined;
|
|
74
|
+
export { CLIENT_NAV_DATA_TYPE, navigationDecision, parseClientNavigationData, type ClientNavigationData, type FullLoadReason, } from './navigation-data.ts';
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { normalizePathname } from './path.js';
|
|
2
|
+
import { effectiveSpeculation, routeEntryFor, routeSection, } from './route-table.js';
|
|
3
|
+
/**
|
|
4
|
+
* Page transitions, as data from the route table (`docs/guides/ui-kit-page-transitions.md`).
|
|
5
|
+
* Pure and import-free (no Solid, no DOM), so the server head, tests and
|
|
6
|
+
* the client navigation runtime share it.
|
|
7
|
+
*
|
|
8
|
+
* - Layer 1 (every page, zero JS): `speculationRulesFor(table)` is the
|
|
9
|
+
* page's `<script type="speculationrules">`: hover prefetch for public
|
|
10
|
+
* GET page links, prerender where a route opts in, never `auth:
|
|
11
|
+
* "required"` routes or `/_server`.
|
|
12
|
+
* - Layer 2 (opt-in, `navigation: { client: "<section>" }`):
|
|
13
|
+
* `clientNavigationFor(table, url)` says whether the page at `url`
|
|
14
|
+
* installs the kit's client navigation runtime, its named section, and
|
|
15
|
+
* that section's route patterns (the only ones it may follow without a
|
|
16
|
+
* full load; `application/kit-nav+json` data block in the head).
|
|
17
|
+
*/
|
|
18
|
+
/** Links the rules never cover: the kit's opt-out, `nofollow`, downloads, new tabs. */
|
|
19
|
+
export const SPECULATION_EXCLUDED_LINKS = '[data-kit-reload], [rel~="nofollow"], [download], [target="_blank"]';
|
|
20
|
+
/** Chrome's `moderate`: on 200 ms hover or pointerdown, at most 2 at once. */
|
|
21
|
+
export const SPECULATION_EAGERNESS = 'moderate';
|
|
22
|
+
const pathEntries = (table) => Object.entries(table).filter((pair) => pair[1].path !== null);
|
|
23
|
+
/**
|
|
24
|
+
* Set on `<html>` by the client navigation runtime once it is installed,
|
|
25
|
+
* with the page's section as its value (`data-kit-client-nav="app"`):
|
|
26
|
+
* links to that section's routes are then fetched by the runtime itself,
|
|
27
|
+
* so the browser's speculative prefetch of the same URL (a second,
|
|
28
|
+
* concurrent server render) is switched off for them. Links into other
|
|
29
|
+
* sections (full loads) keep theirs, and so does every link with JS off.
|
|
30
|
+
*/
|
|
31
|
+
export const CLIENT_NAV_ACTIVE_ATTR = 'data-kit-client-nav';
|
|
32
|
+
/** One rule over the given route patterns (URL Pattern pathnames, same origin, any search). */
|
|
33
|
+
function ruleFor(patterns, excluded = SPECULATION_EXCLUDED_LINKS) {
|
|
34
|
+
return {
|
|
35
|
+
where: {
|
|
36
|
+
and: [
|
|
37
|
+
{
|
|
38
|
+
or: patterns.map((pathname) => ({
|
|
39
|
+
href_matches: { pathname },
|
|
40
|
+
})),
|
|
41
|
+
},
|
|
42
|
+
{ not: { href_matches: { pathname: '/_server/*' } } },
|
|
43
|
+
{ not: { selector_matches: excluded } },
|
|
44
|
+
],
|
|
45
|
+
},
|
|
46
|
+
eagerness: SPECULATION_EAGERNESS,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The page's speculation rules from the route table, or `undefined` when
|
|
51
|
+
* no route speculates. Patterns are the routes' own paths (`/plans/:planId`
|
|
52
|
+
* is a URL Pattern too), matched on this origin with any query string.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* speculationRulesFor(routes);
|
|
57
|
+
* // { prefetch: [{ where: { and: [{ or: [{ href_matches: { pathname: "/" } }, ...] }, ...] }, eagerness: "moderate" }],
|
|
58
|
+
* // prerender: [{ ... "/architecture" ... }] }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function speculationRulesFor(table) {
|
|
62
|
+
const prefetch = [];
|
|
63
|
+
/** Client routes by section: the browser prefetches them only from other sections' pages. */
|
|
64
|
+
const prefetchClient = new Map();
|
|
65
|
+
const prerender = [];
|
|
66
|
+
for (const [, entry] of pathEntries(table)) {
|
|
67
|
+
const how = effectiveSpeculation(entry);
|
|
68
|
+
const pattern = normalizePathname(entry.path);
|
|
69
|
+
const section = routeSection(entry);
|
|
70
|
+
if (how === 'prefetch') {
|
|
71
|
+
if (section === undefined)
|
|
72
|
+
prefetch.push(pattern);
|
|
73
|
+
else
|
|
74
|
+
prefetchClient.set(section, [
|
|
75
|
+
...(prefetchClient.get(section) ?? []),
|
|
76
|
+
pattern,
|
|
77
|
+
]);
|
|
78
|
+
}
|
|
79
|
+
else if (how === 'prerender')
|
|
80
|
+
prerender.push(pattern);
|
|
81
|
+
}
|
|
82
|
+
const prefetchRules = [
|
|
83
|
+
...(prefetch.length > 0 ? [ruleFor(prefetch)] : []),
|
|
84
|
+
// A section's routes: not while that section's runtime runs (it fetches
|
|
85
|
+
// them itself); from any other page the browser still prefetches them.
|
|
86
|
+
...[...prefetchClient].map(([section, patterns]) => ruleFor(patterns, `${SPECULATION_EXCLUDED_LINKS}, html[${CLIENT_NAV_ACTIVE_ATTR}="${section}"] a`)),
|
|
87
|
+
];
|
|
88
|
+
if (prefetchRules.length === 0 && prerender.length === 0)
|
|
89
|
+
return undefined;
|
|
90
|
+
return {
|
|
91
|
+
...(prefetchRules.length > 0 ? { prefetch: prefetchRules } : {}),
|
|
92
|
+
...(prerender.length > 0 ? { prerender: [ruleFor(prerender)] } : {}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* JSON text for an inline `<script>` body: `<` is escaped so no value can
|
|
97
|
+
* close the element (route paths are literals, but the escape is free).
|
|
98
|
+
*/
|
|
99
|
+
export function inlineJson(value) {
|
|
100
|
+
return JSON.stringify(value).replace(/</g, '\\u003c');
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Whether the page at `url` installs the client navigation runtime: its
|
|
104
|
+
* named section and that section's route patterns (no other section's).
|
|
105
|
+
* Only a route in a section rendered per request (`render: "ssr"`, nonce
|
|
106
|
+
* CSP) gets it: a prerendered page has a hash CSP and can't run the next
|
|
107
|
+
* page's inline scripts, so links from it are full loads. `undefined` = a
|
|
108
|
+
* plain document (every link is a full load).
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* clientNavigationFor(routes, "/plans/voltar-main"); // { section: "app", client: ["/", "/plans/:planId"] }
|
|
113
|
+
* clientNavigationFor(routes, "/account"); // { section: "account", client: ["/account", "/sign-in"] }
|
|
114
|
+
* clientNavigationFor(routes, "/architecture"); // undefined (prerendered, a document route)
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export function clientNavigationFor(table, url) {
|
|
118
|
+
const matched = routeEntryFor(table, url);
|
|
119
|
+
const section = matched.path === null ? undefined : routeSection(matched);
|
|
120
|
+
if (section === undefined)
|
|
121
|
+
return undefined;
|
|
122
|
+
const client = pathEntries(table)
|
|
123
|
+
.filter(([, entry]) => routeSection(entry) === section)
|
|
124
|
+
.map(([, entry]) => normalizePathname(entry.path));
|
|
125
|
+
return { section, client };
|
|
126
|
+
}
|
|
127
|
+
export { CLIENT_NAV_DATA_TYPE, navigationDecision, parseClientNavigationData, } from './navigation-data.js';
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { KitCommand } from './kit-commands.ts';
|
|
2
|
+
import type { Listens } from './listen.ts';
|
|
3
|
+
import type { Tagged } from './result.ts';
|
|
4
|
+
import type { Subscription } from './subscriptions.ts';
|
|
5
|
+
import type { CommandHandlers, InitResult, TraceEntry } from '../view/model.ts';
|
|
6
|
+
/**
|
|
7
|
+
* Sends to another page model from a page model's `onTrace` (the planner
|
|
8
|
+
* records into the trace model). Creates the other model if no island has
|
|
9
|
+
* asked for it yet.
|
|
10
|
+
*/
|
|
11
|
+
export interface PageModelPeers {
|
|
12
|
+
readonly send: <M>(def: PageModelTarget<M>, msg: M) => void;
|
|
13
|
+
}
|
|
14
|
+
/** Any page model that takes `M` Messages (what `PageModelPeers.send` accepts). */
|
|
15
|
+
export interface PageModelTarget<M> {
|
|
16
|
+
readonly _tag: 'PageModelDef';
|
|
17
|
+
readonly spec: {
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly update: (model: never, msg: M) => unknown;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** App Command handlers; optional when every Command is a kit Command. */
|
|
23
|
+
type PageCommandsOption<Cmd extends Tagged, Msg> = [
|
|
24
|
+
Exclude<Cmd, KitCommand>
|
|
25
|
+
] extends [never] ? {
|
|
26
|
+
readonly commands?: CommandHandlers<Cmd, Msg>;
|
|
27
|
+
} : {
|
|
28
|
+
readonly commands: CommandHandlers<Cmd, Msg>;
|
|
29
|
+
};
|
|
30
|
+
/** What `definePageModel` takes: `createModel`'s options plus a `name`. */
|
|
31
|
+
export type PageModelSpec<Model, Msg, Cmd extends Tagged> = {
|
|
32
|
+
/**
|
|
33
|
+
* Unique on the page (`"planner"`, `"route"`): names the model's detached
|
|
34
|
+
* root (`pm-<name>`), so its owner ids never mix with an island's.
|
|
35
|
+
*/
|
|
36
|
+
readonly name: string;
|
|
37
|
+
/**
|
|
38
|
+
* Survive client navigation within the page's section (`navigation: {
|
|
39
|
+
* client: "<section>" }` routes, see `docs/guides/ui-kit-page-transitions.md`; a page
|
|
40
|
+
* of another section is a full load and starts over): the instance, its state, timers and
|
|
41
|
+
* subscriptions carry over to the next page instead of being disposed.
|
|
42
|
+
* The next page's islands hydrate against `init` (what its server HTML
|
|
43
|
+
* was rendered from) and then show the carried-over state: one
|
|
44
|
+
* fine-grained update after the pass. Use it for drafts and UI state
|
|
45
|
+
* that should outlive a page (the planner's unsaved edits); leave it off
|
|
46
|
+
* for anything tied to one page's URL (the route model). Full page loads
|
|
47
|
+
* start from `init` as always.
|
|
48
|
+
*/
|
|
49
|
+
readonly persist?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Initial model, `[model, commands]`, or a thunk returning either. Runs
|
|
52
|
+
* once per page on the server (SSR, prerender) and once in the browser:
|
|
53
|
+
* it must give both the same model (read only the URL, constants, or
|
|
54
|
+
* the page's `url`; never time, randomness or storage). Init Commands run
|
|
55
|
+
* in the browser after every island on the page has hydrated.
|
|
56
|
+
*/
|
|
57
|
+
readonly init: InitResult<Model, Cmd> | ((page: PageInfo) => InitResult<Model, Cmd>);
|
|
58
|
+
readonly update: (model: Model, msg: Msg) => Model | readonly [Model, readonly Cmd[]];
|
|
59
|
+
readonly listens?: Listens<Msg> | readonly Listens<Msg>[];
|
|
60
|
+
readonly urlChanged?: (url: string) => Msg;
|
|
61
|
+
readonly subscriptions?: (model: Model) => readonly Subscription<Msg>[];
|
|
62
|
+
/** Every Message + its Commands, with the page's other models to send to. */
|
|
63
|
+
readonly onTrace?: (entry: TraceEntry<Msg, Cmd>, peers: PageModelPeers) => void;
|
|
64
|
+
} & PageCommandsOption<Cmd, Msg>;
|
|
65
|
+
/**
|
|
66
|
+
* A page model's definition: plain data, made once at module level and
|
|
67
|
+
* looked up by islands (`usePageModel(def)`). Carries no state; the state
|
|
68
|
+
* lives in the page's `<AppScope>` (one instance per SSR request, one per
|
|
69
|
+
* browser page).
|
|
70
|
+
*/
|
|
71
|
+
export interface PageModelDef<Model, Msg, Cmd extends Tagged = never> {
|
|
72
|
+
readonly _tag: 'PageModelDef';
|
|
73
|
+
readonly spec: PageModelSpec<Model, Msg, Cmd>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Declares a page model: a Foldkit model (`init` / `update` / Commands /
|
|
77
|
+
* `listens` / `subscriptions`) that several islands on one page share.
|
|
78
|
+
*
|
|
79
|
+
* Each page gets one instance, created on first use outside every island
|
|
80
|
+
* root (Solid signals work across roots), so every island that calls
|
|
81
|
+
* `usePageModel(def)` gets the same fine-grained `state()` and `send`. On
|
|
82
|
+
* the server the same model renders the static HTML from its init state;
|
|
83
|
+
* the browser creates it from the same init, so hydration matches.
|
|
84
|
+
*
|
|
85
|
+
* Island-local state stays `createModel` inside the island.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* // planner.page-model.ts
|
|
90
|
+
* export const plannerPage = definePageModel({
|
|
91
|
+
* name: "planner",
|
|
92
|
+
* init: initialPlanModel,
|
|
93
|
+
* update: updatePlan,
|
|
94
|
+
* commands: createPlanCommands(),
|
|
95
|
+
* listens: plannerListens,
|
|
96
|
+
* onTrace: (entry, peers) => peers.send(tracePage, recordedOf(entry)),
|
|
97
|
+
* });
|
|
98
|
+
*
|
|
99
|
+
* // planner-island.tsx and trace-island.tsx
|
|
100
|
+
* const [plan, send] = usePageModel(plannerPage);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare function definePageModel<Model, Msg, Cmd extends Tagged = never>(spec: PageModelSpec<Model, Msg, Cmd>): PageModelDef<Model, Msg, Cmd>;
|
|
104
|
+
/** What a page model's `init` is given: the page's URL (path and query). */
|
|
105
|
+
export interface PageInfo {
|
|
106
|
+
readonly url: string;
|
|
107
|
+
}
|
|
108
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declares a page model: a Foldkit model (`init` / `update` / Commands /
|
|
3
|
+
* `listens` / `subscriptions`) that several islands on one page share.
|
|
4
|
+
*
|
|
5
|
+
* Each page gets one instance, created on first use outside every island
|
|
6
|
+
* root (Solid signals work across roots), so every island that calls
|
|
7
|
+
* `usePageModel(def)` gets the same fine-grained `state()` and `send`. On
|
|
8
|
+
* the server the same model renders the static HTML from its init state;
|
|
9
|
+
* the browser creates it from the same init, so hydration matches.
|
|
10
|
+
*
|
|
11
|
+
* Island-local state stays `createModel` inside the island.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // planner.page-model.ts
|
|
16
|
+
* export const plannerPage = definePageModel({
|
|
17
|
+
* name: "planner",
|
|
18
|
+
* init: initialPlanModel,
|
|
19
|
+
* update: updatePlan,
|
|
20
|
+
* commands: createPlanCommands(),
|
|
21
|
+
* listens: plannerListens,
|
|
22
|
+
* onTrace: (entry, peers) => peers.send(tracePage, recordedOf(entry)),
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // planner-island.tsx and trace-island.tsx
|
|
26
|
+
* const [plan, send] = usePageModel(plannerPage);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function definePageModel(spec) {
|
|
30
|
+
return { _tag: 'PageModelDef', spec };
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import-free URL path helpers. Safe for `.model.ts` files (no Solid, no
|
|
3
|
+
* DOM, no Effect): route models use them to turn a pathname into a route.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes a pathname: drops query/hash, ensures a leading slash and
|
|
7
|
+
* removes a trailing slash (except for the root).
|
|
8
|
+
*
|
|
9
|
+
* @param raw - A pathname or URL path (`/architecture/?x=1#y`).
|
|
10
|
+
* @returns The normalized pathname (`/architecture`).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* normalizePathname("architecture/"); // "/architecture"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function normalizePathname(raw: string): string;
|
|
18
|
+
/**
|
|
19
|
+
* Picks the route whose normalized path equals the normalized `pathname`.
|
|
20
|
+
*
|
|
21
|
+
* @param pathname - The current pathname.
|
|
22
|
+
* @param routes - Known route paths.
|
|
23
|
+
* @param fallback - Returned when no route matches.
|
|
24
|
+
* @returns The matching route path, else `fallback`.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* resolveRoute("/architecture/", ["/", "/architecture"], "/"); // "/architecture"
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function resolveRoute<R extends string>(pathname: string, routes: readonly R[], fallback: R): R;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import-free URL path helpers. Safe for `.model.ts` files (no Solid, no
|
|
3
|
+
* DOM, no Effect): route models use them to turn a pathname into a route.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Normalizes a pathname: drops query/hash, ensures a leading slash and
|
|
7
|
+
* removes a trailing slash (except for the root).
|
|
8
|
+
*
|
|
9
|
+
* @param raw - A pathname or URL path (`/architecture/?x=1#y`).
|
|
10
|
+
* @returns The normalized pathname (`/architecture`).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* normalizePathname("architecture/"); // "/architecture"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export function normalizePathname(raw) {
|
|
18
|
+
const withoutQuery = raw.split('?')[0]?.split('#')[0] ?? '/';
|
|
19
|
+
const trimmed = withoutQuery.trim();
|
|
20
|
+
if (!trimmed || trimmed === '/')
|
|
21
|
+
return '/';
|
|
22
|
+
const withLeading = trimmed.startsWith('/') ? trimmed : `/${trimmed}`;
|
|
23
|
+
return withLeading.length > 1 && withLeading.endsWith('/')
|
|
24
|
+
? withLeading.slice(0, -1)
|
|
25
|
+
: withLeading;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Picks the route whose normalized path equals the normalized `pathname`.
|
|
29
|
+
*
|
|
30
|
+
* @param pathname - The current pathname.
|
|
31
|
+
* @param routes - Known route paths.
|
|
32
|
+
* @param fallback - Returned when no route matches.
|
|
33
|
+
* @returns The matching route path, else `fallback`.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* resolveRoute("/architecture/", ["/", "/architecture"], "/"); // "/architecture"
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function resolveRoute(pathname, routes, fallback) {
|
|
41
|
+
const normalized = normalizePathname(pathname);
|
|
42
|
+
for (const route of routes) {
|
|
43
|
+
if (normalizePathname(route) === normalized) {
|
|
44
|
+
return route;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return fallback;
|
|
48
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Result as ResultValue, Tagged } from './result.ts';
|
|
2
|
+
declare const QUERY_BRAND: unique symbol;
|
|
3
|
+
/**
|
|
4
|
+
* Branded, framework-controlled query definition produced exclusively by
|
|
5
|
+
* `createQueryRegistry(...)`. Raw functions cannot satisfy `QueryDef` and are
|
|
6
|
+
* rejected by `<Query query={...} />` at compile time.
|
|
7
|
+
*/
|
|
8
|
+
export interface QueryDef<Input, T, E extends Tagged, Id extends string = string> {
|
|
9
|
+
readonly [QUERY_BRAND]: true;
|
|
10
|
+
/** `"<registry id>/<name>"`, e.g. `"planner/plan"`. */
|
|
11
|
+
readonly id: Id;
|
|
12
|
+
/** Registry key, e.g. `"plan"`. */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Serialized input (`inputKeyOf`). */
|
|
15
|
+
readonly inputKey: (input: Input) => string;
|
|
16
|
+
readonly execute: (input: Input) => Promise<ResultValue<T, E>> | ResultValue<T, E>;
|
|
17
|
+
}
|
|
18
|
+
export type AnyQueryFn = (() => Promise<ResultValue<any, any>> | ResultValue<any, any>) | ((input: any) => Promise<ResultValue<any, any>> | ResultValue<any, any>);
|
|
19
|
+
type QueryResultOf<Fn extends AnyQueryFn> = Awaited<ReturnType<Fn>>;
|
|
20
|
+
export type QueryValueOf<Fn extends AnyQueryFn> = QueryResultOf<Fn> extends ResultValue<infer T, any> ? T : never;
|
|
21
|
+
export type QueryErrorOf<Fn extends AnyQueryFn> = QueryResultOf<Fn> extends ResultValue<any, infer E> ? E : never;
|
|
22
|
+
export type QueryInputOf<Fn extends AnyQueryFn> = Parameters<Fn> extends [] ? void : Parameters<Fn> extends [infer Input] ? Input : Parameters<Fn> extends [(infer Input)?] ? Input | undefined : never;
|
|
23
|
+
export type QueryRegistry<Id extends string, Defs extends Record<string, AnyQueryFn>> = {
|
|
24
|
+
readonly [K in keyof Defs & string]: QueryDef<QueryInputOf<Defs[K]>, QueryValueOf<Defs[K]>, QueryErrorOf<Defs[K]>, `${Id}/${K}`>;
|
|
25
|
+
};
|
|
26
|
+
/** Any `createQueryRegistry(...)` result. */
|
|
27
|
+
export type AnyQueryRegistry = {
|
|
28
|
+
readonly [name: string]: QueryDef<any, any, any>;
|
|
29
|
+
};
|
|
30
|
+
/** The registry id of a registry type (`"planner"` for `typeof plannerQueries`). */
|
|
31
|
+
export type RegistryIdOf<R extends AnyQueryRegistry> = R[keyof R]['id'] extends `${infer Id}/${string}` ? Id : string;
|
|
32
|
+
/** The input type of query `K` of registry `R`. */
|
|
33
|
+
export type RegistryInputOf<R extends AnyQueryRegistry, K extends keyof R> = R[K] extends QueryDef<infer Input, any, any> ? Input : never;
|
|
34
|
+
/**
|
|
35
|
+
* Registers a per-feature set of server functions as branded `QueryDef`s
|
|
36
|
+
* accepted by `<Query query={...} input={...} />`.
|
|
37
|
+
*
|
|
38
|
+
* `id` names the registry (unique per app, e.g. the feature name). Each
|
|
39
|
+
* query's id is `"<id>/<key>"`: `listen(...)`, `refetch(...)` and mutation
|
|
40
|
+
* `invalidates` lists refer to queries by it, so two registries that both
|
|
41
|
+
* have a `plan` query never cross-talk.
|
|
42
|
+
*
|
|
43
|
+
* Scoped per feature (e.g. `plannerQueries`) so unused route registries stay
|
|
44
|
+
* tree-shakeable across bundles.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* export const plannerQueries = createQueryRegistry("planner", {
|
|
49
|
+
* plan: loadPlan,
|
|
50
|
+
* character: loadCharacter,
|
|
51
|
+
* });
|
|
52
|
+
* plannerQueries.plan.id; // "planner/plan"
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function createQueryRegistry<const Id extends string, const Defs extends Record<string, AnyQueryFn>>(id: Id, defs: Defs): QueryRegistry<Id, Defs>;
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { inputKeyOf, queryIdOf } from './query-key.js';
|
|
2
|
+
const QUERY_BRAND = Symbol('our.kit.query');
|
|
3
|
+
/**
|
|
4
|
+
* Registers a per-feature set of server functions as branded `QueryDef`s
|
|
5
|
+
* accepted by `<Query query={...} input={...} />`.
|
|
6
|
+
*
|
|
7
|
+
* `id` names the registry (unique per app, e.g. the feature name). Each
|
|
8
|
+
* query's id is `"<id>/<key>"`: `listen(...)`, `refetch(...)` and mutation
|
|
9
|
+
* `invalidates` lists refer to queries by it, so two registries that both
|
|
10
|
+
* have a `plan` query never cross-talk.
|
|
11
|
+
*
|
|
12
|
+
* Scoped per feature (e.g. `plannerQueries`) so unused route registries stay
|
|
13
|
+
* tree-shakeable across bundles.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* export const plannerQueries = createQueryRegistry("planner", {
|
|
18
|
+
* plan: loadPlan,
|
|
19
|
+
* character: loadCharacter,
|
|
20
|
+
* });
|
|
21
|
+
* plannerQueries.plan.id; // "planner/plan"
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function createQueryRegistry(id, defs) {
|
|
25
|
+
const registry = {};
|
|
26
|
+
for (const name of Object.keys(defs)) {
|
|
27
|
+
const run = defs[name];
|
|
28
|
+
registry[name] = {
|
|
29
|
+
[QUERY_BRAND]: true,
|
|
30
|
+
id: queryIdOf(id, name),
|
|
31
|
+
name,
|
|
32
|
+
inputKey: inputKeyOf,
|
|
33
|
+
execute: (input) => input === undefined ? run() : run(input),
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return registry;
|
|
37
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import-free query identity helpers, shared by registries (`query-def.ts`),
|
|
3
|
+
* the scope (`query-scope.ts`), `listen(...)` and the `Refetch` Command.
|
|
4
|
+
* Safe for `.model.ts` files.
|
|
5
|
+
*
|
|
6
|
+
* - A **query id** is `"<registry id>/<query name>"` (e.g. `"planner/plan"`):
|
|
7
|
+
* unique across registries, so two features that both name a query `plan`
|
|
8
|
+
* never cross-talk.
|
|
9
|
+
* - An **input key** is the query input serialized as JSON (`""` for no
|
|
10
|
+
* input): two inputs of one query are two separate outcomes / caches.
|
|
11
|
+
*/
|
|
12
|
+
/** `"<registry>/<name>"`. */
|
|
13
|
+
export declare function queryIdOf(registry: string, name: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Serializes a query input for keys. `undefined` (no input) is `""`;
|
|
16
|
+
* anything JSON cannot encode is `"[unserializable]"`.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* inputKeyOf({ simulateOverload: true }); // '{"simulateOverload":true}'
|
|
21
|
+
* inputKeyOf(undefined); // ""
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function inputKeyOf(input: unknown): string;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import-free query identity helpers, shared by registries (`query-def.ts`),
|
|
3
|
+
* the scope (`query-scope.ts`), `listen(...)` and the `Refetch` Command.
|
|
4
|
+
* Safe for `.model.ts` files.
|
|
5
|
+
*
|
|
6
|
+
* - A **query id** is `"<registry id>/<query name>"` (e.g. `"planner/plan"`):
|
|
7
|
+
* unique across registries, so two features that both name a query `plan`
|
|
8
|
+
* never cross-talk.
|
|
9
|
+
* - An **input key** is the query input serialized as JSON (`""` for no
|
|
10
|
+
* input): two inputs of one query are two separate outcomes / caches.
|
|
11
|
+
*/
|
|
12
|
+
/** `"<registry>/<name>"`. */
|
|
13
|
+
export function queryIdOf(registry, name) {
|
|
14
|
+
return `${registry}/${name}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Serializes a query input for keys. `undefined` (no input) is `""`;
|
|
18
|
+
* anything JSON cannot encode is `"[unserializable]"`.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* inputKeyOf({ simulateOverload: true }); // '{"simulateOverload":true}'
|
|
23
|
+
* inputKeyOf(undefined); // ""
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function inputKeyOf(input) {
|
|
27
|
+
if (input === undefined)
|
|
28
|
+
return '';
|
|
29
|
+
try {
|
|
30
|
+
return JSON.stringify(input) ?? '';
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return '[unserializable]';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Tagged } from './result.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Minimum shape every expected `<Query>` failure must have: a `_tag`
|
|
4
|
+
* discriminant plus a user-safe `message`. Effect `Schema.TaggedError`s that
|
|
5
|
+
* declare `message: Schema.String` satisfy this once they pass through
|
|
6
|
+
* `runEffectResult` (as `PlainTaggedError<E>`).
|
|
7
|
+
*/
|
|
8
|
+
export interface QueryFailure extends Tagged {
|
|
9
|
+
readonly message: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Synthetic error `<Query>` hands to its `error` cases when loading failed in
|
|
13
|
+
* a way the query's typed `E` does not describe: a thrown error, an Effect
|
|
14
|
+
* defect, or a network/RPC failure. The original error is logged to the
|
|
15
|
+
* console and never shown to the user.
|
|
16
|
+
*/
|
|
17
|
+
export interface UnexpectedQueryError {
|
|
18
|
+
readonly _tag: 'Unexpected';
|
|
19
|
+
readonly message: string;
|
|
20
|
+
}
|
|
21
|
+
/** Message shown for every `Unexpected` query error. */
|
|
22
|
+
export declare const UNEXPECTED_QUERY_MESSAGE = "Something went wrong.";
|
|
23
|
+
/**
|
|
24
|
+
* Everything a `<Query>` can fail with: the query's typed failures `E` plus
|
|
25
|
+
* `UnexpectedQueryError`. Discriminated by `_tag`, so `<Query error={...}>`
|
|
26
|
+
* case objects and `listen(...)` `failed` handlers must cover `Unexpected`.
|
|
27
|
+
*/
|
|
28
|
+
export type QueryError<E extends Tagged> = E | UnexpectedQueryError;
|