mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-28 10:32:27 +08:00
fix(etapi): some endpoints failing due to async (closes #2105)
This commit is contained in:
parent
ff1a8d2280
commit
cecbe5862c
@ -4,10 +4,10 @@ import eu from "./etapi_utils.js";
|
|||||||
import backupService from "../services/backup.js";
|
import backupService from "../services/backup.js";
|
||||||
|
|
||||||
function register(router: Router) {
|
function register(router: Router) {
|
||||||
eu.route(router, "put", "/etapi/backup/:backupName", async (req, res, next) => {
|
eu.route(router, "put", "/etapi/backup/:backupName", (req, res, next) => {
|
||||||
await backupService.backupNow(req.params.backupName);
|
backupService.backupNow(req.params.backupName)
|
||||||
|
.then(() => res.sendStatus(204))
|
||||||
res.sendStatus(204);
|
.catch(() => res.sendStatus(500));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import etapiTokenService from "../services/etapi_tokens.js";
|
|||||||
import config from "../services/config.js";
|
import config from "../services/config.js";
|
||||||
import type { NextFunction, Request, RequestHandler, Response, Router } from "express";
|
import type { NextFunction, Request, RequestHandler, Response, Router } from "express";
|
||||||
import type { ValidatorMap } from "./etapi-interface.js";
|
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";
|
const GENERIC_CODE = "GENERIC";
|
||||||
|
|
||||||
type HttpMethod = "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head";
|
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));
|
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));
|
router[method](path, ...middleware, (req: Request, res: Response, next: NextFunction) => processRequest(req, res, routeHandler, next, method, path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,46 +15,46 @@ function isValidDate(date: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function register(router: Router) {
|
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;
|
const { date } = req.params;
|
||||||
|
|
||||||
if (!isValidDate(date)) {
|
if (!isValidDate(date)) {
|
||||||
throw getDateInvalidError(date);
|
throw getDateInvalidError(date);
|
||||||
}
|
}
|
||||||
const note = await specialNotesService.getInboxNote(date);
|
const note = specialNotesService.getInboxNote(date);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
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;
|
const { date } = req.params;
|
||||||
|
|
||||||
if (!isValidDate(date)) {
|
if (!isValidDate(date)) {
|
||||||
throw getDateInvalidError(date);
|
throw getDateInvalidError(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
const note = await dateNotesService.getDayNote(date);
|
const note = dateNotesService.getDayNote(date);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
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;
|
const { date } = req.params;
|
||||||
|
|
||||||
if (!isValidDate(date)) {
|
if (!isValidDate(date)) {
|
||||||
throw getDateInvalidError(date);
|
throw getDateInvalidError(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
const note = await dateNotesService.getWeekFirstDayNote(date);
|
const note = dateNotesService.getWeekFirstDayNote(date);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
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;
|
const { week } = req.params;
|
||||||
|
|
||||||
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
|
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
|
||||||
throw getWeekInvalidError(week);
|
throw getWeekInvalidError(week);
|
||||||
}
|
}
|
||||||
|
|
||||||
const note = await dateNotesService.getWeekNote(week);
|
const note = dateNotesService.getWeekNote(week);
|
||||||
|
|
||||||
if (!note) {
|
if (!note) {
|
||||||
throw getWeekNotFoundError(week);
|
throw getWeekNotFoundError(week);
|
||||||
@ -63,14 +63,14 @@ function register(router: Router) {
|
|||||||
res.json(mappers.mapNoteToPojo(note));
|
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;
|
const { month } = req.params;
|
||||||
|
|
||||||
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
|
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
|
||||||
throw getMonthInvalidError(month);
|
throw getMonthInvalidError(month);
|
||||||
}
|
}
|
||||||
|
|
||||||
const note = await dateNotesService.getMonthNote(month);
|
const note = dateNotesService.getMonthNote(month);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
res.json(mappers.mapNoteToPojo(note));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ import searchService from "./search/services/search.js";
|
|||||||
import SearchContext from "./search/search_context.js";
|
import SearchContext from "./search/search_context.js";
|
||||||
import hiddenSubtree from "./hidden_subtree.js";
|
import hiddenSubtree from "./hidden_subtree.js";
|
||||||
import { t } from "i18next";
|
import { t } from "i18next";
|
||||||
|
import { BNote } from "./backend_script_entrypoint.js";
|
||||||
const { LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT } = hiddenSubtree;
|
const { LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT } = hiddenSubtree;
|
||||||
|
|
||||||
function getInboxNote(date: string) {
|
function getInboxNote(date: string) {
|
||||||
@ -17,7 +18,7 @@ function getInboxNote(date: string) {
|
|||||||
throw new Error("Unable to find workspace note");
|
throw new Error("Unable to find workspace note");
|
||||||
}
|
}
|
||||||
|
|
||||||
let inbox;
|
let inbox: BNote;
|
||||||
|
|
||||||
if (!workspaceNote.isRoot()) {
|
if (!workspaceNote.isRoot()) {
|
||||||
inbox = workspaceNote.searchNoteInSubtree("#workspaceInbox");
|
inbox = workspaceNote.searchNoteInSubtree("#workspaceInbox");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user