diff --git a/src/app.ts b/src/app.ts index 809e478c887..fa9bd11c400 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,7 +7,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts"; import * as colors from "@std/fmt/colors"; import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts"; import { FreshReqContext } from "./context.ts"; -import { type Method, type Router, UrlPatternRouter } from "./router.ts"; +import { type Method, Router } from "./router.ts"; import { type FreshConfig, normalizeConfig, @@ -124,7 +124,7 @@ export let getBuildCache: (app: App) => BuildCache | null; export let setBuildCache: (app: App, cache: BuildCache | null) => void; export class App { - #router: Router> = new UrlPatternRouter< + #router: Router> = new Router< MiddlewareFn >(); #islandRegistry: ServerIslandRegistry = new Map(); @@ -201,19 +201,19 @@ export class App { } mountApp(path: string, app: App): this { - const routes = app.#router._routes; + const routes = app.#router.routes; app.#islandRegistry.forEach((value, key) => { this.#islandRegistry.set(key, value); }); - const middlewares = app.#router._middlewares; + const middlewares = app.#router.middlewares; // Special case when user calls one of these: // - `app.mountApp("/", otherApp)` // - `app.mountApp("/*", otherApp)` const isSelf = path === "/*" || path === "/"; if (isSelf && middlewares.length > 0) { - this.#router._middlewares.push(...middlewares); + this.#router.middlewares.push(...middlewares); } for (let i = 0; i < routes.length; i++) { diff --git a/src/router.ts b/src/router.ts index e128bacfd80..3b8ec1efc9e 100644 --- a/src/router.ts +++ b/src/router.ts @@ -14,26 +14,14 @@ export interface RouteResult { pattern: string | null; } -export interface Router { - _routes: Route[]; - _middlewares: T[]; - addMiddleware(fn: T): void; - add( - method: Method | "ALL", - pathname: string | URLPattern, - handlers: T[], - ): void; - match(method: Method, url: URL): RouteResult; -} - export const IS_PATTERN = /[*:{}+?()]/; -export class UrlPatternRouter implements Router { - readonly _routes: Route[] = []; - readonly _middlewares: T[] = []; +export class Router { + readonly routes: Route[] = []; + readonly middlewares: T[] = []; addMiddleware(fn: T): void { - this._middlewares.push(fn); + this.middlewares.push(fn); } add(method: Method | "ALL", pathname: string | URLPattern, handlers: T[]) { @@ -41,13 +29,13 @@ export class UrlPatternRouter implements Router { typeof pathname === "string" && pathname !== "/*" && IS_PATTERN.test(pathname) ) { - this._routes.push({ + this.routes.push({ path: new URLPattern({ pathname }), handlers, method, }); } else { - this._routes.push({ + this.routes.push({ path: pathname, handlers, method, @@ -64,12 +52,12 @@ export class UrlPatternRouter implements Router { pattern: null, }; - if (this._middlewares.length > 0) { - result.handlers.push(this._middlewares); + if (this.middlewares.length > 0) { + result.handlers.push(this.middlewares); } - for (let i = 0; i < this._routes.length; i++) { - const route = this._routes[i]; + for (let i = 0; i < this.routes.length; i++) { + const route = this.routes[i]; // Fast path for string based routes which are expected // to be either wildcard `*` match or an exact pathname match. diff --git a/src/router_test.ts b/src/router_test.ts index f930b13e31a..13331c208e4 100644 --- a/src/router_test.ts +++ b/src/router_test.ts @@ -1,5 +1,5 @@ import { expect } from "@std/expect"; -import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts"; +import { IS_PATTERN, pathToPattern, Router } from "./router.ts"; Deno.test("IS_PATTERN", () => { expect(IS_PATTERN.test("/foo")).toEqual(false); @@ -11,8 +11,8 @@ Deno.test("IS_PATTERN", () => { expect(IS_PATTERN.test("/foo/(a)")).toEqual(true); }); -Deno.test("UrlPatternRouter - GET get first match", () => { - const router = new UrlPatternRouter(); +Deno.test("Router - GET get first match", () => { + const router = new Router(); const A = () => {}; const B = () => {}; const C = () => {}; @@ -30,8 +30,8 @@ Deno.test("UrlPatternRouter - GET get first match", () => { }); }); -Deno.test("UrlPatternRouter - GET get matches with middlewares", () => { - const router = new UrlPatternRouter(); +Deno.test("Router - GET get matches with middlewares", () => { + const router = new Router(); const A = () => {}; const B = () => {}; const C = () => {}; @@ -49,8 +49,8 @@ Deno.test("UrlPatternRouter - GET get matches with middlewares", () => { }); }); -Deno.test("UrlPatternRouter - GET extract params", () => { - const router = new UrlPatternRouter(); +Deno.test("Router - GET extract params", () => { + const router = new Router(); const A = () => {}; router.add("GET", new URLPattern({ pathname: "/:foo/:bar/c" }), [A]); @@ -74,8 +74,8 @@ Deno.test("UrlPatternRouter - GET extract params", () => { }); }); -Deno.test("UrlPatternRouter - Wrong method match", () => { - const router = new UrlPatternRouter(); +Deno.test("Router - Wrong method match", () => { + const router = new Router(); const A = () => {}; router.add("GET", "/foo", [A]); @@ -89,8 +89,8 @@ Deno.test("UrlPatternRouter - Wrong method match", () => { }); }); -Deno.test("UrlPatternRouter - wrong + correct method", () => { - const router = new UrlPatternRouter(); +Deno.test("Router - wrong + correct method", () => { + const router = new Router(); const A = () => {}; const B = () => {}; router.add("GET", "/foo", [A]); @@ -106,8 +106,8 @@ Deno.test("UrlPatternRouter - wrong + correct method", () => { }); }); -Deno.test("UrlPatternRouter - convert patterns automatically", () => { - const router = new UrlPatternRouter(); +Deno.test("Router - convert patterns automatically", () => { + const router = new Router(); const A = () => {}; router.add("GET", "/books/:id", [A]);