| 13 | module.exports = apiProxy; |
| 14 | |
| 15 | function apiProxy(dataAdapter) { |
| 16 | return function(req, res, next) { |
| 17 | var api; |
| 18 | |
| 19 | api = _.pick(req, 'query', 'method', 'body'); |
| 20 | |
| 21 | api.path = apiProxy.getApiPath(req.path); |
| 22 | api.api = apiProxy.getApiName(req.path); |
| 23 | api.headers = { |
| 24 | 'x-forwarded-for': apiProxy.getXForwardedForHeader(req.headers, req.ip) |
| 25 | }; |
| 26 | |
| 27 | api.headers = apiProxy.setXHTTPMethodOverride(req.headers, api.headers); |
| 28 | |
| 29 | dataAdapter.request(req, api, { |
| 30 | convertErrorCode: false |
| 31 | }, function(err, response, body) { |
| 32 | if (err) return next(err); |
| 33 | |
| 34 | // Pass through statusCode. |
| 35 | res.status(response.statusCode); |
| 36 | if (!response.jsonp){ |
| 37 | res.json(body); |
| 38 | }else{ |
| 39 | res.jsonp(body); |
| 40 | } |
| 41 | }); |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | apiProxy.getApiPath = function getApiPath(path) { |
| 46 | var sepIndex = path.indexOf(separator), |