Skip to content

Commit

Permalink
fix formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
lenkan committed Sep 2, 2023
1 parent b1dadcc commit 6878ff3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Deno http router

Simple, zero dependency, module for http routing in deno. It uses the built in URLPattern implementation for path matching.
Simple, zero dependency, module for http routing in deno. It uses the built in
URLPattern implementation for path matching.

## Example

Expand Down
41 changes: 32 additions & 9 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
export type HttpRouteHandler = (request: Request, match: URLPatternResult) => Response | Promise<Response>;
export type HttpRouteHandler = (
request: Request,
match: URLPatternResult,
) => Response | Promise<Response>;

type HttpRoute = {
pattern: URLPattern;
handler: HttpRouteHandler;
method: string;
};

export type HttpMethod = "get" | "post" | "delete" | "patch" | "put" | "options";
export type HttpMethod =
| "get"
| "post"
| "delete"
| "patch"
| "put"
| "options";

export class HttpRouter {
#routes: HttpRoute[] = [];

get = (pattern: URLPatternInput, handler: HttpRouteHandler) => this.#use("get", pattern, handler);
post = (pattern: URLPatternInput, handler: HttpRouteHandler) => this.#use("post", pattern, handler);
put = (pattern: URLPatternInput, handler: HttpRouteHandler) => this.#use("put", pattern, handler);
delete = (pattern: URLPatternInput, handler: HttpRouteHandler) => this.#use("delete", pattern, handler);
get = (pattern: URLPatternInput, handler: HttpRouteHandler) =>
this.#use("get", pattern, handler);
post = (pattern: URLPatternInput, handler: HttpRouteHandler) =>
this.#use("post", pattern, handler);
put = (pattern: URLPatternInput, handler: HttpRouteHandler) =>
this.#use("put", pattern, handler);
delete = (pattern: URLPatternInput, handler: HttpRouteHandler) =>
this.#use("delete", pattern, handler);

all = (pattern: URLPatternInput, handler: Partial<Record<HttpMethod, HttpRouteHandler>> | HttpRouteHandler) => {
all = (
pattern: URLPatternInput,
handler: Partial<Record<HttpMethod, HttpRouteHandler>> | HttpRouteHandler,
) => {
if (typeof handler === "object") {
for (const [method, h] of Object.entries(handler)) {
this.#use(method as HttpMethod, pattern, h);
Expand All @@ -26,7 +42,11 @@ export class HttpRouter {
}
};

#use = (method: HttpMethod | "all", pattern: string | URLPatternInput, handler: HttpRouteHandler) => {
#use = (
method: HttpMethod | "all",
pattern: string | URLPatternInput,
handler: HttpRouteHandler,
) => {
if (typeof pattern === "string") {
this.#routes.push({
method,
Expand All @@ -49,7 +69,10 @@ export class HttpRouter {

async handleRequest(request: Request): Promise<Response> {
for (const route of this.#routes) {
if (route.method.toUpperCase() === "ALL" || route.method.toUpperCase() === request.method.toUpperCase()) {
if (
route.method.toUpperCase() === "ALL" ||
route.method.toUpperCase() === request.method.toUpperCase()
) {
const url = new URL(request.url, "http://example.com");
const result = route.pattern.exec(url);

Expand Down
9 changes: 7 additions & 2 deletions src/router_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { assertEquals, assertRejects } from "https://deno.land/[email protected]/assert/mod.ts";
import {
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";

import { HttpRouter } from "./router.ts";

Expand All @@ -13,7 +16,9 @@ Deno.test("get random json body", async () => {
const body = { message: crypto.randomUUID() };
router.get("/", () => Response.json(body));

const response = await router.handleRequest(new Request(new URL("/", baseurl)));
const response = await router.handleRequest(
new Request(new URL("/", baseurl)),
);

assertEquals(await response.json(), body);
});
Expand Down

0 comments on commit 6878ff3

Please sign in to comment.