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
server-ts: Convert routes/custom
Elian Doran4940207b1583
diff --git a/src/app.js b/src/app.jsindex 01cc83760c..ba4d2da87c 100644--- a/src/app.js+++ b/src/app.js@@ -39,7 +39,7 @@ app.use(favicon(`${__dirname}/../images/app-icons/win/icon.ico`));require('./routes/assets').register(app);require('./routes/routes.js').register(app);-require('./routes/custom.js').register(app);+require('./routes/custom').register(app);require('./routes/error_handlers.js').register(app);// triggers sync timerdiff --git a/src/routes/custom.js b/src/routes/custom.tssimilarity index 72%rename from src/routes/custom.jsrename to src/routes/custom.tsindex 2e32e5446f..1bdb94ac02 100644--- a/src/routes/custom.js+++ b/src/routes/custom.ts@@ -1,21 +1,22 @@-const log = require('../services/log');-const fileService = require('./api/files');-const scriptService = require('../services/script');-const cls = require('../services/cls');-const sql = require('../services/sql');-const becca = require('../becca/becca');--function handleRequest(req, res) {+import log = require('../services/log');+import fileService = require('./api/files');+import scriptService = require('../services/script');+import cls = require('../services/cls');+import sql = require('../services/sql');+import becca = require('../becca/becca');+import { Request, Response, Router } from 'express';++function handleRequest(req: Request, res: Response) {// express puts content after first slash into 0 index elementconst path = req.params.path + req.params[0];- const attributeIds = sql.getColumn("SELECT attributeId FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name IN ('customRequestHandler', 'customResourceProvider')");+ const attributeIds = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 0 AND type = 'label' AND name IN ('customRequestHandler', 'customResourceProvider')");const attrs = attributeIds.map(attrId => becca.getAttribute(attrId));for (const attr of attrs) {- if (!attr.value.trim()) {+ if (!attr?.value.trim()) {continue;}@@ -25,7 +26,7 @@ function handleRequest(req, res) {try {match = path.match(regex);}- catch (e) {+ catch (e: any) {log.error(`Testing path for label '${attr.attributeId}', regex '${attr.value}' failed with error: ${e.message}, stack: ${e.stack}`);continue;}@@ -46,7 +47,7 @@ function handleRequest(req, res) {res});}- catch (e) {+ catch (e: any) {log.error(`Custom handler '${note.noteId}' failed with: ${e.message}, ${e.stack}`);res.setHeader("Content-Type", "text/plain")@@ -72,10 +73,10 @@ function handleRequest(req, res) {.send(message);}-function register(router) {+function register(router: Router) {// explicitly no CSRF middleware since it's meant to allow integration from external services- router.all('/custom/:path*', (req, res, next) => {+ router.all('/custom/:path*', (req: Request, res: Response, next) => {cls.namespace.bindEmitter(req);cls.namespace.bindEmitter(res);@@ -83,6 +84,6 @@ function register(router) {});}-module.exports = {+export = {register};diff --git a/src/services/backend_script_api_interface.ts b/src/services/backend_script_api_interface.tsindex 7031d1bc3f..f74b65ad7f 100644--- a/src/services/backend_script_api_interface.ts+++ b/src/services/backend_script_api_interface.ts@@ -1,7 +1,11 @@+import { Request, Response } from "express";import AbstractBeccaEntity = require("../becca/entities/abstract_becca_entity");import BNote = require("../becca/entities/bnote");export interface ApiParams {startNote?: BNote;originEntity?: AbstractBeccaEntity<any>;+ pathParams?: string[],+ req?: Request,+ res?: Response}\ No newline at end of file