diff --git a/package.json b/package.json index cc6bcaef7..7632c2eab 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "normalizr": "2.0.1", "pg": "6.0.1", "pg-hstore": "2.3.2", + "query-string": "^4.2.2", "querystring": "0.2.0", "randomstring": "1.1.5", "react": "15.1.0", diff --git a/server/api/auth.js b/server/api/auth.js index 1f00e0663..5ca77764a 100644 --- a/server/api/auth.js +++ b/server/api/auth.js @@ -9,7 +9,7 @@ import { User, Team } from '../models'; const router = new Router(); router.post('auth.slack', async (ctx) => { - const { code } = ctx.request.body; + const { code } = ctx.body; ctx.assertPresent(code, 'code is required'); const body = { diff --git a/server/api/collections.js b/server/api/collections.js index fc13da0b4..0054cde57 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -14,7 +14,7 @@ router.post('atlases.create', auth(), async (ctx) => { name, description, type, - } = ctx.request.body; + } = ctx.body; ctx.assertPresent(name, 'name is required'); const user = ctx.state.user; @@ -32,7 +32,7 @@ router.post('atlases.create', auth(), async (ctx) => { }); router.post('atlases.info', auth(), async (ctx) => { - let { id } = ctx.request.body; + let { id } = ctx.body; ctx.assertPresent(id, 'id is required'); const user = ctx.state.user; @@ -79,7 +79,7 @@ router.post('atlases.list', auth(), pagination(), async (ctx) => { }); router.post('atlases.updateNavigationTree', auth(), async (ctx) => { - let { id, tree } = ctx.request.body; + let { id, tree } = ctx.body; ctx.assertPresent(id, 'id is required'); const user = ctx.state.user; diff --git a/server/api/documents.js b/server/api/documents.js index 091276431..bd0fb5e4e 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -13,7 +13,7 @@ const router = new Router(); // FIXME: This really needs specs :/ router.post('documents.info', auth({ require: false }), async (ctx) => { - let { id } = ctx.request.body; + let { id } = ctx.body; ctx.assertPresent(id, 'id is required'); const document = await Document.findOne({ @@ -44,7 +44,7 @@ router.post('documents.info', auth({ require: false }), async (ctx) => { }); router.post('documents.search', auth(), async (ctx) => { - let { query } = ctx.request.body; + let { query } = ctx.body; ctx.assertPresent(query, 'query is required'); const user = await ctx.state.user; @@ -86,7 +86,7 @@ router.post('documents.create', auth(), async (ctx) => { title, text, parentDocument, - } = ctx.request.body; + } = ctx.body; ctx.assertPresent(atlas, 'atlas is required'); ctx.assertPresent(title, 'title is required'); ctx.assertPresent(text, 'text is required'); @@ -138,7 +138,7 @@ router.post('documents.update', auth(), async (ctx) => { id, title, text, - } = ctx.request.body; + } = ctx.body; ctx.assertPresent(id, 'id is required'); ctx.assertPresent(title, 'title is required'); ctx.assertPresent(text, 'text is required'); @@ -174,7 +174,7 @@ router.post('documents.update', auth(), async (ctx) => { router.post('documents.delete', auth(), async (ctx) => { let { id, - } = ctx.request.body; + } = ctx.body; ctx.assertPresent(id, 'id is required'); const user = ctx.state.user; diff --git a/server/api/index.js b/server/api/index.js index 11f8fc09d..fe87dae8e 100644 --- a/server/api/index.js +++ b/server/api/index.js @@ -10,6 +10,7 @@ import collections from './collections'; import documents from './documents'; import validation from './validation'; +import methodOverride from '../middlewares/methodOverride'; const api = new Koa(); const router = new Router(); @@ -40,6 +41,7 @@ api.use(async (ctx, next) => { }); api.use(bodyParser()); +api.use(methodOverride()); api.use(validation()); router.use('/', auth.routes()); @@ -56,4 +58,4 @@ api.use(async () => { throw httpErrors.NotFound(); }); -export default api; \ No newline at end of file +export default api; diff --git a/server/api/user.js b/server/api/user.js index 26a210fdf..659d176e9 100644 --- a/server/api/user.js +++ b/server/api/user.js @@ -16,7 +16,7 @@ router.post('user.info', auth(), async (ctx) => { }); router.post('user.s3Upload', auth(), async (ctx) => { - let { filename, kind, size } = ctx.request.body; + let { filename, kind, size } = ctx.body; ctx.assertPresent(filename, 'filename is required'); ctx.assertPresent(kind, 'kind is required'); ctx.assertPresent(size, 'size is required'); @@ -46,4 +46,4 @@ router.post('user.s3Upload', auth(), async (ctx) => { }}; }); -export default router; \ No newline at end of file +export default router; diff --git a/server/middlewares/methodOverride.js b/server/middlewares/methodOverride.js new file mode 100644 index 000000000..c49bca09d --- /dev/null +++ b/server/middlewares/methodOverride.js @@ -0,0 +1,13 @@ +import queryString from 'query-string'; + +export default function methodOverride(options) { + return async function methodOverrideMiddleware(ctx, next) { + if (ctx.method === 'POST') { + ctx.body = ctx.request.body; + } else { + ctx.method= 'POST'; + ctx.body = queryString.parse(ctx.querystring); + } + return next(); + } +}; diff --git a/server/static/service-worker.js b/server/static/service-worker.js index 2b2a9902d..c3423eb8a 100644 --- a/server/static/service-worker.js +++ b/server/static/service-worker.js @@ -61,7 +61,7 @@ Cache.prototype.addAll||(Cache.prototype.addAll=function(t){function e(t){this.n global.toolbox.router.get(/\/static\//, global.toolbox.cacheFirst); // API get calls - global.toolbox.router.post(/\/api\/[\w\.]+/, global.toolbox.networkFirst); + global.toolbox.router.get(/\/api\/[\w\.]+/, global.toolbox.networkFirst); global.toolbox.router.default = global.toolbox.networkFirst; diff --git a/src/utils/ApiClient.js b/src/utils/ApiClient.js index 79a3ada09..0940cdbfd 100644 --- a/src/utils/ApiClient.js +++ b/src/utils/ApiClient.js @@ -14,7 +14,7 @@ class ApiClient { let modifiedPath; if (method === 'GET') { - modifiedPath = path + this.constructQueryString(data); + modifiedPath = `${path}?${this.constructQueryString(data)}`; } else if (method === 'POST' || method === 'PUT') { body = JSON.stringify(data); } @@ -78,7 +78,7 @@ class ApiClient { } post = (path, data) => { - return this.fetch(path, 'POST', data); + return this.fetch(path, 'GET', data); } // Helpers