Skip to content

Commit

Permalink
Router (#12)
Browse files Browse the repository at this point in the history
* add router

* add test for router

* export router in mod.ts
  • Loading branch information
omar2205 authored Oct 1, 2024
1 parent 1240fe5 commit 5a12f43
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { serve, ServeInit } from "https://deno.land/std@0.163.0/http/server.ts";
import { Context } from "./context.ts";
import decode from "./decode.ts";
import { Handler, ServerError } from "./types.ts";
import { Router } from "./router.ts";

const notFound = {
status: 404,
Expand Down Expand Up @@ -88,5 +89,12 @@ export class WebApp {
}
};

use = (path: string, router: Router) => {
for (const [id, handler] of router.routes) {
const [method, pathname] = id.split(",");
this.#add(path + pathname, method, handler);
}
};

serve = (opts?: ServeInit) => serve(this.handle, opts);
}
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import { WebApp } from "./app.ts";
export default () => new WebApp();
export { Context } from "./context.ts";
export type { Handler } from "./types.ts";
export { Router } from "./router.ts";
25 changes: 25 additions & 0 deletions router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { WebApp } from "./app.ts";
import { Handler } from "./types.ts";

export class Router extends WebApp {
routes: Map<string, Handler>;

constructor() {
super();
this.routes = new Map();

// deno-fmt-ignore-line
const methods = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head'] as const;
for (const method of methods) {
this[method] = (path, handler) =>
this.#add(path, method.toUpperCase(), handler);
}
}

#add(pathname: string, method: string, handler: Handler) {
const id = method + ',' + pathname;
this.routes.set(id, handler);

return this;
}
}
13 changes: 13 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { assertEquals } from "https://deno.land/std@0.161.0/testing/asserts.ts";
import fast, { Context } from "./mod.ts";
import { Router } from "./router.ts";

function makeRequest(path: string, init: RequestInit = {}) {
path = "https://example.com" + path;
return new Request(path, { ...init });
}

const app = fast();
const router = new Router();

Deno.test("404", async () => {
const req = makeRequest("/404");
Expand Down Expand Up @@ -130,3 +132,14 @@ Deno.test("decode", async () => {
const res5 = await app.handle(req5);
assertEquals(res5.status, 201);
});

router.get("/", () => "router home");
app.use("/api", router);

Deno.test("router", async () => {
const req = makeRequest("/api/");
const res = await app.handle(req);
const data = await res.text();
assertEquals(res.status, 200);
assertEquals(data, "router home");
});

0 comments on commit 5a12f43

Please sign in to comment.