Skip to content

Commit 24f0e1c

Browse files
committed
Add intercepting filters
1 parent 0cf5ee4 commit 24f0e1c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,30 @@ Since Martin Fowler states that
576576

577577
#### Intercepting Filters
578578

579+
>Create a chain of composable filters to implement common pre-processing and post-processing tasks during a Web page request.
579580
581+
![Composite](https://rawgit.com/mgechev/angularjs-in-patterns/master/images/intercepting-filters.svg "Fig. 3")
582+
583+
In some cases you need to do some kind of pre and/or post processing of HTTP requests. In the case of the Intercepting Filters you pre/post process given HTTP request/response in order to include logging, security or any other concern, which is influenced by the request body or headers. Basically the Intercepting Filters pattern include a chain of filters, each of which process data in given order. The output of each filter is input of the next one.
584+
585+
In AngularJS we have the idea of the Intercepting Filters in `$httpProvider`. `$httpProvider` has an array property called `interceptors`, which contains a list of objects. Each object may have properties called: `request`, `response`, `requestError`, `responseError`.
586+
587+
`requestError` is an interceptor, which gets called when a previous interceptor threw an error or resolved with a rejection, respectively `responseError` is being called when the previous `response` interceptor has thrown an error.
588+
589+
Here is a basic example how you can add interceptors using object literal:
590+
591+
```JavaScript
592+
$httpProvider.interceptors.push(function($q, dependency1, dependency2) {
593+
return {
594+
'request': function(config) {
595+
// same as above
596+
},
597+
'response': function(response) {
598+
// same as above
599+
}
600+
};
601+
});
602+
```
580603

581604
### Directives
582605

0 commit comments

Comments
 (0)