Skip to content

Commit 4ac654d

Browse files
committed
ts: lib/response.js
1 parent b02b193 commit 4ac654d

1 file changed

Lines changed: 45 additions & 53 deletions

File tree

lib/response.js renamed to lib/response.ts

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,45 @@
1-
'use strict'
21
// response
32
// external modules
4-
const request = require('request')
5-
3+
import * as request from "request";
64
// core
7-
const config = require('./config')
8-
const logger = require('./logger')
9-
const models = require('./models')
10-
const utils = require('./utils')
11-
const history = require('./history')
12-
13-
// public
14-
exports.responseError = responseError
15-
exports.errorForbidden = errorForbidden
16-
exports.errorNotFound = errorNotFound
17-
exports.errorBadRequest = errorBadRequest
18-
exports.errorTooLong = errorTooLong
19-
exports.errorInternalError = errorInternalError
20-
exports.errorServiceUnavailable = errorServiceUnavailable
21-
exports.newNote = newNote
22-
exports.showPublishSlide = showPublishSlide
23-
exports.publishNoteActions = publishNoteActions
24-
exports.publishSlideActions = publishSlideActions
25-
exports.githubActions = githubActions
26-
exports.gitlabActions = gitlabActions
27-
exports.checkViewPermission = checkViewPermission
28-
exports.newCheckViewPermission = newCheckViewPermission
29-
exports.responseCodiMD = responseCodiMD
5+
import * as config from "./config";
6+
import * as logger from "./logger";
7+
import * as models from "./models";
8+
import * as utils from "./utils";
9+
import * as history from "./history";
3010

