From 5dad5bc0b1671f528f80ea93658f766d019933cd Mon Sep 17 00:00:00 2001 From: Emma Date: Sat, 4 Jan 2025 18:37:29 -0700 Subject: [PATCH] did more docs --- README.md | 4 ++-- deno.json | 2 +- router.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ types.ts | 6 ++++++ 4 files changed, 64 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ae62173..956cf83 100755 --- a/README.md +++ b/README.md @@ -47,10 +47,10 @@ const nestedRouter = new Router(); nestedRouter .route('/users') - .get((ctx) => { + .get((req, ctx) => { return new Response('GET /users'); }) - .post((ctx) => { + .post((req, ctx) => { return new Response('POST /users'); }); diff --git a/deno.json b/deno.json index 2c50863..ffefe09 100755 --- a/deno.json +++ b/deno.json @@ -1,7 +1,7 @@ { "name": "@bearmetal/router", "description": "A simple router for Deno", - "version": "0.2.0", + "version": "0.2.1", "stable": true, "files": [ "mod.ts", diff --git a/router.ts b/router.ts index 64917d2..be930b2 100755 --- a/router.ts +++ b/router.ts @@ -76,6 +76,22 @@ export class Router { private routes: RouteConfig[] = []; private middleware: MiddlewareConfig[] = []; + /** + * @description defines a new route + * @param path the path to match, uses the same syntax as the URLPattern constructor + * @returns a RouteConfigurator object + * + * @example + * ```ts + * router.route('/users') + * .get((req, ctx) => { + * return new Response('GET /users'); + * }) + * .post((req, ctx) => { + * return new Response('POST /users'); + * }); + * ``` + */ route(path: string): RouteConfigurator { path = path.startsWith("/") ? path : `/${path}`; @@ -114,6 +130,20 @@ export class Router { }; } + /** + * @description adds a middleware to the router + * @param pathOrMiddleware the path to match, uses the same syntax as the URLPattern constructor + * @param middlewareOrRouter the middleware to add, or a Router object to nest routes + * @returns the Router object + * + * @example + * ```ts + * router.use('/users', async (req, ctx, next) => { + * console.log('Using middleware'); + * return await next(); + * }); + * ``` + */ use( pathOrMiddleware: string | Middleware, middlewareOrRouter?: Middleware | Router, @@ -184,6 +214,19 @@ export class Router { return this; } + /** + * @description handles incoming requests + * @param req the incoming request + * @returns a Response object + * + * @example + * ```ts + * Deno.serve({ + * port: 8000, + * handler: router.handle + * }); + * ``` + */ handle = async (req: Request): Promise => { const url = new URL(req.url); const method = req.method; @@ -303,6 +346,18 @@ export class Router { : `${normalizedBase}/${normalizedRoute}`; } + /** + * @description serves a directory as a static website + * @param dir the directory to serve + * @param root the root path to serve the directory from + * @param opts optional options + * @returns the Router object + * + * @example + * ```ts + * router.serveDirectory('/public', './public'); + * ``` + */ serveDirectory(dir: string, root: string, opts?: { showIndex: boolean }) { this.route(root + "*").get(async (_req, ctx) => { const { showIndex } = opts ?? { showIndex: false }; diff --git a/types.ts b/types.ts index f2f04d6..0240f29 100755 --- a/types.ts +++ b/types.ts @@ -11,10 +11,16 @@ export interface RouterContext { request: Request; } +/** + * @description a function that handles incoming requests + */ export type Handler = ( req: Request, ctx: RouterContext, ) => Promise | Response; +/** + * @description a middleware function + */ export type Middleware = ( req: Request, ctx: RouterContext,