Skip to content

Commit

Permalink
add paramsProcessor support to give application a hook to add general…
Browse files Browse the repository at this point in the history
… service params processing
  • Loading branch information
lingyan committed Dec 14, 2018
1 parent 9ccb80e commit dae0d05
Show file tree
Hide file tree
Showing 4 changed files with 671 additions and 1,002 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,27 @@ fetcher
});
```

## XHR Params Processing

For some applications, there may be a situation where you need to process the service params passed in XHR request before params are sent to the actual service. Typically, you would process these params in the service itself. However, if you want to perform processing across many services (i.e. sanitization for security), then you can use the `paramsProcessor` option.

`paramsProcessor` is a function that is passed into the `Fetcher.middleware` method. It is passed three arguments, the request object, the serviceInfo object, and the service params object. The `paramsProcessor` function can then modify the service params if needed.

Here is an example:

```js
/**
Using the app.js from above, you can modify the Fetcher.middleware
method to pass in the paramsProcessor function.
*/
app.use('/myCustomAPIEndpoint', Fetcher.middleware({
paramsProcessor: function (req, serviceInfo, params) {
console.log(serviceInfo.resource, serviceInfo.operation);
return Object.assign({foo: 'fillDefaultValueForFoo'}, params);
}
}));
```

## 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.
Expand Down
20 changes: 17 additions & 3 deletions libs/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ function getErrorResponse(err) {
* @param {Function} [options.statsCollector] The function will be invoked with 1 argument:
* the stats object, which contains resource, operation, params (request params),
* statusCode, err, and time (elapsed time)
* @param {Function} [options.paramsProcessor] The function will be invoked with 3 arguments:
* the req object, the serviceInfo object, and the params object. It is expected to return the processed params object.
* @constructor
*/
function Request (operation, resource, options) {
Expand All @@ -97,6 +99,7 @@ function Request (operation, resource, options) {
this._clientConfig = {};
this._startTime = 0;
this._statsCollector = options.statsCollector;
this._paramsProcessor = options.paramsProcessor;
}

/**
Expand All @@ -107,7 +110,9 @@ function Request (operation, resource, options) {
* @chainable
*/
Request.prototype.params = function (params) {
this._params = params;
this._params = (typeof this._paramsProcessor === 'function')
? this._paramsProcessor(this.req, {operation: this.operation, resource: this.resource}, params)
: params;
return this;
};
/**
Expand Down Expand Up @@ -243,6 +248,8 @@ function executeRequest (request, resolve, reject) {
* @param {Function} [options.statsCollector] The function will be invoked with 1 argument:
* the stats object, which contains resource, operation, params (request params),
* statusCode, err, and time (elapsed time)
* @param {Function} [options.paramsProcessor] The function will be invoked with 3 arguments:
* the req object, the serviceInfo object, and the params object. It is expected to return the processed params object.
* @constructor
*/
function Fetcher (options) {
Expand Down Expand Up @@ -339,6 +346,8 @@ Fetcher.isRegistered = function (name) {
* @param {Function} [options.statsCollector] The function will be invoked with 1 argument:
the stats object, which contains resource, operation, params (request params),
statusCode, err, and time (elapsed time)
* @param {Function} [options.paramsProcessor] The function will be invoked with 3 arguments:
* the req object, the serviceInfo object, and the params object. It is expected to return the processed params object.
* @returns {Function} middleware
* @param {Object} req
* @param {Object} res
Expand All @@ -349,6 +358,9 @@ Fetcher.middleware = function (options) {
var responseFormatter = options.responseFormatter || function noOp(req, res, data) {
return data;
};
var paramsProcessor = options.paramsProcessor || function noOp(req, params) {
return params;
};
return function (req, res, next) {
var request;
var error;
Expand All @@ -369,7 +381,8 @@ Fetcher.middleware = function (options) {
request = new Request(OP_READ, resource, {
req: req,
serviceMeta: serviceMeta,
statsCollector: options.statsCollector
statsCollector: options.statsCollector,
paramsProcessor: options.paramsProcessor
});
request
.params(parseParamValues(qs.parse(path.join('&'))))
Expand Down Expand Up @@ -433,7 +446,8 @@ Fetcher.middleware = function (options) {
request = new Request(operation, singleRequest.resource, {
req: req,
serviceMeta: serviceMeta,
statsCollector: options.statsCollector
statsCollector: options.statsCollector,
paramsProcessor: options.paramsProcessor
});
request
.params(singleRequest.params)
Expand Down

0 comments on commit dae0d05

Please sign in to comment.