@ogygia/router 0.12.0 → 0.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/dist/LayoutChain.svelte +2 -18
- package/dist/LayoutChain.svelte.d.ts +11 -18
- package/dist/RawHtml.svelte +1 -5
- package/dist/client-api.d.ts +9 -3
- package/dist/client-api.js +18 -25
- package/dist/ctx.d.ts +3 -1
- package/dist/ctx.js +3 -9
- package/dist/define.d.ts +2 -7
- package/dist/define.js +30 -12
- package/dist/infer.d.ts +12 -25
- package/dist/match.d.ts +16 -13
- package/dist/match.js +156 -49
- package/dist/respond.d.ts +1 -5
- package/dist/respond.js +15 -14
- package/dist/router.d.ts +9 -2
- package/dist/router.js +35 -16
- package/dist/stream.d.ts +22 -8
- package/dist/stream.js +98 -49
- package/dist/view.d.ts +2 -2
- package/dist/visitor.d.ts +10 -0
- package/dist/visitor.js +11 -13
- package/dist/vite.d.ts +7 -1
- package/dist/vite.js +5 -6
- package/package.json +11 -4
package/dist/LayoutChain.svelte
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
<script>
|
|
2
|
-
|
|
3
|
-
* Renders a route's layout chain around its page. `chain` is the layout components from OUTERMOST to
|
|
4
|
-
* innermost (app shell → section shell → …); each is an inert component that renders `{@render
|
|
5
|
-
* children()}` and may read `data` (the cascaded load data at that level). At the bottom the PAGE
|
|
6
|
-
* COMPONENT is rendered AS MARKUP (`<Page {...props} />`), not as a held region — so any import-
|
|
7
|
-
* attribute mark on it (`with { wake }`, `with { render: 'deferred' }`, `with { region: 'raw' }`,
|
|
8
|
-
* `with { keep }`) emits its shell exactly as it would at any other usage site. That is what makes
|
|
9
|
-
* page-level interactivity/deferral work: the router is a normal placement site for the component.
|
|
10
|
-
*
|
|
11
|
-
* `fallback` (a deferred page's loading placeholder) is provided as the component's reserved
|
|
12
|
-
* `ogygiaFallback` snippet — the router is the deferred page's use site, so it supplies the slot a
|
|
13
|
-
* deferred component would otherwise get from its `{#snippet ogygiaFallback()}` child.
|
|
14
|
-
*
|
|
15
|
-
* Server-rendered inside document(); the layouts and the page are real regions, so islands inside
|
|
16
|
-
* them wake and their CSS rides the response.
|
|
17
|
-
*/
|
|
18
|
-
let { chain, component: Page, props, data, fallback: Fallback } = $props();
|
|
1
|
+
<script>let { chain, component: Page, props, data, fallback: Fallback } = $props();
|
|
2
|
+
export {};
|
|
19
3
|
</script>
|
|
20
4
|
|
|
21
5
|
{#snippet nest(i)}
|
|
@@ -1,19 +1,12 @@
|
|
|
1
|
+
import type { Component } from 'svelte';
|
|
2
|
+
type AnyComponent = Component<Record<string, unknown>>;
|
|
3
|
+
interface Props {
|
|
4
|
+
chain: AnyComponent[];
|
|
5
|
+
component: AnyComponent;
|
|
6
|
+
props: Record<string, unknown>;
|
|
7
|
+
data: unknown;
|
|
8
|
+
fallback?: AnyComponent;
|
|
9
|
+
}
|
|
10
|
+
declare const LayoutChain: Component<Props, {}, "">;
|
|
11
|
+
type LayoutChain = ReturnType<typeof LayoutChain>;
|
|
1
12
|
export default LayoutChain;
|
|
2
|
-
type LayoutChain = {
|
|
3
|
-
$on?(type: string, callback: (e: any) => void): () => void;
|
|
4
|
-
$set?(props: Partial<$$ComponentProps>): void;
|
|
5
|
-
};
|
|
6
|
-
declare const LayoutChain: import("svelte").Component<{
|
|
7
|
-
chain: any;
|
|
8
|
-
component: any;
|
|
9
|
-
props: any;
|
|
10
|
-
data: any;
|
|
11
|
-
fallback: any;
|
|
12
|
-
}, {}, "">;
|
|
13
|
-
type $$ComponentProps = {
|
|
14
|
-
chain: any;
|
|
15
|
-
component: any;
|
|
16
|
-
props: any;
|
|
17
|
-
data: any;
|
|
18
|
-
fallback: any;
|
|
19
|
-
};
|
package/dist/RawHtml.svelte
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
<script
|
|
2
|
-
* The page component behind an html view (a resolver that answered `{ __oghtml, html }`, a mounted
|
|
3
|
-
* document): the html is finished, this just drops it in. Static: ships as HTML, no client JS.
|
|
4
|
-
*/
|
|
5
|
-
let { html } = $props();
|
|
1
|
+
<script>let { html } = $props();
|
|
6
2
|
</script>
|
|
7
3
|
|
|
8
4
|
{@html html}
|
package/dist/client-api.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ export declare class ApiError extends Error {
|
|
|
4
4
|
readonly body: unknown;
|
|
5
5
|
constructor(status: number, body: unknown, message?: string);
|
|
6
6
|
}
|
|
7
|
-
/** keys of `App` that carry verb `M` — endpoint entries only (pages have no verb slots). */
|
|
8
7
|
type KeysWith<App, M extends string> = { [K in keyof App & string]: App[K] extends Record<M, {
|
|
9
8
|
out: unknown;
|
|
10
9
|
}> ? K : never; }[keyof App & string];
|
|
@@ -14,7 +13,6 @@ type OutOf<App, K extends keyof App, M extends string> = App[K] extends Record<M
|
|
|
14
13
|
type InOf<App, K extends keyof App, M extends string> = App[K] extends Record<M, {
|
|
15
14
|
in: infer I;
|
|
16
15
|
}> ? I : undefined;
|
|
17
|
-
/** `params` is REQUIRED exactly when the pattern has any (same rule as `href`). */
|
|
18
16
|
type ParamsOpt<K extends string> = {} extends Params<K> ? {
|
|
19
17
|
params?: HrefParams<K>;
|
|
20
18
|
} : {
|
|
@@ -29,7 +27,6 @@ type CallOpts<K extends string, In = undefined> = ParamsOpt<K> & BodyOpt<In> & {
|
|
|
29
27
|
search?: Record<string, string | number>;
|
|
30
28
|
headers?: Record<string, string>;
|
|
31
29
|
};
|
|
32
|
-
/** args tuple: the options object itself is omittable when nothing in it is required. */
|
|
33
30
|
type CallArgs<K extends string, In = undefined> = {} extends Params<K> & (undefined extends In ? {} : {
|
|
34
31
|
body: In;
|
|
35
32
|
}) ? [opts?: CallOpts<K, In>] : [opts: CallOpts<K, In>];
|
|
@@ -46,5 +43,14 @@ export interface ApiClient<App> {
|
|
|
46
43
|
put<K extends KeysWith<App, "put">>(path: K, ...args: CallArgs<K, InOf<App, K, "put">>): Promise<OutOf<App, K, "put">>;
|
|
47
44
|
patch<K extends KeysWith<App, "patch">>(path: K, ...args: CallArgs<K, InOf<App, K, "patch">>): Promise<OutOf<App, K, "patch">>;
|
|
48
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* A typed fetch client for a router's endpoints, from the app's `$infer` map. Paths are the keys
|
|
48
|
+
* that have the verb; `params` is required when the pattern has any; the body is the schema's
|
|
49
|
+
* input; the result is the handler's plain return (a raw `Response` return types as `unknown`).
|
|
50
|
+
* A non-2xx answer throws {@link ApiError}. It loads only the path grammar, so islands can use it.
|
|
51
|
+
*
|
|
52
|
+
* const cms = api<App>('https://cms.example.com');
|
|
53
|
+
* const post = await cms.get('/posts/[id]', { params: { id: '1' } });
|
|
54
|
+
*/
|
|
49
55
|
export declare function api<App>(base: string, opts?: ApiClientOptions): ApiClient<App>;
|
|
50
56
|
export {};
|
package/dist/client-api.js
CHANGED
|
@@ -1,25 +1,5 @@
|
|
|
1
1
|
import { fill } from "./match.js";
|
|
2
2
|
// src/client-api.ts
|
|
3
|
-
/**
|
|
4
|
-
* The typed API client — `$infer` for ENDPOINTS, consumed. The table already knows every
|
|
5
|
-
* endpoint's path grammar, body schema, and (for PLAIN returns, which `finalize()` serializes
|
|
6
|
-
* to JSON) its payload type; `api<App>()` hands all of that to the caller as a fetch client:
|
|
7
|
-
*
|
|
8
|
-
* export type App = typeof app.$infer; // the one map the app already exports
|
|
9
|
-
* const cms = api<App>('https://cms.internal');
|
|
10
|
-
* const post = await cms.get('/posts/[id]', { params: { id: '1' } }); // out: typed
|
|
11
|
-
* await cms.post('/api/[id]', { params: { id }, body }); // body: schema-typed
|
|
12
|
-
*
|
|
13
|
-
* Paths are constrained to keys that HAVE the verb (page paths never carry one, so they are
|
|
14
|
-
* excluded for free); `params` is required exactly when the pattern has any; a handler that
|
|
15
|
-
* returned a raw `Response` types as `unknown` (its payload is inside the Response — write
|
|
16
|
-
* plain returns for typed endpoints). Non-2xx answers throw {@link ApiError} carrying the
|
|
17
|
-
* status and the parsed error body.
|
|
18
|
-
*
|
|
19
|
-
* BROWSER-SAFE by construction: this module imports only the light path grammar (match.ts) —
|
|
20
|
-
* no svelte, no node builtins — so islands can import `@ogygia/router/api` directly. (Not
|
|
21
|
-
* `/client`: that subpath is a plugin's browser realm, booted on every page.)
|
|
22
|
-
*/
|
|
23
3
|
var ApiError = class extends Error {
|
|
24
4
|
status;
|
|
25
5
|
body;
|
|
@@ -29,16 +9,26 @@ var ApiError = class extends Error {
|
|
|
29
9
|
this.body = body;
|
|
30
10
|
}
|
|
31
11
|
};
|
|
12
|
+
/**
|
|
13
|
+
* A typed fetch client for a router's endpoints, from the app's `$infer` map. Paths are the keys
|
|
14
|
+
* that have the verb; `params` is required when the pattern has any; the body is the schema's
|
|
15
|
+
* input; the result is the handler's plain return (a raw `Response` return types as `unknown`).
|
|
16
|
+
* A non-2xx answer throws {@link ApiError}. It loads only the path grammar, so islands can use it.
|
|
17
|
+
*
|
|
18
|
+
* const cms = api<App>('https://cms.example.com');
|
|
19
|
+
* const post = await cms.get('/posts/[id]', { params: { id: '1' } });
|
|
20
|
+
*/
|
|
32
21
|
function api(base, opts = {}) {
|
|
33
22
|
const f = opts.fetch ?? fetch;
|
|
34
23
|
const root = base.replace(TRAILING_SLASH_RE, "");
|
|
35
24
|
const call = async (method, path, o = {}) => {
|
|
36
|
-
let
|
|
25
|
+
let search = "";
|
|
37
26
|
if (o.search) {
|
|
38
27
|
const qs = new URLSearchParams();
|
|
39
28
|
for (const [k, v] of Object.entries(o.search)) qs.set(k, String(v));
|
|
40
|
-
|
|
29
|
+
search = `?${qs}`;
|
|
41
30
|
}
|
|
31
|
+
const url = `${root}${fill(path, o.params ?? {})}${search}`;
|
|
42
32
|
const has_body = o.body !== void 0;
|
|
43
33
|
const res = await f(url, {
|
|
44
34
|
method,
|
|
@@ -51,9 +41,12 @@ function api(base, opts = {}) {
|
|
|
51
41
|
...has_body ? { body: JSON.stringify(o.body) } : {}
|
|
52
42
|
});
|
|
53
43
|
if (res.status === 204) return void 0;
|
|
54
|
-
const
|
|
55
|
-
if (!res.ok)
|
|
56
|
-
|
|
44
|
+
const is_json = (res.headers.get("content-type") ?? "").includes("json");
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
const body = is_json ? await res.json().catch(() => void 0) : await res.text();
|
|
47
|
+
throw new ApiError(res.status, body);
|
|
48
|
+
}
|
|
49
|
+
return is_json ? await res.json() : await res.text();
|
|
57
50
|
};
|
|
58
51
|
return {
|
|
59
52
|
get: (p, ...a) => call("GET", p, a[0]),
|
package/dist/ctx.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { Simplify } from "./view.js";
|
|
2
2
|
import { RequestEvent } from "@sveltejs/kit";
|
|
3
|
-
/** The visitor's identity — plain claims (`sub`, roles, whatever the app's session carries).
|
|
3
|
+
/** The visitor's identity — plain claims (`sub`, roles, whatever the app's session carries). A
|
|
4
|
+
* logged-out visitor has no `sub`: `anonymousVisitor()` gives `vid`, a random id from a cookie. */
|
|
4
5
|
export type Visitor = Record<string, unknown> & {
|
|
5
6
|
sub?: string;
|
|
7
|
+
vid?: string;
|
|
6
8
|
};
|
|
7
9
|
export interface Ctx<P = Record<string, string | undefined>, S = Record<string, string>, I = undefined> {
|
|
8
10
|
/** Path params — pattern-typed, or the `params` schema's coerced output (404 on bad input). */
|
package/dist/ctx.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { json_response, redirect_response } from "./respond.js";
|
|
1
|
+
import { json_response, redirect_response, text_response } from "./respond.js";
|
|
2
2
|
// src/ctx.ts
|
|
3
3
|
/** Signature-bound claims attached by the federation handle after verification (fragment federation). */
|
|
4
|
-
const CLAIMS = Symbol.for("ogygia.claims
|
|
4
|
+
const CLAIMS = Symbol.for("ogygia.claims");
|
|
5
5
|
function make_ctx(params, url, request, event, href, routeId, visitor_resolver) {
|
|
6
6
|
const search = {};
|
|
7
7
|
for (const [k, v] of url.searchParams) if (!(k in search)) search[k] = v;
|
|
@@ -35,13 +35,7 @@ function make_ctx(params, url, request, event, href, routeId, visitor_resolver)
|
|
|
35
35
|
event,
|
|
36
36
|
json: (data, init) => json_response(data, init),
|
|
37
37
|
redirect: (location, status = 303) => redirect_response(status, location),
|
|
38
|
-
text: (body, init) =>
|
|
39
|
-
...init,
|
|
40
|
-
headers: {
|
|
41
|
-
"content-type": "text/plain; charset=utf-8",
|
|
42
|
-
...init?.headers
|
|
43
|
-
}
|
|
44
|
-
}),
|
|
38
|
+
text: (body, init) => text_response(body, init),
|
|
45
39
|
href,
|
|
46
40
|
state: {}
|
|
47
41
|
};
|
package/dist/define.d.ts
CHANGED
|
@@ -44,6 +44,8 @@ export interface LoadDef<Data = unknown, P extends string = string, Prm = LooseP
|
|
|
44
44
|
}
|
|
45
45
|
/** `load(fn)` / `load(pattern, fn)`. The returned function memoizes on `c` (run once per request);
|
|
46
46
|
* the dispatcher's concurrent branch-load pass and any `await other_load(c)` share the same promise.
|
|
47
|
+
* A failed run is the request's too: every later call in it gets the same failure. A call made after
|
|
48
|
+
* the request ended rejects with the reason it ended.
|
|
47
49
|
* In a page's `load` slot, `load(fn)` takes the page's params through its contextual return type
|
|
48
50
|
* (like a verb wrapper); anywhere else they are loose. */
|
|
49
51
|
export declare function load<Data, Prm = LooseParams, Srch = Record<string, string>>(run: (c: Ctx<Prm, Srch>) => Data | Promise<Data>): LoadDef<Awaited<Data>, string, Prm, Srch>;
|
|
@@ -64,14 +66,9 @@ export declare function action<Ret, P extends string, Srch = Record<string, stri
|
|
|
64
66
|
/** Either load form `page()` / `layout()` accept: the `load()` value wrapper OR a bare typed fn. */
|
|
65
67
|
export type LoadInput = LoadDef | ((c: Ctx) => unknown);
|
|
66
68
|
export type ActionInput = ActionDef | ((c: Ctx) => unknown);
|
|
67
|
-
/** A page's `load` slot, given the params and search its `c` reads: a `load()` wrapper (which takes
|
|
68
|
-
* `P` from this) or a bare fn. */
|
|
69
69
|
type PageLoad<P, S> = LoadDef<unknown, string, P, S> | ((c: Ctx<P, S>) => unknown);
|
|
70
|
-
/** One of a page's `actions` (mirrors {@link PageLoad}). */
|
|
71
70
|
type PageAction<P, S> = ActionDef<unknown, string, P, S> | ((c: Ctx<P, S>) => unknown);
|
|
72
|
-
/** A page's `c.params`: its `params` schema's output, else the key's (`P`). */
|
|
73
71
|
type PageParams<P, PS> = [PS] extends [StandardSchemaV1] ? InferOutput<PS> : P;
|
|
74
|
-
/** A page's `c.search`: its `search` schema's output, else the raw query. */
|
|
75
72
|
type PageSearch<SS> = [SS] extends [StandardSchemaV1] ? InferOutput<SS> : Record<string, string>;
|
|
76
73
|
/** The `+page.server.ts`-shaped second argument to `page()`. Always an object (ruled). Route input is
|
|
77
74
|
* declared here (`params`/`search` schemas — coerced, typed into `c`, shown in `$infer`); `load` and
|
|
@@ -195,7 +192,6 @@ export interface LayoutDef<Name extends string = string, Load = unknown> {
|
|
|
195
192
|
/** phantom — carries the authored load type to `$infer`; never read at runtime */
|
|
196
193
|
readonly __loadType?: Load;
|
|
197
194
|
}
|
|
198
|
-
/** Prepend a layout to one entry's chain, preserving all generics (endpoints pass through). */
|
|
199
195
|
type WithLayout<D, Def extends LayoutDef> = D extends PageDef<infer L, infer Load, infer A, infer PS, infer SS, infer KP> ? PageDef<readonly [Def, ...L], Load, A, PS, SS, KP> : D;
|
|
200
196
|
export interface LayoutFn<Def extends LayoutDef> {
|
|
201
197
|
readonly __oglayout: Def;
|
|
@@ -227,7 +223,6 @@ export interface VerbEntry<Out = unknown, In = undefined, P = unknown> {
|
|
|
227
223
|
params: P;
|
|
228
224
|
};
|
|
229
225
|
}
|
|
230
|
-
/** Params a handler sees with no table key to type them (a wrapper outside a table): indexable. */
|
|
231
226
|
type LooseParams = Record<string, string | undefined>;
|
|
232
227
|
/** One verb slot of an endpoint object, as `RouteTable` accepts it: any handler, or a verb wrapper.
|
|
233
228
|
* What a slot's `c` IS comes from its table key ({@link KeyedTable}). */
|
package/dist/define.js
CHANGED
|
@@ -1,20 +1,40 @@
|
|
|
1
|
+
import { STALE, flights } from "@ogygia/machine";
|
|
2
|
+
import { request_scope } from "@ogygia/core/internal/server";
|
|
1
3
|
// src/define.ts
|
|
2
4
|
function memo_of(c) {
|
|
3
5
|
const holder = c;
|
|
4
|
-
|
|
6
|
+
if (holder.__ogloads) return holder.__ogloads;
|
|
7
|
+
const memo = flights.keyed()({
|
|
8
|
+
name: "router load",
|
|
9
|
+
key: ["def"],
|
|
10
|
+
ignore: { c: "the request this memo is made for: one per request" },
|
|
11
|
+
run: async (i) => {
|
|
12
|
+
try {
|
|
13
|
+
return {
|
|
14
|
+
ok: true,
|
|
15
|
+
value: await i.def.run(i.c)
|
|
16
|
+
};
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return {
|
|
19
|
+
ok: false,
|
|
20
|
+
error
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
request_scope(c.request).adopt(memo);
|
|
26
|
+
return holder.__ogloads = memo;
|
|
5
27
|
}
|
|
6
28
|
function load(a, b) {
|
|
7
29
|
const pattern = typeof a === "string" ? a : void 0;
|
|
8
30
|
const run = typeof a === "string" ? b : a;
|
|
9
|
-
const fn = ((c) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
return p;
|
|
17
|
-
});
|
|
31
|
+
const fn = ((c) => memo_of(c).get({
|
|
32
|
+
def: fn,
|
|
33
|
+
c
|
|
34
|
+
}).then((v) => {
|
|
35
|
+
if (v === STALE) return Promise.reject(request_scope(c.request).signal.reason);
|
|
36
|
+
return v.ok ? v.value : Promise.reject(v.error);
|
|
37
|
+
}));
|
|
18
38
|
fn.__ogload = true;
|
|
19
39
|
fn.pattern = pattern;
|
|
20
40
|
fn.run = run;
|
|
@@ -34,8 +54,6 @@ function to_load(input) {
|
|
|
34
54
|
if (input.__ogload) return input;
|
|
35
55
|
return load(input);
|
|
36
56
|
}
|
|
37
|
-
/** Normalize a bare function to an ActionDef; an `action()` wrapper passes through. Both take the
|
|
38
|
-
* widest input (`never` params/search: a load typed for any page fits). */
|
|
39
57
|
function to_action(input) {
|
|
40
58
|
if (input.__ogaction) return input;
|
|
41
59
|
return action(input);
|
package/dist/infer.d.ts
CHANGED
|
@@ -3,48 +3,32 @@ import { ActionFailure } from "./respond.js";
|
|
|
3
3
|
import { InferOutput, Params, Simplify, StandardSchemaV1 } from "./view.js";
|
|
4
4
|
import { Snippet } from "svelte";
|
|
5
5
|
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
6
|
-
/** The data a load contributes — a `load()` wrapper OR a bare function (both authoring forms). `{}`
|
|
7
|
-
* when absent / non-object. LoadDef is checked first (it is itself callable). A `Response` arm is
|
|
8
|
-
* the load's SHORT-CIRCUIT (the dispatcher returns it verbatim and never renders the component), so
|
|
9
|
-
* it is excluded from `data` — the component only ever sees the non-Response arm. */
|
|
10
6
|
type Obj<D> = D extends object ? D : {};
|
|
11
|
-
type
|
|
12
|
-
type DataOf<L> = L extends LoadDef<infer D, string, never, never> ?
|
|
7
|
+
type DataWithoutShortCircuit<D> = Obj<Exclude<D, Response>>;
|
|
8
|
+
type DataOf<L> = L extends LoadDef<infer D, string, never, never> ? DataWithoutShortCircuit<D> : L extends ((c: never) => infer R) ? DataWithoutShortCircuit<Awaited<R>> : {};
|
|
13
9
|
type LayoutData<L> = L extends LayoutDef<string, infer LD> ? DataOf<LD> : {};
|
|
14
|
-
/** Merge a layout chain's data (outermost-first) — the whole tuple. */
|
|
15
10
|
type ChainData<Ls extends readonly LayoutDef[]> = Ls extends readonly [infer H, ...infer R] ? LayoutData<H> & (R extends readonly LayoutDef[] ? ChainData<R> : {}) : {};
|
|
16
11
|
type Layouts<V> = V extends PageDef<infer L, unknown, unknown, unknown, unknown> ? L : readonly [];
|
|
17
12
|
type LoadType<V> = V extends PageDef<readonly LayoutDef[], infer Load, unknown, unknown, unknown> ? Load : undefined;
|
|
18
13
|
type ActionsType<V> = V extends PageDef<readonly LayoutDef[], unknown, infer A, unknown, unknown> ? A : undefined;
|
|
19
14
|
type ParamsSchemaType<V> = V extends PageDef<readonly LayoutDef[], unknown, unknown, infer PS, unknown> ? PS : undefined;
|
|
20
15
|
type SearchSchemaType<V> = V extends PageDef<readonly LayoutDef[], unknown, unknown, unknown, infer SS> ? SS : undefined;
|
|
21
|
-
/** A page's merged `data`: its layout chain's data ∧ its own load's data (Kit's PageData). */
|
|
22
16
|
type PageData<V> = ChainData<Layouts<V>> & DataOf<LoadType<V>>;
|
|
23
|
-
/** The `form` prop: union of the page's action returns, unwrapping `fail(status, F)` → `F`; `null`
|
|
24
|
-
* when there are no actions (Kit's ActionData shape). Redirects throw, so contribute nothing. */
|
|
25
17
|
type ActionData<R> = R extends ActionFailure<infer F> ? F : R;
|
|
26
18
|
type FormOf<V> = ActionsType<V> extends Record<string, (c: never) => infer R> ? ActionData<Awaited<R>> | null : null;
|
|
27
|
-
/** Path params: the `params` schema's output when present, else pattern-derived from the key. */
|
|
28
19
|
type ParamsOf<K extends string, V> = ParamsSchemaType<V> extends StandardSchemaV1 ? InferOutput<ParamsSchemaType<V>> : Params<K>;
|
|
29
|
-
/** Query params: the `search` schema's output when present, else `{}`. */
|
|
30
20
|
type SearchOf<V> = SearchSchemaType<V> extends StandardSchemaV1 ? InferOutput<SearchSchemaType<V>> : {};
|
|
31
|
-
/** Page entries: `{ [path]: { data, form, params, search } }` for every PageDef key. */
|
|
32
21
|
type PageEntries<T> = { [K in keyof T & string as T[K] extends AnyPageDef ? K : never]: T[K] extends AnyPageDef ? {
|
|
33
22
|
data: Simplify<PageData<T[K]>>;
|
|
34
23
|
form: FormOf<T[K]>;
|
|
35
24
|
params: Simplify<ParamsOf<K, T[K]>>;
|
|
36
25
|
search: Simplify<SearchOf<T[K]>>;
|
|
37
26
|
} : never; };
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
type LayoutEntries<Ls extends readonly LayoutDef[], Acc = {}> = Ls extends readonly [infer H, ...infer R] ? H extends LayoutDef<infer Name, unknown> ? { [P in `(${Name})`]: {
|
|
41
|
-
data: Simplify<Acc & LayoutData<H>>;
|
|
27
|
+
type LayoutEntries<Ls extends readonly LayoutDef[], AncestorData = {}> = Ls extends readonly [infer H, ...infer R] ? H extends LayoutDef<infer Name, unknown> ? { [P in `(${Name})`]: {
|
|
28
|
+
data: Simplify<AncestorData & LayoutData<H>>;
|
|
42
29
|
children: Snippet;
|
|
43
|
-
}; } & (R extends readonly LayoutDef[] ? LayoutEntries<R,
|
|
44
|
-
/** All layout entries across the table (a name repeats with the same prefix → intersection idempotent). */
|
|
30
|
+
}; } & (R extends readonly LayoutDef[] ? LayoutEntries<R, AncestorData & LayoutData<H>> : {}) : {} : {};
|
|
45
31
|
type AllLayoutEntries<T> = UnionToIntersection<{ [K in keyof T]: T[K] extends AnyPageDef ? LayoutEntries<Layouts<T[K]>> : {}; }[keyof T]>;
|
|
46
|
-
/** A handler's PAYLOAD type: a PLAIN return is the JSON body (`finalize()` serializes it); a
|
|
47
|
-
* `Response` return erases to `unknown` (the payload type is inside the Response, unknowable). */
|
|
48
32
|
type PayloadOf<R> = [R] extends [never] ? unknown : R extends Response ? unknown : R;
|
|
49
33
|
type SlotOut<H> = H extends VerbEntry<infer O, unknown> ? PayloadOf<O> : H extends ((c: never) => infer R) ? PayloadOf<Awaited<R>> : unknown;
|
|
50
34
|
type SlotIn<H> = H extends VerbEntry<unknown, infer I> ? I : undefined;
|
|
@@ -52,13 +36,16 @@ type SlotEntry<H> = {
|
|
|
52
36
|
out: SlotOut<H>;
|
|
53
37
|
in: SlotIn<H>;
|
|
54
38
|
};
|
|
55
|
-
/** One endpoint's verb map, lowercase-keyed: `{ get: { out, in }, post: … }` — plus `params` from
|
|
56
|
-
* the table key. Pages never appear here (they carry `__ogkind`), so a key having `get`/`post`
|
|
57
|
-
* is what the `api()` client constrains on. */
|
|
58
39
|
type EndpointVerbs<V> = { [M in keyof V & string as V[M] extends undefined ? never : Lowercase<M>]: SlotEntry<V[M]>; };
|
|
59
40
|
type EndpointEntries<T> = { [K in keyof T & string as T[K] extends AnyPageDef ? never : K]: T[K] extends AnyPageDef ? never : Simplify<EndpointVerbs<T[K]> & {
|
|
60
41
|
params: Simplify<Params<K>>;
|
|
61
42
|
}>; };
|
|
62
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* The `$infer` map an app exports (`export type App = typeof app.$infer`): `App['/docs/[slug]']`
|
|
45
|
+
* is a page's `{ data, form, params, search }`, `App['(name)']` a layout's `{ data, children }`, and
|
|
46
|
+
* an endpoint's key holds its lowercase verbs' `{ out, in }`. `data` merges the layout chain's loads
|
|
47
|
+
* with the page's own, as Kit's cascade does. It is computed from loads and schemas, never from
|
|
48
|
+
* components.
|
|
49
|
+
*/
|
|
63
50
|
export type InferMap<T extends TableShape> = Simplify<PageEntries<T> & EndpointEntries<T> & AllLayoutEntries<T>>;
|
|
64
51
|
export {};
|
package/dist/match.d.ts
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pattern compilation + matching for the ogygia router. Kit's route grammar, reused verbatim so
|
|
3
|
-
* nothing new is learned:
|
|
4
|
-
* /docs/[slug] — one required segment → { slug: string }
|
|
5
|
-
* /docs/[...rest] — rest, may be empty → { rest: string }
|
|
6
|
-
* /[[lang]]/docs — optional segment → { lang?: string }
|
|
7
|
-
* Matching is trailing-slash-insensitive by default (refinement 1): `/docs/foo` and `/docs/foo/`
|
|
8
|
-
* hit the same route with identical params. This module is pure — no rendering, no request — so the
|
|
9
|
-
* whole match/specificity story is unit-testable by calling `compile()` + `match_path()` directly.
|
|
10
|
-
*/
|
|
11
1
|
export interface ParamSpec {
|
|
12
2
|
name: string;
|
|
13
3
|
/** `[...rest]` — captures the remaining path (slashes included), may be '' */
|
|
@@ -20,11 +10,15 @@ export interface CompiledPattern {
|
|
|
20
10
|
pattern: string;
|
|
21
11
|
regex: RegExp;
|
|
22
12
|
params: ParamSpec[];
|
|
23
|
-
/** higher = more specific; used to order overlapping matches (static beats dynamic) */
|
|
24
|
-
score: number;
|
|
25
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Compiles a route pattern in Kit's grammar (`/docs/[slug]`, `/files/[...rest]`, `/[[lang]]/docs`,
|
|
16
|
+
* and mixed segments like `/[id].json`) into a trailing-slash-insensitive regex and its params.
|
|
17
|
+
* Throws on a malformed pattern. A table's order is {@link compile_all}'s.
|
|
18
|
+
*/
|
|
26
19
|
export declare function compile(pattern: string): CompiledPattern;
|
|
27
|
-
/** Compile a whole table's keys and sort
|
|
20
|
+
/** Compile a whole table's keys and sort them in Kit's route order, so the first match is the
|
|
21
|
+
* route Kit would pick for the same files (test/router-route-order.test.ts). */
|
|
28
22
|
export declare function compile_all(patterns: string[]): CompiledPattern[];
|
|
29
23
|
export interface PathMatch {
|
|
30
24
|
pattern: string;
|
|
@@ -32,6 +26,15 @@ export interface PathMatch {
|
|
|
32
26
|
}
|
|
33
27
|
/** Match one compiled pattern against a pathname. Returns params, or null if it doesn't match. */
|
|
34
28
|
export declare function match_one(c: CompiledPattern, pathname: string): PathMatch | null;
|
|
29
|
+
/** Fills a pattern's `[param]`, `[[optional]]` and `[...rest]` tokens from `params`, URL-encoded
|
|
30
|
+
* as Kit's `resolve()` writes them: a rest value keeps its `/` (each segment encoded), and an
|
|
31
|
+
* empty optional or rest param that is a whole segment drops that segment. A missing `[param]`
|
|
32
|
+
* becomes empty. Throws on a value that would fill a whole segment with `.` or `..`: every URL
|
|
33
|
+
* parser drops that segment, so the URL would name another route (`%2e` is read as a dot too).
|
|
34
|
+
* Throws on an empty segment too, other than a dropped one or the last: a rest value with an
|
|
35
|
+
* empty piece (a leading, trailing or doubled `/`), or a param that fills its segment empty,
|
|
36
|
+
* would build `//host` (a URL of another origin) or a path that routes elsewhere.
|
|
37
|
+
* Tests: test/property-fill-match.test.ts, test/router-match.test.ts. */
|
|
35
38
|
export declare function fill(pattern: string, params: Record<string, string | number>): string;
|
|
36
39
|
/** First (most-specific) match in a pre-sorted list, or null. */
|
|
37
40
|
export declare function match_path(sorted: CompiledPattern[], pathname: string): PathMatch | null;
|