Skip to content

Build your personal knowledge base with Trilium Notes

refactor(core): type shared routes against core's own Request/Response

Elian Doran7113ffa43146
diff --git a/packages/trilium-core/src/http_interface.ts b/packages/trilium-core/src/http_interface.ts
new file mode 100644
index 0000000000..5411dfaaad
--- /dev/null
+++ b/packages/trilium-core/src/http_interface.ts
@@ -0,0 +1,60 @@
+import type { File } from "./services/import/common";
+
+/** One value out of a query string. Bracket notation (`?a[b]=c`) nests, which no shared route reads. */
+export type QueryValue = string | QueryString | (string | QueryString)[];
+
+/**
+ * A parsed query string, as wide as the server's query parser can make it. Declare the shape a
+ * route expects as {@link Request}'s second argument rather than narrowing a read by hand.
+ */
+export interface QueryString { [key: string]: QueryValue | undefined }
+
+/** Request headers, lower-cased. A header sent more than once arrives as an array. */
+export type RequestHeaders = Record<string, string | string[] | undefined>;
+
+/**
+ * The request surface a shared route handler is allowed to read.
+ *
+ * Deliberately narrower than Express: core also runs in standalone's Web Worker, where the request
+ * is assembled by `BrowserRouter` and no Express object exists. Express's own `Request` satisfies
+ * this structurally, so the server passes its objects through unchanged.
+ *
+ * @param P the shape of the path parameters the route declares, e.g. `Request<{ noteId: string }>`.
+ * A wildcard segment matches more than once, so an undeclared parameter can be an array.
+ * @param Q the shape of the query string, e.g. `Request<{ noteId: string }, { preview?: string }>`.
+ */
+export interface Request<P = Record<string, string | string[]>, Q = QueryString> {
+ params: P;
+ query: Q;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ body: any;
+ headers: RequestHeaders;
+ method: string;
+ originalUrl: string;
+ /** The parsed multipart upload — multer on the server, `BrowserRouter` in standalone. */
+ file?: File;
+ get(name: string): string | undefined;
+}
+
+/**
+ * The response surface a shared route handler is allowed to write to, and the counterpart to
+ * {@link Request}. Express's `Response` and standalone's mock response both satisfy it.
+ *
+ * The header and status methods return the response itself, so a handler can chain them as
+ * `res.setHeader("Content-Type", "text/plain").status(404).send(message)`.
+ */
+export interface Response {
+ status(code: number): this;
+ set(name: string, value: string): this;
+ setHeader(name: string, value: string): this;
+ removeHeader(name: string): void;
+ send(body?: unknown): this;
+ sendStatus(code: number): this;
+ /**
+ * Writes one chunk of a body assembled piece by piece, which only the OPML export does. Binary
+ * is deliberately absent: standalone's response joins the chunks into a string. Send bytes with
+ * `send()` instead, or widen this together with that implementation.
+ */
+ write(chunk: string): boolean;
+ end(): unknown;
+}
diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts
index 23a9f22af8..432f391f94 100644
--- a/packages/trilium-core/src/index.ts
+++ b/packages/trilium-core/src/index.ts
@@ -154,6 +154,7 @@ export { default as imageService } from "./services/image";
export { t } from "i18next";
export type { RequestProvider, ExecOpts, CookieJar, FetchApiOpts, FetchResourceOpts, FetchedResource } from "./services/request";
export type * from "./meta";
+export type { QueryString, QueryValue, Request, RequestHeaders, Response } from "./http_interface";
export * as routeHelpers from "./routes/helpers";
export { getZipProvider, type ZipArchive, type ZipProvider } from "./services/zip_provider";
diff --git a/packages/trilium-core/src/routes/api/attachments.ts b/packages/trilium-core/src/routes/api/attachments.ts
index 16f52fec7b..c60bf2a7a9 100644
--- a/packages/trilium-core/src/routes/api/attachments.ts
+++ b/packages/trilium-core/src/routes/api/attachments.ts
@@ -1,9 +1,6 @@
import { ConvertAttachmentToNoteResponse, isAcceptedImageMime } from "@triliumnext/commons";
import { ValidationError } from "../../errors";
-import type { Request } from "express";
-import type { File } from "../../services/import/common.js";
-
-type FileRequest<P> = Omit<Request<P>, "file"> & { file?: File };
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import blobService from "../../services/blob.js";
@@ -47,7 +44,7 @@ function saveAttachment(req: Request<{ noteId: string }>) {
note.saveAttachment({ attachmentId, role, mime, title, content }, matchBy);
}
-async function uploadAttachment(req: FileRequest<{ noteId: string }>) {
+async function uploadAttachment(req: Request<{ noteId: string }>) {
const { noteId } = req.params;
const { file } = req;
diff --git a/packages/trilium-core/src/routes/api/attributes.ts b/packages/trilium-core/src/routes/api/attributes.ts
index 4272271b4a..182ede7cdc 100644
--- a/packages/trilium-core/src/routes/api/attributes.ts
+++ b/packages/trilium-core/src/routes/api/attributes.ts
@@ -1,5 +1,5 @@
import { UpdateAttributeResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import BAttribute from "../../becca/entities/battribute.js";
diff --git a/packages/trilium-core/src/routes/api/autocomplete.ts b/packages/trilium-core/src/routes/api/autocomplete.ts
index 7f4077c5e6..26edd86b46 100644
--- a/packages/trilium-core/src/routes/api/autocomplete.ts
+++ b/packages/trilium-core/src/routes/api/autocomplete.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import * as cls from "../../services/context.js";
diff --git a/packages/trilium-core/src/routes/api/backup.ts b/packages/trilium-core/src/routes/api/backup.ts
index d98ed2de1d..beb1e21110 100644
--- a/packages/trilium-core/src/routes/api/backup.ts
+++ b/packages/trilium-core/src/routes/api/backup.ts
@@ -1,6 +1,6 @@
import type { BackupDatabaseNowResponse, ExistingBackupsResponse } from "@triliumnext/commons";
import { getBackup } from "../../services/backup.js";
-import { Request, Response } from "express";
+import { Request, Response } from "../../http_interface";
async function getExistingBackups(): Promise<ExistingBackupsResponse> {
const backup = getBackup();
diff --git a/packages/trilium-core/src/routes/api/board.ts b/packages/trilium-core/src/routes/api/board.ts
index 8d76478925..01835b09af 100644
--- a/packages/trilium-core/src/routes/api/board.ts
+++ b/packages/trilium-core/src/routes/api/board.ts
@@ -1,7 +1,7 @@
import {
type BoardColumnsKey, boardColumnsKey, promotedAttributeDefinitionParser
} from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import type BNote from "../../becca/entities/bnote.js";
diff --git a/packages/trilium-core/src/routes/api/branches.ts b/packages/trilium-core/src/routes/api/branches.ts
index feb2fbfc9b..4f66c517fd 100644
--- a/packages/trilium-core/src/routes/api/branches.ts
+++ b/packages/trilium-core/src/routes/api/branches.ts
@@ -1,7 +1,7 @@
import branchService from "../../services/branches.js";
import eraseService from "../../services/erase.js";
import eventService from "../../services/events.js";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import entityChangesService from "../../services/entity_changes.js";
diff --git a/packages/trilium-core/src/routes/api/bulk_action.ts b/packages/trilium-core/src/routes/api/bulk_action.ts
index d76ec43ea3..5996a686af 100644
--- a/packages/trilium-core/src/routes/api/bulk_action.ts
+++ b/packages/trilium-core/src/routes/api/bulk_action.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import bulkActionService from "../../services/bulk_actions.js";
diff --git a/packages/trilium-core/src/routes/api/cloning.ts b/packages/trilium-core/src/routes/api/cloning.ts
index ac28ca70c6..a9b9405ae0 100644
--- a/packages/trilium-core/src/routes/api/cloning.ts
+++ b/packages/trilium-core/src/routes/api/cloning.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import cloningService from "../../services/cloning.js";
diff --git a/packages/trilium-core/src/routes/api/deleted_notes.ts b/packages/trilium-core/src/routes/api/deleted_notes.ts
index c6e33bfb6e..195fcad192 100644
--- a/packages/trilium-core/src/routes/api/deleted_notes.ts
+++ b/packages/trilium-core/src/routes/api/deleted_notes.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import type { NoteRow, NoteType } from "@triliumnext/commons";
import { NotFoundError } from "../../errors.js";
diff --git a/packages/trilium-core/src/routes/api/export.ts b/packages/trilium-core/src/routes/api/export.ts
index 59ef56b0ee..e380ab9f63 100644
--- a/packages/trilium-core/src/routes/api/export.ts
+++ b/packages/trilium-core/src/routes/api/export.ts
@@ -1,4 +1,4 @@
-import type { Request, Response } from "express";
+import type { Request, Response } from "../../http_interface";
import becca from "../../becca/becca.js";
import opmlExportService from "../../services/export/opml.js";
diff --git a/packages/trilium-core/src/routes/api/files.ts b/packages/trilium-core/src/routes/api/files.ts
index af3600f707..c915150c54 100644
--- a/packages/trilium-core/src/routes/api/files.ts
+++ b/packages/trilium-core/src/routes/api/files.ts
@@ -1,16 +1,13 @@
-import { Request, Response } from "express";
+import { Request, Response } from "../../http_interface";
import becca from "../../becca/becca";
import type BAttachment from "../../becca/entities/battachment";
import type BNote from "../../becca/entities/bnote";
-import type { File } from "../../services/import/common.js";
import noteService from "../../services/notes.js";
import { convertOfficeToHtml } from "../../services/office_preview.js";
import protected_session from "../../services/protected_session";
import { downloadData, downloadNoteInt } from "../helpers";
import { serveContentWithRanges } from "../partial_content";
-type FileRequest<P> = Omit<Request<P>, "file"> & { file?: File };
-
const downloadFile = (req: Request<{ noteId: string }>, res: Response) => downloadNoteInt(req.params.noteId, res, true);
const openFile = (req: Request<{ noteId: string }>, res: Response) => downloadNoteInt(req.params.noteId, res, false);
@@ -47,7 +44,7 @@ function openPartialAttachment(req: Request<{ attachmentId: string }>, res: Resp
* PDF viewer saves annotations through. `replace=1` writes straight over the content — for an editor
* saving its own work, where a revision per save would bury the note in them.
*/
-function updateFile(req: FileRequest<{ noteId: string }>) {
+function updateFile(req: Request<{ noteId: string }>) {
const note = becca.getNoteOrThrow(req.params.noteId);
const file = req.file;
@@ -77,7 +74,7 @@ function updateFile(req: FileRequest<{ noteId: string }>) {
}
/** The attachment counterpart of {@link updateFile}: replacing an attachment's file with another. */
-function updateAttachment(req: FileRequest<{ attachmentId: string }>) {
+function updateAttachment(req: Request<{ attachmentId: string }>) {
const attachment = becca.getAttachmentOrThrow(req.params.attachmentId);
const file = req.file;
if (!file) {
diff --git a/packages/trilium-core/src/routes/api/fonts.spec.ts b/packages/trilium-core/src/routes/api/fonts.spec.ts
index f84abdac97..e2bc555e56 100644
--- a/packages/trilium-core/src/routes/api/fonts.spec.ts
+++ b/packages/trilium-core/src/routes/api/fonts.spec.ts
@@ -1,4 +1,4 @@
-import type { Request, Response } from "express";
+import type { Request, Response } from "../../http_interface";
import { beforeEach, describe, expect, it } from "vitest";
import * as cls from "../../services/context.js";
diff --git a/packages/trilium-core/src/routes/api/fonts.ts b/packages/trilium-core/src/routes/api/fonts.ts
index a9d17baf88..188669fa91 100644
--- a/packages/trilium-core/src/routes/api/fonts.ts
+++ b/packages/trilium-core/src/routes/api/fonts.ts
@@ -1,5 +1,5 @@
import { customFontFamily, customFontNoteId, SYSTEM_MONOSPACE_FONT_STACK, SYSTEM_SANS_SERIF_FONT_STACK, type OptionMap } from "@triliumnext/commons";
-import type { Request, Response } from "express";
+import type { Request, Response } from "../../http_interface";
import optionService from "../../services/options.js";
import sqlInit from "../../services/sql_init.js";
diff --git a/packages/trilium-core/src/routes/api/image.ts b/packages/trilium-core/src/routes/api/image.ts
index 3f48d1dfc2..e472073083 100644
--- a/packages/trilium-core/src/routes/api/image.ts
+++ b/packages/trilium-core/src/routes/api/image.ts
@@ -1,8 +1,5 @@
import { type ImageCompressionOptions, isAcceptedImageMime, isImageAttachmentRole, isSvgMime, NOTE_TYPE_IMAGE_ATTACHMENTS } from "@triliumnext/commons";
-import type { Request, Response } from "express";
-import type { File } from "../../services/import/common.js";
-
-type FileRequest<P> = Omit<Request<P>, "file"> & { file?: File };
+import type { Request, Response } from "../../http_interface";
import becca from "../../becca/becca.js";
import type BNote from "../../becca/entities/bnote.js";
@@ -111,7 +108,7 @@ function returnAttachedImage(req: Request<{ attachmentId: string }>, res: Respon
}
}
-async function updateImage(req: FileRequest<{ noteId: string }>) {
+async function updateImage(req: Request<{ noteId: string }>) {
const { noteId } = req.params;
const { file } = req;
diff --git a/packages/trilium-core/src/routes/api/import.ts b/packages/trilium-core/src/routes/api/import.ts
index 859a90dc70..cf84bb635b 100644
--- a/packages/trilium-core/src/routes/api/import.ts
+++ b/packages/trilium-core/src/routes/api/import.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import type { File } from "../../services/import/common.js";
diff --git a/packages/trilium-core/src/routes/api/link_embed.spec.ts b/packages/trilium-core/src/routes/api/link_embed.spec.ts
index 07a513211f..d2ee7bf804 100644
--- a/packages/trilium-core/src/routes/api/link_embed.spec.ts
+++ b/packages/trilium-core/src/routes/api/link_embed.spec.ts
@@ -1,5 +1,5 @@
import { extractYouTubeVideoId } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { Jimp } from "jimp";
import { beforeEach, describe, expect, it, vi } from "vitest";
diff --git a/packages/trilium-core/src/routes/api/link_embed.ts b/packages/trilium-core/src/routes/api/link_embed.ts
index 0ccc90a998..a4c6eb60f1 100644
--- a/packages/trilium-core/src/routes/api/link_embed.ts
+++ b/packages/trilium-core/src/routes/api/link_embed.ts
@@ -5,7 +5,7 @@ import {
linkPreviewImageName,
safeHostname
} from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { parse } from "node-html-parser";
import { ValidationError } from "../../errors.js";
diff --git a/packages/trilium-core/src/routes/api/login.ts b/packages/trilium-core/src/routes/api/login.ts
index e7fe0b95fb..13af586bf5 100644
--- a/packages/trilium-core/src/routes/api/login.ts
+++ b/packages/trilium-core/src/routes/api/login.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import events from "../../services/events.js";
import passwordEncryptionService from "../../services/encryption/password_encryption.js";
import protectedSession from "../../services/protected_session.js";
diff --git a/packages/trilium-core/src/routes/api/note_map.ts b/packages/trilium-core/src/routes/api/note_map.ts
index e6b9243aae..d276715063 100644
--- a/packages/trilium-core/src/routes/api/note_map.ts
+++ b/packages/trilium-core/src/routes/api/note_map.ts
@@ -2,7 +2,7 @@ import BAttribute from "../../becca/entities/battribute";
import BNote from "../../becca/entities/bnote";
import becca from "../../becca/becca";
import type { BacklinkCountResponse, BacklinksResponse, NoteMapNote } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { findExcerpts, findLlmChatExcerpts, findMindMapExcerpts } from "../../services/backlink_excerpts";
diff --git a/packages/trilium-core/src/routes/api/notes.ts b/packages/trilium-core/src/routes/api/notes.ts
index bd1b3870bb..5c5f33119b 100644
--- a/packages/trilium-core/src/routes/api/notes.ts
+++ b/packages/trilium-core/src/routes/api/notes.ts
@@ -1,5 +1,5 @@
import type { AttributeRow, CreateChildrenResponse, DeleteNotesPreview, MetadataResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import blobService from "../../services/blob";
import eraseService from "../../services/erase.js";
diff --git a/packages/trilium-core/src/routes/api/ocr.ts b/packages/trilium-core/src/routes/api/ocr.ts
index 156f675833..32aba3c283 100644
--- a/packages/trilium-core/src/routes/api/ocr.ts
+++ b/packages/trilium-core/src/routes/api/ocr.ts
@@ -1,5 +1,5 @@
import type { TextRepresentationResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import blobService from "../../services/blob.js";
diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.ts
index 3630bf9841..1abe413866 100644
--- a/packages/trilium-core/src/routes/api/options.ts
+++ b/packages/trilium-core/src/routes/api/options.ts
@@ -1,7 +1,7 @@
import type { OptionNames, UserFont } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import attributeService from "../../services/attributes.js";
import config from "../../services/config.js";
diff --git a/packages/trilium-core/src/routes/api/others.ts b/packages/trilium-core/src/routes/api/others.ts
index bbe6855c40..70cc685070 100644
--- a/packages/trilium-core/src/routes/api/others.ts
+++ b/packages/trilium-core/src/routes/api/others.ts
@@ -1,7 +1,7 @@
import becca from "../../becca/becca";
import { RenderMarkdownResponse, ToMarkdownResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { ValidationError } from "../../errors.js";
import markdown from "../../services/export/markdown.js";
diff --git a/packages/trilium-core/src/routes/api/password.ts b/packages/trilium-core/src/routes/api/password.ts
index 9ce57a66ca..876cddf518 100644
--- a/packages/trilium-core/src/routes/api/password.ts
+++ b/packages/trilium-core/src/routes/api/password.ts
@@ -1,5 +1,5 @@
import type { ChangePasswordResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import passwordService from "../../services/encryption/password.js";
import { ValidationError } from "../../errors.js";
diff --git a/packages/trilium-core/src/routes/api/recent_changes.ts b/packages/trilium-core/src/routes/api/recent_changes.ts
index 46210c437d..2b49e62a96 100644
--- a/packages/trilium-core/src/routes/api/recent_changes.ts
+++ b/packages/trilium-core/src/routes/api/recent_changes.ts
@@ -1,11 +1,11 @@
import protectedSessionService from "../../services/protected_session.js";
import noteService from "../../services/notes.js";
import becca from "../../becca/becca.js";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import type { RecentChangeRow } from "@triliumnext/commons";
import { getSql } from "../../services/sql/index.js";
-function getRecentChanges(req: Request<{ ancestorNoteId: string }, unknown, unknown, { deletedOnly?: string }>) {
+function getRecentChanges(req: Request<{ ancestorNoteId: string }, { deletedOnly?: string }>) {
const { ancestorNoteId } = req.params;
const deletedOnly = req.query.deletedOnly === "true";
diff --git a/packages/trilium-core/src/routes/api/recent_notes.ts b/packages/trilium-core/src/routes/api/recent_notes.ts
index ec568a00bf..81d124e59f 100644
--- a/packages/trilium-core/src/routes/api/recent_notes.ts
+++ b/packages/trilium-core/src/routes/api/recent_notes.ts
@@ -4,7 +4,7 @@ import BRecentNote from "../../becca/entities/brecent_note.js";
import { getSql } from "../../services/sql/index.js";
import dateUtils from "../../services/utils/date.js";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
function addRecentNote(req: Request) {
new BRecentNote({
diff --git a/packages/trilium-core/src/routes/api/relation-map.ts b/packages/trilium-core/src/routes/api/relation-map.ts
index b57750ab27..04f73d0de1 100644
--- a/packages/trilium-core/src/routes/api/relation-map.ts
+++ b/packages/trilium-core/src/routes/api/relation-map.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import { RelationMapPostResponse } from "@triliumnext/commons";
import { getSql } from "../../services/sql/index.js";
diff --git a/packages/trilium-core/src/routes/api/revisions.ts b/packages/trilium-core/src/routes/api/revisions.ts
index 3a77f642f6..4f0cd47e85 100644
--- a/packages/trilium-core/src/routes/api/revisions.ts
+++ b/packages/trilium-core/src/routes/api/revisions.ts
@@ -5,7 +5,7 @@ import {
RevisionItem,
RevisionPojo
} from "@triliumnext/commons";
-import type { Request, Response } from "express";
+import type { Request, Response } from "../../http_interface";
import becca from "../../becca/becca.js";
import type BNote from "../../becca/entities/bnote.js";
diff --git a/packages/trilium-core/src/routes/api/script.ts b/packages/trilium-core/src/routes/api/script.ts
index bc9a182111..dd29b5a159 100644
--- a/packages/trilium-core/src/routes/api/script.ts
+++ b/packages/trilium-core/src/routes/api/script.ts
@@ -1,6 +1,6 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import attributeService from "../../services/attributes.js";
diff --git a/packages/trilium-core/src/routes/api/search.ts b/packages/trilium-core/src/routes/api/search.ts
index 66cbe7f2bb..66270ce7e5 100644
--- a/packages/trilium-core/src/routes/api/search.ts
+++ b/packages/trilium-core/src/routes/api/search.ts
@@ -2,7 +2,7 @@ import {
dayjs, type SearchResultDetails, type SearchResultDetailsRequest,
type SearchResultDetailsResponse, type SearchWithTokensResponse, type TemplatesResponse
} from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import becca_service from "../../becca/becca_service.js";
@@ -139,8 +139,7 @@ function quickSearch(req: Request<{ searchString: string }>) {
}
function search(
- req: Request<{ searchString: string }, unknown, unknown,
- { ancestorNoteId?: string, includeTokens?: string }>
+ req: Request<{ searchString: string }, { ancestorNoteId?: string, includeTokens?: string }>
): string[] | SearchWithTokensResponse {
const { searchString } = req.params;
const { ancestorNoteId, includeTokens } = req.query;
diff --git a/packages/trilium-core/src/routes/api/setup.ts b/packages/trilium-core/src/routes/api/setup.ts
index 434680a069..6207dc0775 100644
--- a/packages/trilium-core/src/routes/api/setup.ts
+++ b/packages/trilium-core/src/routes/api/setup.ts
@@ -14,7 +14,7 @@ import { asSetupTargetScreen, getSetupPlatform, hasExistingData } from "../../se
import { getLog } from "../../services/log.js";
import appInfo from "../../services/app_info.js";
import optionService from "../../services/options.js";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { SetupSyncFromServerResponse } from "@triliumnext/commons";
function getStatus() {
diff --git a/packages/trilium-core/src/routes/api/similar_notes.ts b/packages/trilium-core/src/routes/api/similar_notes.ts
index 62bf3e985a..b0c775ce83 100644
--- a/packages/trilium-core/src/routes/api/similar_notes.ts
+++ b/packages/trilium-core/src/routes/api/similar_notes.ts
@@ -1,5 +1,5 @@
import { SimilarNoteResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import similarity from "../../becca/similarity.js";
diff --git a/packages/trilium-core/src/routes/api/space_usage.ts b/packages/trilium-core/src/routes/api/space_usage.ts
index b40f2bccf7..d271a8b745 100644
--- a/packages/trilium-core/src/routes/api/space_usage.ts
+++ b/packages/trilium-core/src/routes/api/space_usage.ts
@@ -1,5 +1,5 @@
import type { EraseExcessRevisionsOptions } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { getLog } from "../../services/log.js";
import { formatSize } from "../../services/utils/index.js";
diff --git a/packages/trilium-core/src/routes/api/special_notes.ts b/packages/trilium-core/src/routes/api/special_notes.ts
index 26097e5605..44390a48ce 100644
--- a/packages/trilium-core/src/routes/api/special_notes.ts
+++ b/packages/trilium-core/src/routes/api/special_notes.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import * as cls from "../../services/context.js";
diff --git a/packages/trilium-core/src/routes/api/spreadsheet.ts b/packages/trilium-core/src/routes/api/spreadsheet.ts
index 2b000b2510..6422dad826 100644
--- a/packages/trilium-core/src/routes/api/spreadsheet.ts
+++ b/packages/trilium-core/src/routes/api/spreadsheet.ts
@@ -1,5 +1,5 @@
import type { ResolvedImage } from "@triliumnext/commons/src/lib/spreadsheet/render_to_xlsx.js";
-import type { Request, Response } from "express";
+import type { Request, Response } from "../../http_interface";
import becca from "../../becca/becca.js";
import { decodeUtf8, encodeBase64 } from "../../services/utils/binary.js";
diff --git a/packages/trilium-core/src/routes/api/sql.ts b/packages/trilium-core/src/routes/api/sql.ts
index c180f7a216..6a2ced75bf 100644
--- a/packages/trilium-core/src/routes/api/sql.ts
+++ b/packages/trilium-core/src/routes/api/sql.ts
@@ -1,4 +1,4 @@
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import { getSql } from "../../services/sql/index.js";
diff --git a/packages/trilium-core/src/routes/api/stats.ts b/packages/trilium-core/src/routes/api/stats.ts
index 9ad86b49a1..2415cc143b 100644
--- a/packages/trilium-core/src/routes/api/stats.ts
+++ b/packages/trilium-core/src/routes/api/stats.ts
@@ -1,5 +1,5 @@
import becca from "../../becca/becca.js";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { NoteSizeResponse, SubtreeSizeResponse } from "@triliumnext/commons";
import { getSql } from "../../services/sql/index.js";
diff --git a/packages/trilium-core/src/routes/api/sync.ts b/packages/trilium-core/src/routes/api/sync.ts
index caece8876d..bb9f31b166 100644
--- a/packages/trilium-core/src/routes/api/sync.ts
+++ b/packages/trilium-core/src/routes/api/sync.ts
@@ -1,5 +1,5 @@
import { type EntityChange, type EntityChangeRecord, SyncTestResponse } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import { t } from "i18next";
import consistencyChecksService from "../../services/consistency_checks.js";
diff --git a/packages/trilium-core/src/routes/api/tree.ts b/packages/trilium-core/src/routes/api/tree.ts
index 3ef73ce47c..b3fbecc1fe 100644
--- a/packages/trilium-core/src/routes/api/tree.ts
+++ b/packages/trilium-core/src/routes/api/tree.ts
@@ -1,5 +1,5 @@
import type { AttributeRow, BranchRow, NoteRow } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../../http_interface";
import becca from "../../becca/becca.js";
import { NotFoundError } from "../../errors.js";
diff --git a/packages/trilium-core/src/routes/helpers.ts b/packages/trilium-core/src/routes/helpers.ts
index 5f2bfd733c..204faadbed 100644
--- a/packages/trilium-core/src/routes/helpers.ts
+++ b/packages/trilium-core/src/routes/helpers.ts
@@ -1,5 +1,5 @@
import { isSvgMime } from "@triliumnext/commons";
-import { Response } from "express";
+import { Response } from "../http_interface";
import becca from "../becca/becca";
import BNote from "../becca/entities/bnote";
import protected_session from "../services/protected_session";
diff --git a/packages/trilium-core/src/routes/partial_content.spec.ts b/packages/trilium-core/src/routes/partial_content.spec.ts
index decbdc8d41..4c16833469 100644
--- a/packages/trilium-core/src/routes/partial_content.spec.ts
+++ b/packages/trilium-core/src/routes/partial_content.spec.ts
@@ -1,4 +1,4 @@
-import type { Request, Response } from "express";
+import type { Request, Response } from "../http_interface";
import { describe, expect, it } from "vitest";
import { serveContentWithRanges } from "./partial_content.js";
diff --git a/packages/trilium-core/src/routes/partial_content.ts b/packages/trilium-core/src/routes/partial_content.ts
index 366c601e94..43a66ea331 100644
--- a/packages/trilium-core/src/routes/partial_content.ts
+++ b/packages/trilium-core/src/routes/partial_content.ts
@@ -1,4 +1,4 @@
-import type { Request, Response } from "express";
+import type { Request, Response } from "../http_interface";
import { wrapStringOrBuffer } from "../services/utils/binary.js";
import { getContentDisposition } from "../services/utils/index.js";
@@ -40,7 +40,8 @@ export function serveContentWithRanges(req: Request, res: Response, { content, f
res.setHeader("ETag", /^(W\/)?".*"$/.test(etag) ? etag : `"${etag}"`);
}
- const rangeHeader = req.headers?.range;
+ const rawRange = req.headers.range;
+ const rangeHeader = Array.isArray(rawRange) ? rawRange[0] : rawRange;
let range: ByteRange | null;
try {
diff --git a/packages/trilium-core/src/services/backend_script_api.ts b/packages/trilium-core/src/services/backend_script_api.ts
index af5a901b81..3a8f374a76 100644
--- a/packages/trilium-core/src/services/backend_script_api.ts
+++ b/packages/trilium-core/src/services/backend_script_api.ts
@@ -1,6 +1,6 @@
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 "express";
+import type { Request, Response } from "../http_interface";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity";
import Becca from "../becca/becca-interface";
import * as htmlParser from "node-html-parser";
@@ -117,14 +117,15 @@ export interface Api {
// `@triliumnext/commons`, where they're required) because this interface types
// *every* backend script, and they're only populated for custom request handlers.
/**
- * Express request object. 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.
+ * 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.
*/
req?: Request;
/**
- * Express response object — write the HTTP response here. Only present in custom
- * request handlers; `undefined` otherwise.
+ * 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;
/**
diff --git a/packages/trilium-core/src/services/backend_script_api_interface.ts b/packages/trilium-core/src/services/backend_script_api_interface.ts
index 2966beb92e..42aea57796 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 "express";
+import type { Request, Response } from "../http_interface";
import type BNote from "../becca/entities/bnote.js";
import AbstractBeccaEntity from "../becca/entities/abstract_becca_entity.js";
diff --git a/packages/trilium-core/src/services/backup.ts b/packages/trilium-core/src/services/backup.ts
index b64179b59c..c5c890e607 100644
--- a/packages/trilium-core/src/services/backup.ts
+++ b/packages/trilium-core/src/services/backup.ts
@@ -5,7 +5,7 @@ import type {
SetupBackupSettings,
SetupExistingBackup
} from "@triliumnext/commons";
-import type { Response } from "express";
+import type { Response } from "../http_interface";
import { getContext } from "./context.js";
import dateUtils from "./utils/date.js";
diff --git a/packages/trilium-core/src/services/export/opml.spec.ts b/packages/trilium-core/src/services/export/opml.spec.ts
index e9c57e7fdb..1f9b0aad86 100644
--- a/packages/trilium-core/src/services/export/opml.spec.ts
+++ b/packages/trilium-core/src/services/export/opml.spec.ts
@@ -1,4 +1,4 @@
-import type { Response } from "express";
+import type { Response } from "../../http_interface";
import { beforeAll, describe, expect, it } from "vitest";
import becca from "../../becca/becca.js";
diff --git a/packages/trilium-core/src/services/export/opml.ts b/packages/trilium-core/src/services/export/opml.ts
index fbe0951606..14b495bf53 100644
--- a/packages/trilium-core/src/services/export/opml.ts
+++ b/packages/trilium-core/src/services/export/opml.ts
@@ -1,4 +1,4 @@
-import type { Response } from "express";
+import type { Response } from "../../http_interface";
import becca from "../../becca/becca.js";
import type BBranch from "../../becca/entities/bbranch.js";
diff --git a/packages/trilium-core/src/services/export/single.spec.ts b/packages/trilium-core/src/services/export/single.spec.ts
index e635686129..0c3f0ae829 100644
--- a/packages/trilium-core/src/services/export/single.spec.ts
+++ b/packages/trilium-core/src/services/export/single.spec.ts
@@ -1,4 +1,4 @@
-import type { Response } from "express";
+import type { Response } from "../../http_interface";
import { describe, expect, it, vi } from "vitest";
import becca from "../../becca/becca.js";
diff --git a/packages/trilium-core/src/services/export/single.ts b/packages/trilium-core/src/services/export/single.ts
index ba4209ed04..9804007a08 100644
--- a/packages/trilium-core/src/services/export/single.ts
+++ b/packages/trilium-core/src/services/export/single.ts
@@ -1,6 +1,6 @@
-import type { Response } from "express";
+import type { Response } from "../../http_interface";
import html from "html";
import mimeTypes from "mime-types";
diff --git a/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts b/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts
index 810f76cb18..da02f779e7 100644
--- a/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts
+++ b/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts
@@ -1,5 +1,5 @@
import type { EntityChange, EntityChangeRecord, EntityRow, NoteRow } from "@triliumnext/commons";
-import type { Request } from "express";
+import type { Request } from "../http_interface";
import { beforeAll, describe, expect, it } from "vitest";
import becca from "../becca/becca.js";