From de550f31dfb09d37ff76d3f7f7685a4caae12e0d Mon Sep 17 00:00:00 2001 From: Adrian Obelmejias Date: Tue, 30 Aug 2016 16:58:35 -0400 Subject: [PATCH] Use standard for linting rules --- .eslintrc.json | 12 +----------- README.md | 3 +++ package.json | 4 +++- src/middleware/index.js | 2 +- src/middleware/validators.js | 2 +- src/models/users.js | 6 +++--- src/modules/auth/controller.js | 2 +- src/modules/index.js | 2 +- src/modules/users/controller.js | 12 ++++++------ src/utils/auth.js | 2 +- test/utils.js | 4 ++-- 11 files changed, 23 insertions(+), 28 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 8af25b1..34ae792 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,16 +1,6 @@ { "parser": "babel-eslint", - "extends": "airbnb", - "plugins": [ - "react" - ], - "rules": { - "semi": [2, "never"], - "no-param-reassign": [2, { "props": false }], - "strict": 0, - "comma-dangle": [2, "never"], - "max-len": [2, 80, 2, { "ignoreComments": true }] - }, + "extends": "standard", "env": { "node": true, "mocha": true diff --git a/README.md b/README.md index 73666e1..9093e66 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ #koa2-api-boilerplate +[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com) + Boilerplate for building APIs with [koa2](https://github.com/koajs/koa/tree/v2.x) and mongodb. This project covers basic necessities of most APIs. @@ -6,6 +8,7 @@ This project covers basic necessities of most APIs. * Database (mongoose) * Testing (mocha) * Doc generation with apidoc +* linting using standard ##Requirements * node __^4.0.0__ diff --git a/package.json b/package.json index 284aa66..2694b93 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,9 @@ "babel-register": "^6.5.1", "chai": "^3.5.0", "eslint": "^3.4.0", - "eslint-config-airbnb": "^10.0.1", + "eslint-config-standard": "^6.0.0", + "eslint-plugin-promise": "^2.0.1", + "eslint-plugin-standard": "^2.0.0", "mocha": "^3.0.2", "nodemon": "^1.8.1", "supertest": "^2.0.0" diff --git a/src/middleware/index.js b/src/middleware/index.js index 4f26450..2febd47 100644 --- a/src/middleware/index.js +++ b/src/middleware/index.js @@ -1,4 +1,4 @@ -export function errorMiddleware() { +export function errorMiddleware () { return async (ctx, next) => { try { await next() diff --git a/src/middleware/validators.js b/src/middleware/validators.js index 9672e34..5257b33 100644 --- a/src/middleware/validators.js +++ b/src/middleware/validators.js @@ -3,7 +3,7 @@ import config from '../../config' import { getToken } from '../utils/auth' import { verify } from 'jsonwebtoken' -export async function ensureUser(ctx, next) { +export async function ensureUser (ctx, next) { const token = getToken(ctx) if (!token) { diff --git a/src/models/users.js b/src/models/users.js index 51b1ed3..56f808a 100644 --- a/src/models/users.js +++ b/src/models/users.js @@ -10,7 +10,7 @@ const User = new mongoose.Schema({ password: { type: String, required: true } }) -User.pre('save', function preSave(next) { +User.pre('save', function preSave (next) { const user = this if (!user.isModified('password')) { @@ -35,7 +35,7 @@ User.pre('save', function preSave(next) { .catch(err => next(err)) }) -User.methods.validatePassword = function validatePassword(password) { +User.methods.validatePassword = function validatePassword (password) { const user = this return new Promise((resolve, reject) => { @@ -47,7 +47,7 @@ User.methods.validatePassword = function validatePassword(password) { }) } -User.methods.generateToken = function generateToken() { +User.methods.generateToken = function generateToken () { const user = this return jwt.sign({ id: user.id }, config.token) diff --git a/src/modules/auth/controller.js b/src/modules/auth/controller.js index 17ce132..728225a 100644 --- a/src/modules/auth/controller.js +++ b/src/modules/auth/controller.js @@ -50,7 +50,7 @@ import passport from 'koa-passport' * } */ -export async function authUser(ctx, next) { +export async function authUser (ctx, next) { return passport.authenticate('local', (user) => { if (!user) { ctx.throw(401) diff --git a/src/modules/index.js b/src/modules/index.js index 057379c..24d13b7 100644 --- a/src/modules/index.js +++ b/src/modules/index.js @@ -1,7 +1,7 @@ import glob from 'glob' import Router from 'koa-router' -exports = module.exports = function initModules(app) { +exports = module.exports = function initModules (app) { glob(`${__dirname}/*`, { ignore: '**/index.js' }, (err, matches) => { if (err) { throw err } diff --git a/src/modules/users/controller.js b/src/modules/users/controller.js index dd2da49..22d3593 100644 --- a/src/modules/users/controller.js +++ b/src/modules/users/controller.js @@ -38,7 +38,7 @@ import User from '../../models/users' * "error": "Unprocessable Entity" * } */ -export async function createUser(ctx) { +export async function createUser (ctx) { const user = new User(ctx.request.body.user) try { await user.save() @@ -84,7 +84,7 @@ export async function createUser(ctx) { * * @apiUse TokenError */ -export async function getUsers(ctx) { +export async function getUsers (ctx) { const users = await User.find({}, '-password') ctx.body = { users } } @@ -116,7 +116,7 @@ export async function getUsers(ctx) { * * @apiUse TokenError */ -export async function getUser(ctx, next) { +export async function getUser (ctx, next) { try { const user = await User.findById(ctx.params.id, '-password') if (!user) { @@ -134,7 +134,7 @@ export async function getUser(ctx, next) { ctx.throw(500) } - if(next) return next() + if (next) { return next() } } /** @@ -177,7 +177,7 @@ export async function getUser(ctx, next) { * * @apiUse TokenError */ -export async function updateUser(ctx) { +export async function updateUser (ctx) { const user = ctx.body.user Object.assign(user, ctx.request.body.user) @@ -210,7 +210,7 @@ export async function updateUser(ctx) { * @apiUse TokenError */ -export async function deleteUser(ctx) { +export async function deleteUser (ctx) { const user = ctx.body.user await user.remove() diff --git a/src/utils/auth.js b/src/utils/auth.js index c80e7a2..288098b 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -1,4 +1,4 @@ -export function getToken(ctx) { +export function getToken (ctx) { const header = ctx.request.header.authorization if (!header) { return null diff --git a/test/utils.js b/test/utils.js index f02445c..e036fac 100644 --- a/test/utils.js +++ b/test/utils.js @@ -1,6 +1,6 @@ import mongoose from 'mongoose' -export function cleanDb() { +export function cleanDb () { for (const collection in mongoose.connection.collections) { if (mongoose.connection.collections.hasOwnProperty(collection)) { mongoose.connection.collections[collection].remove() @@ -8,7 +8,7 @@ export function cleanDb() { } } -export function authUser(agent, callback) { +export function authUser (agent, callback) { agent .post('/users') .set('Accept', 'application/json')