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

View File

@ -1,6 +1,4 @@
import joi from 'joi';
import error from '../error';
import { capitalize } from 'lodash/string';
import { getMethod } from '../utils';
let prefix;
@ -18,14 +16,14 @@ export default (server, a, b, names, options) => {
async handler(request, reply) {
let instanceb = await b.findOne({
where: {
id: request.params.bid
}
id: request.params.bid,
},
});
let instancea = await a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const fn = getMethod(instancea, names.b, false, 'add') ||
@ -35,6 +33,6 @@ export default (server, a, b, names, options) => {
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);
destroyScope(server, a, b, names);
update(server, a, b, names);
}
};
export const get = (server, a, b, names) => {
server.route({
@ -30,21 +30,21 @@ export const get = (server, a, b, names) => {
const base = a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const method = getMethod(base, names.b);
const list = await method({ where: {
id: request.params.bid
id: request.params.bid,
}, include });
reply(list);
},
config: defaultConfig
})
}
config: defaultConfig,
});
};
export const list = (server, a, b, names) => {
server.route({
@ -58,8 +58,8 @@ export const list = (server, a, b, names) => {
const base = await a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const method = getMethod(base, names.b);
@ -68,9 +68,9 @@ export const list = (server, a, b, names) => {
reply(list);
},
config: defaultConfig
})
}
config: defaultConfig,
});
};
export const scope = (server, a, b, names) => {
let scopes = Object.keys(b.options.scopes);
@ -86,15 +86,15 @@ export const scope = (server, a, b, names) => {
const base = await a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const method = getMethod(base, names.b);
const list = await method({
scope: request.params.scope,
where,
include
include,
});
reply(list);
@ -104,17 +104,17 @@ export const scope = (server, a, b, names) => {
validate: {
params: joi.object().keys({
scope: joi.string().valid(...scopes),
aid: joi.number().integer().required()
})
}
}, defaultConfig)
})
}
aid: joi.number().integer().required(),
}),
},
}, defaultConfig),
});
};
export const scopeScope = (server, a, b, names) => {
let scopes = {
a: Object.keys(a.options.scopes),
b: Object.keys(b.options.scopes)
b: Object.keys(b.options.scopes),
};
server.route({
@ -129,9 +129,9 @@ export const scopeScope = (server, a, b, names) => {
let list = await b.scope(request.params.scopeb).findAll({
where,
include: include.concat({
model: a.scope(request.params.scopea)
})
})
model: a.scope(request.params.scopea),
}),
});
reply(list);
},
@ -140,12 +140,12 @@ export const scopeScope = (server, a, b, names) => {
validate: {
params: joi.object().keys({
scopea: joi.string().valid(...scopes.a),
scopeb: joi.string().valid(...scopes.b)
})
}
}, defaultConfig)
})
}
scopeb: joi.string().valid(...scopes.b),
}),
},
}, defaultConfig),
});
};
export const destroy = (server, a, b, names) => {
server.route({
@ -159,8 +159,8 @@ export const destroy = (server, a, b, names) => {
const base = await a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const method = getMethod(base, names.b, true, 'get');
@ -170,9 +170,9 @@ export const destroy = (server, a, b, names) => {
));
reply(list);
}
})
}
},
});
};
export const destroyScope = (server, a, b, names) => {
let scopes = Object.keys(b.options.scopes);
@ -188,8 +188,8 @@ export const destroyScope = (server, a, b, names) => {
const base = await a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const method = getMethod(base, names.b, true, 'get');
@ -197,10 +197,10 @@ export const destroyScope = (server, a, b, names) => {
const list = await method({
scope: request.params.scope,
where,
include
include,
});
await* list.map(instance => instance.destroy());
await Promise.all(list.map(instance => instance.destroy()));
reply(list);
},
@ -209,12 +209,12 @@ export const destroyScope = (server, a, b, names) => {
validate: {
params: joi.object().keys({
scope: joi.string().valid(...scopes),
aid: joi.number().integer().required()
})
}
}, defaultConfig)
aid: joi.number().integer().required(),
}),
},
}, defaultConfig),
});
}
};
export const update = (server, a, b, names) => {
server.route({
@ -228,18 +228,18 @@ export const update = (server, a, b, names) => {
const base = await a.findOne({
where: {
id: request.params.aid
}
id: request.params.aid,
},
});
const method = getMethod(base, names.b);
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);
},
config: defaultConfig
})
}
config: defaultConfig,
});
};

View File

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

View File

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

View File

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

View File

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

View File

@ -13,7 +13,7 @@ export const parseInclude = request => {
return a;
}).filter(a => a);
}
};
export const parseWhere = request => {
const where = omit(request.query, 'include');
@ -27,7 +27,7 @@ export const parseWhere = request => {
}
return where;
}
};
export const getMethod = (model, association, plural = true, method = 'get') => {
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);
return false;
}
};