More types for history, config/interfaces and Request.flash

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2020-05-24 17:07:13 +02:00
parent fa301ab450
commit 1f517bfb99
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
3 changed files with 14 additions and 12 deletions

View file

@ -36,7 +36,7 @@ export interface Config {
forbiddenNoteIDs: string[]; forbiddenNoteIDs: string[];
defaultPermission: string; defaultPermission: string;
dbURL: string; dbURL: string;
db: any; db;
sslKeyPath: string; sslKeyPath: string;
sslCertPath: string; sslCertPath: string;
sslCAPath: string[]; sslCAPath: string[];
@ -153,5 +153,6 @@ export interface Config {
linkifyHeaderStyle: string; linkifyHeaderStyle: string;
// TODO: Remove escape hatch for dynamically added properties // TODO: Remove escape hatch for dynamically added properties
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[propName: string]: any; [propName: string]: any;
} }

View file

@ -6,6 +6,7 @@ import LZString from 'lz-string'
import { logger } from './logger' import { logger } from './logger'
import { Note, User } from './models' import { Note, User } from './models'
import { errors } from './errors' import { errors } from './errors'
import { LogEntry } from 'winston'
// public // public
@ -34,7 +35,7 @@ function parseHistoryArrayToMap (historyArray: HistoryObject[]): Map<string, His
return historyMap return historyMap
} }
function getHistory (userId, callback: (err: any, history: any) => void): void { function getHistory (userId, callback: (err: unknown, history: Map<string, HistoryObject> | null) => void): void {
User.findOne({ User.findOne({
where: { where: {
id: userId id: userId
@ -44,7 +45,7 @@ function getHistory (userId, callback: (err: any, history: any) => void): void {
return callback(null, null) return callback(null, null)
} }
if (user.history) { if (user.history) {
const history = JSON.parse(user.history) const history: HistoryObject[] = JSON.parse(user.history)
// migrate LZString encoded note id to base64url encoded note id // migrate LZString encoded note id to base64url encoded note id
for (let i = 0, l = history.length; i < l; i++) { for (let i = 0, l = history.length; i < l; i++) {
// Calculate minimal string length for an UUID that is encoded // Calculate minimal string length for an UUID that is encoded
@ -74,14 +75,14 @@ function getHistory (userId, callback: (err: any, history: any) => void): void {
return callback(null, parseHistoryArrayToMap(history)) return callback(null, parseHistoryArrayToMap(history))
} }
logger.debug(`read empty history: ${user.id}`) logger.debug(`read empty history: ${user.id}`)
return callback(null, []) return callback(null, new Map<string, HistoryObject>())
}).catch(function (err) { }).catch(function (err) {
logger.error('read history failed: ' + err) logger.error('read history failed: ' + err)
return callback(err, null) return callback(err, null)
}) })
} }
function setHistory (userId: string, history: any[], callback: (err: any | null, count: [number, User[]] | null) => void): void { function setHistory (userId: string, history: HistoryObject[], callback: (err: LogEntry | null, count: [number, User[]] | null) => void): void {
User.update({ User.update({
history: JSON.stringify(history) history: JSON.stringify(history)
}, { }, {
@ -109,7 +110,7 @@ function updateHistory (userId: string, noteId: string, document, time): void {
noteHistory.text = noteInfo.title noteHistory.text = noteInfo.title
noteHistory.time = time || Date.now() noteHistory.time = time || Date.now()
noteHistory.tags = noteInfo.tags noteHistory.tags = noteInfo.tags
setHistory(userId, history, function (err, _) { setHistory(userId, parseHistoryMapToArray(history), function (err, _) {
if (err) { if (err) {
logger.log(err) logger.log(err)
} }
@ -118,7 +119,7 @@ function updateHistory (userId: string, noteId: string, document, time): void {
} }
} }
function historyGet (req, res): any { function historyGet (req, res): void {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
getHistory(req.user.id, function (err, history) { getHistory(req.user.id, function (err, history) {
if (err) return errors.errorInternalError(res) if (err) return errors.errorInternalError(res)
@ -132,7 +133,7 @@ function historyGet (req, res): any {
} }
} }
function historyPost (req, res): any { function historyPost (req, res): void {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
const noteId = req.params.noteId const noteId = req.params.noteId
if (!noteId) { if (!noteId) {
@ -160,7 +161,7 @@ function historyPost (req, res): any {
if (!history[noteId]) return errors.errorNotFound(res) if (!history[noteId]) return errors.errorNotFound(res)
if (req.body.pinned === 'true' || req.body.pinned === 'false') { if (req.body.pinned === 'true' || req.body.pinned === 'false') {
history[noteId].pinned = (req.body.pinned === 'true') history[noteId].pinned = (req.body.pinned === 'true')
setHistory(req.user.id, history, function (err, _) { setHistory(req.user.id, parseHistoryMapToArray(history), function (err, _) {
if (err) return errors.errorInternalError(res) if (err) return errors.errorInternalError(res)
res.end() res.end()
}) })
@ -174,7 +175,7 @@ function historyPost (req, res): any {
} }
} }
function historyDelete (req, res): any { function historyDelete (req, res): void {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
const noteId = req.params.noteId const noteId = req.params.noteId
if (!noteId) { if (!noteId) {
@ -187,7 +188,7 @@ function historyDelete (req, res): any {
if (err) return errors.errorInternalError(res) if (err) return errors.errorInternalError(res)
if (!history) return errors.errorNotFound(res) if (!history) return errors.errorNotFound(res)
delete history[noteId] delete history[noteId]
setHistory(req.user.id, history, function (err, _) { setHistory(req.user.id, parseHistoryMapToArray(history), function (err, _) {
if (err) return errors.errorInternalError(res) if (err) return errors.errorInternalError(res)
res.end() res.end()
}) })

View file

@ -3,6 +3,6 @@ import { User } from './models'
declare module 'express' { declare module 'express' {
export interface Request { export interface Request {
user?: User; user?: User;
flash (type: string, msg?: string): any; flash (type: string, msg?: string): [] | object | number;
} }
} }