eslint fixes and consistency in request handlers
This commit is contained in:
2
index.js
2
index.js
@@ -1,5 +1,5 @@
|
||||
require('@babel/register')({
|
||||
ignore: [/(node_modules)/]
|
||||
ignore: [/(node_modules)/],
|
||||
});
|
||||
|
||||
module.exports = require('./src');
|
||||
|
||||
@@ -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);
|
||||
})
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 }),
|
||||
|
||||
@@ -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' });
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -9,5 +9,5 @@ export {
|
||||
destroy,
|
||||
findOne,
|
||||
query,
|
||||
update
|
||||
}
|
||||
update,
|
||||
};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 }),
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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],
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user