mirror of
https://github.com/zadam/trilium.git
synced 2026-03-24 12:50:10 +01:00
fix(standalone): component ID not preserved in WS
This commit is contained in:
@@ -183,6 +183,7 @@ export class BrowserRouter {
|
||||
path,
|
||||
params,
|
||||
query,
|
||||
headers: headers ?? {},
|
||||
body: parsedBody
|
||||
};
|
||||
|
||||
|
||||
@@ -32,6 +32,18 @@ function toExpressLikeReq(req: BrowserRequest) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts context headers from the request and sets them in the execution context,
|
||||
* mirroring what the server does in route_api.ts.
|
||||
*/
|
||||
function setContextFromHeaders(req: BrowserRequest) {
|
||||
const headers = req.headers ?? {};
|
||||
const ctx = getContext();
|
||||
ctx.set("componentId", headers["trilium-component-id"]);
|
||||
ctx.set("localNowDateTime", headers["trilium-local-now-datetime"]);
|
||||
ctx.set("hoistedNoteId", headers["trilium-hoisted-note-id"] || "root");
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a core route handler to work with the BrowserRouter.
|
||||
* Core handlers expect an Express-like request object with params, query, and body.
|
||||
@@ -41,6 +53,7 @@ function toExpressLikeReq(req: BrowserRequest) {
|
||||
function wrapHandler(handler: (req: any) => unknown, transactional: boolean) {
|
||||
return (req: BrowserRequest) => {
|
||||
return getContext().init(() => {
|
||||
setContextFromHeaders(req);
|
||||
const expressLikeReq = toExpressLikeReq(req);
|
||||
if (transactional) {
|
||||
return getSql().transactional(() => handler(expressLikeReq));
|
||||
@@ -72,6 +85,7 @@ function createRoute(router: BrowserRouter) {
|
||||
return (method: HttpMethod, path: string, _middleware: any[], handler: (req: any) => unknown, resultHandler?: ((req: any, res: any, result: unknown) => unknown) | null) => {
|
||||
router.register(method, path, (req: BrowserRequest) => {
|
||||
return getContext().init(() => {
|
||||
setContextFromHeaders(req);
|
||||
const expressLikeReq = toExpressLikeReq(req);
|
||||
const result = getSql().transactional(() => handler(expressLikeReq));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user