Skip to content

Commit

Permalink
Merge pull request #218 from sejori/base-router
Browse files Browse the repository at this point in the history
Base router
  • Loading branch information
sejori authored Sep 21, 2024
2 parents 181e82b + 40e3d95 commit bc59513
Show file tree
Hide file tree
Showing 35 changed files with 456 additions and 427 deletions.
5 changes: 3 additions & 2 deletions example/reactSSR/handlers/react.handler.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { JSX, ReactNode } from "react";
import { renderToString } from "react-dom/server";
import { Handler, RequestContext, ssr } from "../../../mod.ts";
import htmlTemplate from "../src/document.ts";

export const reactHandler =
(
component: (props: Record<string, unknown>) => unknown,
component: (props: Record<string, ReactNode>) => JSX.Element,
title: string,
entrypoint: string
): Handler =>
(ctx: RequestContext<{ env?: { ENVIRONMENT: string } }>) => {
return ssr(
() => {
const ssrHTML = renderToString(component(ctx.state), null);
const ssrHTML = renderToString(component(ctx.state as Record<string, ReactNode>));
return htmlTemplate({
title,
ssrHTML,
Expand Down
4 changes: 2 additions & 2 deletions example/reactSSR/router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Router, logger, cacher } from "../../mod.ts"; //"https://deno.land/x/peko/mod.ts"
import { HttpRouter, logger, cacher } from "../../mod.ts"; //"https://deno.land/x/peko/mod.ts"
import { reactHandler } from "./handlers/react.handler.ts";
import { githubHandler } from "./handlers/github.handler.ts";
import { reqTime } from "./middleware/reqTime.middleware.ts";
Expand All @@ -7,7 +7,7 @@ import { parrotHandler } from "./handlers/parrot.handler.ts";
import About from "./src/pages/About.tsx";
import Home from "./src/pages/Home.tsx";

const router = new Router();
const router = new HttpRouter();
router.use(logger(console.log));

// SSR, with cache because static page
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Peko from "../../mod.ts"; // "https://deno.land/x/peko/mod.ts"

const html = String;
const router = new Peko.Router();
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
Expand Down
224 changes: 0 additions & 224 deletions lib/Router.ts

This file was deleted.

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 "../Router.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
Loading

0 comments on commit bc59513

Please sign in to comment.