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:
+44
-51
@@ -1,5 +1,11 @@
|
||||
import { expect } from "@std/expect";
|
||||
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";
|
||||
import {
|
||||
IS_PATTERN,
|
||||
mergePath,
|
||||
pathToPattern,
|
||||
patternToSegments,
|
||||
UrlPatternRouter,
|
||||
} from "./router.ts";
|
||||
|
||||
Deno.test("IS_PATTERN", () => {
|
||||
expect(IS_PATTERN.test("/foo")).toEqual(false);
|
||||
@@ -11,66 +17,26 @@ Deno.test("IS_PATTERN", () => {
|
||||
expect(IS_PATTERN.test("/foo/(a)")).toEqual(true);
|
||||
});
|
||||
|
||||
Deno.test("UrlPatternRouter - GET get first match", () => {
|
||||
const router = new UrlPatternRouter();
|
||||
const A = () => {};
|
||||
const B = () => {};
|
||||
const C = () => {};
|
||||
router.add("GET", "/", [A]);
|
||||
router.add("GET", "/", [B]);
|
||||
router.add("GET", "/", [C]);
|
||||
|
||||
const res = router.match("GET", new URL("/", "http://localhost"));
|
||||
expect(res).toEqual({
|
||||
params: {},
|
||||
handlers: [[A]],
|
||||
methodMatch: true,
|
||||
pattern: "/",
|
||||
patternMatch: true,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("UrlPatternRouter - GET get matches with middlewares", () => {
|
||||
const router = new UrlPatternRouter();
|
||||
const A = () => {};
|
||||
const B = () => {};
|
||||
const C = () => {};
|
||||
router.add("ALL", "/*", [A]);
|
||||
router.add("ALL", "/*", [B]);
|
||||
router.add("GET", "/", [C]);
|
||||
|
||||
const res = router.match("GET", new URL("/", "http://localhost"));
|
||||
expect(res).toEqual({
|
||||
params: {},
|
||||
handlers: [[A], [B], [C]],
|
||||
methodMatch: true,
|
||||
pattern: "/",
|
||||
patternMatch: true,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("UrlPatternRouter - GET extract params", () => {
|
||||
const router = new UrlPatternRouter();
|
||||
const A = () => {};
|
||||
router.add("GET", new URLPattern({ pathname: "/:foo/:bar/c" }), [A]);
|
||||
router.add("GET", "/:foo/:bar/c", [A]);
|
||||
|
||||
let res = router.match("GET", new URL("/a/b/c", "http://localhost"));
|
||||
expect(res).toEqual({
|
||||
params: { foo: "a", bar: "b" },
|
||||
handlers: [[A]],
|
||||
handlers: [A],
|
||||
methodMatch: true,
|
||||
pattern: "/:foo/:bar/c",
|
||||
patternMatch: true,
|
||||
});
|
||||
|
||||
// Decode params
|
||||
res = router.match("GET", new URL("/a%20a/b/c", "http://localhost"));
|
||||
expect(res).toEqual({
|
||||
params: { foo: "a a", bar: "b" },
|
||||
handlers: [[A]],
|
||||
handlers: [A],
|
||||
methodMatch: true,
|
||||
pattern: "/:foo/:bar/c",
|
||||
patternMatch: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -81,11 +47,10 @@ Deno.test("UrlPatternRouter - Wrong method match", () => {
|
||||
|
||||
const res = router.match("POST", new URL("/foo", "http://localhost"));
|
||||
expect(res).toEqual({
|
||||
params: {},
|
||||
params: Object.create(null),
|
||||
handlers: [],
|
||||
methodMatch: false,
|
||||
pattern: "/foo",
|
||||
patternMatch: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -98,11 +63,10 @@ Deno.test("UrlPatternRouter - wrong + correct method", () => {
|
||||
|
||||
const res = router.match("POST", new URL("/foo", "http://localhost"));
|
||||
expect(res).toEqual({
|
||||
params: {},
|
||||
handlers: [[B]],
|
||||
params: Object.create(null),
|
||||
handlers: [B],
|
||||
methodMatch: true,
|
||||
pattern: "/foo",
|
||||
patternMatch: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -116,10 +80,9 @@ Deno.test("UrlPatternRouter - convert patterns automatically", () => {
|
||||
params: {
|
||||
id: "foo",
|
||||
},
|
||||
handlers: [[A]],
|
||||
handlers: [A],
|
||||
methodMatch: true,
|
||||
pattern: "/books/:id",
|
||||
patternMatch: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -170,4 +133,34 @@ Deno.test("pathToPattern", async (t) => {
|
||||
expect(() => pathToPattern("foo/foo-[[name]]")).toThrow();
|
||||
expect(() => pathToPattern("foo/[[name]]-bar")).toThrow();
|
||||
});
|
||||
|
||||
await t.step("keep groups", () => {
|
||||
expect(pathToPattern("foo/(foo)/bar", { keepGroups: true })).toEqual(
|
||||
"/foo/(foo)/bar",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("patternToSegments", () => {
|
||||
expect(patternToSegments("/", "")).toEqual([""]);
|
||||
expect(patternToSegments("/foo", "")).toEqual([""]);
|
||||
expect(patternToSegments("/foo/bar", "")).toEqual(["", "foo"]);
|
||||
|
||||
expect(patternToSegments("/:foo", "")).toEqual([""]);
|
||||
expect(patternToSegments("/:foo/:bar", "")).toEqual(["", ":foo"]);
|
||||
expect(patternToSegments("/:foo-:bar/foo", "")).toEqual(["", ":foo-:bar"]);
|
||||
expect(patternToSegments("/foo/", "")).toEqual(["", "foo"]);
|
||||
|
||||
expect(patternToSegments("/foo/bar", "", true)).toEqual(["", "foo", "bar"]);
|
||||
});
|
||||
|
||||
Deno.test("mergePath", () => {
|
||||
expect(mergePath("", "/foo")).toEqual("/foo");
|
||||
expect(mergePath("/", "/foo")).toEqual("/foo");
|
||||
expect(mergePath("/foo/bar", "/")).toEqual("/foo/bar");
|
||||
expect(mergePath("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
|
||||
expect(mergePath("*", "/baz")).toEqual("/baz");
|
||||
expect(mergePath("/*", "/baz")).toEqual("/baz");
|
||||
expect(mergePath("/foo", "*")).toEqual("/foo/*");
|
||||
expect(mergePath("/foo", "/*")).toEqual("/foo/*");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user