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(core): declare the script API's req/res as the public script types
Elian Doran0cdd5148bdd1
diff --git a/apps/server/src/routes/custom.ts b/apps/server/src/routes/custom.tsindex d227e210e6..341c348efa 100644--- a/apps/server/src/routes/custom.ts+++ b/apps/server/src/routes/custom.ts@@ -1,3 +1,4 @@+import type { ScriptRequest } from "@triliumnext/commons/src/lib/script_api.js";import { becca, cls, getLog, routeHelpers, scriptService, utils } from "@triliumnext/core";import type { Request, Response, Router } from "express";@@ -74,7 +75,10 @@ function handleRequest(req: Request, res: Response) {try {scriptService.executeNote(note, {pathParams: match.slice(1),- req,+ // `ScriptRequest` declares `params` flat, while Express 5 types a wildcard as+ // `string | string[]` — the same widening `splitPath` above works around. A+ // handler reads its path through `pathParams` rather than through `params`.+ req: req as unknown as ScriptRequest,res});} catch (e: unknown) {diff --git a/packages/trilium-core/src/services/backend_script_api.ts b/packages/trilium-core/src/services/backend_script_api.tsindex 3a8f374a76..5c7f30a6d8 100644--- a/packages/trilium-core/src/services/backend_script_api.ts+++ b/packages/trilium-core/src/services/backend_script_api.ts@@ -1,6 +1,5 @@import { type AttributeRow, dayjs, formatLogMessage } from "@triliumnext/commons";-import type { BackendApi as PublicBackendApi, ScriptBNote as PublicScriptBNote } from "@triliumnext/commons/src/lib/script_api.js";-import type { Request, Response } from "../http_interface";+import type { BackendApi as PublicBackendApi, ScriptBNote as PublicScriptBNote, ScriptRequest, ScriptResponse } from "@triliumnext/commons/src/lib/script_api.js";import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity";import Becca from "../becca/becca-interface";import * as htmlParser from "node-html-parser";@@ -119,15 +118,15 @@ export interface Api {/*** The request. Only present when the script runs as a custom request handler (a note with the* `#customRequestHandler` label invoked via `/custom/...`); `undefined` for every other backend- * script. A custom handler only runs on the server, so the object is Express's `req` and carries- * the rest of its API beyond what {@link Request} names.+ * script. {@link ScriptRequest} is the same subset the in-editor language service offers; the+ * object itself is Express's `req`, whose full API the Custom Request Handler guide points to.*/- req?: Request;+ req?: ScriptRequest;/*** The response to write the HTTP reply to, under the same conditions as {@link ApiParams.req}* and likewise Express's own `res` at runtime.*/- res?: Response;+ res?: ScriptResponse;/*** Capture groups from the `#customRequestHandler` regex that matched this request's* URL, in order. Only present in custom request handlers.diff --git a/packages/trilium-core/src/services/backend_script_api_interface.ts b/packages/trilium-core/src/services/backend_script_api_interface.tsindex 42aea57796..757ddf9871 100644--- a/packages/trilium-core/src/services/backend_script_api_interface.ts+++ b/packages/trilium-core/src/services/backend_script_api_interface.ts@@ -1,4 +1,4 @@-import type { Request, Response } from "../http_interface";+import type { ScriptRequest, ScriptResponse } from "@triliumnext/commons/src/lib/script_api.js";import type BNote from "../becca/entities/bnote.js";import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";@@ -7,6 +7,6 @@ export interface ApiParams {startNote?: BNote | null;originEntity?: AbstractBeccaEntity<any> | null;pathParams?: string[];- req?: Request;- res?: Response;+ req?: ScriptRequest;+ res?: ScriptResponse;}