问题
We need a global space to capture http 401,403, and 500 responses. I looked at a few tutorials and tried an approach of extending http. Here is my custom HTTP (largely copied from online)
import { Http, ConnectionBackend, Request, RequestOptions, RequestOptionsArgs, Response, Headers} from '@angular/http';
import { Router} from '@angular/router';
import { Injectable} from '@angular/core';
import { Observable} from 'rxjs/Rx';
@Injectable()
export class CustomHttp extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router : Router) {
super(backend, defaultOptions);
}
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.request(url, options));
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.get(url, options));
}
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.post(url, body, this.getRequestOptionArgs(options)));
}
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.put(url, body, this.getRequestOptionArgs(options)));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return this.intercept(super.delete(url, options));
}
getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new Headers();
}
options.headers.append('Content-Type', 'application/json');
return options;
}
intercept(observable: Observable<Response>): Observable<Response> {
return observable.catch((err, source) => {
console.log('CustomHttp Error status: ' + err.status);
return Observable.throw(err);
});
}
}
Here is how I bootstrap my app and try to replace default HTTP with my implementation:
import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
import { HTTP_PROVIDERS, Http, XHRBackend, RequestOptions} from '@angular/http';
import { ROUTER_DIRECTIVES, Router } from '@angular/router';
import { APP_ROUTER_PROVIDERS } from './app.routes';
import { provide, PLATFORM_DIRECTIVES} from '@angular/core';
import { LocationStrategy, HashLocationStrategy } from '@angular/common';
import { CustomHttp } from './utils/http/customhttp';
bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS,
HTTP_PROVIDERS,
provide(LocationStrategy, { useClass: HashLocationStrategy }),
provide(PLATFORM_DIRECTIVES, { useValue: [ROUTER_DIRECTIVES], multi: true }),
provide( Http, {
useFactory: (xhrBackend: XHRBackend, requestOptions: RequestOptions, router: Router) => new CustomHttp(xhrBackend, requestOptions, router),
deps: [XHRBackend, RequestOptions, Router]
}),
]);
The no compile errors but when I try to make some http requests its using the default provider and not my implementation. I know this because I purposefully call a webapi endpoint that returns a 500 and don't set into my catch clause and write a log statement.
Here are my js dependancies:
"dependencies": {
"@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4",
"@angular/forms": "0.2.0",
"@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
"@angular/router": "3.0.0-beta.1",
"@angular/router-deprecated": "2.0.0-rc.2",
"@angular/upgrade": "2.0.0-rc.4",
"systemjs": "0.19.27",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12",
"lodash": "4.13.1",
"angular2-in-memory-web-api": "0.0.14",
"bootstrap": "^3.3.6"
}
What glue am I missing here?
回答1:
Ensure you don't have HTTP_PROVIDERS
or Http
provided on some component, otherwise this might be used instead (depending on where exactly it is provided).
回答2:
This worked for me. I had to configure my base service url. It should detect the environment when I am running in my local and when deploying to prod server.
@Injectable()
export class HttpService extends Http {
static SERVICE_BASE_URL = '';
constructor(backend: XHRBackend, options: RequestOptions) {
super(backend, options);
if (/http:\/\/(localhost|127.0.0.1)/.test(window.location.href)) {
HttpService.SERVICE_BASE_URL = 'http://localhost:8000';
} else {
HttpService.SERVICE_BASE_URL = 'http://mywebsite.com/api';
}
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.get(this.toTargetUrl(url), options);
}
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return super.post(this.toTargetUrl(url), body, options);
}
put(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return super.put(this.toTargetUrl(url), body, options);
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
return super.delete(this.toTargetUrl(url), options);
}
toTargetUrl(url: string): string {
return url = /^https?:/.test(url) ? url : HttpService.SERVICE_BASE_URL + url;
}
}
This is how I configured in app.module.ts
providers: [
{
provide: Http,
useFactory: HttpFactory,
deps: [XHRBackend, RequestOptions]
},
UtilsProvider,
LocalStorageProvider
]
export function HttpFactory(backend: XHRBackend, options: RequestOptions) {
return new HttpService(backend, options);
}
来源:https://stackoverflow.com/questions/39419987/angular2-provide-custom-http-not-working