Skip to content

Commit

Permalink
feat: base router abstracted
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Ringrose committed Sep 11, 2024
1 parent ea6892e commit 9c22dc0
Show file tree
Hide file tree
Showing 29 changed files with 423 additions and 525 deletions.
12 changes: 12 additions & 0 deletions lib/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BaseRouter } from "./routers/_Router.ts";

export class RequestContext<T extends object = Record<string, unknown>> {
url: URL;
state: T;
params: Record<string, string> = {};

constructor(public router: BaseRouter, public request: Request, state?: T) {
this.url = new URL(request.url);
this.state = state ? state : ({} as T);
}
}
26 changes: 0 additions & 26 deletions lib/handlers/graph.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/handlers/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestContext } from "../routers/httpRouter.ts";
import { RequestContext } from "../context.ts";
import { Crypto } from "../utils/Crypto.ts";
import { mergeHeaders } from "../utils/helpers.ts";
import { Handler, HandlerOptions } from "../types.ts";
Expand Down
21 changes: 11 additions & 10 deletions lib/routers/_router.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Handler, Middleware, RequestContext } from "../types.ts";
import { RequestContext } from "../context.ts";
import { Handler, Middleware } from "../types.ts";
import { Cascade } from "../utils/Cascade.ts";

export interface RouteConfig {
export interface BaseRouteConfig {
path: string;
middleware?: Middleware | Middleware[];
handler?: Handler;
}

export class Route {
export class BaseRoute {
path: string;
middleware: Middleware[];
handler?: Handler;

constructor(routeObj: RouteConfig) {
constructor(routeObj: BaseRouteConfig) {
this.path = routeObj.path;
this.handler = routeObj.handler && Cascade.promisify(routeObj.handler);
this.middleware = [routeObj.middleware]
Expand All @@ -30,11 +31,11 @@ export class Route {
}
}

export class Router<
Config extends RouteConfig = RouteConfig,
R extends Route = Route
export class BaseRouter<
Config extends BaseRouteConfig = BaseRouteConfig,
R extends BaseRoute = BaseRoute
> {
Route = Route;
Route = BaseRoute;

constructor(
public routes: R[] = [], // <- use this as a hashmap for routes
Expand Down Expand Up @@ -89,10 +90,10 @@ export class Router<
const routeObj =
typeof arg1 !== "string"
? arg1
: arguments.length === 2
: arguments.length === 2
? arg2 instanceof Function
? { path: arg1, handler: arg2 as Handler }
: { path: arg1, ...arg2 as Omit<Config, "path"> }
: { path: arg1, ...(arg2 as Omit<Config, "path">) }
: {
path: arg1,
middleware: arg2 as Middleware | Middleware[],
Expand Down
20 changes: 10 additions & 10 deletions lib/routers/httpRouter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Middleware, Handler, RequestContext } from "../types.ts";
import { Route, Router, RouteConfig } from "./_router.ts";
import { RequestContext } from "../context.ts";
import { Middleware, Handler } from "../types.ts";
import { BaseRoute, BaseRouter, BaseRouteConfig } from "./_Router.ts";

export interface HttpRouteConfig extends RouteConfig {
export interface HttpRouteConfig extends BaseRouteConfig {
path: `/${string}`;
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
handler: Handler;
}

export class HttpRoute extends Route {
export class HttpRoute extends BaseRoute {
declare path: `/${string}`;
declare method: HttpRouteConfig["method"];

Expand Down Expand Up @@ -47,13 +48,13 @@ export class HttpRoute extends Route {
}
}

export class HttpRouter<Config extends RouteConfig = HttpRouteConfig, R extends Route = HttpRoute> extends Router<Config, R> {
export class HttpRouter<
Config extends HttpRouteConfig = HttpRouteConfig,
R extends HttpRoute = HttpRoute
> extends BaseRouter<Config, R> {
Route = HttpRoute;

constructor(
public routes: R[] = [],
public middleware: Middleware[] = []
) {
constructor(public routes: R[] = [], public middleware: Middleware[] = []) {
super();
}

Expand Down Expand Up @@ -85,4 +86,3 @@ export class HttpRouter<Config extends RouteConfig = HttpRouteConfig, R extends
return newRoute;
};
}
export { RequestContext };
92 changes: 0 additions & 92 deletions lib/routers/schemaRouter.ts

This file was deleted.

13 changes: 1 addition & 12 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import { Router } from "./routers/_router.ts";

export class RequestContext<T extends object = Record<string, unknown>> {
url: URL;
state: T;
params: Record<string, string> = {};

constructor(public router: Router, public request: Request, state?: T) {
this.url = new URL(request.url);
this.state = state ? state : ({} as T);
}
}
import { RequestContext } from "./context.ts";

export type Result = void | Response | undefined;
export type Next = () => Promise<Result> | Result;
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/CacheItem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestContext } from "../types.ts";
import { RequestContext } from "../context.ts";

export class CacheItem {
key: string;
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/Cascade.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestContext, Middleware, Result, Next, Handler } from "../types.ts";
import { RequestContext } from "../context.ts";
import { Middleware, Result, Next, Handler } from "../types.ts";

export type PromiseHandler = (
ctx: RequestContext,
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/Profiler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Router, Route } from "../routers/_router.ts";
import { BaseRouter, BaseRoute } from "../routers/_Router.ts";

type ProfileConfig = {
mode?: "serve" | "handle";
url?: string;
count?: number;
excludedRoutes?: Route[];
excludedRoutes?: BaseRoute[];
};

type ProfileResults = Record<
Route["path"],
BaseRoute["path"],
{
avgTime: number;
requests: {
Expand All @@ -26,7 +26,7 @@ export class Profiler {
* @param config
* @returns results: ProfileResults
*/
static async run(router: Router, config?: ProfileConfig) {
static async run(router: BaseRouter, config?: ProfileConfig) {
const url = (config && config.url) || `http://localhost:7777`;
const count = (config && config.count) || 100;
const excludedRoutes = (config && config.excludedRoutes) || [];
Expand Down
Loading

0 comments on commit 9c22dc0

Please sign in to comment.