From 0202d62151facf252791f42c21a5e81fd7ee6ad8 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sat, 6 Mar 2021 22:06:15 +0100 Subject: [PATCH] Fixes and mod.ts --- README.md | 15 ++++++++ app.ts | 101 ++++++++++++++------------------------------------- mimetypes.ts | 5 +-- mod.ts | 2 + router.ts | 6 +-- 5 files changed, 49 insertions(+), 80 deletions(-) create mode 100644 README.md create mode 100644 mod.ts diff --git a/README.md b/README.md new file mode 100644 index 0000000..77ebf53 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# YADWF + +> **Y**et **a**nother **D**eno **w**eb-**f**ramework + +## Initial setup + +```ts +import { YADWF } from "https://deno.land/x/yadwf@v0.2/mod.ts"; + +const app = new YADWF(); + +app.get("/", (ctx) => ctx.text("Hello World!")); + +app.start({ port: 1337 }); +``` diff --git a/app.ts b/app.ts index 55ae7ad..c3bd166 100644 --- a/app.ts +++ b/app.ts @@ -11,10 +11,10 @@ import { } from "./deps.ts"; import { getNestedDirectories } from "./util.ts"; -export type Handler = (c: Context) => Promise | void; +export type Handler = (ctx: Context) => Promise | void; export type Middleware = (next: Handler) => Handler; -export class Application { +export class YADWF { #server?: Server; #router: Router; @@ -25,19 +25,19 @@ export class Application { private async startServer(server: Server) { this.#server = server; for await (const req of this.#server) { - const c = new Context(req); - log.info(`${c.protocol} ${c.method} ${c.path}`); + const ctx = new Context(req); + log.info(`${ctx.protocol} ${ctx.method} ${ctx.path}`); const { handler, params } = this.#router.find( - HTTPMethods.indexOf(c.method) as Method, - c.path, + HTTPMethods.indexOf(ctx.method) as Method, + ctx.path, ); - c.params = params; // Named parameters + ctx.params = params; // Named parameters try { - await handler(c); + await handler(ctx); } catch (e) { log.error(e); } - req.respond(c.response); + req.respond(ctx.response); } } @@ -70,92 +70,52 @@ export class Application { this.#server?.close(); } - get( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + get(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.GET, handler, ...middlewares); return this; } - post( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + post(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.POST, handler, ...middlewares); return this; } - put( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + put(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.PUT, handler, ...middlewares); return this; } - delete( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + delete(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.DELETE, handler, ...middlewares); return this; } - trace( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + trace(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.TRACE, handler, ...middlewares); return this; } - options( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + options(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.OPTIONS, handler, ...middlewares); return this; } - patch( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + patch(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.PATCH, handler, ...middlewares); return this; } - connect( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + connect(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.CONNECT, handler, ...middlewares); return this; } - head( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + head(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { this.addPath(path, Method.HEAD, handler, ...middlewares); return this; } - any( - path: string, - handler: Handler, - ...middlewares: Middleware[] - ): Application { + any(path: string, handler: Handler, ...middlewares: Middleware[]): YADWF { const methods = Object.values(Method).filter((n) => typeof n === "number"); for (const method of methods) { this.addPath(path, method as Method, handler, ...middlewares); @@ -163,32 +123,27 @@ export class Application { return this; } - file(path: string, file: string, ...middlewares: Middleware[]): Application { + file(path: string, file: string, ...middlewares: Middleware[]): YADWF { this.addPath( path, Method.GET, - async (c) => await c.file(file), + async (ctx) => await ctx.file(file), ...middlewares, ); return this; } - static( - path: string, - directory: string, - ...middlewares: Middleware[] - ): Application { + static(path: string, directory: string, ...middlewares: Middleware[]): YADWF { if (!path.endsWith("/")) path += "/"; - (async () => { - const directories = await getNestedDirectories(directory); + getNestedDirectories(directory).then((directories) => { directories.push(""); for (const dir of directories) { - const hdl = async (c: Context) => { - const file = c.path.substring(join(path, dir, sep).length); + const hdl = async (ctx: Context) => { + const file = ctx.path.substring(join(path, dir, sep).length); if (file.length === 0) { - return await c.file(join(directory, dir, "index.html")); + return await ctx.file(join(directory, dir, "index.html")); } - return await c.file(join(directory, dir, file)); + return await ctx.file(join(directory, dir, file)); }; this.addPath( `${path + dir}/*`.replace(/\/+/, "/"), @@ -197,7 +152,7 @@ export class Application { ...middlewares, ); } - })(); + }); return this; } } diff --git a/mimetypes.ts b/mimetypes.ts index 774abb3..5297125 100644 --- a/mimetypes.ts +++ b/mimetypes.ts @@ -2,10 +2,7 @@ import { extname } from "./deps.ts"; export function getContentType(filename: string): string { const extension = extname(filename); - if (!Object.keys(extensions).includes(extension) || !extension) { - return "application/octet-stream"; - } - return extensions[extension]; + return extensions[extension] || "application/octet-stream"; } export const utf8 = "charset=UTF-8"; diff --git a/mod.ts b/mod.ts new file mode 100644 index 0000000..0a6b8f0 --- /dev/null +++ b/mod.ts @@ -0,0 +1,2 @@ +export * from "./app.ts"; +export * from "./context.ts"; diff --git a/router.ts b/router.ts index be9a910..98be4c1 100644 --- a/router.ts +++ b/router.ts @@ -104,7 +104,7 @@ export class PathHandler { private readonly method: Method, private readonly handler: Handler, ) { - if (this.path.startsWith(":")) { + if (this.path && this.path.startsWith(":")) { this.name = this.path.slice(1); this.path = "*"; } @@ -154,5 +154,5 @@ export interface RouterResult { params: Record; } -export const NotFoundHandler: Handler = (c: Context) => - c.text("404 Not found", Status.NotFound); +export const NotFoundHandler: Handler = (ctx: Context) => + ctx.text("404 Not found", Status.NotFound);