Skip to content

Commit

Permalink
update to jsr
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsVomMars committed Jun 21, 2024
1 parent 83b3190 commit b60d66f
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.importMap": "./import_map.json"
"deno.importMap": "./deno.json"
}
2 changes: 2 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# TODOs

- [x] Import map
- [x] Methods
- [ ] Response types
Expand Down
8 changes: 4 additions & 4 deletions app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { serve, type ServeInit } from "http";
// import { serve, type ServeInit } from "@std/http";
import { Handler, Method, Router } from "./router/mod.ts";
import { Context } from "./context/mod.ts";

Expand All @@ -8,13 +8,13 @@ export default class YADWF {
this.#router = new Router();
}

public async start(options?: ServeInit) {
await serve(async (req: Request) => {
public start(options: Deno.ServeOptions = {}) {
Deno.serve(options, async (req: Request) => {
const context = new Context(req);
const handler = this.#router.find(context);
await handler(context);
return context.response;
}, options);
});
}

private addPath(method: Method, path: string, handler: Handler): YADWF {
Expand Down
17 changes: 10 additions & 7 deletions context/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Status, STATUS_TEXT } from "http";
import { STATUS_CODE, STATUS_TEXT, type StatusCode } from "@std/http";
import { Method } from "../router/method.ts";

export default class Context {
#request: Request;

#message: string;
#headers: Headers;
#status: Status;
#status: StatusCode;
#statusText: string;
#url: URL;
#params: Record<string, string> | undefined;
Expand All @@ -15,8 +15,8 @@ export default class Context {
this.#request = req;
this.#message = "";
this.#headers = new Headers();
this.#status = Status.OK;
this.#statusText = STATUS_TEXT[Status.OK];
this.#status = STATUS_CODE.OK;
this.#statusText = STATUS_TEXT[STATUS_CODE.OK];
this.#url = new URL(req.url);
}

Expand Down Expand Up @@ -79,10 +79,13 @@ export default class Context {
}

public status(code: number, text?: string): Context;
public status(code: Status, text?: string): Context {
public status(code: StatusCode, text?: string): Context {
this.#status = code;
if (text) this.#statusText = text;
else this.#statusText = STATUS_TEXT[code];
if (text) {
this.#statusText = text;
} else {
this.#statusText = STATUS_TEXT[STATUS_CODE.OK];
}
return this;
}

Expand Down
5 changes: 5 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"@std/http": "jsr:@std/http@^0.224.5"
}
}
61 changes: 61 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions import_map.json

This file was deleted.

10 changes: 2 additions & 8 deletions router/handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Status, STATUS_TEXT } from "http";
import { STATUS_CODE, STATUS_TEXT } from "@std/http";
import type { Context } from "../context/mod.ts";

export type Handler = (
context: Context,
) => Promise<void | Context> | void | Context;

export function NotFoundHandler(ctx: Context) {
ctx.status(Status.NotFound).text(STATUS_TEXT[Status.NotFound]);
ctx.status(STATUS_CODE.NotFound).text(STATUS_TEXT[STATUS_CODE.NotFound]);
}

export type HandlerResult = {
Expand All @@ -15,9 +15,3 @@ export type HandlerResult = {
};

export const EMPTY_HANDLER: HandlerResult = { handler: undefined, params: {} };
export function makeHandlerResult(
handler: Handler | undefined,
params: Record<string, string> = {},
): HandlerResult {
return { handler, params };
}
4 changes: 2 additions & 2 deletions router/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EMPTY_HANDLER, HandlerResult, makeHandlerResult } from "./handler.ts";
import { EMPTY_HANDLER, HandlerResult } from "./handler.ts";
import { type Handler, Method } from "./mod.ts";

export default class Node {
Expand Down Expand Up @@ -32,7 +32,7 @@ export default class Node {
const path = paths.shift();
if (path === undefined) {
const handler = this.#handlers.get(method);
return makeHandlerResult(handler);
return { handler, params: {} };
}
const child = this.#children.get(path);
let handler: HandlerResult;
Expand Down

0 comments on commit b60d66f

Please sign in to comment.