diff --git a/server/api/__snapshots__/user.test.js.snap b/server/api/__snapshots__/user.test.js.snap index b85f2b67..7bd0ec4e 100644 --- a/server/api/__snapshots__/user.test.js.snap +++ b/server/api/__snapshots__/user.test.js.snap @@ -1,6 +1,7 @@ exports[`test should require authentication 1`] = ` Object { - "message": "Authentication required" + "error": "Authentication required", + "success": false } `; @@ -11,6 +12,7 @@ Object { "id": "86fde1d4-0050-428f-9f0b-0bf77f8bdf61", "name": "User 1", "username": "user1" - } + }, + "success": true } `; diff --git a/server/api/index.js b/server/api/index.js index 1df8e389..cd608430 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -14,6 +14,7 @@ import apiKeys from './apiKeys'; import validation from './middlewares/validation'; import methodOverride from '../middlewares/methodOverride'; import cache from '../middlewares/cache'; +import apiWrapper from './middlewares/apiWrapper'; const api = new Koa(); const router = new Router(); @@ -39,7 +40,10 @@ api.use(async (ctx, next) => { ctx.app.emit('error', err, ctx); } - ctx.body = { message }; + ctx.body = { + success: false, + error: message, + }; } }); @@ -47,6 +51,7 @@ api.use(bodyParser()); api.use(methodOverride()); api.use(cache()); api.use(validation()); +api.use(apiWrapper()); router.use('/', auth.routes()); router.use('/', user.routes()); @@ -59,9 +64,4 @@ router.use('/', apiKeys.routes()); // allow middleware to catch any routes which were not explicitly defined. api.use(router.routes()); -// API 404 handler -api.use(async () => { - throw httpErrors.NotFound(); -}); - export default api; diff --git a/server/api/middlewares/apiWrapper.js b/server/api/middlewares/apiWrapper.js new file mode 100644 index 00000000..be65a62a --- /dev/null +++ b/server/api/middlewares/apiWrapper.js @@ -0,0 +1,12 @@ +export default function apiWrapper(_options) { + return async function apiWrapperMiddleware(ctx, next) { + await next(); + + const success = ctx.status < 400; + + ctx.body = { + ...ctx.body, + success, + }; + }; +} diff --git a/server/index.js b/server/index.js index dbd9cad6..25afafad 100644 --- a/server/index.js +++ b/server/index.js @@ -13,12 +13,14 @@ const app = new Koa(); app.use(compress()); if (process.env.NODE_ENV === 'development') { - const convert = require('koa-convert') + /* eslint-disable global-require */ + const convert = require('koa-convert'); const webpack = require('webpack'); const devMiddleware = require('koa-webpack-dev-middleware'); const hotMiddleware = require('koa-webpack-hot-middleware'); const config = require('../webpack.config.dev'); const compile = webpack(config); + /* eslint-enable global-require */ app.use(convert(devMiddleware(compile, { // display no info to console (only warnings and errors) @@ -43,13 +45,13 @@ if (process.env.NODE_ENV === 'development') { // options for formating the statistics stats: { - colors: true - } + colors: true, + }, }))); app.use(convert(hotMiddleware(compile, { - log: console.log, + log: console.log, // eslint-disable-line path: '/__webpack_hmr', - heartbeat: 10 * 1000 + heartbeat: 10 * 1000, }))); app.use(logger()); } diff --git a/server/presenters.js b/server/presenters.js index f7ae67a4..75ee7dc2 100644 --- a/server/presenters.js +++ b/server/presenters.js @@ -1,20 +1,11 @@ -import Sequelize from 'sequelize'; import _ from 'lodash'; -import { Document, Atlas, User, Revision } from './models'; +import { Document, Atlas, User } from './models'; -export function presentUser(ctx, user) { - ctx.cache.set(user.id, user); +import presentUser from './presenters/user'; - return new Promise(async (resolve, _reject) => { - const data = { - id: user.id, - name: user.name, - username: user.username, - avatarUrl: user.slackData.image_192, - }; - resolve(data); - }); -} +export { + presentUser, +}; export function presentTeam(ctx, team) { ctx.cache.set(team.id, team); diff --git a/server/routes.js b/server/routes.js index 4d5a00e3..d98cf6f7 100644 --- a/server/routes.js +++ b/server/routes.js @@ -9,16 +9,6 @@ import subdomainRedirect from './middlewares/subdomainRedirect'; const koa = new Koa(); const router = new Router(); -// // error handler -// koa.use(async (ctx, next) => { -// try { -// await next(); -// } catch (err) { -// ctx.status = err.status || 500; -// ctx.body = err.message; -// } -// }); - router.get('/service-worker.js', async (ctx) => { ctx.set('Content-Type', 'application/javascript'); if (process.env.NODE_ENV === 'production') ctx.set('Cache-Control', `max-age=${30}`);