Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(standalone): give custom request handlers the request URL
Elian Doran928e259e6cab
diff --git a/apps/standalone/src/lightweight/browser_routes.spec.ts b/apps/standalone/src/lightweight/browser_routes.spec.tsindex 26c8a3cfd3..0025eb482f 100644--- a/apps/standalone/src/lightweight/browser_routes.spec.ts+++ b/apps/standalone/src/lightweight/browser_routes.spec.ts@@ -317,7 +317,7 @@ describe("custom request handlers and resource providers", () => {cls.init(() => {createHandler("Greeter", "greet/([a-z]+)", `api.res.status(200).send("handled:" + api.pathParams[0]);`);createHandler("Thrower", "explode", `throw new Error("boom in handler");`);- createHandler("Echo", "echo", `api.res.json({ method: api.req.method, body: api.req.body });`);+ createHandler("Echo", "echo", `api.res.json({ method: api.req.method, url: api.req.url, body: api.req.body });`);// A handler that answers after an await: what the server gets for free by holding the// connection open, this runtime has to wait for.createHandler("Deferred", "deferred", `@@ -363,15 +363,17 @@ return (async () => {expect(text(res.body)).toBe("handled:world");});- it("reaches a handler over a method other than GET, carrying the parsed body", async () => {+ it("reaches a handler over a method other than GET, carrying the URL and the parsed body", async () => {const body = new TextEncoder().encode(JSON.stringify({ hello: "there" })).buffer as ArrayBuffer;- const res = await router.dispatch("POST", "http://localhost/custom/echo", body, {+ const res = await router.dispatch("POST", "http://localhost/custom/echo?x=1", body, {"content-type": "application/json"});expect(res.status).toBe(200);expect(res.headers["Content-Type"]).toContain("application/json");- expect(parseJson(res.body)).toEqual({ method: "POST", body: { hello: "there" } });+ expect(parseJson(res.body)).toEqual({+ method: "POST", url: "/custom/echo?x=1", body: { hello: "there" }+ });});// `send()` leaving a number in the body would reach BrowserRouter as something it cannotdiff --git a/apps/standalone/src/lightweight/browser_routes.ts b/apps/standalone/src/lightweight/browser_routes.tsindex 49d1bb6712..94a8909da9 100644--- a/apps/standalone/src/lightweight/browser_routes.ts+++ b/apps/standalone/src/lightweight/browser_routes.ts@@ -35,12 +35,14 @@ type HttpMethod = 'get' | 'post' | 'put' | 'patch' | 'delete';function toCoreRequest(req: BrowserRequest): Request {/* v8 ignore next -- @preserve: BrowserRouter.dispatch always sets req.headers, so the ?? fallback is unreachable. */const headers = req.headers ?? {};+ const { pathname, search } = new URL(req.url);return {params: req.params,query: req.query,body: req.body,headers,method: req.method,+ url: pathname + search,// A file posted for an import route has no `buffer` until an importer that needs one calls// `loadUploadedBytes()`, as on the server when multer leaves a zip on disk.file: req.file as Request["file"],diff --git a/packages/trilium-core/src/http_interface.ts b/packages/trilium-core/src/http_interface.tsindex 5411dfaaad..b20e0fe108 100644--- a/packages/trilium-core/src/http_interface.ts+++ b/packages/trilium-core/src/http_interface.ts@@ -30,6 +30,8 @@ export interface Request<P = Record<string, string | string[]>, Q = QueryString>body: any;headers: RequestHeaders;method: string;+ /** The path and query string the route sees, such as `/custom/echo?x=1`. */+ url: string;originalUrl: string;/** The parsed multipart upload — multer on the server, `BrowserRouter` in standalone. */file?: File;