@surfaces-ui/next 0.6.0 → 0.8.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/NOTICE +50 -0
- package/dist/routes.d.ts +6 -26
- package/dist/routes.js +17 -110
- package/dist/routes.js.map +1 -1
- package/package.json +6 -6
package/NOTICE
CHANGED
|
@@ -2,3 +2,53 @@ Surfaces UI
|
|
|
2
2
|
Copyright 2026 Josh Schoen
|
|
3
3
|
|
|
4
4
|
This product is licensed under the Apache License, Version 2.0 (see LICENSE).
|
|
5
|
+
|
|
6
|
+
--------------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
@surfaces-ui/icons includes drawings from Lucide (https://lucide.dev), taken
|
|
9
|
+
from lucide-static 1.84.0. Other packages include some of them by depending on
|
|
10
|
+
it or by bundling it. They are used under this license:
|
|
11
|
+
|
|
12
|
+
ISC License
|
|
13
|
+
|
|
14
|
+
Copyright (c) 2026 Lucide Icons and Contributors
|
|
15
|
+
|
|
16
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
17
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
18
|
+
copyright notice and this permission notice appear in all copies.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
21
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
22
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
23
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
24
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
25
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
26
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
The following Lucide icons are derived from the Feather project:
|
|
31
|
+
|
|
32
|
+
airplay, alert-circle, alert-octagon, alert-triangle, aperture, arrow-down-circle, arrow-down-left, arrow-down-right, arrow-down, arrow-left-circle, arrow-left, arrow-right-circle, arrow-right, arrow-up-circle, arrow-up-left, arrow-up-right, arrow-up, at-sign, calendar, cast, check, chevron-down, chevron-left, chevron-right, chevron-up, chevrons-down, chevrons-left, chevrons-right, chevrons-up, circle, clipboard, clock, code, columns, command, compass, corner-down-left, corner-down-right, corner-left-down, corner-left-up, corner-right-down, corner-right-up, corner-up-left, corner-up-right, crosshair, database, divide-circle, divide-square, dollar-sign, download, external-link, feather, frown, hash, headphones, help-circle, info, italic, key, layout, life-buoy, link-2, link, loader, lock, log-in, log-out, maximize, meh, minimize, minimize-2, minus-circle, minus-square, minus, monitor, moon, more-horizontal, more-vertical, move, music, navigation-2, navigation, octagon, pause-circle, percent, plus-circle, plus-square, plus, power, radio, rss, search, server, share, shopping-bag, sidebar, smartphone, smile, square, table-2, tablet, target, terminal, trash-2, trash, triangle, tv, type, upload, x-circle, x-octagon, x-square, x, zoom-in, zoom-out
|
|
33
|
+
|
|
34
|
+
The MIT License (MIT) (for the icons listed above)
|
|
35
|
+
|
|
36
|
+
Copyright (c) 2013-present Cole Bemis
|
|
37
|
+
|
|
38
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
39
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
40
|
+
in the Software without restriction, including without limitation the rights
|
|
41
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
42
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
43
|
+
furnished to do so, subject to the following conditions:
|
|
44
|
+
|
|
45
|
+
The above copyright notice and this permission notice shall be included in all
|
|
46
|
+
copies or substantial portions of the Software.
|
|
47
|
+
|
|
48
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
49
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
50
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
51
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
52
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
53
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
54
|
+
SOFTWARE.
|
package/dist/routes.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type PageDocument, type RouteMatch } from "@surfaces-ui/spec";
|
|
2
2
|
import type { ThemeRequest } from "@surfaces-ui/tokens";
|
|
3
3
|
/** What a route says about itself to search engines and link previews. */
|
|
4
4
|
export interface SiteMeta {
|
|
@@ -24,45 +24,25 @@ export interface SiteRoutes {
|
|
|
24
24
|
theme?: ThemeRequest;
|
|
25
25
|
pages: SiteRoute[];
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
export type RouteSegment = {
|
|
29
|
-
kind: "fixed";
|
|
30
|
-
name: string;
|
|
31
|
-
} | {
|
|
32
|
-
kind: "value";
|
|
33
|
-
name: string;
|
|
34
|
-
};
|
|
35
|
-
/** The segments of a route, or what is wrong with it. */
|
|
36
|
-
export declare function parseRoute(route: string): {
|
|
37
|
-
segments: RouteSegment[];
|
|
38
|
-
} | {
|
|
39
|
-
problem: string;
|
|
40
|
-
};
|
|
41
|
-
/** The names a route takes values for, in order: ["slug"] for "/essays/[slug]". */
|
|
42
|
-
export declare function routeValueNames(route: string): string[];
|
|
27
|
+
export { type RouteSegment, parseRoute, routePath, routeValueNames } from "@surfaces-ui/spec";
|
|
43
28
|
export interface SiteProblem {
|
|
44
29
|
route: string;
|
|
45
30
|
message: string;
|
|
46
31
|
}
|
|
47
32
|
/**
|
|
48
|
-
* What is wrong with a site's routes: a route that is not a path, two pages on one route,
|
|
49
|
-
* would answer the same address ("/[a]" and "/[b]").
|
|
50
|
-
*
|
|
33
|
+
* What is wrong with a site's routes: a route that is not a path, two pages on one route, a route with no page, or
|
|
34
|
+
* two routes that would answer the same address ("/[a]" and "/[b]"). The rules are `routeProblems` in
|
|
35
|
+
* `@surfaces-ui/spec`, shared with app documents.
|
|
51
36
|
*/
|
|
52
37
|
export declare function siteProblems(site: SiteRoutes): SiteProblem[];
|
|
53
38
|
/** A page found for an address, with the values its route took from it. */
|
|
54
|
-
export
|
|
55
|
-
entry: SiteRoute;
|
|
56
|
-
values: Record<string, string>;
|
|
57
|
-
}
|
|
39
|
+
export type MatchedRoute = RouteMatch<SiteRoute>;
|
|
58
40
|
/**
|
|
59
41
|
* The page for an address, given as Next.js gives a catch-all route's segments (`["essays", "first-post"]`; none
|
|
60
42
|
* for the home page). A route whose segments are all fixed wins over one that takes values, segment by segment
|
|
61
43
|
* from the left, as in Next.js.
|
|
62
44
|
*/
|
|
63
45
|
export declare function matchRoute(site: SiteRoutes, segments: readonly string[] | string | undefined): MatchedRoute | undefined;
|
|
64
|
-
/** The address of a route with its values filled in: "/essays/first-post". Values are encoded. */
|
|
65
|
-
export declare function routePath(route: string, values?: Record<string, string>): string;
|
|
66
46
|
/**
|
|
67
47
|
* Where a route's values come from, by route: a list of values when it takes one ("/essays/[slug]" →
|
|
68
48
|
* `["first-post", "second-post"]`), or of objects when it takes several. From files, a CMS or a database; the app
|
package/dist/routes.js
CHANGED
|
@@ -1,66 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/** The segments of a route, or what is wrong with it. */
|
|
4
|
-
export function parseRoute(route) {
|
|
5
|
-
if (typeof route !== "string" || !route.startsWith("/"))
|
|
6
|
-
return { problem: `A route is a path that starts with "/", not "${String(route)}".` };
|
|
7
|
-
if (route !== "/" && route.endsWith("/"))
|
|
8
|
-
return { problem: `Write "${route}" without the "/" at the end.` };
|
|
9
|
-
const parts = route === "/" ? [] : route.slice(1).split("/");
|
|
10
|
-
const segments = [];
|
|
11
|
-
const names = new Set();
|
|
12
|
-
for (const part of parts) {
|
|
13
|
-
const value = VALUE.exec(part);
|
|
14
|
-
if (value) {
|
|
15
|
-
const name = value[1];
|
|
16
|
-
if (names.has(name))
|
|
17
|
-
return { problem: `"${route}" names [${name}] twice.` };
|
|
18
|
-
names.add(name);
|
|
19
|
-
segments.push({ kind: "value", name });
|
|
20
|
-
}
|
|
21
|
-
else if (part.startsWith("[")) {
|
|
22
|
-
return { problem: `"${part}" in "${route}" is not a value Surfaces can fill: write one name in brackets, such as [slug]. Catch-all segments ([...path]) are not supported.` };
|
|
23
|
-
}
|
|
24
|
-
else if (!part || part === "." || part === ".." || !FIXED.test(part)) {
|
|
25
|
-
return { problem: `"${part}" in "${route}" is not a path segment.` };
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
segments.push({ kind: "fixed", name: part });
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return { segments };
|
|
32
|
-
}
|
|
33
|
-
/** The names a route takes values for, in order: ["slug"] for "/essays/[slug]". */
|
|
34
|
-
export function routeValueNames(route) {
|
|
35
|
-
const parsed = parseRoute(route);
|
|
36
|
-
return "segments" in parsed ? parsed.segments.filter((s) => s.kind === "value").map((s) => s.name) : [];
|
|
37
|
-
}
|
|
1
|
+
import { findRoute, parseRoute, routePath, routeProblems, routeValueNames } from "@surfaces-ui/spec";
|
|
2
|
+
export { parseRoute, routePath, routeValueNames } from "@surfaces-ui/spec";
|
|
38
3
|
/**
|
|
39
|
-
* What is wrong with a site's routes: a route that is not a path, two pages on one route,
|
|
40
|
-
* would answer the same address ("/[a]" and "/[b]").
|
|
41
|
-
*
|
|
4
|
+
* What is wrong with a site's routes: a route that is not a path, two pages on one route, a route with no page, or
|
|
5
|
+
* two routes that would answer the same address ("/[a]" and "/[b]"). The rules are `routeProblems` in
|
|
6
|
+
* `@surfaces-ui/spec`, shared with app documents.
|
|
42
7
|
*/
|
|
43
8
|
export function siteProblems(site) {
|
|
44
|
-
const
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
const parsed = parseRoute(route);
|
|
48
|
-
if ("problem" in parsed) {
|
|
49
|
-
problems.push({ route, message: parsed.problem });
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
if (!page || !Array.isArray(page.components))
|
|
53
|
-
problems.push({ route, message: `"${route}" has no page.` });
|
|
54
|
-
const shape = parsed.segments.map((s) => (s.kind === "fixed" ? s.name : "[]")).join("/");
|
|
55
|
-
const before = shapes.get(shape);
|
|
56
|
-
if (before === route)
|
|
57
|
-
problems.push({ route, message: `Two pages are on "${route}".` });
|
|
58
|
-
else if (before)
|
|
59
|
-
problems.push({ route, message: `"${route}" and "${before}" answer the same addresses. Give one of them a fixed segment.` });
|
|
60
|
-
else
|
|
61
|
-
shapes.set(shape, route);
|
|
62
|
-
}
|
|
63
|
-
return problems;
|
|
9
|
+
const pages = site.pages ?? [];
|
|
10
|
+
const missing = pages.filter(({ route, page }) => "segments" in parseRoute(route) && (!page || !Array.isArray(page.components))).map(({ route }) => ({ route, message: `"${route}" has no page.` }));
|
|
11
|
+
return [...routeProblems(pages), ...missing];
|
|
64
12
|
}
|
|
65
13
|
/**
|
|
66
14
|
* The page for an address, given as Next.js gives a catch-all route's segments (`["essays", "first-post"]`; none
|
|
@@ -68,58 +16,17 @@ export function siteProblems(site) {
|
|
|
68
16
|
* from the left, as in Next.js.
|
|
69
17
|
*/
|
|
70
18
|
export function matchRoute(site, segments) {
|
|
71
|
-
const parts =
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const values = {};
|
|
80
|
-
let fits = true;
|
|
81
|
-
for (const [i, segment] of parsed.segments.entries()) {
|
|
82
|
-
const part = parts[i];
|
|
83
|
-
if (segment.kind === "fixed")
|
|
84
|
-
fits = segment.name === part;
|
|
85
|
-
else if (part)
|
|
86
|
-
values[segment.name] = part;
|
|
87
|
-
else
|
|
88
|
-
fits = false;
|
|
89
|
-
if (!fits)
|
|
90
|
-
break;
|
|
19
|
+
const parts = segments === undefined ? [] : typeof segments === "string" ? [segments] : [...segments];
|
|
20
|
+
const decoded = [];
|
|
21
|
+
for (const part of parts) {
|
|
22
|
+
try {
|
|
23
|
+
decoded.push(decodeURIComponent(part));
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
91
27
|
}
|
|
92
|
-
if (!fits)
|
|
93
|
-
continue;
|
|
94
|
-
// "0" for a fixed segment sorts before "1" for a value: the more fixed segments to the left, the better.
|
|
95
|
-
const rank = parsed.segments.map((s) => (s.kind === "fixed" ? "0" : "1")).join("");
|
|
96
|
-
if (!best || rank < best.rank)
|
|
97
|
-
best = { match: { entry, values }, rank };
|
|
98
|
-
}
|
|
99
|
-
return best?.match;
|
|
100
|
-
}
|
|
101
|
-
function decodeSegment(part) {
|
|
102
|
-
try {
|
|
103
|
-
return decodeURIComponent(part);
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
return undefined;
|
|
107
28
|
}
|
|
108
|
-
|
|
109
|
-
/** The address of a route with its values filled in: "/essays/first-post". Values are encoded. */
|
|
110
|
-
export function routePath(route, values = {}) {
|
|
111
|
-
const parsed = parseRoute(route);
|
|
112
|
-
if ("problem" in parsed)
|
|
113
|
-
throw new Error(parsed.problem);
|
|
114
|
-
const parts = parsed.segments.map((segment) => {
|
|
115
|
-
if (segment.kind === "fixed")
|
|
116
|
-
return segment.name;
|
|
117
|
-
const value = values[segment.name];
|
|
118
|
-
if (!value)
|
|
119
|
-
throw new Error(`"${route}" needs a value for [${segment.name}].`);
|
|
120
|
-
return encodeURIComponent(value);
|
|
121
|
-
});
|
|
122
|
-
return `/${parts.join("/")}`;
|
|
29
|
+
return findRoute(site.pages ?? [], decoded);
|
|
123
30
|
}
|
|
124
31
|
/**
|
|
125
32
|
* Every address of a site as Next.js's `generateStaticParams` wants it for an optional catch-all route,
|
package/dist/routes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAiCA,MAAM,KAAK,GAAG,kCAAkC,CAAC;AACjD,MAAM,KAAK,GAAG,gCAAgC,CAAC;AAE/C,yDAAyD;AACzD,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,gDAAgD,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAC/I,IAAI,KAAK,KAAK,GAAG,IAAI,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,UAAU,KAAK,+BAA+B,EAAE,CAAC;IAC7G,MAAM,KAAK,GAAG,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC7D,MAAM,QAAQ,GAAmB,EAAE,CAAC;IACpC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAChC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC/B,IAAI,KAAK,EAAE,CAAC;YACV,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;YACvB,IAAI,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;gBAAE,OAAO,EAAE,OAAO,EAAE,IAAI,KAAK,YAAY,IAAI,UAAU,EAAE,CAAC;YAC7E,KAAK,CAAC,GAAG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C,CAAC;QAC7B,IAAI,CAAC,MAAM;YAAE,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,0CAA0C,KAAK,mBAAmB,CAAC,CAAC;QAC7J,KAAK,MAAM,KAAK,IAAI,MAAM,MAAM,EAAE,EAAE,CAAC;YACnC,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;YAC7G,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,KAAK,CAAC,MAAM,iDAAiD,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC7J,sEAAsE;YACtE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;QACjG,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type { PageDocument } from \"@surfaces-ui/spec\";\nimport type { ThemeRequest } from \"@surfaces-ui/tokens\";\n\n/** What a route says about itself to search engines and link previews. */\nexport interface SiteMeta {\n title?: string;\n description?: string;\n /** An image for link previews: a name the app's `resolveImage` turns into an address, or an http(s) address. */\n image?: string;\n}\n\n/** One page of a site and the route it is served on. */\nexport interface SiteRoute {\n /** A path: \"/\", \"/pricing\", \"/essays/[slug]\". A segment in brackets takes a value from the app. */\n route: string;\n page: PageDocument;\n meta?: SiteMeta;\n}\n\n/**\n * The pages of a site, each on its route. This is the part of the planned site document (`docs/plans/sites.md`,\n * step 4) that a Next.js app needs to serve pages, so that document will be accepted here as it is.\n */\nexport interface SiteRoutes {\n title?: string;\n /** The look of every page, unless a page asks for its own. */\n theme?: ThemeRequest;\n pages: SiteRoute[];\n}\n\n/** One part of a route: a fixed name, or a value named in brackets. */\nexport type RouteSegment = { kind: \"fixed\"; name: string } | { kind: \"value\"; name: string };\n\nconst FIXED = /^[A-Za-z0-9._~!$&'()*+,;=:@%-]+$/;\nconst VALUE = /^\\[([A-Za-z_][A-Za-z0-9_]*)\\]$/;\n\n/** The segments of a route, or what is wrong with it. */\nexport function parseRoute(route: string): { segments: RouteSegment[] } | { problem: string } {\n if (typeof route !== \"string\" || !route.startsWith(\"/\")) return { problem: `A route is a path that starts with \"/\", not \"${String(route)}\".` };\n if (route !== \"/\" && route.endsWith(\"/\")) return { problem: `Write \"${route}\" without the \"/\" at the end.` };\n const parts = route === \"/\" ? [] : route.slice(1).split(\"/\");\n const segments: RouteSegment[] = [];\n const names = new Set<string>();\n for (const part of parts) {\n const value = VALUE.exec(part);\n if (value) {\n const name = value[1]!;\n if (names.has(name)) return { problem: `\"${route}\" names [${name}] twice.` };\n names.add(name);\n segments.push({ kind: \"value\", name });\n } else if (part.startsWith(\"[\")) {\n return { problem: `\"${part}\" in \"${route}\" is not a value Surfaces can fill: write one name in brackets, such as [slug]. Catch-all segments ([...path]) are not supported.` };\n } else if (!part || part === \".\" || part === \"..\" || !FIXED.test(part)) {\n return { problem: `\"${part}\" in \"${route}\" is not a path segment.` };\n } else {\n segments.push({ kind: \"fixed\", name: part });\n }\n }\n return { segments };\n}\n\n/** The names a route takes values for, in order: [\"slug\"] for \"/essays/[slug]\". */\nexport function routeValueNames(route: string): string[] {\n const parsed = parseRoute(route);\n return \"segments\" in parsed ? parsed.segments.filter((s) => s.kind === \"value\").map((s) => s.name) : [];\n}\n\nexport interface SiteProblem {\n route: string;\n message: string;\n}\n\n/**\n * What is wrong with a site's routes: a route that is not a path, two pages on one route, or two routes that\n * would answer the same address (\"/[a]\" and \"/[b]\"). A fixed route always wins over one with a value, so\n * \"/essays/new\" beside \"/essays/[slug]\" is fine.\n */\nexport function siteProblems(site: SiteRoutes): SiteProblem[] {\n const problems: SiteProblem[] = [];\n const shapes = new Map<string, string>();\n for (const { route, page } of site.pages ?? []) {\n const parsed = parseRoute(route);\n if (\"problem\" in parsed) {\n problems.push({ route, message: parsed.problem });\n continue;\n }\n if (!page || !Array.isArray(page.components)) problems.push({ route, message: `\"${route}\" has no page.` });\n const shape = parsed.segments.map((s) => (s.kind === \"fixed\" ? s.name : \"[]\")).join(\"/\");\n const before = shapes.get(shape);\n if (before === route) problems.push({ route, message: `Two pages are on \"${route}\".` });\n else if (before) problems.push({ route, message: `\"${route}\" and \"${before}\" answer the same addresses. Give one of them a fixed segment.` });\n else shapes.set(shape, route);\n }\n return problems;\n}\n\n/** A page found for an address, with the values its route took from it. */\nexport interface MatchedRoute {\n entry: SiteRoute;\n values: Record<string, string>;\n}\n\n/**\n * The page for an address, given as Next.js gives a catch-all route's segments (`[\"essays\", \"first-post\"]`; none\n * for the home page). A route whose segments are all fixed wins over one that takes values, segment by segment\n * from the left, as in Next.js.\n */\nexport function matchRoute(site: SiteRoutes, segments: readonly string[] | string | undefined): MatchedRoute | undefined {\n const parts = (segments === undefined ? [] : typeof segments === \"string\" ? [segments] : [...segments]).map(decodeSegment);\n if (parts.some((part) => part === undefined)) return undefined;\n let best: { match: MatchedRoute; rank: string } | undefined;\n for (const entry of site.pages ?? []) {\n const parsed = parseRoute(entry.route);\n if (\"problem\" in parsed || parsed.segments.length !== parts.length) continue;\n const values: Record<string, string> = {};\n let fits = true;\n for (const [i, segment] of parsed.segments.entries()) {\n const part = parts[i]!;\n if (segment.kind === \"fixed\") fits = segment.name === part;\n else if (part) values[segment.name] = part;\n else fits = false;\n if (!fits) break;\n }\n if (!fits) continue;\n // \"0\" for a fixed segment sorts before \"1\" for a value: the more fixed segments to the left, the better.\n const rank = parsed.segments.map((s) => (s.kind === \"fixed\" ? \"0\" : \"1\")).join(\"\");\n if (!best || rank < best.rank) best = { match: { entry, values }, rank };\n }\n return best?.match;\n}\n\nfunction decodeSegment(part: string): string | undefined {\n try {\n return decodeURIComponent(part);\n } catch {\n return undefined;\n }\n}\n\n/** The address of a route with its values filled in: \"/essays/first-post\". Values are encoded. */\nexport function routePath(route: string, values: Record<string, string> = {}): string {\n const parsed = parseRoute(route);\n if (\"problem\" in parsed) throw new Error(parsed.problem);\n const parts = parsed.segments.map((segment) => {\n if (segment.kind === \"fixed\") return segment.name;\n const value = values[segment.name];\n if (!value) throw new Error(`\"${route}\" needs a value for [${segment.name}].`);\n return encodeURIComponent(value);\n });\n return `/${parts.join(\"/\")}`;\n}\n\n/**\n * Where a route's values come from, by route: a list of values when it takes one (\"/essays/[slug]\" →\n * `[\"first-post\", \"second-post\"]`), or of objects when it takes several. From files, a CMS or a database; the app\n * decides, and Surfaces only asks.\n */\nexport type RouteValues = Record<string, () => Iterable<string | Record<string, string>> | Promise<Iterable<string | Record<string, string>>>>;\n\nexport interface SiteParamsOptions {\n /** The name of the catch-all segment in the app's folder, `app/[[...route]]`. Default \"route\". */\n param?: string;\n}\n\n/**\n * Every address of a site as Next.js's `generateStaticParams` wants it for an optional catch-all route,\n * `app/[[...route]]/page.tsx`: `[{ route: [] }, { route: [\"pricing\"] }, { route: [\"essays\", \"first-post\"] }]`.\n * A route that takes values with none given is an error, so a static export never leaves a page out quietly.\n */\nexport async function siteParams(site: SiteRoutes, values: RouteValues = {}, options: SiteParamsOptions = {}): Promise<Record<string, string[]>[]> {\n const param = options.param ?? \"route\";\n const problems = siteProblems(site);\n if (problems.length) throw new Error(`The site's routes have problems: ${problems.map((p) => p.message).join(\" \")}`);\n const unknown = Object.keys(values).filter((route) => !site.pages.some((p) => p.route === route));\n if (unknown.length) throw new Error(`Values are given for routes the site does not have: ${unknown.join(\", \")}.`);\n const params: Record<string, string[]>[] = [];\n for (const { route } of site.pages) {\n const names = routeValueNames(route);\n if (!names.length) {\n params.push({ [param]: route === \"/\" ? [] : route.slice(1).split(\"/\") });\n continue;\n }\n const source = values[route];\n if (!source) throw new Error(`\"${route}\" takes ${names.map((n) => `[${n}]`).join(\" and \")}: pass its values to siteParams, as { \"${route}\": () => [...] }.`);\n for (const given of await source()) {\n const filled = typeof given === \"string\" ? (names.length === 1 ? { [names[0]!]: given } : undefined) : given;\n if (!filled) throw new Error(`\"${route}\" takes ${names.length} values, so give each as an object, such as { ${names.map((n) => `${n}: \"…\"`).join(\", \")} }.`);\n // Next.js hands the segments back decoded, so they are given decoded.\n params.push({ [param]: routePath(route, filled).slice(1).split(\"/\").map(decodeURIComponent) });\n }\n }\n return params;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"routes.js","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsC,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AA8BzI,OAAO,EAAqB,UAAU,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAO9F;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,IAAgB;IAC3C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;IAC/B,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,UAAU,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,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CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;YAC7G,IAAI,CAAC,MAAM;gBAAE,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,WAAW,KAAK,CAAC,MAAM,iDAAiD,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC7J,sEAAsE;YACtE,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;QACjG,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { type PageDocument, type RouteMatch, findRoute, parseRoute, routePath, routeProblems, routeValueNames } from \"@surfaces-ui/spec\";\nimport type { ThemeRequest } from \"@surfaces-ui/tokens\";\n\n/** What a route says about itself to search engines and link previews. */\nexport interface SiteMeta {\n title?: string;\n description?: string;\n /** An image for link previews: a name the app's `resolveImage` turns into an address, or an http(s) address. */\n image?: string;\n}\n\n/** One page of a site and the route it is served on. */\nexport interface SiteRoute {\n /** A path: \"/\", \"/pricing\", \"/essays/[slug]\". A segment in brackets takes a value from the app. */\n route: string;\n page: PageDocument;\n meta?: SiteMeta;\n}\n\n/**\n * The pages of a site, each on its route. This is the part of the planned site document (`docs/plans/sites.md`,\n * step 4) that a Next.js app needs to serve pages, so that document will be accepted here as it is.\n */\nexport interface SiteRoutes {\n title?: string;\n /** The look of every page, unless a page asks for its own. */\n theme?: ThemeRequest;\n pages: SiteRoute[];\n}\n\nexport { type RouteSegment, parseRoute, routePath, routeValueNames } from \"@surfaces-ui/spec\";\n\nexport interface SiteProblem {\n route: string;\n message: string;\n}\n\n/**\n * What is wrong with a site's routes: a route that is not a path, two pages on one route, a route with no page, or\n * two routes that would answer the same address (\"/[a]\" and \"/[b]\"). The rules are `routeProblems` in\n * `@surfaces-ui/spec`, shared with app documents.\n */\nexport function siteProblems(site: SiteRoutes): SiteProblem[] {\n const pages = site.pages ?? [];\n const missing = pages.filter(({ route, page }) => \"segments\" in parseRoute(route) && (!page || !Array.isArray(page.components))).map(({ route }) => ({ route, message: `\"${route}\" has no page.` }));\n return [...routeProblems(pages), ...missing];\n}\n\n/** A page found for an address, with the values its route took from it. */\nexport type MatchedRoute = RouteMatch<SiteRoute>;\n\n/**\n * The page for an address, given as Next.js gives a catch-all route's segments (`[\"essays\", \"first-post\"]`; none\n * for the home page). A route whose segments are all fixed wins over one that takes values, segment by segment\n * from the left, as in Next.js.\n */\nexport function matchRoute(site: SiteRoutes, segments: readonly string[] | string | undefined): MatchedRoute | undefined {\n const parts = segments === undefined ? [] : typeof segments === \"string\" ? [segments] : [...segments];\n const decoded: string[] = [];\n for (const part of parts) {\n try {\n decoded.push(decodeURIComponent(part));\n } catch {\n return undefined;\n }\n }\n return findRoute(site.pages ?? [], decoded);\n}\n\n/**\n * Where a route's values come from, by route: a list of values when it takes one (\"/essays/[slug]\" →\n * `[\"first-post\", \"second-post\"]`), or of objects when it takes several. From files, a CMS or a database; the app\n * decides, and Surfaces only asks.\n */\nexport type RouteValues = Record<string, () => Iterable<string | Record<string, string>> | Promise<Iterable<string | Record<string, string>>>>;\n\nexport interface SiteParamsOptions {\n /** The name of the catch-all segment in the app's folder, `app/[[...route]]`. Default \"route\". */\n param?: string;\n}\n\n/**\n * Every address of a site as Next.js's `generateStaticParams` wants it for an optional catch-all route,\n * `app/[[...route]]/page.tsx`: `[{ route: [] }, { route: [\"pricing\"] }, { route: [\"essays\", \"first-post\"] }]`.\n * A route that takes values with none given is an error, so a static export never leaves a page out quietly.\n */\nexport async function siteParams(site: SiteRoutes, values: RouteValues = {}, options: SiteParamsOptions = {}): Promise<Record<string, string[]>[]> {\n const param = options.param ?? \"route\";\n const problems = siteProblems(site);\n if (problems.length) throw new Error(`The site's routes have problems: ${problems.map((p) => p.message).join(\" \")}`);\n const unknown = Object.keys(values).filter((route) => !site.pages.some((p) => p.route === route));\n if (unknown.length) throw new Error(`Values are given for routes the site does not have: ${unknown.join(\", \")}.`);\n const params: Record<string, string[]>[] = [];\n for (const { route } of site.pages) {\n const names = routeValueNames(route);\n if (!names.length) {\n params.push({ [param]: route === \"/\" ? [] : route.slice(1).split(\"/\") });\n continue;\n }\n const source = values[route];\n if (!source) throw new Error(`\"${route}\" takes ${names.map((n) => `[${n}]`).join(\" and \")}: pass its values to siteParams, as { \"${route}\": () => [...] }.`);\n for (const given of await source()) {\n const filled = typeof given === \"string\" ? (names.length === 1 ? { [names[0]!]: given } : undefined) : given;\n if (!filled) throw new Error(`\"${route}\" takes ${names.length} values, so give each as an object, such as { ${names.map((n) => `${n}: \"…\"`).join(\", \")} }.`);\n // Next.js hands the segments back decoded, so they are given decoded.\n params.push({ [param]: routePath(route, filled).slice(1).split(\"/\").map(decodeURIComponent) });\n }\n }\n return params;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@surfaces-ui/next",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Serve Surfaces UI pages from a Next.js app: routes, metadata and server-rendered pages, with Next.js doing the routing, building and hosting",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"surfaces-ui",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"url": "git+https://github.com/teradata-labs/surfaces-ui.git",
|
|
17
17
|
"directory": "packages/next"
|
|
18
18
|
},
|
|
19
|
-
"homepage": "https://
|
|
19
|
+
"homepage": "https://surfaces-ui.com",
|
|
20
20
|
"bugs": {
|
|
21
21
|
"url": "https://github.com/teradata-labs/surfaces-ui/issues"
|
|
22
22
|
},
|
|
@@ -44,9 +44,9 @@
|
|
|
44
44
|
"react-dom": ">=19"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@surfaces-ui/components": "0.
|
|
48
|
-
"@surfaces-ui/spec": "0.
|
|
49
|
-
"@surfaces-ui/tokens": "0.
|
|
47
|
+
"@surfaces-ui/components": "0.8.0",
|
|
48
|
+
"@surfaces-ui/spec": "0.8.0",
|
|
49
|
+
"@surfaces-ui/tokens": "0.8.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@testing-library/react": "^16.3.3",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"next": "16.4.6",
|
|
57
57
|
"react": "^19.3.1",
|
|
58
58
|
"react-dom": "^19.3.1",
|
|
59
|
-
"@surfaces-ui/data": "0.
|
|
59
|
+
"@surfaces-ui/data": "0.8.0"
|
|
60
60
|
},
|
|
61
61
|
"publishConfig": {
|
|
62
62
|
"access": "public",
|