Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Convert routes/api/tree

Elian Doran6e042c20e9d7
diff --git a/src/routes/api/tree.js b/src/routes/api/tree.ts
similarity index 75%
rename from src/routes/api/tree.js
rename to src/routes/api/tree.ts
index c8188068b9..0dcb42e678 100644
--- a/src/routes/api/tree.js
+++ b/src/routes/api/tree.ts
@@ -1,16 +1,18 @@
"use strict";
-const becca = require('../../becca/becca');
-const log = require('../../services/log');
-const NotFoundError = require('../../errors/not_found_error');
-
-function getNotesAndBranchesAndAttributes(noteIds) {
- noteIds = new Set(noteIds);
- const collectedNoteIds = new Set();
- const collectedAttributeIds = new Set();
- const collectedBranchIds = new Set();
-
- function collectEntityIds(note) {
+import becca = require('../../becca/becca');
+import log = require('../../services/log');
+import NotFoundError = require('../../errors/not_found_error');
+import { Request } from 'express';
+import BNote = require('../../becca/entities/bnote');
+
+function getNotesAndBranchesAndAttributes(_noteIds: string[] | Set<string>) {
+ const noteIds = new Set(_noteIds);
+ const collectedNoteIds = new Set<string>();
+ const collectedAttributeIds = new Set<string>();
+ const collectedBranchIds = new Set<string>();
+
+ function collectEntityIds(note?: BNote) {
if (!note || collectedNoteIds.has(note.noteId)) {
return;
}
@@ -18,15 +20,18 @@ function getNotesAndBranchesAndAttributes(noteIds) {
collectedNoteIds.add(note.noteId);
for (const branch of note.getParentBranches()) {
- collectedBranchIds.add(branch.branchId);
+ if (branch.branchId) {
+ collectedBranchIds.add(branch.branchId);
+ }
collectEntityIds(branch.parentNote);
}
for (const childNote of note.children) {
const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, note.noteId);
-
- collectedBranchIds.add(childBranch.branchId);
+ if (childBranch && childBranch.branchId) {
+ collectedBranchIds.add(childBranch.branchId);
+ }
}
for (const attr of note.ownedAttributes) {
@@ -122,11 +127,11 @@ function getNotesAndBranchesAndAttributes(noteIds) {
};
}
-function getTree(req) {
- const subTreeNoteId = req.query.subTreeNoteId || 'root';
- const collectedNoteIds = new Set([subTreeNoteId]);
+function getTree(req: Request) {
+ const subTreeNoteId = req.query.subTreeNoteId === "string" ? req.query.subTreeNoteId : "" || 'root';
+ const collectedNoteIds = new Set<string>([subTreeNoteId]);
- function collect(parentNote) {
+ function collect(parentNote: BNote) {
if (!parentNote) {
console.trace(parentNote);
}
@@ -136,7 +141,7 @@ function getTree(req) {
const childBranch = becca.getBranchFromChildAndParent(childNote.noteId, parentNote.noteId);
- if (childBranch.isExpanded) {
+ if (childBranch?.isExpanded) {
collect(childBranch.childNote);
}
}
@@ -151,11 +156,11 @@ function getTree(req) {
return getNotesAndBranchesAndAttributes(collectedNoteIds);
}
-function load(req) {
+function load(req: Request) {
return getNotesAndBranchesAndAttributes(req.body.noteIds);
}
-module.exports = {
+export = {
getTree,
load
};
diff --git a/src/routes/routes.js b/src/routes/routes.js
index 47bd7fe1a4..2e53e45463 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -22,7 +22,7 @@ const loginRoute = require('./login.js');
const indexRoute = require('./index.js');
// API routes
-const treeApiRoute = require('./api/tree.js');
+const treeApiRoute = require('./api/tree');
const notesApiRoute = require('./api/notes');
const branchesApiRoute = require('./api/branches');
const attachmentsApiRoute = require('./api/attachments');