Skip to content

Commit a02d80f

Browse files
committed
lint: lib/response.ts
- add typing for req.user.id - add typing annotate in response.ts and fix type error in errorPage/index.ts
1 parent 1962625 commit a02d80f

File tree

4 files changed

+76
-50
lines changed

4 files changed

+76
-50
lines changed

lib/errorPage/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1+
import {Request, Response} from "express";
12
import config from "../config";
23

34
import {responseError} from "../response";
45

5-
export function errorForbidden(req, res) {
6+
export function errorForbidden(req: Request, res: Response): void {
67
if (req.user) {
7-
return responseError(res, '403', 'Forbidden', 'oh no.')
8+
return responseError(res, 403, 'Forbidden', 'oh no.')
89
}
910

1011
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
1112
res.redirect(config.serverURL + '/')
1213
}
1314

14-
export function errorNotFound(req, res) {
15-
responseError(res, '404', 'Not Found', 'oops.')
15+
export function errorNotFound(req: Request, res: Response): void {
16+
responseError(res, 404, 'Not Found', 'oops.')
1617
}
1718

18-
export function errorInternalError(req, res) {
19-
responseError(res, '500', 'Internal Error', 'wtf.')
19+
export function errorInternalError(req: Request, res: Response): void {
20+
responseError(res, 500, 'Internal Error', 'wtf.')
2021
}

lib/response.ts

Lines changed: 62 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// response
22
// external modules
33
import * as request from "request";
4+
import {Request, Response} from "express";
5+
import {Includeable} from "sequelize";
46
// core
57
import config from "./config";
68
import {logger} from "./logger";
@@ -9,9 +11,9 @@ import {createNoteWithRevision} from "./services/note";
911
import * as utils from "./utils";
1012
import * as history from "./history";
1113

12-
export function errorForbidden(req, res) {
14+
export function errorForbidden(req: Request, res: Response): void {
1315
if (req.user) {
14-
responseError(res, '403', 'Forbidden', 'oh no.')
16+
responseError(res, 403, 'Forbidden', 'oh no.')
1517
} else {
1618
const nextURL = new URL('', config.serverURL)
1719
nextURL.search = (new URLSearchParams({next: req.originalUrl})).toString()
@@ -20,27 +22,27 @@ export function errorForbidden(req, res) {
2022
}
2123
}
2224

23-
export function errorNotFound(req, res) {
24-
responseError(res, '404', 'Not Found', 'oops.')
25+
export function errorNotFound(req: Request, res: Response): void {
26+
responseError(res, 404, 'Not Found', 'oops.')
2527
}
2628

27-
export function errorBadRequest(req, res) {
28-
responseError(res, '400', 'Bad Request', 'something not right.')
29+
export function errorBadRequest(req: Request, res: Response): void {
30+
responseError(res, 400, 'Bad Request', 'something not right.')
2931
}
3032

31-
export function errorTooLong(req, res) {
32-
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
33+
export function errorTooLong(req: Request, res: Response): void {
34+
responseError(res, 413, 'Payload Too Large', 'Shorten your note!')
3335
}
3436

35-
export function errorInternalError(req, res) {
36-
responseError(res, '500', 'Internal Error', 'wtf.')
37+
export function errorInternalError(req: Request, res: Response): void {
38+
responseError(res, 500, 'Internal Error', 'wtf.')
3739
}
3840

39-
export function errorServiceUnavailable(req, res) {
41+
export function errorServiceUnavailable(req: Request, res: Response): void {
4042
res.status(503).send('I\'m busy right now, try again later.')
4143
}
4244

43-
export function responseError(res, code, detail, msg) {
45+
export function responseError(res: Response, code: number, detail: string, msg: string): void {
4446
res.status(code).render('error.ejs', {
4547
title: code + ' ' + detail + ' ' + msg,
4648
code: code,
@@ -49,7 +51,7 @@ export function responseError(res, code, detail, msg) {
4951
})
5052
}
5153

52-
export function responseCodiMD(res, note) {
54+
export function responseCodiMD(res: Response, note: Note): void {
5355
const body = note.content
5456
const extracted = Note.extractMeta(body)
5557
const meta = Note.parseMeta(extracted.meta)
@@ -64,13 +66,17 @@ export function responseCodiMD(res, note) {
6466
})
6567
}
6668

67-
function updateHistory(userId, note, document, time?: any) {
69+
function updateHistory(userId, note, document, time?: number) {
6870
const noteId = note.alias ? note.alias : Note.encodeNoteId(note.id)
6971
history.updateHistory(userId, noteId, document, time)
7072
logger.info('history updated')
7173
}
7274

73-
export function newNote(req, res, next?: any) {
75+
type NewNoteReq = Request & {
76+
alias?: string
77+
}
78+
79+
export function newNote(req: NewNoteReq, res: Response): void {
7480
let owner = null
7581
let body = ''
7682
if (req.body && req.body.length > config.documentMaxLength) {
@@ -100,7 +106,7 @@ export function newNote(req, res, next?: any) {
100106
})
101107
}
102108

103-
export function newCheckViewPermission(note, isLogin, userId) {
109+
export function newCheckViewPermission(note: Note, isLogin: boolean, userId: string): boolean {
104110
if (note.permission === 'private') {
105111
return note.ownerId === userId
106112
}
@@ -110,25 +116,17 @@ export function newCheckViewPermission(note, isLogin, userId) {
110116
return true
111117
}
112118

113-
export function checkViewPermission(req, note) {
119+
export function checkViewPermission(req: Request, note: Note): boolean {
114120
if (note.permission === 'private') {
115-
if (!req.isAuthenticated() || note.ownerId !== req.user.id) {
116-
return false
117-
} else {
118-
return true
119-
}
120-
} else if (note.permission === 'limited' || note.permission === 'protected') {
121-
if (!req.isAuthenticated()) {
122-
return false
123-
} else {
124-
return true
125-
}
126-
} else {
127-
return true
121+
return !(!req.isAuthenticated() || note.ownerId !== req.user.id);
128122
}
123+
if (note.permission === 'limited' || note.permission === 'protected') {
124+
return req.isAuthenticated();
125+
}
126+
return true
129127
}
130128

131-
function findNote(req, res, callback, include?: any) {
129+
function findNote(req, res, callback: (note: Note) => void, include?: Includeable[] | null) {
132130
const noteId = req.params.noteId
133131
const id = req.params.noteId || req.params.shortid
134132
Note.parseNoteId(id, function (err, _id) {
@@ -164,8 +162,7 @@ function findNote(req, res, callback, include?: any) {
164162

165163
function actionDownload(req, res, note) {
166164
const body = note.content
167-
const title = Note.decodeTitle(note.title)
168-
let filename = title
165+
let filename = Note.decodeTitle(note.title)
169166
filename = encodeURIComponent(filename)
170167
res.set({
171168
'Access-Control-Allow-Origin': '*', // allow CORS as API
@@ -179,7 +176,11 @@ function actionDownload(req, res, note) {
179176
res.send(body)
180177
}
181178

182-
export function publishNoteActions(req, res, next) {
179+
interface PublishActionParams {
180+
action: 'download' | 'edit'
181+
}
182+
183+
export function publishNoteActions(req: Request<PublishActionParams>, res: Response): void {
183184
findNote(req, res, function (note) {
184185
const action = req.params.action
185186
switch (action) {
@@ -196,7 +197,7 @@ export function publishNoteActions(req, res, next) {
196197
})
197198
}
198199

199-
export function publishSlideActions(req, res, next) {
200+
export function publishSlideActions(req: Request<PublishActionParams>, res: Response): void {
200201
findNote(req, res, function (note) {
201202
const action = req.params.action
202203
switch (action) {
@@ -210,7 +211,12 @@ export function publishSlideActions(req, res, next) {
210211
})
211212
}
212213

213-
export function githubActions(req, res, next) {
214+
interface GithubActionParams extends Record<string, string> {
215+
action: 'gist'
216+
noteId: string
217+
}
218+
219+
export function githubActions(req: Request<GithubActionParams>, res: Response): void {
214220
const noteId = req.params.noteId
215221
findNote(req, res, function (note) {
216222
const action = req.params.action
@@ -225,7 +231,7 @@ export function githubActions(req, res, next) {
225231
})
226232
}
227233

228-
function githubActionGist(req, res, note) {
234+
function githubActionGist(req: Request, res: Response, note: Note) {
229235
const code = req.query.code
230236
const state = req.query.state
231237
if (!code || !state) {
@@ -282,13 +288,18 @@ function githubActionGist(req, res, note) {
282288
}
283289
}
284290

285-
export function gitlabActions(req, res, next) {
291+
interface GitLabParams extends Record<string, string>{
292+
noteId: string
293+
action: 'projects'
294+
}
295+
296+
export function gitlabActions(req: Request<GitLabParams>, res: Response): void {
286297
const noteId = req.params.noteId
287-
findNote(req, res, function (note) {
298+
findNote(req, res, function () {
288299
const action = req.params.action
289300
switch (action) {
290301
case 'projects':
291-
gitlabActionProjects(req, res, note)
302+
gitlabActionProjects(req, res)
292303
break
293304
default:
294305
res.redirect(config.serverURL + '/' + noteId)
@@ -297,7 +308,15 @@ export function gitlabActions(req, res, next) {
297308
})
298309
}
299310

300-
function gitlabActionProjects(req, res, note) {
311+
interface GitLabActionResponse {
312+
baseURL: string
313+
version: string
314+
accesstoken: string
315+
profileid: string
316+
projects?: Record<string, string>
317+
}
318+
319+
function gitlabActionProjects(req: Request, res: Response) {
301320
if (req.isAuthenticated()) {
302321
User.findOne({
303322
where: {
@@ -307,7 +326,7 @@ function gitlabActionProjects(req, res, note) {
307326
if (!user) {
308327
return errorNotFound(req, res)
309328
}
310-
const ret: any = {baseURL: config.gitlab.baseURL, version: config.gitlab.version}
329+
const ret: Partial<GitLabActionResponse> = {baseURL: config.gitlab.baseURL, version: config.gitlab.version}
311330
ret.accesstoken = user.accessToken
312331
ret.profileid = user.profileid
313332
request(
@@ -330,7 +349,7 @@ function gitlabActionProjects(req, res, note) {
330349
}
331350
}
332351

333-
export function showPublishSlide(req, res, next) {
352+
export function showPublishSlide(req: Request, res: Response): void {
334353
const include = [{
335354
model: User,
336355
as: 'owner'

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"target": "ES2019",
66
"outDir": "./dist/",
77
"module": "CommonJS",
8-
"esModuleInterop": true
8+
"esModuleInterop": true,
9+
"typeRoots": ["./typings", "./node_modules/@types"]
910
},
1011
"include": [
1112
"./lib/**/*"

typings/passport/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare namespace Express {
2+
export interface User {
3+
id?: string
4+
}
5+
}

0 commit comments

Comments
 (0)