@ogygia/router 0.0.0 → 0.12.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/LICENSE +21 -0
- package/README.md +41 -1
- package/dist/LayoutChain.svelte +34 -0
- package/dist/LayoutChain.svelte.d.ts +19 -0
- package/dist/RawHtml.svelte +8 -0
- package/dist/RawHtml.svelte.d.ts +6 -0
- package/dist/client-api.d.ts +50 -0
- package/dist/client-api.js +67 -0
- package/dist/ctx.d.ts +51 -0
- package/dist/ctx.js +50 -0
- package/dist/define.d.ts +294 -0
- package/dist/define.js +159 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +7 -0
- package/dist/infer.d.ts +64 -0
- package/dist/infer.js +0 -0
- package/dist/match.d.ts +37 -0
- package/dist/match.js +125 -0
- package/dist/respond.d.ts +42 -0
- package/dist/respond.js +79 -0
- package/dist/router.d.ts +72 -0
- package/dist/router.js +404 -0
- package/dist/stream.d.ts +36 -0
- package/dist/stream.js +135 -0
- package/dist/view.d.ts +13 -0
- package/dist/view.js +0 -0
- package/dist/visitor.d.ts +8 -0
- package/dist/visitor.js +38 -0
- package/dist/vite.d.ts +2 -0
- package/dist/vite.js +18 -0
- package/package.json +68 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Puru Vijayvargia
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
1
|
# @ogygia/router
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The server router for [ogygia](https://ogygiajs.dev): SvelteKit's routing model as values. Pages, layouts, loads, actions, endpoints and streamed pages in one typed table, with `App['/path']` types and no codegen. It also ships a typed fetch client for your endpoints.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @ogygia/router
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// vite.config.ts
|
|
13
|
+
import { ogygia } from 'ogygia/vite';
|
|
14
|
+
import { router } from '@ogygia/router/vite';
|
|
15
|
+
|
|
16
|
+
export default defineConfig({
|
|
17
|
+
plugins: [ogygia(), router(), sveltekit()]
|
|
18
|
+
});
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The plugin links the CSS of every component the table's pages render.
|
|
22
|
+
|
|
23
|
+
## Use
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
// src/lib/router.ts
|
|
27
|
+
import { routes, page } from '@ogygia/router';
|
|
28
|
+
|
|
29
|
+
export const router = routes({ '/': page(Home), '/docs/[slug]': page(Doc, { load }) });
|
|
30
|
+
export type App = typeof router.$infer;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
// src/hooks.server.ts
|
|
35
|
+
import { sequence } from '@sveltejs/kit/hooks';
|
|
36
|
+
import * as ogygia from 'ogygia/server';
|
|
37
|
+
|
|
38
|
+
export const handle = sequence(ogygia.handle(), router.handle);
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The typed client is `api<App>()` from `@ogygia/router/api` (browser-safe).
|
|
42
|
+
|
|
43
|
+
Docs: [@ogygia/router](https://ogygiajs.dev/docs/router/overview), with the full [Server router](https://ogygiajs.dev/docs/router/guide) guide.
|
|
@@ -0,0 +1,34 @@
|
|
|
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();
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
{#snippet nest(i)}
|
|
22
|
+
{#if i < chain.length}
|
|
23
|
+
{@const Layout = chain[i]}
|
|
24
|
+
<Layout {data}>{@render nest(i + 1)}</Layout>
|
|
25
|
+
{:else if Fallback}
|
|
26
|
+
<Page {...props}>
|
|
27
|
+
{#snippet ogygiaFallback()}<Fallback {data} />{/snippet}
|
|
28
|
+
</Page>
|
|
29
|
+
{:else}
|
|
30
|
+
<Page {...props} />
|
|
31
|
+
{/if}
|
|
32
|
+
{/snippet}
|
|
33
|
+
|
|
34
|
+
{@render nest(0)}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
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
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
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();
|
|
6
|
+
</script>
|
|
7
|
+
|
|
8
|
+
{@html html}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { HrefParams, Params } from "./view.js";
|
|
2
|
+
export declare class ApiError extends Error {
|
|
3
|
+
readonly status: number;
|
|
4
|
+
readonly body: unknown;
|
|
5
|
+
constructor(status: number, body: unknown, message?: string);
|
|
6
|
+
}
|
|
7
|
+
/** keys of `App` that carry verb `M` — endpoint entries only (pages have no verb slots). */
|
|
8
|
+
type KeysWith<App, M extends string> = { [K in keyof App & string]: App[K] extends Record<M, {
|
|
9
|
+
out: unknown;
|
|
10
|
+
}> ? K : never; }[keyof App & string];
|
|
11
|
+
type OutOf<App, K extends keyof App, M extends string> = App[K] extends Record<M, {
|
|
12
|
+
out: infer O;
|
|
13
|
+
}> ? O : unknown;
|
|
14
|
+
type InOf<App, K extends keyof App, M extends string> = App[K] extends Record<M, {
|
|
15
|
+
in: infer I;
|
|
16
|
+
}> ? I : undefined;
|
|
17
|
+
/** `params` is REQUIRED exactly when the pattern has any (same rule as `href`). */
|
|
18
|
+
type ParamsOpt<K extends string> = {} extends Params<K> ? {
|
|
19
|
+
params?: HrefParams<K>;
|
|
20
|
+
} : {
|
|
21
|
+
params: HrefParams<K>;
|
|
22
|
+
};
|
|
23
|
+
type BodyOpt<In> = undefined extends In ? {
|
|
24
|
+
body?: In;
|
|
25
|
+
} : {
|
|
26
|
+
body: In;
|
|
27
|
+
};
|
|
28
|
+
type CallOpts<K extends string, In = undefined> = ParamsOpt<K> & BodyOpt<In> & {
|
|
29
|
+
search?: Record<string, string | number>;
|
|
30
|
+
headers?: Record<string, string>;
|
|
31
|
+
};
|
|
32
|
+
/** args tuple: the options object itself is omittable when nothing in it is required. */
|
|
33
|
+
type CallArgs<K extends string, In = undefined> = {} extends Params<K> & (undefined extends In ? {} : {
|
|
34
|
+
body: In;
|
|
35
|
+
}) ? [opts?: CallOpts<K, In>] : [opts: CallOpts<K, In>];
|
|
36
|
+
export interface ApiClientOptions {
|
|
37
|
+
/** Extra headers on every call (auth tokens, signed hops — compose with `sign_headers`). */
|
|
38
|
+
headers?: Record<string, string>;
|
|
39
|
+
/** Custom fetch (a Kit event's cookie-forwarding fetch, a test stub). Default: global. */
|
|
40
|
+
fetch?: typeof fetch;
|
|
41
|
+
}
|
|
42
|
+
export interface ApiClient<App> {
|
|
43
|
+
get<K extends KeysWith<App, "get">>(path: K, ...args: CallArgs<K>): Promise<OutOf<App, K, "get">>;
|
|
44
|
+
delete<K extends KeysWith<App, "delete">>(path: K, ...args: CallArgs<K>): Promise<OutOf<App, K, "delete">>;
|
|
45
|
+
post<K extends KeysWith<App, "post">>(path: K, ...args: CallArgs<K, InOf<App, K, "post">>): Promise<OutOf<App, K, "post">>;
|
|
46
|
+
put<K extends KeysWith<App, "put">>(path: K, ...args: CallArgs<K, InOf<App, K, "put">>): Promise<OutOf<App, K, "put">>;
|
|
47
|
+
patch<K extends KeysWith<App, "patch">>(path: K, ...args: CallArgs<K, InOf<App, K, "patch">>): Promise<OutOf<App, K, "patch">>;
|
|
48
|
+
}
|
|
49
|
+
export declare function api<App>(base: string, opts?: ApiClientOptions): ApiClient<App>;
|
|
50
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { fill } from "./match.js";
|
|
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
|
+
var ApiError = class extends Error {
|
|
24
|
+
status;
|
|
25
|
+
body;
|
|
26
|
+
constructor(status, body, message) {
|
|
27
|
+
super(message ?? `endpoint answered ${status}`);
|
|
28
|
+
this.status = status;
|
|
29
|
+
this.body = body;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function api(base, opts = {}) {
|
|
33
|
+
const f = opts.fetch ?? fetch;
|
|
34
|
+
const root = base.replace(TRAILING_SLASH_RE, "");
|
|
35
|
+
const call = async (method, path, o = {}) => {
|
|
36
|
+
let url = root + fill(path, o.params ?? {});
|
|
37
|
+
if (o.search) {
|
|
38
|
+
const qs = new URLSearchParams();
|
|
39
|
+
for (const [k, v] of Object.entries(o.search)) qs.set(k, String(v));
|
|
40
|
+
url += `?${qs}`;
|
|
41
|
+
}
|
|
42
|
+
const has_body = o.body !== void 0;
|
|
43
|
+
const res = await f(url, {
|
|
44
|
+
method,
|
|
45
|
+
headers: {
|
|
46
|
+
accept: "application/json",
|
|
47
|
+
...has_body ? { "content-type": "application/json" } : {},
|
|
48
|
+
...opts.headers,
|
|
49
|
+
...o.headers
|
|
50
|
+
},
|
|
51
|
+
...has_body ? { body: JSON.stringify(o.body) } : {}
|
|
52
|
+
});
|
|
53
|
+
if (res.status === 204) return void 0;
|
|
54
|
+
const body = (res.headers.get("content-type") ?? "").includes("json") ? await res.json().catch(() => void 0) : await res.text();
|
|
55
|
+
if (!res.ok) throw new ApiError(res.status, body);
|
|
56
|
+
return body;
|
|
57
|
+
};
|
|
58
|
+
return {
|
|
59
|
+
get: (p, ...a) => call("GET", p, a[0]),
|
|
60
|
+
delete: (p, ...a) => call("DELETE", p, a[0]),
|
|
61
|
+
post: (p, ...a) => call("POST", p, a[0]),
|
|
62
|
+
put: (p, ...a) => call("PUT", p, a[0]),
|
|
63
|
+
patch: (p, ...a) => call("PATCH", p, a[0])
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const TRAILING_SLASH_RE = /\/+$/;
|
|
67
|
+
export { ApiError, api };
|
package/dist/ctx.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Simplify } from "./view.js";
|
|
2
|
+
import { RequestEvent } from "@sveltejs/kit";
|
|
3
|
+
/** The visitor's identity — plain claims (`sub`, roles, whatever the app's session carries). */
|
|
4
|
+
export type Visitor = Record<string, unknown> & {
|
|
5
|
+
sub?: string;
|
|
6
|
+
};
|
|
7
|
+
export interface Ctx<P = Record<string, string | undefined>, S = Record<string, string>, I = undefined> {
|
|
8
|
+
/** Path params — pattern-typed, or the `params` schema's coerced output (404 on bad input). */
|
|
9
|
+
params: Simplify<P>;
|
|
10
|
+
/** Query params — raw `{ [k]: string }`, or the `search` schema's typed output (400 on bad input). */
|
|
11
|
+
search: Simplify<S>;
|
|
12
|
+
/** A body-verb endpoint's validated JSON body (`POST`/`PUT`/`PATCH` schema); else `undefined`. */
|
|
13
|
+
input: I;
|
|
14
|
+
url: URL;
|
|
15
|
+
request: Request;
|
|
16
|
+
/** The matched route pattern, e.g. `/docs/[slug]` (Kit's `route.id`). */
|
|
17
|
+
route: {
|
|
18
|
+
id: string;
|
|
19
|
+
};
|
|
20
|
+
fetch: typeof fetch;
|
|
21
|
+
cookies?: RequestEvent["cookies"];
|
|
22
|
+
locals?: RequestEvent["locals"];
|
|
23
|
+
/** Set response headers from a load/action/handler. ALWAYS works: the router applies these to
|
|
24
|
+
* the Response IT builds — Kit's own `event.setHeaders` only affects `resolve()`-built
|
|
25
|
+
* responses, and a router-rendered document bypasses resolve (found when a mount load's
|
|
26
|
+
* Server-Timing silently vanished). */
|
|
27
|
+
setHeaders?: RequestEvent["setHeaders"];
|
|
28
|
+
/** @internal headers collected via setHeaders — the dispatcher merges them onto the response. */
|
|
29
|
+
collected_headers?: Map<string, string>;
|
|
30
|
+
/** WHO is this request? THE identity — derived ONCE. Precedence: signature-bound claims from
|
|
31
|
+
* an upstream shell (fragment federation — unforgeable, they arrived inside an Ed25519
|
|
32
|
+
* signature) → the table's `visitor` resolver (`routes(table, { visitor })`) → undefined.
|
|
33
|
+
* Everything downstream READS this instead of re-deriving: experiments stick on it, mounts
|
|
34
|
+
* sign it onward, loads personalize with it. */
|
|
35
|
+
readonly visitor?: Visitor;
|
|
36
|
+
platform?: Readonly<RequestEvent["platform"]>;
|
|
37
|
+
getClientAddress?: RequestEvent["getClientAddress"];
|
|
38
|
+
/** The raw Kit event, when mounted in Kit. */
|
|
39
|
+
event?: RequestEvent;
|
|
40
|
+
/** JSON response (default application/json, no-store). */
|
|
41
|
+
json(data: unknown, init?: ResponseInit): Response;
|
|
42
|
+
/** Redirect Response (default 303). Prefer the throwable `redirect()` inside loads/actions. */
|
|
43
|
+
redirect(location: string, status?: number): Response;
|
|
44
|
+
/** Plain-text response. */
|
|
45
|
+
text(body: string, init?: ResponseInit): Response;
|
|
46
|
+
/** A rename-safe URL to a route in this router: `c.href('/report/[id]', { id })`. */
|
|
47
|
+
href(pattern: string, params?: Record<string, string | number>): string;
|
|
48
|
+
/** A per-request scratch bag (a guard writes, a handler reads). */
|
|
49
|
+
state: Record<string, unknown>;
|
|
50
|
+
}
|
|
51
|
+
export declare function make_ctx(params: Record<string, string | undefined>, url: URL, request: Request, event: RequestEvent | undefined, href: (pattern: string, params?: Record<string, string | number>) => string, routeId: string, visitor_resolver?: (c: Ctx) => Visitor | undefined): Ctx;
|
package/dist/ctx.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { json_response, redirect_response } from "./respond.js";
|
|
2
|
+
// src/ctx.ts
|
|
3
|
+
/** Signature-bound claims attached by the federation handle after verification (fragment federation). */
|
|
4
|
+
const CLAIMS = Symbol.for("ogygia.claims.v1");
|
|
5
|
+
function make_ctx(params, url, request, event, href, routeId, visitor_resolver) {
|
|
6
|
+
const search = {};
|
|
7
|
+
for (const [k, v] of url.searchParams) if (!(k in search)) search[k] = v;
|
|
8
|
+
const collected_headers = /* @__PURE__ */ new Map();
|
|
9
|
+
let visitor_memo;
|
|
10
|
+
let visitor_resolved = false;
|
|
11
|
+
const ctx = {
|
|
12
|
+
collected_headers,
|
|
13
|
+
get visitor() {
|
|
14
|
+
if (!visitor_resolved) {
|
|
15
|
+
visitor_resolved = true;
|
|
16
|
+
visitor_memo = request[CLAIMS] ?? visitor_resolver?.(ctx);
|
|
17
|
+
}
|
|
18
|
+
return visitor_memo;
|
|
19
|
+
},
|
|
20
|
+
params,
|
|
21
|
+
search,
|
|
22
|
+
input: void 0,
|
|
23
|
+
url,
|
|
24
|
+
request,
|
|
25
|
+
route: { id: routeId },
|
|
26
|
+
fetch: event?.fetch ?? fetch,
|
|
27
|
+
cookies: event?.cookies,
|
|
28
|
+
locals: event?.locals,
|
|
29
|
+
setHeaders: (headers) => {
|
|
30
|
+
for (const [k, v] of Object.entries(headers)) collected_headers.set(k.toLowerCase(), v);
|
|
31
|
+
event?.setHeaders?.(headers);
|
|
32
|
+
},
|
|
33
|
+
platform: event?.platform,
|
|
34
|
+
getClientAddress: event?.getClientAddress,
|
|
35
|
+
event,
|
|
36
|
+
json: (data, init) => json_response(data, init),
|
|
37
|
+
redirect: (location, status = 303) => redirect_response(status, location),
|
|
38
|
+
text: (body, init) => new Response(body, {
|
|
39
|
+
...init,
|
|
40
|
+
headers: {
|
|
41
|
+
"content-type": "text/plain; charset=utf-8",
|
|
42
|
+
...init?.headers
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
href,
|
|
46
|
+
state: {}
|
|
47
|
+
};
|
|
48
|
+
return ctx;
|
|
49
|
+
}
|
|
50
|
+
export { make_ctx };
|