Router cleanup (#2099)

This commit is contained in:
Marvin Hagemeister
2023-11-30 14:08:05 +01:00
committed by GitHub
parent a0a756a1ec
commit 8ddc21eb90
15 changed files with 398 additions and 496 deletions
+8 -11
View File
@@ -36,17 +36,14 @@ Deno.test("dev_command config: shows codeframe", async () => {
);
});
Deno.test({
name: "dev_command legacy",
async fn() {
await withPageName(
"./tests/fixture_dev_legacy/main.ts",
async (page, address) => {
await page.goto(`${address}`);
await waitForStyle(page, "h1", "color", "rgb(220, 38, 38)");
},
);
},
Deno.test("dev_command legacy", async () => {
await withPageName(
"./tests/fixture_dev_legacy/main.ts",
async (page, address) => {
await page.goto(`${address}`);
await waitForStyle(page, "h1", "color", "rgb(220, 38, 38)");
},
);
});
Deno.test("dev_command legacy: shows codeframe", async () => {
+31 -35
View File
@@ -79,22 +79,22 @@ import * as $static from "./routes/static.tsx";
import * as $status_overwrite from "./routes/status_overwrite.tsx";
import * as $umlaut_äöüß from "./routes/umlaut-äöüß.tsx";
import * as $wildcard from "./routes/wildcard.tsx";
import * as $$Counter from "./islands/Counter.tsx";
import * as $$FormIsland from "./islands/FormIsland.tsx";
import * as $$Greeter from "./islands/Greeter.tsx";
import * as $$HookIsland from "./islands/HookIsland.tsx";
import * as $$MultipleCounters from "./islands/MultipleCounters.tsx";
import * as $$ReturningNull from "./islands/ReturningNull.tsx";
import * as $$RootFragment from "./islands/RootFragment.tsx";
import * as $$RootFragmentWithConditionalFirst from "./islands/RootFragmentWithConditionalFirst.tsx";
import * as $$StringEventIsland from "./islands/StringEventIsland.tsx";
import * as $$Test from "./islands/Test.tsx";
import * as $$folder_Counter from "./islands/folder/Counter.tsx";
import * as $$folder_subfolder_Counter from "./islands/folder/subfolder/Counter.tsx";
import * as $$kebab_case_counter_test from "./islands/kebab-case-counter-test.tsx";
import * as $$route_groups_islands_islands_Counter from "./routes/route-groups-islands/(_islands)/Counter.tsx";
import * as $$route_groups_islands_islands_invalid from "./routes/route-groups-islands/(_islands)/invalid.tsx";
import { Manifest } from "$fresh/server.ts";
import * as $Counter from "./islands/Counter.tsx";
import * as $FormIsland from "./islands/FormIsland.tsx";
import * as $Greeter from "./islands/Greeter.tsx";
import * as $HookIsland from "./islands/HookIsland.tsx";
import * as $MultipleCounters from "./islands/MultipleCounters.tsx";
import * as $ReturningNull from "./islands/ReturningNull.tsx";
import * as $RootFragment from "./islands/RootFragment.tsx";
import * as $RootFragmentWithConditionalFirst from "./islands/RootFragmentWithConditionalFirst.tsx";
import * as $StringEventIsland from "./islands/StringEventIsland.tsx";
import * as $Test from "./islands/Test.tsx";
import * as $folder_Counter from "./islands/folder/Counter.tsx";
import * as $folder_subfolder_Counter from "./islands/folder/subfolder/Counter.tsx";
import * as $kebab_case_counter_test from "./islands/kebab-case-counter-test.tsx";
import * as $route_groups_islands_islands_Counter from "./routes/route-groups-islands/(_islands)/Counter.tsx";
import * as $route_groups_islands_islands_invalid from "./routes/route-groups-islands/(_islands)/invalid.tsx";
import { type Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -195,28 +195,24 @@ const manifest = {
"./routes/wildcard.tsx": $wildcard,
},
islands: {
"./islands/Counter.tsx": $$Counter,
"./islands/FormIsland.tsx": $$FormIsland,
"./islands/Greeter.tsx": $$Greeter,
"./islands/HookIsland.tsx": $$HookIsland,
"./islands/MultipleCounters.tsx": {
CounterOne: $$MultipleCounters.CounterOne,
CounterTwo: $$MultipleCounters.CounterTwo,
default: $$MultipleCounters.default,
},
"./islands/ReturningNull.tsx": $$ReturningNull,
"./islands/RootFragment.tsx": $$RootFragment,
"./islands/Counter.tsx": $Counter,
"./islands/FormIsland.tsx": $FormIsland,
"./islands/Greeter.tsx": $Greeter,
"./islands/HookIsland.tsx": $HookIsland,
"./islands/MultipleCounters.tsx": $MultipleCounters,
"./islands/ReturningNull.tsx": $ReturningNull,
"./islands/RootFragment.tsx": $RootFragment,
"./islands/RootFragmentWithConditionalFirst.tsx":
$$RootFragmentWithConditionalFirst,
"./islands/StringEventIsland.tsx": $$StringEventIsland,
"./islands/Test.tsx": $$Test,
"./islands/folder/Counter.tsx": $$folder_Counter,
"./islands/folder/subfolder/Counter.tsx": $$folder_subfolder_Counter,
"./islands/kebab-case-counter-test.tsx": $$kebab_case_counter_test,
$RootFragmentWithConditionalFirst,
"./islands/StringEventIsland.tsx": $StringEventIsland,
"./islands/Test.tsx": $Test,
"./islands/folder/Counter.tsx": $folder_Counter,
"./islands/folder/subfolder/Counter.tsx": $folder_subfolder_Counter,
"./islands/kebab-case-counter-test.tsx": $kebab_case_counter_test,
"./routes/route-groups-islands/(_islands)/Counter.tsx":
$$route_groups_islands_islands_Counter,
$route_groups_islands_islands_Counter,
"./routes/route-groups-islands/(_islands)/invalid.tsx":
$$route_groups_islands_islands_invalid,
$route_groups_islands_islands_invalid,
},
baseUrl: import.meta.url,
} satisfies Manifest;
@@ -1,8 +1,5 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { FreshContext } from "$fresh/server.ts";
export async function handler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
export async function handler(_req: Request, ctx: FreshContext) {
return await ctx.renderNotFound();
}
+5 -17
View File
@@ -1,10 +1,7 @@
import { MiddlewareHandler, MiddlewareHandlerContext } from "$fresh/server.ts";
import { FreshContext, MiddlewareHandler } from "$fresh/server.ts";
// cors middleware
async function corsHandler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
async function corsHandler(_req: Request, ctx: FreshContext) {
if (_req.method == "OPTIONS") {
return new Response(null, {
status: 204,
@@ -29,10 +26,7 @@ async function corsHandler(
}
// log middleware
async function logHandler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
async function logHandler(_req: Request, ctx: FreshContext) {
const since = new Date();
const resp = await ctx.next();
const latency = (+new Date()) - (+since);
@@ -40,20 +34,14 @@ async function logHandler(
return resp;
}
async function rootHandler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
async function rootHandler(_req: Request, ctx: FreshContext) {
ctx.state.root = "root_mw";
const resp = await ctx.next();
resp.headers.set("server", "fresh test server");
return resp;
}
async function kindHandler(
_req: Request,
ctx: MiddlewareHandlerContext,
) {
async function kindHandler(_req: Request, ctx: FreshContext) {
const resp = await ctx.next();
resp.headers.set("destination", ctx.destination);
return resp;
+3 -3
View File
@@ -9,8 +9,8 @@ import * as $index from "./routes/index.tsx";
import * as $island from "./routes/island.tsx";
import * as $response from "./routes/response.tsx";
import * as $twind from "./routes/twind.tsx";
import * as $$FooIsland from "./islands/FooIsland.tsx";
import { Manifest } from "$fresh/server.ts";
import * as $FooIsland from "./islands/FooIsland.tsx";
import { type Manifest } from "$fresh/server.ts";
const manifest = {
routes: {
@@ -23,7 +23,7 @@ const manifest = {
"./routes/twind.tsx": $twind,
},
islands: {
"./islands/FooIsland.tsx": $$FooIsland,
"./islands/FooIsland.tsx": $FooIsland,
},
baseUrl: import.meta.url,
} satisfies Manifest;
+8 -11
View File
@@ -671,17 +671,14 @@ Deno.test({
},
});
Deno.test({
name: "/not_found",
fn: async () => {
const resp = await handler(new Request("https://fresh.deno.dev/not_found"));
assert(resp);
assertEquals(resp.status, 404);
const body = await resp.text();
assertStringIncludes(body, "404 not found: /not_found");
assertStringIncludes(body, "Hello Dino");
assertStringIncludes(body, "State root: root_mw");
},
Deno.test("/not_found", async () => {
const resp = await handler(new Request("https://fresh.deno.dev/not_found"));
assert(resp);
assertEquals(resp.status, 404);
const body = await resp.text();
assertStringIncludes(body, "404 not found: /not_found");
assertStringIncludes(body, "Hello Dino");
assertStringIncludes(body, "State root: root_mw");
});
Deno.test("middleware destination", async (t) => {
+39 -38
View File
@@ -58,47 +58,48 @@ Deno.test({
},
});
Deno.test({
name: "passes context to server component",
Deno.test("passes context to server component", async () => {
await withFresh(
"./tests/fixture_server_components/main.ts",
async (address) => {
const res = await fetch(`${address}/context/foo`);
const json = await res.json();
async fn() {
await withFresh(
"./tests/fixture_server_components/main.ts",
async (address) => {
const res = await fetch(`${address}/context/foo`);
const json = await res.json();
assertEquals(typeof json.localAddr, "object");
assertEquals(typeof json.remoteAddr, "object");
json.localAddr.port = 8000;
json.remoteAddr.port = 8000;
assertEquals(typeof json.localAddr, "object");
assertEquals(typeof json.remoteAddr, "object");
json.localAddr.port = 8000;
json.remoteAddr.port = 8000;
assertEquals(
json,
{
localAddr: {
hostname: "localhost",
port: 8000,
transport: "tcp",
},
remoteAddr: {
hostname: "127.0.0.1",
port: 8000,
transport: "tcp",
},
renderNotFound: "AsyncFunction",
url: `${address}/context/foo`,
route: "/context/:id",
params: {
id: "foo",
},
state: {},
isPartial: false,
assertEquals(
json,
{
localAddr: {
hostname: "localhost",
port: 8000,
transport: "tcp",
},
);
},
);
},
remoteAddr: {
hostname: "127.0.0.1",
port: 8000,
transport: "tcp",
},
render: "AsyncFunction",
Component: "Function",
destination: "route",
next: "Function",
basePath: "",
renderNotFound: "AsyncFunction",
url: `${address}/context/foo`,
route: "/context/:id",
params: {
id: "foo",
},
state: {},
isPartial: false,
},
);
},
);
});
Deno.test({