2016-04-29 05:25:37 +00:00
|
|
|
import bodyParser from 'koa-bodyparser';
|
|
|
|
import Koa from 'koa';
|
|
|
|
import Router from 'koa-router';
|
|
|
|
import Sequelize from 'sequelize';
|
2016-09-17 21:53:30 +00:00
|
|
|
import _ from 'lodash';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
|
|
import auth from './auth';
|
|
|
|
import user from './user';
|
2016-06-26 05:36:16 +00:00
|
|
|
import collections from './collections';
|
2016-05-20 03:46:34 +00:00
|
|
|
import documents from './documents';
|
2017-06-26 00:21:33 +00:00
|
|
|
import views from './views';
|
2016-08-23 06:40:11 +00:00
|
|
|
import hooks from './hooks';
|
2016-08-24 07:37:54 +00:00
|
|
|
import apiKeys from './apiKeys';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
2016-08-27 17:48:56 +00:00
|
|
|
import validation from './middlewares/validation';
|
2016-07-22 07:11:54 +00:00
|
|
|
import methodOverride from '../middlewares/methodOverride';
|
2016-08-15 10:51:26 +00:00
|
|
|
import cache from '../middlewares/cache';
|
2016-09-11 20:38:52 +00:00
|
|
|
import apiWrapper from './middlewares/apiWrapper';
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
|
|
const api = new Koa();
|
|
|
|
const router = new Router();
|
|
|
|
|
|
|
|
// API error handler
|
|
|
|
api.use(async (ctx, next) => {
|
|
|
|
try {
|
|
|
|
await next();
|
|
|
|
} catch (err) {
|
|
|
|
ctx.status = err.status || 500;
|
|
|
|
let message = err.message || err.name;
|
|
|
|
|
|
|
|
if (err instanceof Sequelize.ValidationError) {
|
|
|
|
// super basic form error handling
|
|
|
|
ctx.status = 400;
|
|
|
|
if (err.errors && err.errors[0]) {
|
|
|
|
message = `${err.errors[0].message} (${err.errors[0].path})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx.status === 500) {
|
|
|
|
message = 'Internal Server Error';
|
|
|
|
ctx.app.emit('error', err, ctx);
|
|
|
|
}
|
|
|
|
|
2016-09-11 20:38:52 +00:00
|
|
|
ctx.body = {
|
2016-09-11 22:48:03 +00:00
|
|
|
ok: false,
|
2016-09-17 21:53:30 +00:00
|
|
|
error: _.snakeCase(err.id || err.message),
|
|
|
|
status: err.status,
|
|
|
|
message,
|
2016-09-11 20:38:52 +00:00
|
|
|
};
|
2016-04-29 05:25:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
api.use(bodyParser());
|
2016-07-22 07:11:54 +00:00
|
|
|
api.use(methodOverride());
|
2016-08-15 10:51:26 +00:00
|
|
|
api.use(cache());
|
2016-04-29 05:25:37 +00:00
|
|
|
api.use(validation());
|
2016-09-11 20:38:52 +00:00
|
|
|
api.use(apiWrapper());
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
|
|
router.use('/', auth.routes());
|
|
|
|
router.use('/', user.routes());
|
2016-06-26 05:36:16 +00:00
|
|
|
router.use('/', collections.routes());
|
2016-05-20 03:46:34 +00:00
|
|
|
router.use('/', documents.routes());
|
2017-06-26 00:21:33 +00:00
|
|
|
router.use('/', views.routes());
|
2016-08-23 06:40:11 +00:00
|
|
|
router.use('/', hooks.routes());
|
2016-08-24 07:37:54 +00:00
|
|
|
router.use('/', apiKeys.routes());
|
2016-04-29 05:25:37 +00:00
|
|
|
|
|
|
|
// Router is embedded in a Koa application wrapper, because koa-router does not
|
|
|
|
// allow middleware to catch any routes which were not explicitly defined.
|
|
|
|
api.use(router.routes());
|
|
|
|
|
2016-07-22 07:11:54 +00:00
|
|
|
export default api;
|