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')({
|
require('@babel/register')({
|
||||||
ignore: [/(node_modules)/]
|
ignore: [/(node_modules)/],
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = require('./src');
|
module.exports = require('./src');
|
||||||
|
|||||||
@@ -1,22 +1,23 @@
|
|||||||
import httpStatus from 'http-status';
|
import httpStatus from 'http-status';
|
||||||
import { findOne } from '../mongoose/resolvers';
|
import { findOne } from '../mongoose/resolvers';
|
||||||
import { createAutopopulateOptions } from '../mongoose/createAutopopulateOptions';
|
import { createAutopopulateOptions } from '../mongoose/createAutopopulateOptions';
|
||||||
|
import { NotFound } from '../errors';
|
||||||
|
|
||||||
export const upsert = (req, res) => {
|
export const upsert = (req, res) => {
|
||||||
if (!req.model.schema.tree[req.params.slug]) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
req.model.findOne({}, (findErr, doc) => {
|
req.model.findOne({}, (findErr, doc) => {
|
||||||
let global = {};
|
let global = {};
|
||||||
|
|
||||||
if (!doc) {
|
if (!doc) {
|
||||||
if (req.params.slug) {
|
if (req.params.slug) {
|
||||||
global[req.params.slug] = req.body;
|
global[req.params.slug] = req.body;
|
||||||
} else {
|
} else {
|
||||||
global = req.body;
|
global = req.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
return req.model.create(global, (createErr, result) => {
|
return req.model.create(global, (createErr, result) => {
|
||||||
if (createErr) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: createErr });
|
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]) {
|
if (!doc[req.params.slug]) {
|
||||||
// eslint-disable-next-line no-param-reassign
|
Object.assign(doc[req.params.slug], {});
|
||||||
doc[req.params.slug] = {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(req.body).forEach((e) => {
|
Object.assign(doc[req.params.slug], req.body);
|
||||||
// eslint-disable-next-line no-param-reassign
|
|
||||||
doc[req.params.slug][e] = req.body[e];
|
|
||||||
});
|
|
||||||
|
|
||||||
return doc.save((err) => {
|
return doc.save((err) => {
|
||||||
if (err) return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: 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))
|
findOne(query, createAutopopulateOptions(query))
|
||||||
.then((doc) => {
|
.then((doc) => {
|
||||||
const globals = { ...doc };
|
const globals = { ...doc };
|
||||||
|
// eslint-disable-next-line no-underscore-dangle
|
||||||
delete globals._id;
|
delete globals._id;
|
||||||
delete globals.id;
|
delete globals.id;
|
||||||
|
// eslint-disable-next-line no-underscore-dangle
|
||||||
delete globals.__v;
|
delete globals.__v;
|
||||||
|
|
||||||
if (globals[req.params.key]) {
|
if (globals[req.params.key]) {
|
||||||
return res.json(globals[req.params.key]);
|
return res.json(globals[req.params.key]);
|
||||||
} if (req.params.key) {
|
} if (req.params.key) {
|
||||||
return res.status(httpStatus.NOT_FOUND)
|
return res.status(httpStatus.NOT_FOUND).json(new NotFound());
|
||||||
.json({ error: 'not found' });
|
|
||||||
}
|
}
|
||||||
return res.json(globals);
|
return res.json(globals);
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -10,6 +10,6 @@ const registerExpressMiddleware = ({ app, config, router }) => {
|
|||||||
app.use(bodyParser.urlencoded({ extended: true }));
|
app.use(bodyParser.urlencoded({ extended: true }));
|
||||||
app.use(localizationMiddleware(config.localization));
|
app.use(localizationMiddleware(config.localization));
|
||||||
app.use(router);
|
app.use(router);
|
||||||
}
|
};
|
||||||
|
|
||||||
export default registerExpressMiddleware;
|
export default registerExpressMiddleware;
|
||||||
|
|||||||
@@ -2,10 +2,13 @@ import httpStatus from 'http-status';
|
|||||||
|
|
||||||
const create = (req, res) => {
|
const create = (req, res) => {
|
||||||
req.model.create(req.body, (err, result) => {
|
req.model.create(req.body, (err, result) => {
|
||||||
if (err)
|
if (err) {
|
||||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
|
res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.json({ error: err });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(httpStatus.CREATED)
|
res.status(httpStatus.CREATED)
|
||||||
.json({
|
.json({
|
||||||
message: 'success',
|
message: 'success',
|
||||||
result: result.toJSON({ virtuals: true }),
|
result: result.toJSON({ virtuals: true }),
|
||||||
|
|||||||
@@ -1,14 +1,22 @@
|
|||||||
import httpStatus from 'http-status';
|
import httpStatus from 'http-status';
|
||||||
|
import { NotFound } from '../../errors';
|
||||||
|
|
||||||
const destroy = (req, res) => {
|
const destroy = (req, res) => {
|
||||||
req.model.findOneAndDelete({ _id: req.params.id }, (err, doc) => {
|
req.model.findOneAndDelete({ _id: req.params.id }, (err, doc) => {
|
||||||
if (err)
|
if (err) {
|
||||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
|
res.status(httpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.json({ error: err });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!doc)
|
if (!doc) {
|
||||||
return res.status(httpStatus.NOT_FOUND).send('Not Found');
|
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,
|
destroy,
|
||||||
findOne,
|
findOne,
|
||||||
query,
|
query,
|
||||||
update
|
update,
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ import httpStatus from 'http-status';
|
|||||||
const query = (req, res) => {
|
const query = (req, res) => {
|
||||||
req.model.paginate(req.model.apiQuery(req.query, req.locale), { ...req.query }, (err, result) => {
|
req.model.paginate(req.model.apiQuery(req.query, req.locale), { ...req.query }, (err, result) => {
|
||||||
if (err) {
|
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,
|
...result,
|
||||||
docs: result.docs.map((doc) => {
|
docs: result.docs.map((doc) => {
|
||||||
if (req.locale && doc.setLocale) {
|
if (req.locale && doc.setLocale) {
|
||||||
|
|||||||
@@ -1,21 +1,24 @@
|
|||||||
import httpStatus from 'http-status';
|
import httpStatus from 'http-status';
|
||||||
|
import { NotFound } from '../../errors';
|
||||||
|
|
||||||
const update = (req, res) => {
|
const update = (req, res) => {
|
||||||
req.model.findOne({ _id: req.params.id }, '', {}, (err, doc) => {
|
req.model.findOne({ _id: req.params.id }, '', {}, (err, doc) => {
|
||||||
if (!doc)
|
if (!doc) {
|
||||||
return res.status(httpStatus.NOT_FOUND).send('Not Found');
|
res.status(httpStatus.NOT_FOUND).json(new NotFound());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Object.keys(req.body).forEach(e => {
|
Object.assign(doc, req.body);
|
||||||
doc[e] = req.body[e];
|
|
||||||
});
|
|
||||||
|
|
||||||
doc.save((err) => {
|
doc.save((saveError) => {
|
||||||
if (err)
|
if (saveError) {
|
||||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
|
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: saveError });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
return res.json({
|
res.status(httpStatus.OK).json({
|
||||||
message: 'success',
|
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 }) => {
|
const find = ({ Model, locale, fallback }) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
Model.findOne(null, (err, doc) => {
|
Model.findOne(null, (err, doc) => {
|
||||||
if (err || !doc) {
|
if (err || !doc) {
|
||||||
console.log('ok');
|
|
||||||
reject(new NotFound());
|
reject(new NotFound());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = doc;
|
let result = doc;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import mongoose from 'mongoose';
|
import mongoose from 'mongoose';
|
||||||
|
|
||||||
const formatBaseSchema = field => {
|
const formatBaseSchema = (field) => {
|
||||||
return {
|
return {
|
||||||
hide: field.hide || false,
|
hide: field.hide || false,
|
||||||
localized: field.localized || false,
|
localized: field.localized || false,
|
||||||
@@ -10,31 +10,31 @@ const formatBaseSchema = field => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const fieldToSchemaMap = {
|
const fieldToSchemaMap = {
|
||||||
number: field => {
|
number: (field) => {
|
||||||
return { ...formatBaseSchema(field), type: Number };
|
return { ...formatBaseSchema(field), type: Number };
|
||||||
},
|
},
|
||||||
input: field => {
|
input: (field) => {
|
||||||
return { ...formatBaseSchema(field), type: String };
|
return { ...formatBaseSchema(field), type: String };
|
||||||
},
|
},
|
||||||
textarea: field => {
|
textarea: (field) => {
|
||||||
return { ...formatBaseSchema(field), type: String };
|
return { ...formatBaseSchema(field), type: String };
|
||||||
},
|
},
|
||||||
WYSIWYG: field => {
|
WYSIWYG: (field) => {
|
||||||
return { ...formatBaseSchema(field), type: String };
|
return { ...formatBaseSchema(field), type: String };
|
||||||
},
|
},
|
||||||
code: field => {
|
code: (field) => {
|
||||||
return { ...formatBaseSchema(field), type: String };
|
return { ...formatBaseSchema(field), type: String };
|
||||||
},
|
},
|
||||||
boolean: field => {
|
boolean: (field) => {
|
||||||
return { ...formatBaseSchema(field), type: Boolean };
|
return { ...formatBaseSchema(field), type: Boolean };
|
||||||
},
|
},
|
||||||
date: field => {
|
date: (field) => {
|
||||||
return {
|
return {
|
||||||
...formatBaseSchema(field),
|
...formatBaseSchema(field),
|
||||||
type: Date
|
type: Date,
|
||||||
}
|
};
|
||||||
},
|
},
|
||||||
relationship: field => {
|
relationship: (field) => {
|
||||||
const schema = {
|
const schema = {
|
||||||
...formatBaseSchema(field),
|
...formatBaseSchema(field),
|
||||||
type: mongoose.Schema.Types.ObjectId,
|
type: mongoose.Schema.Types.ObjectId,
|
||||||
@@ -43,7 +43,7 @@ const fieldToSchemaMap = {
|
|||||||
};
|
};
|
||||||
return field.hasMany ? [schema] : schema;
|
return field.hasMany ? [schema] : schema;
|
||||||
},
|
},
|
||||||
repeater: field => {
|
repeater: (field) => {
|
||||||
const schema = {};
|
const schema = {};
|
||||||
if (field.id === false) {
|
if (field.id === false) {
|
||||||
schema._id = false;
|
schema._id = false;
|
||||||
@@ -53,7 +53,7 @@ const fieldToSchemaMap = {
|
|||||||
});
|
});
|
||||||
return [schema];
|
return [schema];
|
||||||
},
|
},
|
||||||
enum: field => {
|
enum: (field) => {
|
||||||
return {
|
return {
|
||||||
...formatBaseSchema(field),
|
...formatBaseSchema(field),
|
||||||
type: String,
|
type: String,
|
||||||
@@ -73,7 +73,7 @@ const fieldToSchemaMap = {
|
|||||||
return {
|
return {
|
||||||
localized: field.localized || false,
|
localized: field.localized || false,
|
||||||
type: [schema],
|
type: [schema],
|
||||||
}
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user