mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
server-ts: Address review
This commit is contained in:
parent
f857b8a9bb
commit
5d452a1525
@ -11,7 +11,7 @@ import protectedSessionService = require('../../services/protected_session');
|
||||
import blobService = require('../../services/blob');
|
||||
import Becca, { ConstructorData } from '../becca-interface';
|
||||
|
||||
let becca: Becca | null = null;
|
||||
let becca: Becca;
|
||||
|
||||
interface ContentOpts {
|
||||
forceSave?: boolean;
|
||||
|
@ -153,7 +153,7 @@ function buildRewardMap(note: BNote) {
|
||||
|
||||
const mimeCache: Record<string, string> = {};
|
||||
|
||||
function trimMime(mime?: string) {
|
||||
function trimMime(mime: string) {
|
||||
if (!mime || mime === 'text/html') {
|
||||
return;
|
||||
}
|
||||
|
@ -7,13 +7,11 @@ import utils = require('./utils');
|
||||
import passwordEncryptionService = require('./encryption/password_encryption');
|
||||
import config = require('./config');
|
||||
import passwordService = require('./encryption/password');
|
||||
import type { Request } from 'express';
|
||||
|
||||
const noAuthentication = config.General && config.General.noAuthentication === true;
|
||||
|
||||
// TODO: We are using custom types for request & response because couldn't extract those pesky express types.
|
||||
interface Request {
|
||||
method: string;
|
||||
path: string;
|
||||
interface AppRequest extends Request {
|
||||
headers: {
|
||||
authorization?: string;
|
||||
"trilium-cred"?: string;
|
||||
@ -30,7 +28,7 @@ interface Response {
|
||||
|
||||
type Callback = () => void;
|
||||
|
||||
function checkAuth(req: Request, res: Response, next: Callback) {
|
||||
function checkAuth(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!sqlInit.isDbInitialized()) {
|
||||
res.redirect("setup");
|
||||
}
|
||||
@ -44,7 +42,7 @@ function checkAuth(req: Request, res: Response, next: Callback) {
|
||||
|
||||
// for electron things which need network stuff
|
||||
// currently, we're doing that for file upload because handling form data seems to be difficult
|
||||
function checkApiAuthOrElectron(req: Request, res: Response, next: Callback) {
|
||||
function checkApiAuthOrElectron(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!req.session.loggedIn && !utils.isElectron() && !noAuthentication) {
|
||||
reject(req, res, "Logged in session not found");
|
||||
}
|
||||
@ -53,7 +51,7 @@ function checkApiAuthOrElectron(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkApiAuth(req: Request, res: Response, next: Callback) {
|
||||
function checkApiAuth(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!req.session.loggedIn && !noAuthentication) {
|
||||
reject(req, res, "Logged in session not found");
|
||||
}
|
||||
@ -62,7 +60,7 @@ function checkApiAuth(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkAppInitialized(req: Request, res: Response, next: Callback) {
|
||||
function checkAppInitialized(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!sqlInit.isDbInitialized()) {
|
||||
res.redirect("setup");
|
||||
}
|
||||
@ -71,7 +69,7 @@ function checkAppInitialized(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkPasswordSet(req: Request, res: Response, next: Callback) {
|
||||
function checkPasswordSet(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!utils.isElectron() && !passwordService.isPasswordSet()) {
|
||||
res.redirect("set-password");
|
||||
} else {
|
||||
@ -79,7 +77,7 @@ function checkPasswordSet(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkPasswordNotSet(req: Request, res: Response, next: Callback) {
|
||||
function checkPasswordNotSet(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!utils.isElectron() && passwordService.isPasswordSet()) {
|
||||
res.redirect("login");
|
||||
} else {
|
||||
@ -87,7 +85,7 @@ function checkPasswordNotSet(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkAppNotInitialized(req: Request, res: Response, next: Callback) {
|
||||
function checkAppNotInitialized(req: AppRequest, res: Response, next: Callback) {
|
||||
if (sqlInit.isDbInitialized()) {
|
||||
reject(req, res, "App already initialized.");
|
||||
}
|
||||
@ -96,7 +94,7 @@ function checkAppNotInitialized(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function checkEtapiToken(req: Request, res: Response, next: Callback) {
|
||||
function checkEtapiToken(req: AppRequest, res: Response, next: Callback) {
|
||||
if (etapiTokenService.isValidAuthHeader(req.headers.authorization)) {
|
||||
next();
|
||||
}
|
||||
@ -105,7 +103,7 @@ function checkEtapiToken(req: Request, res: Response, next: Callback) {
|
||||
}
|
||||
}
|
||||
|
||||
function reject(req: Request, res: Response, message: string) {
|
||||
function reject(req: AppRequest, res: Response, message: string) {
|
||||
log.info(`${req.method} ${req.path} rejected with 401 ${message}`);
|
||||
|
||||
res.setHeader("Content-Type", "text/plain")
|
||||
@ -113,7 +111,7 @@ function reject(req: Request, res: Response, message: string) {
|
||||
.send(message);
|
||||
}
|
||||
|
||||
function checkCredentials(req: Request, res: Response, next: Callback) {
|
||||
function checkCredentials(req: AppRequest, res: Response, next: Callback) {
|
||||
if (!sqlInit.isDbInitialized()) {
|
||||
res.setHeader("Content-Type", "text/plain")
|
||||
.status(400)
|
||||
|
@ -80,7 +80,7 @@ function validateLocalDateTime(str: string | null | undefined) {
|
||||
}
|
||||
}
|
||||
|
||||
function validateUtcDateTime(str?: string) {
|
||||
function validateUtcDateTime(str: string | undefined) {
|
||||
if (!str) {
|
||||
return;
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ function createToken(tokenName: string) {
|
||||
};
|
||||
}
|
||||
|
||||
function parseAuthToken(auth?: string | null) {
|
||||
function parseAuthToken(auth: string | undefined) {
|
||||
if (!auth) {
|
||||
return null;
|
||||
}
|
||||
@ -64,7 +64,7 @@ function parseAuthToken(auth?: string | null) {
|
||||
}
|
||||
}
|
||||
|
||||
function isValidAuthHeader(auth?: string | null) {
|
||||
function isValidAuthHeader(auth: string | undefined) {
|
||||
const parsed = parseAuthToken(auth);
|
||||
|
||||
if (!parsed) {
|
||||
|
@ -623,7 +623,7 @@ function getKeyboardActions() {
|
||||
|
||||
for (const option of optionService.getOptions()) {
|
||||
if (option.name.startsWith('keyboardShortcuts')) {
|
||||
let actionName = option.name.substr(17);
|
||||
let actionName = option.name.substring(17);
|
||||
actionName = actionName.charAt(0).toLowerCase() + actionName.slice(1);
|
||||
|
||||
const action = actions.find(ea => ea.actionName === actionName);
|
||||
|
@ -873,7 +873,7 @@ function getUndeletedParentBranchIds(noteId: string, deleteId: string) {
|
||||
AND parentNote.isDeleted = 0`, [noteId, deleteId]);
|
||||
}
|
||||
|
||||
function scanForLinks(note: BNote | null, content: string) {
|
||||
function scanForLinks(note: BNote, content: string) {
|
||||
if (!note || !['text', 'relationMap'].includes(note.type)) {
|
||||
return;
|
||||
}
|
||||
|
@ -144,7 +144,7 @@ function getRows<T>(query: string, params: Params = []): T[] {
|
||||
}
|
||||
|
||||
function getRawRows<T extends {} | unknown[]>(query: string, params: Params = []): T[] {
|
||||
return (wrap(query, s => s.raw().all(params)) as T[] | null) || [];
|
||||
return (wrap(query, s => s.raw().all(params)) as T[]) || [];
|
||||
}
|
||||
|
||||
function iterateRows(query: string, params: Params = []) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user