Skip to content

Commit 89b9dad

Browse files
author
Dushyant Bhalgami
authored
Merge pull request #52 from topcoder-platform/develop
bring master up to date
2 parents 6889e8a + 0d0ecdd commit 89b9dad

13 files changed

+4811
-381
lines changed

app-bootstrap.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,5 @@ const Joi = require('joi')
66

77
Joi.optionalId = () => Joi.string()
88
Joi.id = () => Joi.optionalId().required()
9-
Joi.page = () =>
10-
Joi.number()
11-
.integer()
12-
.min(1)
13-
.default(1)
14-
Joi.perPage = () =>
15-
Joi.number()
16-
.integer()
17-
.min(1)
18-
.default(20)
9+
Joi.page = () => Joi.number().integer().min(1).default(1)
10+
Joi.perPage = () => Joi.number().integer().min(1).default(20)

app-routes.js

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ const authenticator = require('tc-core-library-js').middleware.jwtAuthenticator
1313
* Configure all routes for express app
1414
* @param app the express app
1515
*/
16-
module.exports = app => {
16+
module.exports = (app) => {
1717
// Load all routes
1818
_.each(routes, (verbs, path) => {
1919
_.each(verbs, (def, verb) => {
2020
const controllerPath = `./src/controllers/${def.controller}`
21-
const method = require(controllerPath)[def.method]; // eslint-disable-line
21+
const method = require(controllerPath)[def.method] // eslint-disable-line
2222
if (!method) {
2323
throw new Error(`${def.method} is undefined`)
2424
}
@@ -32,25 +32,14 @@ module.exports = app => {
3232
// add Authenticator check if route has auth
3333
if (def.auth) {
3434
actions.push((req, res, next) => {
35-
authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(
36-
req,
37-
res,
38-
next
39-
)
35+
authenticator(_.pick(config, ['AUTH_SECRET', 'VALID_ISSUERS']))(req, res, next)
4036
})
4137

4238
actions.push((req, res, next) => {
4339
if (req.authUser.isMachine) {
4440
// M2M
45-
if (
46-
!req.authUser.scopes ||
47-
!helper.checkIfExists(def.scopes, req.authUser.scopes)
48-
) {
49-
next(
50-
new errors.ForbiddenError(
51-
'You are not allowed to perform this action!'
52-
)
53-
)
41+
if (!req.authUser.scopes || !helper.checkIfExists(def.scopes, req.authUser.scopes)) {
42+
next(new errors.ForbiddenError('You are not allowed to perform this action!'))
5443
} else {
5544
next()
5645
}
@@ -59,20 +48,12 @@ module.exports = app => {
5948
// User
6049
if (req.authUser.roles) {
6150
if (!helper.checkIfExists(def.access, req.authUser.roles)) {
62-
next(
63-
new errors.ForbiddenError(
64-
'You are not allowed to perform this action!'
65-
)
66-
)
51+
next(new errors.ForbiddenError('You are not allowed to perform this action!'))
6752
} else {
6853
next()
6954
}
7055
} else {
71-
next(
72-
new errors.ForbiddenError(
73-
'You are not authorized to perform this action'
74-
)
75-
)
56+
next(new errors.ForbiddenError('You are not authorized to perform this action'))
7657
}
7758
}
7859
})
@@ -87,13 +68,9 @@ module.exports = app => {
8768
app.use('*', (req, res) => {
8869
const route = routes[req.baseUrl]
8970
if (route) {
90-
res
91-
.status(HttpStatus.METHOD_NOT_ALLOWED)
92-
.json({ message: 'The requested HTTP method is not supported.' })
71+
res.status(HttpStatus.METHOD_NOT_ALLOWED).json({ message: 'The requested HTTP method is not supported.' })
9372
} else {
94-
res
95-
.status(HttpStatus.NOT_FOUND)
96-
.json({ message: 'The requested resource cannot be found.' })
73+
res.status(HttpStatus.NOT_FOUND).json({ message: 'The requested resource cannot be found.' })
9774
}
9875
})
9976
}

app.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ require('./app-routes')(app)
4040
app.use((err, req, res, next) => {
4141
logger.logFullError(err, req.signature || `${req.method} ${req.url}`)
4242
const errorResponse = {}
43-
const status = err.isJoi
44-
? HttpStatus.BAD_REQUEST
45-
: err.httpStatus || HttpStatus.INTERNAL_SERVER_ERROR
43+
const status = err.isJoi ? HttpStatus.BAD_REQUEST : err.httpStatus || HttpStatus.INTERNAL_SERVER_ERROR
4644

4745
if (_.isArray(err.details)) {
4846
if (err.isJoi) {
49-
_.map(err.details, e => {
47+
_.map(err.details, (e) => {
5048
if (e.message) {
5149
if (_.isUndefined(errorResponse.message)) {
5250
errorResponse.message = e.message

0 commit comments

Comments
 (0)