31-
function errorForbidden (req, res) {
11+
export function errorForbidden(req, res) {
3212
if (req.user) {
3313
responseError(res, '403', 'Forbidden', 'oh no.')
3414
} else {
3515
var nextURL = new URL('', config.serverURL)
36-
nextURL.search = new URLSearchParams({ next: req.originalUrl })
16+
nextURL.search = (new URLSearchParams({next: req.originalUrl})).toString()
3717
req.flash('error', 'You are not allowed to access this page. Maybe try logging in?')
3818
res.redirect(nextURL.toString())
3919
}
4020
}
4121

42-
function errorNotFound (req, res) {
22+
export function errorNotFound(req, res) {
4323
responseError(res, '404', 'Not Found', 'oops.')
4424
}
4525

46-
function errorBadRequest (req, res) {
26+
export function errorBadRequest(req, res) {
4727
responseError(res, '400', 'Bad Request', 'something not right.')
4828
}
4929

50-
function errorTooLong (req, res) {
30+
export function errorTooLong(req, res) {
5131
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
5232
}
5333

54-
function errorInternalError (req, res) {
34+
export function errorInternalError(req, res) {
5535
responseError(res, '500', 'Internal Error', 'wtf.')
5636
}
5737

58-
function errorServiceUnavailable (req, res) {
38+
export function errorServiceUnavailable(req, res) {
5939
res.status(503).send('I\'m busy right now, try again later.')
6040
}
6141

62-
function responseError (res, code, detail, msg) {
42+
export function responseError(res, code, detail, msg) {
6343
res.status(code).render('error.ejs', {
6444
title: code + ' ' + detail + ' ' + msg,
6545
code: code,
@@ -68,7 +48,7 @@ function responseError (res, code, detail, msg) {
6848
})
6949
}
7050

71-
function responseCodiMD (res, note) {
51+
export function responseCodiMD(res, note) {
7252
var body = note.content
7353
var extracted = models.Note.extractMeta(body)
7454
var meta = models.Note.parseMeta(extracted.meta)
@@ -83,13 +63,13 @@ function responseCodiMD (res, note) {
8363
})
8464
}
8565

86-
function updateHistory (userId, note, document, time) {
66+
function updateHistory(userId, note, document, time?: any) {
8767
var noteId = note.alias ? note.alias : models.Note.encodeNoteId(note.id)
8868
history.updateHistory(userId, noteId, document, time)
8969
logger.info('history updated')
9070
}
9171

92-
function newNote (req, res, next) {
72+
export function newNote(req, res, next?: any) {
9373
var owner = null
9474
var body = ''
9575
if (req.body && req.body.length > config.documentMaxLength) {
@@ -119,7 +99,7 @@ function newNote (req, res, next) {
11999
})
120100
}
121101

122-
function newCheckViewPermission (note, isLogin, userId) {
102+
export function newCheckViewPermission(note, isLogin, userId) {
123103
if (note.permission === 'private') {
124104
return note.ownerId === userId
125105
}
@@ -129,17 +109,25 @@ function newCheckViewPermission (note, isLogin, userId) {
129109
return true
130110
}
131111

132-
function checkViewPermission (req, note) {
112+
export function checkViewPermission(req, note) {
133113
if (note.permission === 'private') {
134-
if (!req.isAuthenticated() || note.ownerId !== req.user.id) { return false } else { return true }
114+
if (!req.isAuthenticated() || note.ownerId !== req.user.id) {
115+
return false
116+
} else {
117+
return true
118+
}
135119
} else if (note.permission === 'limited' || note.permission === 'protected') {
136-
if (!req.isAuthenticated()) { return false } else { return true }
120+
if (!req.isAuthenticated()) {
121+
return false
122+
} else {
123+
return true
124+
}
137125
} else {
138126
return true
139127
}
140128
}
141129

142-
function findNote (req, res, callback, include) {
130+
function findNote(req, res, callback, include?: any) {
143131
var noteId = req.params.noteId
144132
var id = req.params.noteId || req.params.shortid
145133
models.Note.parseNoteId(id, function (err, _id) {
@@ -173,7 +161,7 @@ function findNote (req, res, callback, include) {
173161
})
174162
}
175163

176-
function actionDownload (req, res, note) {
164+
function actionDownload(req, res, note) {
177165
var body = note.content
178166
var title = models.Note.decodeTitle(note.title)
179167
var filename = title
@@ -190,7 +178,7 @@ function actionDownload (req, res, note) {
190178
res.send(body)
191179
}
192180

193-
function publishNoteActions (req, res, next) {
181+
export function publishNoteActions(req, res, next) {
194182
findNote(req, res, function (note) {
195183
var action = req.params.action
196184
switch (action) {
@@ -207,7 +195,7 @@ function publishNoteActions (req, res, next) {
207195
})
208196
}
209197

210-
function publishSlideActions (req, res, next) {
198+
export function publishSlideActions(req, res, next) {
211199
findNote(req, res, function (note) {
212200
var action = req.params.action
213201
switch (action) {
@@ -221,7 +209,7 @@ function publishSlideActions (req, res, next) {
221209
})
222210
}
223211

224-
function githubActions (req, res, next) {
212+
export function githubActions(req, res, next) {
225213
var noteId = req.params.noteId
226214
findNote(req, res, function (note) {
227215
var action = req.params.action
@@ -236,7 +224,7 @@ function githubActions (req, res, next) {
236224
})
237225
}
238226

239-
function githubActionGist (req, res, note) {
227+
function githubActionGist(req, res, note) {
240228
var code = req.query.code
241229
var state = req.query.state
242230
if (!code || !state) {
@@ -293,7 +281,7 @@ function githubActionGist (req, res, note) {
293281
}
294282
}
295283

296-
function gitlabActions (req, res, next) {
284+
export function gitlabActions(req, res, next) {
297285
var noteId = req.params.noteId
298286
findNote(req, res, function (note) {
299287
var action = req.params.action
@@ -308,15 +296,17 @@ function gitlabActions (req, res, next) {
308296
})
309297
}
310298

311-
function gitlabActionProjects (req, res, note) {
299+
function gitlabActionProjects(req, res, note) {
312300
if (req.isAuthenticated()) {
313301
models.User.findOne({
314302
where: {
315303
id: req.user.id
316304
}
317305
}).then(function (user) {
318-
if (!user) { return errorNotFound(req, res) }
319-
var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
306+
if (!user) {
307+
return errorNotFound(req, res)
308+
}
309+
var ret: any = {baseURL: config.gitlab.baseURL, version: config.gitlab.version}
320310
ret.accesstoken = user.accessToken
321311
ret.profileid = user.profileid
322312
request(
@@ -339,7 +329,7 @@ function gitlabActionProjects (req, res, note) {
339329
}
340330
}
341331

342-
function showPublishSlide (req, res, next) {
332+
export function showPublishSlide(req, res, next) {
343333
var include = [{
344334
model: models.User,
345335
as: 'owner'
@@ -350,7 +340,9 @@ function showPublishSlide (req, res, next) {
350340
findNote(req, res, function (note) {
351341
// force to use short id
352342
var shortid = req.params.shortid
353-
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) }
343+
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) {
344+
return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid))
345+
}
354346
note.increment('viewcount').then(function (note) {
355347
if (!note) {
356348
return errorNotFound(req, res)

0 commit comments

Comments
 (0)