Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 26d5462

Browse files
committed
refactor(routes/custom): update path "/custom/*path" for express v5
1 parent 8d09ff4 commit 26d5462

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/routes/custom.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,26 @@ import type { Request, Response, Router } from "express";
88
import { safeExtractMessageAndStackFromError } from "../services/utils.js";
99

1010
function handleRequest(req: Request, res: Response) {
11-
// express puts content after first slash into 0 index element
1211

13-
const path = req.params.path + req.params[0];
12+
// handle path from "*path" route wildcard
13+
// in express v4, you could just add
14+
// req.params.path + req.params[0], but with v5
15+
// we get a split array that we have to join ourselves again
16+
17+
// @TriliumNextTODO: remove typecasting once express types are fixed
18+
// they currently only treat req.params as string, while in reality
19+
// it can also be a string[], when using wildcards
20+
const splitPath = req.params.path as unknown as string[];
21+
22+
//const path = splitPath.map(segment => encodeURIComponent(segment)).join("/")
23+
// naively join the "decoded" paths using a slash
24+
// this is to mimick handleRequest behaviour
25+
// as with the previous express v4.
26+
// @TriliumNextTODO: using something like =>
27+
// splitPath.map(segment => encodeURIComponent(segment)).join("/")
28+
// might be safer
29+
30+
const path = splitPath.join("/")
1431

1532
const attributeIds = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name IN ('customRequestHandler', 'customResourceProvider')");
1633

@@ -70,7 +87,7 @@ function handleRequest(req: Request, res: Response) {
7087
function register(router: Router) {
7188
// explicitly no CSRF middleware since it's meant to allow integration from external services
7289

73-
router.all("/custom/:path*", (req: Request, res: Response, _next) => {
90+
router.all("/custom/*path", (req: Request, res: Response, _next) => {
7491
cls.namespace.bindEmitter(req);
7592
cls.namespace.bindEmitter(res);
7693

0 commit comments

Comments
 (0)