Skip to content

Commit 12e8802

Browse files
committed
refactor(core): cleanup Router
1 parent fcd9480 commit 12e8802

3 files changed

Lines changed: 28 additions & 40 deletions

File tree

src/app.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
77
import * as colors from "@std/fmt/colors";
88
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
99
import { FreshReqContext } from "./context.ts";
10-
import { type Method, type Router, UrlPatternRouter } from "./router.ts";
10+
import { type Method, Router } from "./router.ts";
1111
import {
1212
type FreshConfig,
1313
normalizeConfig,
@@ -124,7 +124,7 @@ export let getBuildCache: (app: App<any>) => BuildCache | null;
124124
export let setBuildCache: (app: App<any>, cache: BuildCache | null) => void;
125125

126126
export class App<State> {
127-
#router: Router<MiddlewareFn<State>> = new UrlPatternRouter<
127+
#router: Router<MiddlewareFn<State>> = new Router<
128128
MiddlewareFn<State>
129129
>();
130130
#islandRegistry: ServerIslandRegistry = new Map();
@@ -201,19 +201,19 @@ export class App<State> {
201201
}
202202

203203
mountApp(path: string, app: App<State>): this {
204-
const routes = app.#router._routes;
204+
const routes = app.#router.routes;
205205
app.#islandRegistry.forEach((value, key) => {
206206
this.#islandRegistry.set(key, value);
207207
});
208208

209-
const middlewares = app.#router._middlewares;
209+
const middlewares = app.#router.middlewares;
210210

211211
// Special case when user calls one of these:
212212
// - `app.mountApp("/", otherApp)`
213213
// - `app.mountApp("/*", otherApp)`
214214
const isSelf = path === "/*" || path === "/";
215215
if (isSelf && middlewares.length > 0) {
216-
this.#router._middlewares.push(...middlewares);
216+
this.#router.middlewares.push(...middlewares);
217217
}
218218

219219
for (let i = 0; i < routes.length; i++) {

src/router.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,28 @@ export interface RouteResult<T> {
1414
pattern: string | null;
1515
}
1616

17-
export interface Router<T> {
18-
_routes: Route<T>[];
19-
_middlewares: T[];
20-
addMiddleware(fn: T): void;
21-
add(
22-
method: Method | "ALL",
23-
pathname: string | URLPattern,
24-
handlers: T[],
25-
): void;
26-
match(method: Method, url: URL): RouteResult<T>;
27-
}
28-
2917
export const IS_PATTERN = /[*:{}+?()]/;
3018

31-
export class UrlPatternRouter<T> implements Router<T> {
32-
readonly _routes: Route<T>[] = [];
33-
readonly _middlewares: T[] = [];
19+
export class Router<T> {
20+
readonly routes: Route<T>[] = [];
21+
readonly middlewares: T[] = [];
3422

3523
addMiddleware(fn: T): void {
36-
this._middlewares.push(fn);
24+
this.middlewares.push(fn);
3725
}
3826

3927
add(method: Method | "ALL", pathname: string | URLPattern, handlers: T[]) {
4028
if (
4129
typeof pathname === "string" && pathname !== "/*" &&
4230
IS_PATTERN.test(pathname)
4331
) {
44-
this._routes.push({
32+
this.routes.push({
4533
path: new URLPattern({ pathname }),
4634
handlers,
4735
method,
4836
});
4937
} else {
50-
this._routes.push({
38+
this.routes.push({
5139
path: pathname,
5240
handlers,
5341
method,
@@ -64,12 +52,12 @@ export class UrlPatternRouter<T> implements Router<T> {
6452
pattern: null,
6553
};
6654

67-
if (this._middlewares.length > 0) {
68-
result.handlers.push(this._middlewares);
55+
if (this.middlewares.length > 0) {
56+
result.handlers.push(this.middlewares);
6957
}
7058

71-
for (let i = 0; i < this._routes.length; i++) {
72-
const route = this._routes[i];
59+
for (let i = 0; i < this.routes.length; i++) {
60+
const route = this.routes[i];
7361

7462
// Fast path for string based routes which are expected
7563
// to be either wildcard `*` match or an exact pathname match.

src/router_test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect } from "@std/expect";
2-
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";
2+
import { IS_PATTERN, pathToPattern, Router } from "./router.ts";
33

44
Deno.test("IS_PATTERN", () => {
55
expect(IS_PATTERN.test("/foo")).toEqual(false);
@@ -11,8 +11,8 @@ Deno.test("IS_PATTERN", () => {
1111
expect(IS_PATTERN.test("/foo/(a)")).toEqual(true);
1212
});
1313

14-
Deno.test("UrlPatternRouter - GET get first match", () => {
15-
const router = new UrlPatternRouter();
14+
Deno.test("Router - GET get first match", () => {
15+
const router = new Router();
1616
const A = () => {};
1717
const B = () => {};
1818
const C = () => {};
@@ -30,8 +30,8 @@ Deno.test("UrlPatternRouter - GET get first match", () => {
3030
});
3131
});
3232

33-
Deno.test("UrlPatternRouter - GET get matches with middlewares", () => {
34-
const router = new UrlPatternRouter();
33+
Deno.test("Router - GET get matches with middlewares", () => {
34+
const router = new Router();
3535
const A = () => {};
3636
const B = () => {};
3737
const C = () => {};
@@ -49,8 +49,8 @@ Deno.test("UrlPatternRouter - GET get matches with middlewares", () => {
4949
});
5050
});
5151

52-
Deno.test("UrlPatternRouter - GET extract params", () => {
53-
const router = new UrlPatternRouter();
52+
Deno.test("Router - GET extract params", () => {
53+
const router = new Router();
5454
const A = () => {};
5555
router.add("GET", new URLPattern({ pathname: "/:foo/:bar/c" }), [A]);
5656

@@ -74,8 +74,8 @@ Deno.test("UrlPatternRouter - GET extract params", () => {
7474
});
7575
});
7676

77-
Deno.test("UrlPatternRouter - Wrong method match", () => {
78-
const router = new UrlPatternRouter();
77+
Deno.test("Router - Wrong method match", () => {
78+
const router = new Router();
7979
const A = () => {};
8080
router.add("GET", "/foo", [A]);
8181

@@ -89,8 +89,8 @@ Deno.test("UrlPatternRouter - Wrong method match", () => {
8989
});
9090
});
9191

92-
Deno.test("UrlPatternRouter - wrong + correct method", () => {
93-
const router = new UrlPatternRouter();
92+
Deno.test("Router - wrong + correct method", () => {
93+
const router = new Router();
9494
const A = () => {};
9595
const B = () => {};
9696
router.add("GET", "/foo", [A]);
@@ -106,8 +106,8 @@ Deno.test("UrlPatternRouter - wrong + correct method", () => {
106106
});
107107
});
108108

109-
Deno.test("UrlPatternRouter - convert patterns automatically", () => {
110-
const router = new UrlPatternRouter();
109+
Deno.test("Router - convert patterns automatically", () => {
110+
const router = new Router();
111111
const A = () => {};
112112
router.add("GET", "/books/:id", [A]);
113113

0 commit comments

Comments
 (0)