Compare commits
No commits in common. "3b1a969145d3bceedac13b1d3081a72963626911" and "5dad5bc0b1671f528f80ea93658f766d019933cd" have entirely different histories.
3b1a969145
...
5dad5bc0b1
@ -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.3-b",
|
"version": "0.2.1",
|
||||||
"stable": true,
|
"stable": true,
|
||||||
"files": [
|
"files": [
|
||||||
"mod.ts",
|
"mod.ts",
|
||||||
@ -10,8 +10,7 @@
|
|||||||
],
|
],
|
||||||
"exports": {
|
"exports": {
|
||||||
".": "./mod.ts",
|
".": "./mod.ts",
|
||||||
"./types": "./types.ts",
|
"./types": "./types.ts"
|
||||||
"./util/response": "./util/response.ts"
|
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
".vscode/"
|
".vscode/"
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
const handlers = {
|
|
||||||
get: () => {
|
|
||||||
return new Response("Hello World");
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default handlers;
|
|
@ -1,6 +1,5 @@
|
|||||||
import Router from "./router.ts";
|
import Router from "./router.ts";
|
||||||
import type { Handler, RouteConfigurator } from "./types.ts";
|
import type { Handler, RouteConfigurator } from "./types.ts";
|
||||||
import { isRelativePath } from "./util/isRelativePath.ts";
|
|
||||||
|
|
||||||
function crawl(dir: string, callback: (path: string) => void) {
|
function crawl(dir: string, callback: (path: string) => void) {
|
||||||
for (const entry of Deno.readDirSync(dir)) {
|
for (const entry of Deno.readDirSync(dir)) {
|
||||||
@ -25,20 +24,13 @@ function crawl(dir: string, callback: (path: string) => void) {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export class FileRouter extends Router {
|
export class FileRouter extends Router {
|
||||||
constructor(root: string, _import?: (path: string) => Promise<unknown>) {
|
constructor(root: string) {
|
||||||
super();
|
super();
|
||||||
crawl(root, async (path) => {
|
crawl(root, async (path) => {
|
||||||
let relativePath = path.replace(root, "").replace(/index\/?/, "");
|
let relativePath = path.replace(root, "");
|
||||||
if (path.endsWith(".ts") || path.endsWith(".js")) {
|
if (path.endsWith(".ts") || path.endsWith(".js")) {
|
||||||
if (isRelativePath(path)) {
|
|
||||||
path = Deno.build.os === "windows"
|
|
||||||
? `${Deno.cwd()}\\${path.replace(/^.?.?\\/, "")}`
|
|
||||||
: `${Deno.cwd()}/${path.replace(/^.?.?\//, "")}`;
|
|
||||||
}
|
|
||||||
relativePath = relativePath.replace(/\.[tj]s/, "");
|
relativePath = relativePath.replace(/\.[tj]s/, "");
|
||||||
const handlers = _import
|
const handlers = await import(path);
|
||||||
? await _import(path)
|
|
||||||
: await import("file://" + path);
|
|
||||||
|
|
||||||
if (handlers.default) {
|
if (handlers.default) {
|
||||||
handlers.default instanceof Router
|
handlers.default instanceof Router
|
||||||
@ -63,15 +55,6 @@ export class FileRouter extends Router {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Deno.env.get("BEARMETAL_ROUTER_DEBUG") === "true") {
|
|
||||||
this.route("/debug/dump").get((_ctx) => {
|
|
||||||
console.log("Dumping routes:");
|
|
||||||
return new Response(
|
|
||||||
this.routes.map((r) => r.pattern.pathname).join("\n"),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
mod.ts
2
mod.ts
@ -5,3 +5,5 @@
|
|||||||
|
|
||||||
export { Router } from "./router.ts";
|
export { Router } from "./router.ts";
|
||||||
export { FileRouter } from "./file_router.ts";
|
export { FileRouter } from "./file_router.ts";
|
||||||
|
|
||||||
|
export type { Handler, Middleware } from "./types.ts";
|
||||||
|
@ -73,7 +73,7 @@ import { NotFound } from "./util/response.ts";
|
|||||||
* });
|
* });
|
||||||
*/
|
*/
|
||||||
export class Router {
|
export class Router {
|
||||||
protected routes: RouteConfig[] = [];
|
private routes: RouteConfig[] = [];
|
||||||
private middleware: MiddlewareConfig[] = [];
|
private middleware: MiddlewareConfig[] = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
12
types.ts
12
types.ts
@ -3,9 +3,6 @@
|
|||||||
* BearMetal Router types
|
* BearMetal Router types
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @description a context object for a request
|
|
||||||
*/
|
|
||||||
export interface RouterContext {
|
export interface RouterContext {
|
||||||
url: URL;
|
url: URL;
|
||||||
params: Record<string, string | undefined>;
|
params: Record<string, string | undefined>;
|
||||||
@ -30,26 +27,17 @@ export type Middleware = (
|
|||||||
next: () => Promise<Response>,
|
next: () => Promise<Response>,
|
||||||
) => Promise<Response>;
|
) => Promise<Response>;
|
||||||
|
|
||||||
/**
|
|
||||||
* @description a route configuration
|
|
||||||
*/
|
|
||||||
export interface RouteConfig {
|
export interface RouteConfig {
|
||||||
pattern: URLPattern;
|
pattern: URLPattern;
|
||||||
handlers: { [method: string]: Handler };
|
handlers: { [method: string]: Handler };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @description a middleware configuration
|
|
||||||
*/
|
|
||||||
export interface MiddlewareConfig {
|
export interface MiddlewareConfig {
|
||||||
pattern: URLPattern;
|
pattern: URLPattern;
|
||||||
handler: Middleware;
|
handler: Middleware;
|
||||||
path: string;
|
path: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @description a route configurator
|
|
||||||
*/
|
|
||||||
export interface RouteConfigurator {
|
export interface RouteConfigurator {
|
||||||
get(handler: Handler): RouteConfigurator;
|
get(handler: Handler): RouteConfigurator;
|
||||||
post(handler: Handler): RouteConfigurator;
|
post(handler: Handler): RouteConfigurator;
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
export function isRelativePath(path: string) {
|
|
||||||
return !path.startsWith("/") &&
|
|
||||||
(path.startsWith("./") || path.startsWith("../"));
|
|
||||||
}
|
|
@ -1,35 +1,4 @@
|
|||||||
/**
|
export const NotFound = (msg?: string) =>
|
||||||
* @module
|
|
||||||
* BearMetal Router response utilities
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @description a response with a status of 404
|
|
||||||
*/
|
|
||||||
export const NotFound = (msg?: string): Response =>
|
|
||||||
new Response(msg ?? "Not Found", { status: 404 });
|
new Response(msg ?? "Not Found", { status: 404 });
|
||||||
/**
|
export const InternalError = (msg?: string) =>
|
||||||
* @description a response with a status of 500
|
|
||||||
*/
|
|
||||||
export const InternalError = (msg?: string): Response =>
|
|
||||||
new Response(msg ?? "Internal Server Error", { status: 500 });
|
new Response(msg ?? "Internal Server Error", { status: 500 });
|
||||||
/**
|
|
||||||
* @description a response with a status of 400
|
|
||||||
*/
|
|
||||||
export const BadRequest = (msg?: string): Response =>
|
|
||||||
new Response(msg ?? "Bad Request", { status: 400 });
|
|
||||||
/**
|
|
||||||
* @description a response with a status of 401
|
|
||||||
*/
|
|
||||||
export const Unauthorized = (msg?: string): Response =>
|
|
||||||
new Response(msg ?? "Unauthorized", { status: 401 });
|
|
||||||
/**
|
|
||||||
* @description a response with a status of 403
|
|
||||||
*/
|
|
||||||
export const Forbidden = (msg?: string): Response =>
|
|
||||||
new Response(msg ?? "Forbidden", { status: 403 });
|
|
||||||
/**
|
|
||||||
* @description a response with a status of 200
|
|
||||||
*/
|
|
||||||
export const Ok = (msg?: string): Response =>
|
|
||||||
new Response(msg ?? "OK", { status: 200 });
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user