Skip to content

Commit

Permalink
feat(plugin-core-api): added format option in query string
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanMatthias committed Dec 5, 2018
1 parent f3dc0e3 commit aa2e6b9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/core-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"helmet": "^3.12.0",
"http-status-codes": "^1.3.0",
"ip": "^1.1.5",
"js2xmlparser": "^3.0.0",
"json-2-csv": "^2.4.0",
"json-query": "^2.2.2",
"pluralize": "^7.0.0",
"tcp-port-used": "^1.0.1"
Expand Down
28 changes: 28 additions & 0 deletions packages/plugin-core-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ module.exports = async (app: any, options: any) => {

// ------------------------------------------------------------ Setup models
const api = new Route('/api/v1');
const contentType = new Route();

// Enforce all requests to be JSON by default (even errors)
// This can be overridden by the Accepts header on the request

contentType
.position('init')
.use((req, res, next) => {
if (req.originalUrl.match(/^\/api\/v1\//)) res.contentType('json');
if (req.query.format) {
switch (req.query.format) {
case 'json':
res.contentType('json');
break;
case 'html':
res.contentType('html');
break;
case 'xml':
res.contentType('xml');
break;
case 'csv':
res.contentType('text/csv');
}
}

next();
});

// If the body has a password, hash it for all routes
api
Expand All @@ -37,5 +64,6 @@ module.exports = async (app: any, options: any) => {
next();
});

app.useRouter(contentType);
app.useRouter(api);
};

0 comments on commit aa2e6b9

Please sign in to comment.