did more docs
This commit is contained in:
parent
0c8d1865ff
commit
5dad5bc0b1
@ -47,10 +47,10 @@ const nestedRouter = new Router();
|
|||||||
|
|
||||||
nestedRouter
|
nestedRouter
|
||||||
.route('/users')
|
.route('/users')
|
||||||
.get((ctx) => {
|
.get((req, ctx) => {
|
||||||
return new Response('GET /users');
|
return new Response('GET /users');
|
||||||
})
|
})
|
||||||
.post((ctx) => {
|
.post((req, ctx) => {
|
||||||
return new Response('POST /users');
|
return new Response('POST /users');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@bearmetal/router",
|
"name": "@bearmetal/router",
|
||||||
"description": "A simple router for Deno",
|
"description": "A simple router for Deno",
|
||||||
"version": "0.2.0",
|
"version": "0.2.1",
|
||||||
"stable": true,
|
"stable": true,
|
||||||
"files": [
|
"files": [
|
||||||
"mod.ts",
|
"mod.ts",
|
||||||
|
55
router.ts
55
router.ts
@ -76,6 +76,22 @@ export class Router {
|
|||||||
private routes: RouteConfig[] = [];
|
private routes: RouteConfig[] = [];
|
||||||
private middleware: MiddlewareConfig[] = [];
|
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 {
|
route(path: string): RouteConfigurator {
|
||||||
path = path.startsWith("/") ? path : `/${path}`;
|
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(
|
use(
|
||||||
pathOrMiddleware: string | Middleware,
|
pathOrMiddleware: string | Middleware,
|
||||||
middlewareOrRouter?: Middleware | Router,
|
middlewareOrRouter?: Middleware | Router,
|
||||||
@ -184,6 +214,19 @@ export class Router {
|
|||||||
return this;
|
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<Response> => {
|
handle = async (req: Request): Promise<Response> => {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
const method = req.method;
|
const method = req.method;
|
||||||
@ -303,6 +346,18 @@ export class Router {
|
|||||||
: `${normalizedBase}/${normalizedRoute}`;
|
: `${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 }) {
|
serveDirectory(dir: string, root: string, opts?: { showIndex: boolean }) {
|
||||||
this.route(root + "*").get(async (_req, ctx) => {
|
this.route(root + "*").get(async (_req, ctx) => {
|
||||||
const { showIndex } = opts ?? { showIndex: false };
|
const { showIndex } = opts ?? { showIndex: false };
|
||||||
|
6
types.ts
6
types.ts
@ -11,10 +11,16 @@ export interface RouterContext {
|
|||||||
request: Request;
|
request: Request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @description a function that handles incoming requests
|
||||||
|
*/
|
||||||
export type Handler = (
|
export type Handler = (
|
||||||
req: Request,
|
req: Request,
|
||||||
ctx: RouterContext,
|
ctx: RouterContext,
|
||||||
) => Promise<Response> | Response;
|
) => Promise<Response> | Response;
|
||||||
|
/**
|
||||||
|
* @description a middleware function
|
||||||
|
*/
|
||||||
export type Middleware = (
|
export type Middleware = (
|
||||||
req: Request,
|
req: Request,
|
||||||
ctx: RouterContext,
|
ctx: RouterContext,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user