chore(eslint): use eslint for a more consistent style throughout the code

This commit is contained in:
Mahdi Dibaiee 2016-06-30 18:28:02 +04:30
parent 3d78b467ac
commit 07af23cb5a
9 changed files with 137 additions and 140 deletions

3
.eslintrc Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "pichak"
}

View File

@ -1,14 +1,15 @@
{ {
"name": "hapi-sequelize-crud", "name": "hapi-sequelize-crud",
"version": "2.0.11", "version": "2.1.0",
"description": "Hapi plugin that automatically generates RESTful API for CRUD", "description": "Hapi plugin that automatically generates RESTful API for CRUD",
"main": "build/index.js", "main": "build/index.js",
"config": { "config": {
"ghooks": { "ghooks": {
"pre-commit": "grunt" "pre-commit": "npm run lint && grunt"
} }
}, },
"scripts": { "scripts": {
"lint": "eslint src test",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
@ -21,6 +22,8 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"babel": "5.8.3", "babel": "5.8.3",
"eslint": "2.10.2",
"eslint-config-pichak": "1.0.1",
"ghooks": "1.0.3", "ghooks": "1.0.3",
"grunt": "0.4.5", "grunt": "0.4.5",
"grunt-babel": "5.0.3", "grunt-babel": "5.0.3",

View File

@ -1,6 +1,4 @@
import joi from 'joi';
import error from '../error'; import error from '../error';
import { capitalize } from 'lodash/string';
import { getMethod } from '../utils'; import { getMethod } from '../utils';
let prefix; let prefix;
@ -18,14 +16,14 @@ export default (server, a, b, names, options) => {
async handler(request, reply) { async handler(request, reply) {
let instanceb = await b.findOne({ let instanceb = await b.findOne({
where: { where: {
id: request.params.bid id: request.params.bid,
} },
}); });
let instancea = await a.findOne({ let instancea = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const fn = getMethod(instancea, names.b, false, 'add') || const fn = getMethod(instancea, names.b, false, 'add') ||
@ -35,6 +33,6 @@ export default (server, a, b, names, options) => {
reply([instancea, instanceb]); reply([instancea, instanceb]);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };

View File

@ -17,7 +17,7 @@ export default (server, a, b, names, options) => {
destroy(server, a, b, names); destroy(server, a, b, names);
destroyScope(server, a, b, names); destroyScope(server, a, b, names);
update(server, a, b, names); update(server, a, b, names);
} };
export const get = (server, a, b, names) => { export const get = (server, a, b, names) => {
server.route({ server.route({
@ -30,21 +30,21 @@ export const get = (server, a, b, names) => {
const base = a.findOne({ const base = a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b); const method = getMethod(base, names.b);
const list = await method({ where: { const list = await method({ where: {
id: request.params.bid id: request.params.bid,
}, include }); }, include });
reply(list); reply(list);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const list = (server, a, b, names) => { export const list = (server, a, b, names) => {
server.route({ server.route({
@ -58,8 +58,8 @@ export const list = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b); const method = getMethod(base, names.b);
@ -68,9 +68,9 @@ export const list = (server, a, b, names) => {
reply(list); reply(list);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const scope = (server, a, b, names) => { export const scope = (server, a, b, names) => {
let scopes = Object.keys(b.options.scopes); let scopes = Object.keys(b.options.scopes);
@ -86,15 +86,15 @@ export const scope = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b); const method = getMethod(base, names.b);
const list = await method({ const list = await method({
scope: request.params.scope, scope: request.params.scope,
where, where,
include include,
}); });
reply(list); reply(list);
@ -104,17 +104,17 @@ export const scope = (server, a, b, names) => {
validate: { validate: {
params: joi.object().keys({ params: joi.object().keys({
scope: joi.string().valid(...scopes), scope: joi.string().valid(...scopes),
aid: joi.number().integer().required() aid: joi.number().integer().required(),
}) }),
} },
}, defaultConfig) }, defaultConfig),
}) });
} };
export const scopeScope = (server, a, b, names) => { export const scopeScope = (server, a, b, names) => {
let scopes = { let scopes = {
a: Object.keys(a.options.scopes), a: Object.keys(a.options.scopes),
b: Object.keys(b.options.scopes) b: Object.keys(b.options.scopes),
}; };
server.route({ server.route({
@ -129,9 +129,9 @@ export const scopeScope = (server, a, b, names) => {
let list = await b.scope(request.params.scopeb).findAll({ let list = await b.scope(request.params.scopeb).findAll({
where, where,
include: include.concat({ include: include.concat({
model: a.scope(request.params.scopea) model: a.scope(request.params.scopea),
}) }),
}) });
reply(list); reply(list);
}, },
@ -140,12 +140,12 @@ export const scopeScope = (server, a, b, names) => {
validate: { validate: {
params: joi.object().keys({ params: joi.object().keys({
scopea: joi.string().valid(...scopes.a), scopea: joi.string().valid(...scopes.a),
scopeb: joi.string().valid(...scopes.b) scopeb: joi.string().valid(...scopes.b),
}) }),
} },
}, defaultConfig) }, defaultConfig),
}) });
} };
export const destroy = (server, a, b, names) => { export const destroy = (server, a, b, names) => {
server.route({ server.route({
@ -159,8 +159,8 @@ export const destroy = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b, true, 'get'); const method = getMethod(base, names.b, true, 'get');
@ -170,9 +170,9 @@ export const destroy = (server, a, b, names) => {
)); ));
reply(list); reply(list);
} },
}) });
} };
export const destroyScope = (server, a, b, names) => { export const destroyScope = (server, a, b, names) => {
let scopes = Object.keys(b.options.scopes); let scopes = Object.keys(b.options.scopes);
@ -188,8 +188,8 @@ export const destroyScope = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b, true, 'get'); const method = getMethod(base, names.b, true, 'get');
@ -197,10 +197,10 @@ export const destroyScope = (server, a, b, names) => {
const list = await method({ const list = await method({
scope: request.params.scope, scope: request.params.scope,
where, where,
include include,
}); });
await* list.map(instance => instance.destroy()); await Promise.all(list.map(instance => instance.destroy()));
reply(list); reply(list);
}, },
@ -209,12 +209,12 @@ export const destroyScope = (server, a, b, names) => {
validate: { validate: {
params: joi.object().keys({ params: joi.object().keys({
scope: joi.string().valid(...scopes), scope: joi.string().valid(...scopes),
aid: joi.number().integer().required() aid: joi.number().integer().required(),
}) }),
} },
}, defaultConfig) }, defaultConfig),
}); });
} };
export const update = (server, a, b, names) => { export const update = (server, a, b, names) => {
server.route({ server.route({
@ -228,18 +228,18 @@ export const update = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b); const method = getMethod(base, names.b);
const list = await method({ where, include }); const list = await method({ where, include });
await* list.map(instance => instance.update(request.payload)); await Promise.all(list.map(instance => instance.update(request.payload)));
reply(list); reply(list);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };

View File

@ -1,6 +1,4 @@
import joi from 'joi';
import error from '../error'; import error from '../error';
import _ from 'lodash';
import { parseInclude, parseWhere, getMethod } from '../utils'; import { parseInclude, parseWhere, getMethod } from '../utils';
let prefix; let prefix;
@ -14,7 +12,7 @@ export default (server, a, b, names, options) => {
create(server, a, b, names); create(server, a, b, names);
destroy(server, a, b, names); destroy(server, a, b, names);
update(server, a, b, names); update(server, a, b, names);
} };
export const get = (server, a, b, names) => { export const get = (server, a, b, names) => {
server.route({ server.route({
@ -28,8 +26,8 @@ export const get = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b, false); const method = getMethod(base, names.b, false);
@ -42,9 +40,9 @@ export const get = (server, a, b, names) => {
} }
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const create = (server, a, b, names) => { export const create = (server, a, b, names) => {
server.route({ server.route({
@ -55,8 +53,8 @@ export const create = (server, a, b, names) => {
async handler(request, reply) { async handler(request, reply) {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.id id: request.params.id,
} },
}); });
const method = getMethod(base, names.b, false, 'create'); const method = getMethod(base, names.b, false, 'create');
@ -65,9 +63,9 @@ export const create = (server, a, b, names) => {
reply(instance); reply(instance);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const destroy = (server, a, b, names) => { export const destroy = (server, a, b, names) => {
server.route({ server.route({
@ -81,8 +79,8 @@ export const destroy = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b, false, 'get'); const method = getMethod(base, names.b, false, 'get');
@ -92,9 +90,9 @@ export const destroy = (server, a, b, names) => {
reply(instance); reply(instance);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const update = (server, a, b, names) => { export const update = (server, a, b, names) => {
server.route({ server.route({
@ -108,8 +106,8 @@ export const update = (server, a, b, names) => {
const base = await a.findOne({ const base = await a.findOne({
where: { where: {
id: request.params.aid id: request.params.aid,
} },
}); });
const method = getMethod(base, names.b, false); const method = getMethod(base, names.b, false);
@ -120,6 +118,6 @@ export const update = (server, a, b, names) => {
reply(instance); reply(instance);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };

View File

@ -19,7 +19,7 @@ export default (server, model, options) => {
destroyAll(server, model); destroyAll(server, model);
destroyScope(server, model); destroyScope(server, model);
update(server, model); update(server, model);
} };
export const list = (server, model) => { export const list = (server, model) => {
server.route({ server.route({
@ -32,15 +32,15 @@ export const list = (server, model) => {
const where = parseWhere(request); const where = parseWhere(request);
const list = await model.findAll({ const list = await model.findAll({
where, include where, include,
}); });
reply(list); reply(list);
}, },
config: defaultConfig config: defaultConfig,
}); });
} };
export const get = (server, model) => { export const get = (server, model) => {
server.route({ server.route({
@ -63,12 +63,12 @@ export const get = (server, model) => {
config: _.defaultsDeep({ config: _.defaultsDeep({
validate: { validate: {
params: joi.object().keys({ params: joi.object().keys({
id: joi.any() id: joi.any(),
}) }),
} },
}, defaultConfig) }, defaultConfig),
}) });
} };
export const scope = (server, model) => { export const scope = (server, model) => {
let scopes = Object.keys(model.options.scopes); let scopes = Object.keys(model.options.scopes);
@ -89,12 +89,12 @@ export const scope = (server, model) => {
config: _.defaultsDeep({ config: _.defaultsDeep({
validate: { validate: {
params: joi.object().keys({ params: joi.object().keys({
scope: joi.string().valid(...scopes) scope: joi.string().valid(...scopes),
}) }),
} },
}, defaultConfig) }, defaultConfig),
}); });
} };
export const create = (server, model) => { export const create = (server, model) => {
server.route({ server.route({
@ -108,9 +108,9 @@ export const create = (server, model) => {
reply(instance); reply(instance);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const destroy = (server, model) => { export const destroy = (server, model) => {
server.route({ server.route({
@ -119,20 +119,19 @@ export const destroy = (server, model) => {
@error @error
async handler(request, reply) { async handler(request, reply) {
const include = parseInclude(request);
const where = parseWhere(request); const where = parseWhere(request);
if (request.params.id) where.id = request.params.id; if (request.params.id) where.id = request.params.id;
const list = await model.findAll({ where }); const list = await model.findAll({ where });
await* list.map(instance => instance.destroy()); await Promise.all(list.map(instance => instance.destroy()));
reply(list.length === 1 ? list[0] : list); reply(list.length === 1 ? list[0] : list);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const destroyAll = (server, model) => { export const destroyAll = (server, model) => {
server.route({ server.route({
@ -141,19 +140,18 @@ export const destroyAll = (server, model) => {
@error @error
async handler(request, reply) { async handler(request, reply) {
const include = parseInclude(request);
const where = parseWhere(request); const where = parseWhere(request);
const list = await model.findAll({ where }); const list = await model.findAll({ where });
await* list.map(instance => instance.destroy()); await Promise.all(list.map(instance => instance.destroy()));
reply(list.length === 1 ? list[0] : list); reply(list.length === 1 ? list[0] : list);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
export const destroyScope = (server, model) => { export const destroyScope = (server, model) => {
let scopes = Object.keys(model.options.scopes); let scopes = Object.keys(model.options.scopes);
@ -169,19 +167,19 @@ export const destroyScope = (server, model) => {
let list = await model.scope(request.params.scope).findAll({ include, where }); let list = await model.scope(request.params.scope).findAll({ include, where });
await* list.map(instance => instance.destroy()); await Promise.all(list.map(instance => instance.destroy()));
reply(list); reply(list);
}, },
config: _.defaultsDeep({ config: _.defaultsDeep({
validate: { validate: {
params: joi.object().keys({ params: joi.object().keys({
scope: joi.string().valid(...scopes) scope: joi.string().valid(...scopes),
}) }),
} },
}, defaultConfig) }, defaultConfig),
}); });
} };
export const update = (server, model) => { export const update = (server, model) => {
server.route({ server.route({
@ -193,8 +191,8 @@ export const update = (server, model) => {
const {id} = request.params; const {id} = request.params;
const instance = await model.findOne({ const instance = await model.findOne({
where: { where: {
id id,
} },
}); });
if (!instance) return void reply(notFound(`${id} not found.`)); if (!instance) return void reply(notFound(`${id} not found.`));
@ -204,9 +202,9 @@ export const update = (server, model) => {
reply(instance); reply(instance);
}, },
config: defaultConfig config: defaultConfig,
}) });
} };
import * as associations from './associations/index'; import * as associations from './associations/index';
export { associations }; export { associations };

View File

@ -8,7 +8,7 @@ export default (target, key, descriptor) => {
console.error(e); console.error(e);
reply(e); reply(e);
} }
} };
return descriptor; return descriptor;
} };

View File

@ -23,7 +23,7 @@ const register = (server, options = {}, next) => {
server.ext({ server.ext({
type: 'onRequest', type: 'onRequest',
method: onRequest method: onRequest,
}); });
for (let modelName of Object.keys(models)) { for (let modelName of Object.keys(models)) {
@ -39,26 +39,23 @@ const register = (server, options = {}, next) => {
for (let key of Object.keys(model.associations)) { for (let key of Object.keys(model.associations)) {
let association = model.associations[key]; let association = model.associations[key];
let { associationType, source, target } = association; let { source, target } = association;
// console.dir(association, null, { depth: null });
let sourceName = source.options.name; let sourceName = source.options.name;
let targetName = target.options.name;
const names = (rev) => { const names = (rev) => {
const arr = [{ const arr = [{
plural: sourceName.plural.toLowerCase(), plural: sourceName.plural.toLowerCase(),
singular: sourceName.singular.toLowerCase(), singular: sourceName.singular.toLowerCase(),
original: sourceName original: sourceName,
}, { }, {
plural: association.options.name.plural.toLowerCase(), plural: association.options.name.plural.toLowerCase(),
singular: association.options.name.singular.toLowerCase(), singular: association.options.name.singular.toLowerCase(),
original: association.options.name original: association.options.name,
}]; }];
return rev ? { b: arr[0], a: arr[1] } : { a: arr[0], b: arr[1] }; return rev ? { b: arr[0], a: arr[1] } : { a: arr[0], b: arr[1] };
} };
let targetAssociations = target.associations[sourceName.plural] || target.associations[sourceName.singular]; let targetAssociations = target.associations[sourceName.plural] || target.associations[sourceName.singular];
let sourceType = association.associationType, let sourceType = association.associationType,
@ -94,10 +91,10 @@ const register = (server, options = {}, next) => {
} }
next(); next();
} };
register.attributes = { register.attributes = {
pkg: require('../package.json') pkg: require('../package.json'),
} };
export { register }; export { register };

View File

@ -13,7 +13,7 @@ export const parseInclude = request => {
return a; return a;
}).filter(a => a); }).filter(a => a);
} };
export const parseWhere = request => { export const parseWhere = request => {
const where = omit(request.query, 'include'); const where = omit(request.query, 'include');
@ -27,7 +27,7 @@ export const parseWhere = request => {
} }
return where; return where;
} };
export const getMethod = (model, association, plural = true, method = 'get') => { export const getMethod = (model, association, plural = true, method = 'get') => {
const a = plural ? association.original.plural : association.original.singular; const a = plural ? association.original.plural : association.original.singular;
@ -36,4 +36,4 @@ export const getMethod = (model, association, plural = true, method = 'get') =>
if (fn) return fn.bind(model); if (fn) return fn.bind(model);
return false; return false;
} };