From 378829d7d102211e616cf145914a86e974fd3081 Mon Sep 17 00:00:00 2001 From: Seb Ringrose Date: Tue, 24 Dec 2024 00:01:00 +0000 Subject: [PATCH] feat: typed state --- example/simpleAuth/app.ts | 6 +----- lib/routers/_Router.ts | 2 +- lib/routers/httpRouter.ts | 4 ++-- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/example/simpleAuth/app.ts b/example/simpleAuth/app.ts index 0f2fa7e..6e36695 100644 --- a/example/simpleAuth/app.ts +++ b/example/simpleAuth/app.ts @@ -1,11 +1,7 @@ import * as Peko from "../../mod.ts"; // "https://deno.land/x/peko/mod.ts" const html = String; -const router = new Peko.HttpRouter<{ - username: string | undefined -}>({ - username: undefined -}); +const router = new Peko.HttpRouter(); const crypto = new Peko.Crypto("SUPER_SECRET_KEY_123"); // <-- replace from env const user = { // <-- replace with db / auth provider query diff --git a/lib/routers/_Router.ts b/lib/routers/_Router.ts index a7c726e..a18e102 100644 --- a/lib/routers/_Router.ts +++ b/lib/routers/_Router.ts @@ -39,7 +39,7 @@ export class BaseRouter< Route = BaseRoute; constructor( - public state: S, + public state?: S, public middleware: Middleware[] = [], public routes: R[] = [] // <- use this as a hashmap for routes ) {} diff --git a/lib/routers/httpRouter.ts b/lib/routers/httpRouter.ts index 882efb7..8e4af00 100644 --- a/lib/routers/httpRouter.ts +++ b/lib/routers/httpRouter.ts @@ -49,13 +49,13 @@ export class HttpRoute extends BaseRoute { } export class HttpRouter< - S extends object = object, + S extends object, Config extends HttpRouteConfig = HttpRouteConfig, R extends HttpRoute = HttpRoute > extends BaseRouter { Route = HttpRoute; - constructor(public state: S, public middleware: Middleware[] = [], public routes: R[] = []) { + constructor(public state?: S, public middleware: Middleware[] = [], public routes: R[] = []) { super(state, middleware, routes); }