diff --git a/index.js b/index.js index 6c09c6868..682549cdd 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ require('@babel/register')({ - ignore: [/(node_modules)/] + ignore: [/(node_modules)/], }); module.exports = require('./src'); diff --git a/src/globals/requestHandlers.js b/src/globals/requestHandlers.js index a1bca21f0..0cbb632d3 100644 --- a/src/globals/requestHandlers.js +++ b/src/globals/requestHandlers.js @@ -1,22 +1,23 @@ import httpStatus from 'http-status'; import { findOne } from '../mongoose/resolvers'; import { createAutopopulateOptions } from '../mongoose/createAutopopulateOptions'; +import { NotFound } from '../errors'; export const upsert = (req, res) => { if (!req.model.schema.tree[req.params.slug]) { - res.status(httpStatus.NOT_FOUND).json({ error: 'not found' }); + res.status(httpStatus.NOT_FOUND).json(new NotFound()); return; } req.model.findOne({}, (findErr, doc) => { let global = {}; - if (!doc) { if (req.params.slug) { global[req.params.slug] = req.body; } else { global = req.body; } + return req.model.create(global, (createErr, result) => { if (createErr) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: createErr }); @@ -29,14 +30,10 @@ export const upsert = (req, res) => { } if (!doc[req.params.slug]) { - // eslint-disable-next-line no-param-reassign - doc[req.params.slug] = {}; + Object.assign(doc[req.params.slug], {}); } - Object.keys(req.body).forEach((e) => { - // eslint-disable-next-line no-param-reassign - doc[req.params.slug][e] = req.body[e]; - }); + Object.assign(doc[req.params.slug], req.body); return doc.save((err) => { if (err) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); @@ -60,15 +57,16 @@ export const fetch = (req, res) => { findOne(query, createAutopopulateOptions(query)) .then((doc) => { const globals = { ...doc }; + // eslint-disable-next-line no-underscore-dangle delete globals._id; delete globals.id; + // eslint-disable-next-line no-underscore-dangle delete globals.__v; if (globals[req.params.key]) { return res.json(globals[req.params.key]); } if (req.params.key) { - return res.status(httpStatus.NOT_FOUND) - .json({ error: 'not found' }); + return res.status(httpStatus.NOT_FOUND).json(new NotFound()); } return res.json(globals); }) diff --git a/src/init/registerExpressMiddleware.js b/src/init/registerExpressMiddleware.js index a31258a7a..d616890ad 100644 --- a/src/init/registerExpressMiddleware.js +++ b/src/init/registerExpressMiddleware.js @@ -10,6 +10,6 @@ const registerExpressMiddleware = ({ app, config, router }) => { app.use(bodyParser.urlencoded({ extended: true })); app.use(localizationMiddleware(config.localization)); app.use(router); -} +}; export default registerExpressMiddleware; diff --git a/src/mongoose/requestHandlers/create.js b/src/mongoose/requestHandlers/create.js index 881ca602f..d89a95f42 100644 --- a/src/mongoose/requestHandlers/create.js +++ b/src/mongoose/requestHandlers/create.js @@ -2,10 +2,13 @@ import httpStatus from 'http-status'; const create = (req, res) => { req.model.create(req.body, (err, result) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); + if (err) { + res.status(httpStatus.INTERNAL_SERVER_ERROR) + .json({ error: err }); + return; + } - return res.status(httpStatus.CREATED) + res.status(httpStatus.CREATED) .json({ message: 'success', result: result.toJSON({ virtuals: true }), diff --git a/src/mongoose/requestHandlers/destroy.js b/src/mongoose/requestHandlers/destroy.js index 2e855059f..35535f762 100644 --- a/src/mongoose/requestHandlers/destroy.js +++ b/src/mongoose/requestHandlers/destroy.js @@ -1,14 +1,22 @@ import httpStatus from 'http-status'; +import { NotFound } from '../../errors'; const destroy = (req, res) => { req.model.findOneAndDelete({ _id: req.params.id }, (err, doc) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); + if (err) { + res.status(httpStatus.INTERNAL_SERVER_ERROR) + .json({ error: err }); + return; + } - if (!doc) - return res.status(httpStatus.NOT_FOUND).send('Not Found'); + if (!doc) { + res.status(httpStatus.NOT_FOUND) + .json(new NotFound()); + return; + } - return res.send(); + res.status(httpStatus.OK) + .send({ result: 'success' }); }); }; diff --git a/src/mongoose/requestHandlers/index.js b/src/mongoose/requestHandlers/index.js index aec3217af..408630158 100644 --- a/src/mongoose/requestHandlers/index.js +++ b/src/mongoose/requestHandlers/index.js @@ -9,5 +9,5 @@ export { destroy, findOne, query, - update -} + update, +}; diff --git a/src/mongoose/requestHandlers/query.js b/src/mongoose/requestHandlers/query.js index 979565de3..ed9dec62b 100644 --- a/src/mongoose/requestHandlers/query.js +++ b/src/mongoose/requestHandlers/query.js @@ -3,9 +3,10 @@ import httpStatus from 'http-status'; const query = (req, res) => { req.model.paginate(req.model.apiQuery(req.query, req.locale), { ...req.query }, (err, result) => { if (err) { - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); + res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); + return; } - return res.json({ + res.status(httpStatus.OK).json({ ...result, docs: result.docs.map((doc) => { if (req.locale && doc.setLocale) { diff --git a/src/mongoose/requestHandlers/update.js b/src/mongoose/requestHandlers/update.js index a92fa6f48..5134b1df4 100644 --- a/src/mongoose/requestHandlers/update.js +++ b/src/mongoose/requestHandlers/update.js @@ -1,21 +1,24 @@ import httpStatus from 'http-status'; +import { NotFound } from '../../errors'; const update = (req, res) => { req.model.findOne({ _id: req.params.id }, '', {}, (err, doc) => { - if (!doc) - return res.status(httpStatus.NOT_FOUND).send('Not Found'); + if (!doc) { + res.status(httpStatus.NOT_FOUND).json(new NotFound()); + return; + } - Object.keys(req.body).forEach(e => { - doc[e] = req.body[e]; - }); + Object.assign(doc, req.body); - doc.save((err) => { - if (err) - return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); + doc.save((saveError) => { + if (saveError) { + res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: saveError }); + return; + } - return res.json({ + res.status(httpStatus.OK).json({ message: 'success', - result: doc.toJSON({ virtuals: true }) + result: doc.toJSON({ virtuals: true }), }); }); }); diff --git a/src/mongoose/resolvers/findOne.js b/src/mongoose/resolvers/findOne.js index a3ab9c080..de89d93ce 100644 --- a/src/mongoose/resolvers/findOne.js +++ b/src/mongoose/resolvers/findOne.js @@ -1,11 +1,11 @@ -import NotFound from '../../errors'; +import { NotFound } from '../../errors'; const find = ({ Model, locale, fallback }) => { return new Promise((resolve, reject) => { Model.findOne(null, (err, doc) => { if (err || !doc) { - console.log('ok'); reject(new NotFound()); + return; } let result = doc; diff --git a/src/mongoose/schema/fieldToSchemaMap.js b/src/mongoose/schema/fieldToSchemaMap.js index 07bc3bedd..48d63391e 100644 --- a/src/mongoose/schema/fieldToSchemaMap.js +++ b/src/mongoose/schema/fieldToSchemaMap.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose'; -const formatBaseSchema = field => { +const formatBaseSchema = (field) => { return { hide: field.hide || false, localized: field.localized || false, @@ -10,31 +10,31 @@ const formatBaseSchema = field => { }; const fieldToSchemaMap = { - number: field => { + number: (field) => { return { ...formatBaseSchema(field), type: Number }; }, - input: field => { + input: (field) => { return { ...formatBaseSchema(field), type: String }; }, - textarea: field => { + textarea: (field) => { return { ...formatBaseSchema(field), type: String }; }, - WYSIWYG: field => { + WYSIWYG: (field) => { return { ...formatBaseSchema(field), type: String }; }, - code: field => { + code: (field) => { return { ...formatBaseSchema(field), type: String }; }, - boolean: field => { + boolean: (field) => { return { ...formatBaseSchema(field), type: Boolean }; }, - date: field => { + date: (field) => { return { ...formatBaseSchema(field), - type: Date - } + type: Date, + }; }, - relationship: field => { + relationship: (field) => { const schema = { ...formatBaseSchema(field), type: mongoose.Schema.Types.ObjectId, @@ -43,7 +43,7 @@ const fieldToSchemaMap = { }; return field.hasMany ? [schema] : schema; }, - repeater: field => { + repeater: (field) => { const schema = {}; if (field.id === false) { schema._id = false; @@ -53,7 +53,7 @@ const fieldToSchemaMap = { }); return [schema]; }, - enum: field => { + enum: (field) => { return { ...formatBaseSchema(field), type: String, @@ -73,7 +73,7 @@ const fieldToSchemaMap = { return { localized: field.localized || false, type: [schema], - } + }; }, };