feat: expose layouts and pages on App (#3086)

This PR extracts the functionality from the `fs_routes` plugin and
exposes it onto `App`. These are the new APIs:

- `app.layout(pattern, route)`
- `app.onError(pattern, route)`
- `app.notFound(pattern, route)`
- `app.route(pattern, route)`

This also removes the need for `.use(staticFiles())`. This line can be
safely removed.

Fixes https://github.com/denoland/fresh/issues/3040
Fixes https://github.com/denoland/fresh/issues/3033
Fixes https://github.com/denoland/fresh/issues/2591
Fixes https://github.com/denoland/fresh/issues/950
Fixes https://github.com/denoland/fresh/issues/1918
This commit is contained in:
Marvin Hagemeister
2025-07-14 19:27:48 +02:00
committed by GitHub
parent ccb621d2a3
commit c11cf7b41a
35 changed files with 1328 additions and 971 deletions
+184 -81
View File
@@ -1,119 +1,180 @@
export type Method = "HEAD" | "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
export type Method =
| "HEAD"
| "GET"
| "POST"
| "PATCH"
| "PUT"
| "DELETE"
| "OPTIONS";
export interface Route<T> {
path: string | URLPattern;
method: Method | "ALL";
handlers: T[];
export type RouteByMethod<T> = {
[m in Method]: T[];
};
export interface StaticRouteDef<T> {
pattern: string | URLPattern;
byMethod: RouteByMethod<T>;
}
export interface DynamicRouteDef<T> {
pattern: URLPattern;
byMethod: RouteByMethod<T>;
}
export interface RouteResult<T> {
params: Record<string, string>;
handlers: T[][];
handlers: T[];
methodMatch: boolean;
patternMatch: boolean;
pattern: string | null;
}
export interface Router<T> {
_routes: Route<T>[];
_middlewares: T[];
addMiddleware(fn: T): void;
add(
method: Method | "ALL",
pathname: string | URLPattern,
method: Method | "OPTIONS" | "ALL",
pathname: string,
handlers: T[],
unshift?: boolean,
): void;
match(method: Method, url: URL): RouteResult<T>;
match(method: Method, url: URL, init?: T[]): RouteResult<T>;
getAllowedMethods(pattern: string): string[];
}
export const IS_PATTERN = /[*:{}+?()]/;
export class UrlPatternRouter<T> implements Router<T> {
readonly _routes: Route<T>[] = [];
readonly _middlewares: T[] = [];
const EMPTY: string[] = [];
addMiddleware(fn: T): void {
this._middlewares.push(fn);
export class UrlPatternRouter<T> implements Router<T> {
#statics = new Map<string, StaticRouteDef<T>>();
#dynamics = new Map<string, DynamicRouteDef<T>>();
#dynamicArr: DynamicRouteDef<T>[] = [];
#allowed = new Map<string, Set<string>>();
getAllowedMethods(pattern: string): string[] {
const allowed = this.#allowed.get(pattern);
if (allowed === undefined) return EMPTY;
return Array.from(allowed);
}
add(method: Method | "ALL", pathname: string | URLPattern, handlers: T[]) {
if (
typeof pathname === "string" && pathname !== "/*" &&
IS_PATTERN.test(pathname)
) {
this._routes.push({
path: new URLPattern({ pathname }),
handlers,
method,
});
add(
method: Method | "ALL",
pathname: string,
handlers: T[],
unshift = false,
) {
let allowed = this.#allowed.get(pathname);
if (allowed === undefined) {
allowed = new Set();
this.#allowed.set(pathname, allowed);
}
if (method === "ALL") {
allowed.add("GET");
allowed.add("POST");
allowed.add("PATCH");
allowed.add("PUT");
allowed.add("DELETE");
allowed.add("HEAD");
allowed.add("OPTIONS");
} else {
this._routes.push({
path: pathname,
handlers,
method,
});
allowed.add(method);
}
let byMethod: RouteByMethod<T>;
if (IS_PATTERN.test(pathname)) {
let def = this.#dynamics.get(pathname);
if (def === undefined) {
def = {
pattern: new URLPattern({ pathname }),
byMethod: {
DELETE: [],
GET: [],
HEAD: [],
OPTIONS: [],
PATCH: [],
POST: [],
PUT: [],
},
};
this.#dynamics.set(pathname, def);
this.#dynamicArr.push(def);
}
byMethod = def.byMethod;
} else {
let def = this.#statics.get(pathname);
if (def === undefined) {
def = {
pattern: pathname,
byMethod: {
DELETE: [],
GET: [],
HEAD: [],
OPTIONS: [],
PATCH: [],
POST: [],
PUT: [],
},
};
this.#statics.set(pathname, def);
}
byMethod = def.byMethod;
}
const fn = unshift ? "unshift" : "push";
if (method === "ALL") {
byMethod.DELETE[fn](...handlers);
byMethod.GET[fn](...handlers);
byMethod.HEAD[fn](...handlers);
byMethod.OPTIONS[fn](...handlers);
byMethod.PATCH[fn](...handlers);
byMethod.POST[fn](...handlers);
byMethod.PUT[fn](...handlers);
} else {
byMethod[method][fn](...handlers);
}
}
match(method: Method, url: URL): RouteResult<T> {
match(method: Method, url: URL, init: T[] = []): RouteResult<T> {
const result: RouteResult<T> = {
params: {},
handlers: [],
params: Object.create(null),
handlers: init,
methodMatch: false,
patternMatch: false,
pattern: null,
};
if (this._middlewares.length > 0) {
result.handlers.push(this._middlewares);
const staticMatch = this.#statics.get(url.pathname);
if (staticMatch !== undefined) {
result.pattern = url.pathname;
const handlers = staticMatch.byMethod[method];
if (handlers.length > 0) {
result.methodMatch = true;
result.handlers.push(...handlers);
}
return result;
}
for (let i = 0; i < this._routes.length; i++) {
const route = this._routes[i];
for (let i = 0; i < this.#dynamicArr.length; i++) {
const route = this.#dynamicArr[i];
// Fast path for string based routes which are expected
// to be either wildcard `*` match or an exact pathname match.
if (
typeof route.path === "string" &&
(route.path === "/*" || route.path === url.pathname)
) {
if (route.method !== "ALL") result.patternMatch = true;
result.pattern = route.path;
const match = route.pattern.exec(url);
if (match === null) continue;
if (route.method === "ALL" || route.method === method) {
result.handlers.push(route.handlers);
result.pattern = route.pattern.pathname;
if (route.path === "/*" && route.method === "ALL") {
continue;
}
const handlers = route.byMethod[method];
if (handlers.length > 0) {
result.methodMatch = true;
result.handlers.push(...handlers);
result.methodMatch = true;
return result;
}
} else if (route.path instanceof URLPattern) {
const match = route.path.exec(url);
if (match !== null) {
if (route.method !== "ALL") result.patternMatch = true;
result.pattern = route.path.pathname;
if (route.method === "ALL" || route.method === method) {
result.handlers.push(route.handlers);
// Decode matched params
for (const [key, value] of Object.entries(match.pathname.groups)) {
result.params[key] = value === undefined ? "" : decodeURI(value);
}
if (route.method === "ALL") {
continue;
}
result.methodMatch = true;
return result;
}
// Decode matched params
for (const [key, value] of Object.entries(match.pathname.groups)) {
result.params[key] = value === undefined ? "" : decodeURI(value);
}
}
break;
}
return result;
@@ -123,7 +184,10 @@ export class UrlPatternRouter<T> implements Router<T> {
/**
* Transform a filesystem URL path to a `path-to-regex` style matcher.
*/
export function pathToPattern(path: string): string {
export function pathToPattern(
path: string,
options?: { keepGroups: boolean },
): string {
const parts = path.split("/");
if (parts[parts.length - 1] === "index") {
if (parts.length === 1) {
@@ -148,7 +212,7 @@ export function pathToPattern(path: string): string {
// Case: /foo/(bar) -> /foo
// Case: /foo/(bar)/bob -> /foo/bob
// Case: /(foo)/bar -> /bar
if (part.startsWith("(") && part.endsWith(")")) {
if (!options?.keepGroups && part.startsWith("(") && part.endsWith(")")) {
continue;
}
@@ -216,3 +280,42 @@ export function pathToPattern(path: string): string {
return route;
}
export function patternToSegments(
path: string,
root: string,
includeLast: boolean = false,
): string[] {
const out: string[] = [root];
if (path === "/" || path === "*" || path === "/*") return out;
let start = -1;
for (let i = 0; i < path.length; i++) {
const ch = path[i];
if (ch === "/") {
if (i > 0) {
const raw = path.slice(start + 1, i);
out.push(raw);
}
start = i;
}
}
if (includeLast && start < path.length - 1) {
out.push(path.slice(start + 1));
}
return out;
}
export function mergePath(basePath: string, path: string): string {
if (basePath.endsWith("*")) basePath = basePath.slice(0, -1);
if (basePath === "/") basePath = "";
if (path === "*" || path === "/*") path = "/*";
const s = (basePath !== "" && path === "/") ? "" : path;
return basePath + s;
}