Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add metadata documentation to httpClient (#41706)
PR Close #41706
- Loading branch information
1 parent
e47ea29
commit 09abee3
Showing
2 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
aio/content/examples/http/src/app/http-interceptors/retry-interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// #docplaster | ||
import {HttpClient, HttpContext, HttpContextToken, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http'; | ||
import {Observable} from 'rxjs'; | ||
/* | ||
// #docregion reading-context | ||
import {retry} from 'rxjs'; | ||
// #enddocregion reading-context | ||
*/ | ||
// #docregion mutable-context | ||
import {retry, tap} from 'rxjs/operators'; | ||
// #enddocregion mutable-context | ||
|
||
// #docregion context-token, mutable-context | ||
export const RETRY_COUNT = new HttpContextToken(() => 3); | ||
// #enddocregion context-token | ||
export const ERROR_COUNT = new HttpContextToken(() => 0); | ||
// #enddocregion mutable-context | ||
|
||
export class FakeService { | ||
constructor(private httpClient: HttpClient) { | ||
// #docregion set-context | ||
this.httpClient | ||
.get('/data/feed', { | ||
context: new HttpContext().set(RETRY_COUNT, 5), | ||
}) | ||
.subscribe(results => {/* ... */}); | ||
// #enddocregion set-context | ||
} | ||
} | ||
|
||
// #docregion reading-context, mutable-context | ||
|
||
export class RetryInterceptor implements HttpInterceptor { | ||
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | ||
const retryCount = req.context.get(RETRY_COUNT); | ||
|
||
return next.handle(req).pipe( | ||
// #enddocregion reading-context | ||
tap(null, | ||
() => { | ||
// An error has occurred, so increment this request's ERROR_COUNT. | ||
req.set(ERROR_COUNT, req.get(ERROR_COUNT) + 1); | ||
}), | ||
// #docregion reading-context | ||
// Retry the request a configurable number of times. | ||
retry(retryCount), | ||
); | ||
} | ||
} | ||
// #enddocregion reading-context, mutable-context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters