From 69818c9394366b954b6ba3bd697bd9d2b09f2a16 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 10 Jun 2017 22:15:25 +0200 Subject: Type functions --- server/middlewares/validators/pagination.ts | 5 +++- server/middlewares/validators/pods.ts | 7 ++++-- server/middlewares/validators/remote/signature.ts | 5 +++- server/middlewares/validators/remote/videos.ts | 9 +++++--- server/middlewares/validators/sort.ts | 13 +++++++---- server/middlewares/validators/users.ts | 11 +++++---- server/middlewares/validators/utils.ts | 5 ++-- server/middlewares/validators/videos.ts | 28 +++++++++++++---------- 8 files changed, 53 insertions(+), 30 deletions(-) (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/pagination.ts b/server/middlewares/validators/pagination.ts index de719c05b..cca8295ff 100644 --- a/server/middlewares/validators/pagination.ts +++ b/server/middlewares/validators/pagination.ts @@ -1,7 +1,10 @@ +import 'express-validator' +import * as express from 'express' + import { checkErrors } from './utils' import { logger } from '../../helpers' -function paginationValidator (req, res, next) { +function paginationValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkQuery('start', 'Should have a number start').optional().isInt() req.checkQuery('count', 'Should have a number count').optional().isInt() diff --git a/server/middlewares/validators/pods.ts b/server/middlewares/validators/pods.ts index c55a88b85..d8eb90168 100644 --- a/server/middlewares/validators/pods.ts +++ b/server/middlewares/validators/pods.ts @@ -1,3 +1,6 @@ +import 'express-validator' +import * as express from 'express' + import { database as db } from '../../initializers/database' import { checkErrors } from './utils' import { logger } from '../../helpers' @@ -5,7 +8,7 @@ import { CONFIG } from '../../initializers' import { hasFriends } from '../../lib' import { isTestInstance } from '../../helpers' -function makeFriendsValidator (req, res, next) { +function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) { // Force https if the administrator wants to make friends if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') { return res.status(400).send('Cannot make friends with a non HTTPS webserver.') @@ -32,7 +35,7 @@ function makeFriendsValidator (req, res, next) { }) } -function podsAddValidator (req, res, next) { +function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('host', 'Should have a host').isHostValid() req.checkBody('email', 'Should have an email').isEmail() req.checkBody('publicKey', 'Should have a public key').notEmpty() diff --git a/server/middlewares/validators/remote/signature.ts b/server/middlewares/validators/remote/signature.ts index 6e3ebe7db..eb5c196eb 100644 --- a/server/middlewares/validators/remote/signature.ts +++ b/server/middlewares/validators/remote/signature.ts @@ -1,7 +1,10 @@ +import 'express-validator' +import * as express from 'express' + import { logger } from '../../../helpers' import { checkErrors } from '../utils' -function signatureValidator (req, res, next) { +function signatureValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('signature.host', 'Should have a signature host').isURL() req.checkBody('signature.signature', 'Should have a signature').notEmpty() diff --git a/server/middlewares/validators/remote/videos.ts b/server/middlewares/validators/remote/videos.ts index 3380c29e2..2037c0085 100644 --- a/server/middlewares/validators/remote/videos.ts +++ b/server/middlewares/validators/remote/videos.ts @@ -1,7 +1,10 @@ +import 'express-validator' +import * as express from 'express' + import { logger } from '../../../helpers' import { checkErrors } from '../utils' -function remoteVideosValidator (req, res, next) { +function remoteVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('data').isEachRemoteRequestVideosValid() logger.debug('Checking remoteVideos parameters', { parameters: req.body }) @@ -9,7 +12,7 @@ function remoteVideosValidator (req, res, next) { checkErrors(req, res, next) } -function remoteQaduVideosValidator (req, res, next) { +function remoteQaduVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('data').isEachRemoteRequestVideosQaduValid() logger.debug('Checking remoteQaduVideos parameters', { parameters: req.body }) @@ -17,7 +20,7 @@ function remoteQaduVideosValidator (req, res, next) { checkErrors(req, res, next) } -function remoteEventsVideosValidator (req, res, next) { +function remoteEventsVideosValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('data').isEachRemoteRequestVideosEventsValid() logger.debug('Checking remoteEventsVideos parameters', { parameters: req.body }) diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index ebc7333c7..3baee9fb3 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -1,3 +1,6 @@ +import 'express-validator' +import * as express from 'express' + import { checkErrors } from './utils' import { logger } from '../../helpers' import { SORTABLE_COLUMNS } from '../../initializers' @@ -7,15 +10,15 @@ const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) -function usersSortValidator (req, res, next) { +function usersSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { checkSort(req, res, next, SORTABLE_USERS_COLUMNS) } -function videoAbusesSortValidator (req, res, next) { +function videoAbusesSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { checkSort(req, res, next, SORTABLE_VIDEO_ABUSES_COLUMNS) } -function videosSortValidator (req, res, next) { +function videosSortValidator (req: express.Request, res: express.Response, next: express.NextFunction) { checkSort(req, res, next, SORTABLE_VIDEOS_COLUMNS) } @@ -29,7 +32,7 @@ export { // --------------------------------------------------------------------------- -function checkSort (req, res, next, sortableColumns) { +function checkSort (req: express.Request, res: express.Response, next: express.NextFunction, sortableColumns: string[]) { req.checkQuery('sort', 'Should have correct sortable column').optional().isIn(sortableColumns) logger.debug('Checking sort parameters', { parameters: req.query }) @@ -37,7 +40,7 @@ function checkSort (req, res, next, sortableColumns) { checkErrors(req, res, next) } -function createSortableColumns (sortableColumns) { +function createSortableColumns (sortableColumns: string[]) { const sortableColumnDesc = sortableColumns.map(sortableColumn => '-' + sortableColumn) return sortableColumns.concat(sortableColumnDesc) diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index e0d1d917a..b7b9ef370 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -1,8 +1,11 @@ +import 'express-validator' +import * as express from 'express' + import { database as db } from '../../initializers/database' import { checkErrors } from './utils' import { logger } from '../../helpers' -function usersAddValidator (req, res, next) { +function usersAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('username', 'Should have a valid username').isUserUsernameValid() req.checkBody('password', 'Should have a valid password').isUserPasswordValid() req.checkBody('email', 'Should have a valid email').isEmail() @@ -23,7 +26,7 @@ function usersAddValidator (req, res, next) { }) } -function usersRemoveValidator (req, res, next) { +function usersRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isInt() logger.debug('Checking usersRemove parameters', { parameters: req.params }) @@ -44,7 +47,7 @@ function usersRemoveValidator (req, res, next) { }) } -function usersUpdateValidator (req, res, next) { +function usersUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isInt() // Add old password verification req.checkBody('password', 'Should have a valid password').optional().isUserPasswordValid() @@ -55,7 +58,7 @@ function usersUpdateValidator (req, res, next) { checkErrors(req, res, next) } -function usersVideoRatingValidator (req, res, next) { +function usersVideoRatingValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('videoId', 'Should have a valid video id').notEmpty().isUUID(4) logger.debug('Checking usersVideoRating parameters', { parameters: req.params }) diff --git a/server/middlewares/validators/utils.ts b/server/middlewares/validators/utils.ts index 710e65529..0424d5942 100644 --- a/server/middlewares/validators/utils.ts +++ b/server/middlewares/validators/utils.ts @@ -1,9 +1,10 @@ +import 'express-validator' +import * as express from 'express' import { inspect } from 'util' import { logger } from '../../helpers' -function checkErrors (req, res, next, statusCode?) { - if (statusCode === undefined) statusCode = 400 +function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) { const errors = req.validationErrors() if (errors) { diff --git a/server/middlewares/validators/videos.ts b/server/middlewares/validators/videos.ts index 47825975c..e99cdefb1 100644 --- a/server/middlewares/validators/videos.ts +++ b/server/middlewares/validators/videos.ts @@ -1,9 +1,13 @@ +import 'express-validator' +import * as multer from 'multer' +import * as express from 'express' + import { database as db } from '../../initializers/database' import { checkErrors } from './utils' import { CONSTRAINTS_FIELDS, SEARCHABLE_COLUMNS } from '../../initializers' import { logger, isVideoDurationValid } from '../../helpers' -function videosAddValidator (req, res, next) { +function videosAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkBody('videofile', 'Should have a valid file').isVideoFile(req.files) req.checkBody('name', 'Should have a valid name').isVideoNameValid() req.checkBody('category', 'Should have a valid category').isVideoCategoryValid() @@ -27,13 +31,13 @@ function videosAddValidator (req, res, next) { return res.status(400).send('Duration of the video file is too big (max: ' + CONSTRAINTS_FIELDS.VIDEOS.DURATION.max + 's).') } - videoFile.duration = duration + videoFile['duration'] = duration next() }) }) } -function videosUpdateValidator (req, res, next) { +function videosUpdateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkBody('name', 'Should have a valid name').optional().isVideoNameValid() req.checkBody('category', 'Should have a valid category').optional().isVideoCategoryValid() @@ -61,7 +65,7 @@ function videosUpdateValidator (req, res, next) { }) } -function videosGetValidator (req, res, next) { +function videosGetValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) logger.debug('Checking videosGet parameters', { parameters: req.params }) @@ -71,7 +75,7 @@ function videosGetValidator (req, res, next) { }) } -function videosRemoveValidator (req, res, next) { +function videosRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) logger.debug('Checking videosRemove parameters', { parameters: req.params }) @@ -88,7 +92,7 @@ function videosRemoveValidator (req, res, next) { }) } -function videosSearchValidator (req, res, next) { +function videosSearchValidator (req: express.Request, res: express.Response, next: express.NextFunction) { const searchableColumns = SEARCHABLE_COLUMNS.VIDEOS req.checkParams('value', 'Should have a valid search').notEmpty() req.checkQuery('field', 'Should have correct searchable column').optional().isIn(searchableColumns) @@ -98,7 +102,7 @@ function videosSearchValidator (req, res, next) { checkErrors(req, res, next) } -function videoAbuseReportValidator (req, res, next) { +function videoAbuseReportValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkBody('reason', 'Should have a valid reason').isVideoAbuseReasonValid() @@ -109,7 +113,7 @@ function videoAbuseReportValidator (req, res, next) { }) } -function videoRateValidator (req, res, next) { +function videoRateValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) req.checkBody('rating', 'Should have a valid rate type').isVideoRatingTypeValid() @@ -120,7 +124,7 @@ function videoRateValidator (req, res, next) { }) } -function videosBlacklistValidator (req, res, next) { +function videosBlacklistValidator (req: express.Request, res: express.Response, next: express.NextFunction) { req.checkParams('id', 'Should have a valid id').notEmpty().isUUID(4) logger.debug('Checking videosBlacklist parameters', { parameters: req.params }) @@ -150,7 +154,7 @@ export { // --------------------------------------------------------------------------- -function checkVideoExists (id, res, callback) { +function checkVideoExists (id: string, res: express.Response, callback: () => void) { db.Video.loadAndPopulateAuthorAndPodAndTags(id, function (err, video) { if (err) { logger.error('Error in video request validator.', { error: err }) @@ -164,7 +168,7 @@ function checkVideoExists (id, res, callback) { }) } -function checkUserCanDeleteVideo (userId, res, callback) { +function checkUserCanDeleteVideo (userId: number, res: express.Response, callback: () => void) { // Retrieve the user who did the request db.User.loadById(userId, function (err, user) { if (err) { @@ -190,7 +194,7 @@ function checkUserCanDeleteVideo (userId, res, callback) { }) } -function checkVideoIsBlacklistable (req, res, callback) { +function checkVideoIsBlacklistable (req: express.Request, res: express.Response, callback: () => void) { if (res.locals.video.isOwned() === true) { return res.status(403).send('Cannot blacklist a local video') } -- cgit v1.2.3