Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base router #218

Merged
merged 12 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading