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(etapi): some endpoints failing due to async (closes #2105)
Elian Dorancecbe5862c39
diff --git a/apps/server/src/etapi/backup.ts b/apps/server/src/etapi/backup.tsindex d73bf1a336..9c986f45a4 100644--- a/apps/server/src/etapi/backup.ts+++ b/apps/server/src/etapi/backup.ts@@ -4,10 +4,10 @@ import eu from "./etapi_utils.js";import backupService from "../services/backup.js";function register(router: Router) {- eu.route(router, "put", "/etapi/backup/:backupName", async (req, res, next) => {- await backupService.backupNow(req.params.backupName);-- res.sendStatus(204);+ eu.route(router, "put", "/etapi/backup/:backupName", (req, res, next) => {+ backupService.backupNow(req.params.backupName)+ .then(() => res.sendStatus(204))+ .catch(() => res.sendStatus(500));});}diff --git a/apps/server/src/etapi/etapi_utils.ts b/apps/server/src/etapi/etapi_utils.tsindex e4f40c927c..a50434f708 100644--- a/apps/server/src/etapi/etapi_utils.ts+++ b/apps/server/src/etapi/etapi_utils.ts@@ -6,7 +6,7 @@ import etapiTokenService from "../services/etapi_tokens.js";import config from "../services/config.js";import type { NextFunction, Request, RequestHandler, Response, Router } from "express";import type { ValidatorMap } from "./etapi-interface.js";-import type { ApiRequestHandler } from "../routes/route_api.js";+import type { ApiRequestHandler, SyncRouteRequestHandler } from "../routes/route_api.js";const GENERIC_CODE = "GENERIC";type HttpMethod = "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head";@@ -73,11 +73,11 @@ function processRequest(req: Request, res: Response, routeHandler: ApiRequestHan}}-function route(router: Router, method: HttpMethod, path: string, routeHandler: ApiRequestHandler) {+function route(router: Router, method: HttpMethod, path: string, routeHandler: SyncRouteRequestHandler) {router[method](path, checkEtapiAuth, (req: Request, res: Response, next: NextFunction) => processRequest(req, res, routeHandler, next, method, path));}-function NOT_AUTHENTICATED_ROUTE(router: Router, method: HttpMethod, path: string, middleware: RequestHandler[], routeHandler: RequestHandler) {+function NOT_AUTHENTICATED_ROUTE(router: Router, method: HttpMethod, path: string, middleware: RequestHandler[], routeHandler: SyncRouteRequestHandler) {router[method](path, ...middleware, (req: Request, res: Response, next: NextFunction) => processRequest(req, res, routeHandler, next, method, path));}diff --git a/apps/server/src/etapi/special_notes.ts b/apps/server/src/etapi/special_notes.tsindex df2f75f9ac..043ce1d3a7 100644--- a/apps/server/src/etapi/special_notes.ts+++ b/apps/server/src/etapi/special_notes.ts@@ -15,46 +15,46 @@ function isValidDate(date: string) {}function register(router: Router) {- eu.route(router, "get", "/etapi/inbox/:date", async (req, res, next) => {+ eu.route(router, "get", "/etapi/inbox/:date", (req, res, next) => {const { date } = req.params;if (!isValidDate(date)) {throw getDateInvalidError(date);}- const note = await specialNotesService.getInboxNote(date);+ const note = specialNotesService.getInboxNote(date);res.json(mappers.mapNoteToPojo(note));});- eu.route(router, "get", "/etapi/calendar/days/:date", async (req, res, next) => {+ eu.route(router, "get", "/etapi/calendar/days/:date", (req, res, next) => {const { date } = req.params;if (!isValidDate(date)) {throw getDateInvalidError(date);}- const note = await dateNotesService.getDayNote(date);+ const note = dateNotesService.getDayNote(date);res.json(mappers.mapNoteToPojo(note));});- eu.route(router, "get", "/etapi/calendar/week-first-day/:date", async (req, res, next) => {+ eu.route(router, "get", "/etapi/calendar/week-first-day/:date", (req, res, next) => {const { date } = req.params;if (!isValidDate(date)) {throw getDateInvalidError(date);}- const note = await dateNotesService.getWeekFirstDayNote(date);+ const note = dateNotesService.getWeekFirstDayNote(date);res.json(mappers.mapNoteToPojo(note));});- eu.route(router, "get", "/etapi/calendar/weeks/:week", async (req, res, next) => {+ eu.route(router, "get", "/etapi/calendar/weeks/:week", (req, res, next) => {const { week } = req.params;if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {throw getWeekInvalidError(week);}- const note = await dateNotesService.getWeekNote(week);+ const note = dateNotesService.getWeekNote(week);if (!note) {throw getWeekNotFoundError(week);@@ -63,14 +63,14 @@ function register(router: Router) {res.json(mappers.mapNoteToPojo(note));});- eu.route(router, "get", "/etapi/calendar/months/:month", async (req, res, next) => {+ eu.route(router, "get", "/etapi/calendar/months/:month", (req, res, next) => {const { month } = req.params;if (!/[0-9]{4}-[0-9]{2}/.test(month)) {throw getMonthInvalidError(month);}- const note = await dateNotesService.getMonthNote(month);+ const note = dateNotesService.getMonthNote(month);res.json(mappers.mapNoteToPojo(note));});diff --git a/apps/server/src/services/special_notes.ts b/apps/server/src/services/special_notes.tsindex df8b87cdc4..4083b8fff1 100644--- a/apps/server/src/services/special_notes.ts+++ b/apps/server/src/services/special_notes.ts@@ -9,6 +9,7 @@ import searchService from "./search/services/search.js";import SearchContext from "./search/search_context.js";import hiddenSubtree from "./hidden_subtree.js";import { t } from "i18next";+import { BNote } from "./backend_script_entrypoint.js";const { LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT } = hiddenSubtree;function getInboxNote(date: string) {@@ -17,7 +18,7 @@ function getInboxNote(date: string) {throw new Error("Unable to find workspace note");}- let inbox;+ let inbox: BNote;if (!workspaceNote.isRoot()) {inbox = workspaceNote.searchNoteInSubtree("#workspaceInbox");