Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
celebrate({
body: Joi.object().keys({
name: Joi.string().required(),
age: Joi.number().integer(),
role: Joi.string().default("admin")
})
}),
function(req, res) {
console.log("HI");
res.send("Hello world!");
}
);
router.post(
"/signup",
celebrate({
body: Joi.object().keys({
name: Joi.string().required(),
age: Joi.number().integer(),
role: Joi.string().default("admin")
})
}),
(req, res) => {
res.send("HI");
console.log(req.body);
// At this point, req.body has been validated and
// req.body.role is equal to req.body.role if provided in the POST or set to 'admin' by joi
}
);
module.exports = router;
newResource.tags = created_tags;
await newResource.save();
res.status(201).json({
code: 201,
message: "Resource Successfully Created",
success: true
});
})
);
// get one resource
router.get(
"/:resource_id",
requireVolunteerStatus,
celebrate({
params: {
resource_id: Joi.objectId().required()
}
}),
errorWrap(async (req, res) => {
const resourceId = req.params.resource_id;
const resource = await Resource.findById(resourceId);
res.json({
code: 200,
result: resource,
success: true
});
})
);
// edit resource
const router = require('express').Router()
const axios = require('axios')
const Joi = require('joi')
const { celebrate } = require('celebrate')
const redisClient = require('../redis')
const signUpSchema = {
body: Joi.object().keys({
account: Joi.string().required(),
password: Joi.string().required(),
}),
}
router.post('/sign_up', celebrate(signUpSchema), (req, res) => {
const { account, password } = req.body
res.json({ account, password })
})
router.get('/analytics', (req, res) => {
const { keyword } = req.query
redisClient.get(`analytics/${keyword}`, (err, cachedData) => {
if (cachedData !== null) {
res.json(cachedData)
} else {
// some heavy works
axios.get(`
https://api.github.com/search/repositories?q=${keyword}+language:javascript&sort=stars&order=desc
`).then((response) => {
const originalData = response.data.items
const data = originalData.reduce((accum, curr) => [
success: true
}
: {
code: 404,
message: "User Not Found",
success: false
};
res.status(ret.code).json(ret);
})
);
// approve user
router.put(
"/:user_id/approve",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string(),
lastName: Joi.string(),
oauthId: Joi.string(),
propicUrl: Joi.string(),
isApproved: Joi.boolean(),
role: Joi.string(),
location: Joi.string(),
email: Joi.string()
})
}),
errorWrap(async (req, res) => {
const userId = req.params.user_id;
const user = await User.findByIdAndUpdate(
userId,
success: true
}
: {
code: 404,
message: "Resource Not Found",
success: false
};
res.status(ret.code).json(ret);
})
);
// delete resource
router.delete(
"/:resource_id",
requireAdminStatus,
celebrate({
params: {
resource_id: Joi.objectId().required()
}
}),
errorWrap(async (req, res) => {
const resourceId = req.params.resource_id;
const resource = await Resource.findByIdAndRemove(resourceId);
const ret = resource
? {
code: 200,
message: "Resource deleted successfully",
success: true
}
: {
code: 404,
message: "Resource not found",
errorWrap(async (req, res) => {
const userId = req.params.user_id;
const user = await User.findById(userId);
res.json({
code: 200,
result: filterSensitiveInfo(user),
success: true
});
})
);
// create new user
router.post(
"/",
requireAdminStatus,
celebrate({
body: Joi.object().keys({
firstName: Joi.string().required(),
lastName: Joi.string().required(),
oauthId: Joi.string().required(),
propicUrl: Joi.string(),
isApproved: Joi.boolean().default(false),
role: Joi.string().required(),
location: Joi.string().required(),
email: Joi.string().required()
})
}),
errorWrap(async (req, res) => {
const data = req.body;
const newUser = new User({
firstName: data.firstName,
lastName: data.lastName,
// If defined, `cors` can be truthy or an object.
// Objects are passed to the middleware directly.
// Other truthy values use the default configuration.
if (route.cors) {
const corsConfig = typeof route.cors === 'object' ? route.cors : undefined;
// Enable the pre-flight OPTIONS request
app.options(route.path, cors(corsConfig));
routeHandlers.push(cors(corsConfig));
}
if (route.preProcess) {
routeHandlers.push(route.preProcess);
}
if (route.validate) {
routeHandlers.push(celebrate(route.validate, {
// silently drop any unknown fields within objects on the ground.
stripUnknown: { arrays: false, objects: true }
}));
}
routeHandlers.push(route.process);
app[route.method].apply(app, [route.path].concat(routeHandlers));
});
const { celebrate, Joi } = require('celebrate');
const usersService = require('../../services/users');
const getUsers = async ({ params }, res) => {
const users = await usersService.getUsers();
res.status(200).send(users);
};
const getSingleUser = async ({ body, params: { userId } }, res) => {
const user = await usersService.getSingleUser(userId);
res.status(200).send(user);
};
const getSingleUserValidation = celebrate({
params: {
userId: Joi.string()
.regex(/^[0-9a-fA-F]{24}$/)
.required()
}
});
const addUser = async ({ body }, res) => {
const user = await usersService.addUser(body);
res.status(200).send(user);
};
const addUserValidation = celebrate({
body: {
email: Joi.string()
.email()
}),
params: Joi.object({
id: Joi.string().alphanum().optional()
}).unknown(),
query: {
},
headers: Joi.object({
}).unknown()
}
export default {
BODY: celebrate({ body }),
PARAMS: celebrate({ params }),
BODY_AND_PARAMS: celebrate({ body, params })
}
};
const getSingleUserValidation = celebrate({
params: {
userId: Joi.string()
.regex(/^[0-9a-fA-F]{24}$/)
.required()
}
});
const addUser = async ({ body }, res) => {
const user = await usersService.addUser(body);
res.status(200).send(user);
};
const addUserValidation = celebrate({
body: {
email: Joi.string()
.email()
.required()
}
});
const updateUser = async ({ body, params: { userId } }, res) => {
await usersService.updateUser(userId, body);
res.status(200).end();
};
const deleteUser = async ({ body, params: { userId } }, res) => {
const success = await usersService.deleteUser(userId);
res.status(success ? 200 : 400).end();
};