Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 7af64a4

Browse files
authored
docs(http): use a class for default RequestOptions override (#2982)
John Papa pointed out that this makes user testing easier if you care about headers in those tests.
1 parent ad3d005 commit 7af64a4

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

public/docs/_examples/server-communication/ts/app/app.module.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import { NgModule } from '@angular/core';
44
import { BrowserModule } from '@angular/platform-browser';
55
import { FormsModule } from '@angular/forms';
6-
// #docregion override-default-options
7-
import { HttpModule, JsonpModule, RequestOptions } from '@angular/http';
6+
import { HttpModule, JsonpModule } from '@angular/http';
7+
88

9-
// #enddocregion override-default-options
109
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
1110
import { HeroData } from './hero-data';
11+
import { requestOptionsProvider } from './default-request-options.service';
1212

1313
import { AppComponent } from './app.component';
1414

@@ -35,16 +35,12 @@ import { WikiSmartComponent } from './wiki/wiki-smart.component';
3535
WikiComponent,
3636
WikiSmartComponent
3737
],
38+
// #docregion provide-default-request-options
39+
providers: [ requestOptionsProvider ],
40+
// #enddocregion provide-default-request-options
3841
bootstrap: [ AppComponent ]
3942
})
40-
// #docregion override-default-request-options
41-
export class AppModule {
42-
constructor(requestOptions: RequestOptions) {
43-
// Set the default 'Content-Type' header
44-
requestOptions.headers.set('Content-Type', 'application/json');
45-
}
46-
}
47-
// #enddocregion override-default-request-options
43+
export class AppModule {}
4844

4945

5046

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// #docregion
2+
import { BaseRequestOptions, RequestOptions } from '@angular/http';
3+
4+
export class DefaultRequestOptions extends BaseRequestOptions {
5+
6+
constructor() {
7+
super();
8+
9+
// Set the default 'Content-Type' header
10+
this.headers.set('Content-Type', 'application/json');
11+
}
12+
}
13+
14+
export const requestOptionsProvider = {provide: RequestOptions, useClass: DefaultRequestOptions };

public/docs/ts/latest/guide/server-communication.jade

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -659,22 +659,28 @@ a#override-default-request-options
659659
The `HttpModule` provides these default options via the `RequestOptions` token.
660660

661661
You can override these defaults to suit your application needs.
662-
The API docs suggest creating a custom sub-class of `RequestOptions`
663-
and providing that as a replacement class for the `RequestOptions` token.
662+
by creating a custom sub-class of `RequestOptions`
663+
that sets the default options for the application.
664664

665-
Perhaps an easier way is to inject the current `RequestOptions` instance
666-
into an application module constructor and re-set its values directly.
667-
This sample sets the default `Content-Type` header to JSON in the root `AppModule` constructor:
668-
+makeExample('server-communication/ts/app/app.module.ts', 'override-default-request-options', 'app/app.module.ts (default request header override)')(format=".")
665+
This sample creates a class that sets the default `Content-Type` header to JSON.
666+
It exports a constant with the necessary `RequestOptions` provider to simplify registration in `AppModule`.
667+
668+
+makeExample('server-communication/ts/app/default-request-options.service.ts', '', 'app/default-request-options.service.ts')(format=".")
669+
:marked
670+
Then it registers the provider in the root `AppModule`.
671+
+makeExample('server-communication/ts/app/app.module.ts', 'provide-default-request-options', 'app/app.module.ts (provide default request header)')(format=".")
672+
.l-sub-section
673+
:marked
674+
Remember to include this provider during setup when unit testing the app's HTTP services.
669675
:marked
670676
After this change, the `header` option setting in `HeroService.addHero` is no longer necessary,
671677

672678
+makeExample('server-communication/ts/app/toh/hero.service.ts', 'addhero', 'app/toh/hero.service.ts (addHero)')(format=".")
673679
:marked
674-
You can confirm this works by looking at the network tab.
680+
You can confirm that `DefaultRequestOptions` is working by examing HTTP requests in the browser developer tools' network tab.
675681
If you're short-circuiting the server call with something like the [_in-memory web api_](#in-mem-web-api),
676682
try commenting-out the `addHero` header option,
677-
set a breakpoint on the POST call, and step through to the backend request
683+
set a breakpoint on the POST call, and step through the request processing
678684
to verify the header is there.
679685

680686
Individual requests options, like this one, take precedence over the default `RequestOptions`.

0 commit comments

Comments
 (0)