Comparing version 0.5.21 to 0.5.22
@@ -282,2 +282,6 @@ /** | ||
* @memberof Fetcher | ||
* @param {Object} [options] Optional configurations | ||
* @param {Function} [options.responseFormatter=no op function] Function to modify the response | ||
before sending to client. First argument is the HTTP request object, | ||
second argument is the HTTP response object and the third argument is the service data object. | ||
* @returns {Function} middleware | ||
@@ -288,3 +292,7 @@ * @param {Object} req | ||
*/ | ||
Fetcher.middleware = function () { | ||
Fetcher.middleware = function (options) { | ||
options = options || {}; | ||
var responseFormatter = options.responseFormatter || function noOp(req, res, data) { | ||
return data; | ||
}; | ||
return function (req, res, next) { | ||
@@ -319,10 +327,10 @@ var request; | ||
var errResponse = getErrorResponse(err); | ||
res.status(errResponse.statusCode).json(errResponse.output); | ||
res.status(errResponse.statusCode).json(responseFormatter(req, res, errResponse.output)); | ||
return; | ||
} | ||
if (req.query.returnMeta) { | ||
res.status(meta.statusCode || 200).json({ | ||
res.status(meta.statusCode || 200).json(responseFormatter(req, res, { | ||
data: data, | ||
meta: meta | ||
}); | ||
})); | ||
} else { | ||
@@ -367,12 +375,12 @@ // TODO: Remove `returnMeta` feature flag after next release | ||
} | ||
if(err) { | ||
if (err) { | ||
var errResponse = getErrorResponse(err); | ||
res.status(errResponse.statusCode).json(errResponse.output); | ||
res.status(errResponse.statusCode).json(responseFormatter(req, res, errResponse.output)); | ||
return; | ||
} | ||
var responseObj = {}; | ||
responseObj[DEFAULT_GUID] = { | ||
responseObj[DEFAULT_GUID] = responseFormatter(req, res, { | ||
data: data, | ||
meta: meta | ||
}; | ||
}); | ||
res.status(meta.statusCode || 200).json(responseObj); | ||
@@ -379,0 +387,0 @@ }); |
{ | ||
"name": "fetchr", | ||
"version": "0.5.21", | ||
"version": "0.5.22", | ||
"description": "Fetchr augments Flux applications by allowing Flux stores to be used on server and client to fetch data", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# Fetchr | ||
# Fetchr | ||
@@ -7,3 +7,3 @@ [![npm version](https://badge.fury.io/js/fetchr.svg)](http://badge.fury.io/js/fetchr) | ||
[![devDependency Status](https://david-dm.org/yahoo/fetchr/dev-status.svg)](https://david-dm.org/yahoo/fetchr#info=devDependencies) | ||
[![Coverage Status](https://coveralls.io/repos/yahoo/fetchr/badge.png?branch=master)](https://coveralls.io/r/yahoo/fetchr?branch=master) | ||
[![Coverage Status](https://coveralls.io/repos/yahoo/fetchr/badge.png?branch=master)](https://coveralls.io/r/yahoo/fetchr?branch=master) | ||
@@ -14,3 +14,3 @@ Universal data access layer for web applications. | ||
Having to write code differently for both environments is duplicative and error prone. Fetchr provides an abstraction layer over your data service calls so that you can fetch data using the same API on the server and client side. | ||
Having to write code differently for both environments is duplicative and error prone. Fetchr provides an abstraction layer over your data service calls so that you can fetch data using the same API on the server and client side. | ||
@@ -138,3 +138,3 @@ ## Install | ||
}); | ||
// for create you can use the body() method to pass data | ||
@@ -264,2 +264,25 @@ fetcher | ||
## XHR Response Formatting | ||
For some applications, there may be a situation where you need to modify an XHR response before it is passed to the client. Typically, you would apply your modifications in the service itself. However, if you want to modify the XHR responses across many services (i.e. add debug information), then you can use the `responseFormatter` option. | ||
`responseFormatter` is a function that is passed into the `Fetcher.middleware` method. It is passed three arguments, the request object, response object and the service response object (i.e. the data returned from your service). The `responseFormatter` function can then modify the service response to add additional information. | ||
Take a look at the example below: | ||
```js | ||
/** | ||
Using the app.js from above, you can modify the Fetcher.middleware | ||
method to pass in the responseFormatter function. | ||
*/ | ||
app.use('/myCustomAPIEndpoint', Fetcher.middleware({ | ||
responseFormatter: function (req, res, data) { | ||
data.debug = 'some debug information'; | ||
return data; | ||
} | ||
})); | ||
``` | ||
Now when an XHR request is performed, your response will contain the `debug` property added above. | ||
## CORS Support | ||
@@ -266,0 +289,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67355
1251
419