Title: RFC: Http interceptors and transformers · Issue #2684 · angular/angular · GitHub
Open Graph Title: RFC: Http interceptors and transformers · Issue #2684 · angular/angular
X Title: RFC: Http interceptors and transformers · Issue #2684 · angular/angular
Description: This issue is intended to define requirements for how the Http library should support common operations, like request and response transformations and error handling. The issue doesn't yet have a proposed design, but discussion, comments...
Open Graph Description: This issue is intended to define requirements for how the Http library should support common operations, like request and response transformations and error handling. The issue doesn't yet have a p...
X Description: This issue is intended to define requirements for how the Http library should support common operations, like request and response transformations and error handling. The issue doesn't yet have...
Opengraph URL: https://github.com/angular/angular/issues/2684
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"RFC: Http interceptors and transformers","articleBody":"This issue is intended to define requirements for how the `Http` library should support common operations, like request and response transformations and error handling. The issue doesn't yet have a proposed design, but discussion, comments and suggestions are welcomed. Un-captured use cases are especially welcomed.\n# History\n\nTwo widely-used feature of angular1 are [request and response interceptors](https://docs.angularjs.org/api/ng/service/$http#interceptors) and [request/response transformers](https://docs.angularjs.org/api/ng/service/$http#interceptors). Interceptors and Transformers have different goals and constraints.\n- Interceptors are expected to have side effects, while transformers' exclusive purpose is to mutate requests or responses synchronously.\n- Interceptors may be asynchronous, tranformers must be synchronous\n- Interceptors are global and configured in Angular 1's \"compile\" phase, while transformers may be global or local. Global transformations can be mutated in the \"run\" phase.\n- Error interceptors are declared explicitly and separately from normal interceptors, whereas transforms handle normal and error cases.\n- Error interceptors can recover from errors by returning a resolved promise\n\nSome common use cases for interceptors:\n- Setting timeouts for requests\n- Setting custom XSRF tokens to request headers, as described in [$http docs](https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection)\n- Add conditional parameters to all request urls (like an access token, or a/b-testing info)\n- Check authentication state before performing request, prompt user to authenticate\n- Have side effects on other parts of application (i.e. if response says user needs to authenticate, route them to login page)\n- Log all response errors returned from the server to a persisted log\n- Log total request/response time in order to do performance analysis\n- Filter out un-interesting request/responses from logs\n- Recover from request exceptions, or response errors (i.e. if request times out, return old data from cache)\n- Unwrapping responses (i.e. if response is {data: {foo:bar}}, return response.data)\n- Cache responses in a custom cache to be used by other services\n\nCommon use cases for transformers\n- serialize/de-serialize data, both directions\n- Strip headers (Angular 1 strips user-specified XSRF header with a default response transformation)\n\nWorth noting are some additional features provided by Angular 1 $http, which would otherwise be handled by transformers:\n- Url-based caching\n- Configurable parameter serialization\n- Header serialization\n# New Considerations for Angular 2’s Http Library\n\nAngular 2 and Angular 2 Http have some technical differences and philosophical differences from Angular 1.\n- Mutable global state is discouraged (i.e. `$http.defaults.transforms.push(...)`).\n- Angular 2 has no \"phases\" like \"config\" and \"run\".\n- Angular 2 provides hierarchical dependency injection, allowing users to modify bindings at different levels of an application's component tree.\n- Angular 2 Http is based primarily on `Observable` instead of Promise\n- Http has goals of supporting upload/download progress events, connection retrying, request cancellation, caching, and polling as first-class use cases. \n## Current Options\n\nThe current library supports creating services that would provide shared interceptors and transformations. Here's an example of an Http-based service that would add custom headers, cache responses and log errors, based on the current RxJS implementation.\n\n``` js\nimport {Http, Request, IRequestOptions} from 'angular2/http';\nimport {ResponseCache} from './my-response-cache';\nimport {ErrorLog} from './my-error-log';\nclass MyConfiguredHttp {\n constructor(public http:Http, public responseCache:ResponseCache, public errorLog:ErrorLog) {}\n request(options:IRequestOptions) {\n if (options.url in this.responseCache){\n //Naive example (This Observable create syntax not yet supported)\n //There's a cached response for this url, return it instead of performing request\n return Rx.Observable.from([this.responseCache[options.url]]);\n }\n\n var req = new Request(options);\n // Set a custom auth header\n req.headers.set('sessionid', '123');\n return this.http.request(req)\n //Try performing the request up to three times\n .retry(3)\n .do(res =\u003e {\n //Naive example, cache the response by the original url\n this.responseCache[options.url] = res;\n })\n .doOnError(res =\u003e {\n // Log the error with my app's logging service\n this.errorLog.push(options, res);\n });\n } \n}\n```\n\nQuestions to answer with final design:\n- What use cases are not able to be supported by the \"shared service\" approach?\n- How can the API be improved to make performance, robustness, and security the default path?\n- How is composability impacted by this approach, and how can it be improved?\n- How could this be simple and intuitive?\n- Does this require too deep of knowledge of Observables/Rx? \n- Does anyone actually read this far down on Github issues?\n","author":{"url":"https://github.com/jeffbcross","@type":"Person","name":"jeffbcross"},"datePublished":"2015-06-22T17:41:39.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":97},"url":"https://github.com/2684/angular/issues/2684"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4a91ce5e-64d6-1100-c429-31877256ea26 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AF64:A97AB:D71F6D:11FF640:6A6054C9 |
| html-safe-nonce | b3f2fd160e5fc4bce0fd6108979acf89d58297a638fb4c8407a95a49bd7d43fb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRjY0OkE5N0FCOkQ3MUY2RDoxMUZGNjQwOjZBNjA1NEM5IiwidmlzaXRvcl9pZCI6IjE4MTkyMTU3ODYwNjgxNjk5MjkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3688502629dcb9b4a5f307039adac18feacb8a2efe2a9765d1a31ffd522091f6 |
| hovercard-subject-tag | issue:90163621 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/2684/issue_layout |
| twitter:image | https://opengraph.githubassets.com/53cb0823674b0fd6d957f12b3643f8c94c97e826974e22f97f1bcb1672b435d7/angular/angular/issues/2684 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/53cb0823674b0fd6d957f12b3643f8c94c97e826974e22f97f1bcb1672b435d7/angular/angular/issues/2684 |
| og:image:alt | This issue is intended to define requirements for how the Http library should support common operations, like request and response transformations and error handling. The issue doesn't yet have a p... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jeffbcross |
| hostname | github.com |
| expected-hostname | github.com |
| None | 60da8c2a42fa2bbf5f7567474990ec467836a84444262a58e200fa91b7f3d2d0 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9824515e740d83d5eb82168a089b806ab0fe04a1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width