mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-18 00:02:28 +08:00
chore: 🤖 improve code
This commit is contained in:
parent
79a7513325
commit
eb514ad4e3
@ -11,62 +11,50 @@ const getMonthInvalidError = (month: string) => new eu.EtapiError(400, "MONTH_IN
|
|||||||
const getYearInvalidError = (year: string) => new eu.EtapiError(400, "YEAR_INVALID", `Year "${year}" is not valid.`);
|
const getYearInvalidError = (year: string) => new eu.EtapiError(400, "YEAR_INVALID", `Year "${year}" is not valid.`);
|
||||||
|
|
||||||
function isValidDate(date: string) {
|
function isValidDate(date: string) {
|
||||||
if (!/[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date)) {
|
return /[0-9]{4}-[0-9]{2}-[0-9]{2}/.test(date) && !!Date.parse(date);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !!Date.parse(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidWeek(week: string) {
|
|
||||||
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function register(router: Router) {
|
function register(router: Router) {
|
||||||
eu.route(router, "get", "/etapi/inbox/:date", (req, res, next) => {
|
eu.route(router, "get", "/etapi/inbox/:date", async (req, res, next) => {
|
||||||
|
const { date } = req.params;
|
||||||
|
|
||||||
|
if (!isValidDate(date)) {
|
||||||
|
throw getDateInvalidError(date);
|
||||||
|
}
|
||||||
|
const note = await specialNotesService.getInboxNote(date);
|
||||||
|
res.json(mappers.mapNoteToPojo(note));
|
||||||
|
});
|
||||||
|
|
||||||
|
eu.route(router, "get", "/etapi/calendar/days/:date", async (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 = specialNotesService.getInboxNote(date);
|
const note = await dateNotesService.getDayNote(date);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
res.json(mappers.mapNoteToPojo(note));
|
||||||
});
|
});
|
||||||
|
|
||||||
eu.route(router, "get", "/etapi/calendar/days/:date", (req, res, next) => {
|
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", async (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 = dateNotesService.getDayNote(date);
|
const note = await dateNotesService.getWeekFirstDayNote(date);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
res.json(mappers.mapNoteToPojo(note));
|
||||||
});
|
});
|
||||||
|
|
||||||
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", (req, res, next) => {
|
eu.route(router, "get", "/etapi/calendar/weeks/:week", async (req, res, next) => {
|
||||||
const { date } = req.params;
|
|
||||||
|
|
||||||
if (!isValidDate(date)) {
|
|
||||||
throw getDateInvalidError(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
const note = dateNotesService.getWeekFirstDayNote(date);
|
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
|
||||||
});
|
|
||||||
|
|
||||||
eu.route(router, "get", "/etapi/calendar/weeks/:week", (req, res, next) => {
|
|
||||||
const { week } = req.params;
|
const { week } = req.params;
|
||||||
|
|
||||||
if (!isValidWeek(week)) {
|
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
|
||||||
throw getWeekInvalidError(week);
|
throw getWeekInvalidError(week);
|
||||||
}
|
}
|
||||||
|
|
||||||
const note = dateNotesService.getWeekNote(week);
|
const note = await dateNotesService.getWeekNote(week);
|
||||||
|
|
||||||
if (!note) {
|
if (!note) {
|
||||||
throw getWeekNotFoundError(week);
|
throw getWeekNotFoundError(week);
|
||||||
@ -75,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", (req, res, next) => {
|
eu.route(router, "get", "/etapi/calendar/months/:month", async (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 = dateNotesService.getMonthNote(month);
|
const note = await dateNotesService.getMonthNote(month);
|
||||||
res.json(mappers.mapNoteToPojo(note));
|
res.json(mappers.mapNoteToPojo(note));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user