Skip to content

Commit

Permalink
extract route handler to enable support for deeper paths
Browse files Browse the repository at this point in the history
  • Loading branch information
floodfx committed Mar 7, 2022
1 parent 91097c3 commit d255665
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/server/live_view_route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { NextFunction, Request, Response } from "express";
import jwt from "jsonwebtoken";
import { nanoid } from "nanoid";
import { LiveViewComponent, LiveViewSocket, PageTitleDefaults } from ".";

type SessionDataProvider<T extends {csrfToken: string}> = (req: Request) => T;

const emptyVoid = () => {};

export const configLiveViewHandler = <T extends {csrfToken: string}>(
component: LiveViewComponent<unknown,unknown>,
rootView: string,
signingSecret: string,
sessionDataProvider: SessionDataProvider<T>,
pageTitleDefaults: PageTitleDefaults
) => {
return async (req:Request, res: Response, next: NextFunction) => {

// new LiveViewId for each request
const liveViewId = nanoid();

// mock socket
const liveViewSocket: LiveViewSocket<T> = {
id: liveViewId,
connected: false, // ws socket not connected on http request
context: {} as T,
sendInternal: emptyVoid,
repeat: emptyVoid,
pageTitle: emptyVoid,
subscribe: emptyVoid,
pushPatch: emptyVoid,
}

// get session data from provider
const session = sessionDataProvider(req);

// mount and render component
const ctx = await component.mount(
{
_csrf_token: session.csrfToken,
_mounts: -1
},
session,
liveViewSocket
);
const view = component.render(ctx);

// render the view with all the data
res.render(rootView, {
page_title: pageTitleDefaults?.title ?? "",
page_title_prefix: pageTitleDefaults?.prefix,
page_title_suffix: pageTitleDefaults?.suffix,
csrf_meta_tag: req.session.csrfToken,
liveViewId,
session: jwt.sign(session, signingSecret),
// TODO support static assets https://github.com/floodfx/liveviewjs/issues/42
statics: jwt.sign(JSON.stringify(view.statics), signingSecret),
inner_content: view.toString()
})
}
}

0 comments on commit d255665

Please sign in to comment.