Skip to content

Commit 05dccb9

Browse files
committed
Fix accept header issues
Setup supportedTypes. Add hack to rewrite application/vnd.api+json to application/json (hopefully get loopback support and wont need this)
1 parent d008f60 commit 05dccb9

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/headers.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
'use strict';
2-
3-
var bodyParser = require('body-parser');
2+
var is = require('type-is');
43

54
module.exports = function (app, options) {
65

7-
//registered at the beginning of the routes phase due to usage of
8-
//express app.use instead of app.middleware.
9-
//We need to do this as bodyParser doesn't support
10-
//JSON APIs application/vnd.api+json format by default
11-
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
6+
app.remotes().before('**', function (ctx, next) {
7+
if (ctx.req.accepts('application/vnd.api+json')) {
8+
ctx.req.headers.accept = 'application/json';
9+
}
10+
next();
11+
});
12+
13+
app.remotes().options.rest = {
14+
//turn off strong remotings error handler so we can handle ourselves in error.js
15+
handleErrors: false,
16+
17+
//extend support for application/vnd.api+json
18+
supportedTypes: ['json', 'application/javascript', 'text/javascript', 'application/vnd.api+json']
19+
};
20+
21+
//extend rest body parser to also parse application/vnd.api+json
22+
app.remotes().options.json = {
23+
strict: false,
24+
type: function (req) {
25+
//if Content-Type is any of the following, then parse otherwise don't
26+
return !!is(req, ['json', 'application/json', 'application/vnd.api+json']);
27+
}
28+
};
1229
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
"body-parser": "^1.14.1",
2929
"debug": "^2.2.0",
3030
"inflection": "^1.7.2",
31-
"lodash": "^3.10.1"
31+
"lodash": "^3.10.1",
32+
"loopback": "^2.22.2",
33+
"type-is": "^1.6.9"
3234
},
3335
"devDependencies": {
3436
"babel-eslint": "^4.1.3",

0 commit comments

Comments
 (0)