-
Notifications
You must be signed in to change notification settings - Fork 43
Request Filter
Thiago da Rosa de Bustamante edited this page Jan 19, 2018
·
6 revisions
A Filter is a function that receives the request and the response object from the gateway and must return a boolean value (or a Promise<boolean>
) to inform if the given request should target the destination API or if it should be ignored.
Each filter must be defined on its own .js file.
Example:
/**
* Where request and response are the original request and response objects created by [http](https://nodejs.org/api/http.html) module.
*/
module.exports = function (request, response) {
return req.query.denyParam !== '1';
};
or, using Promises:
/**
* Where request and response are the original request and response objects created by [http](https://nodejs.org/api/http.html) module.
*/
module.exports = function (request, response) {
return new Promise((resolve, reject) => {
setTimeout(function(){resolve(req.query.denyParam !== '1');}, 10);
};
};
You can configure a request filter middleware through:
- Admin Rest API:
POST /midleware/filters
- SDK:
sdk.middleware.addFilter(name, fileName);
- CLI:
treeGatewayConfig middleware filter -a <name> ./filename.js
Tree Gateway provide some filter middlewares for common tasks already included in its distribution. Check the list here.