Instantly share code, notes, and snippets.

🌈
I'm fine

William Koza wKoza

View GitHub Profile
View use.httpclient.ts
export const HTTP_INTERCEPTORS_1 = new InjectionToken<HttpInterceptor[]>('HTTP_INTERCEPTORS_1');
export const HTTP_INTERCEPTORS_2 = new InjectionToken<HttpInterceptor[]>('HTTP_INTERCEPTORS_2');
@Injectable()
export class Http1Service extends HttpClient {
constructor(backend: HttpBackend, private injector: Injector) {
super(new MyHandlerService1(backend, injector, HTTP_INTERCEPTORS_1));
}
View rewrite.handler.ts
import {InjectionToken, Injector} from '@angular/core';
import {HttpBackend, HttpEvent, HttpInterceptor, HttpReques, HttpHandler, HttpRequest} from '@angular/common/http';
import { Observable } from 'rxjs';
/**
* `HttpHandler` which applies an `HttpInterceptor` to an `HttpRequest`.
*/
export class MyHttpInterceptorHandler implements HttpHandler {
constructor(private next: HttpHandler, private interceptor: HttpInterceptor) {}
View angular.httpclient.ts
@Injectable()
export class HttpInterceptingHandler implements HttpHandler {
private chain: HttpHandler|null = null;
constructor(private backend: HttpBackend, private injector: Injector) {}
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
if (this.chain === null) {
const interceptors = this.injector.get(HTTP_INTERCEPTORS, []);
this.chain = interceptors.reduceRight(
View simple.httpClient.ts
export class Http1Service extends HttpClient { };
export class Http2Service extends HttpClient { };
@NgModule({
providers: [
{provide: Http1Service, useClass: HttpClient},
{provide: Http2Service, useClass: HttpClient},
]
})
View httpClient2.ts
Observable<Article> article$ = this.http.get<Article>('api.learn-angular.fr/article', {responseType: 'blob'});
View httpClient1.ts
Observable<Article[]> articles$ = this.http.get<Article[]>('api.learn-angular.fr/articles');
@wKoza
wKoza / httpclient.ts
Last active Jul 17, 2017
httpclient.ts
View httpclient.ts
Observable<Article[]> articles$ = this.http.get('api.learn-angular.fr/articles')
.map((res:Response) => res.json() as Article[]);
View app_initializer.ts
export function configFactory(config: ConfigService) {
return () => config.load();
}
...
providers: [
    ConfigService,
    {
View promise.ts
Promise<Article[]> articles = this.http.get('api.learn-angular.fr/articles')
.toPromise()
.then((res: Response) => res.json() as Article[])
.catch(this.handleError);
...
private handleError(error: any): Promise<any> {
console.error('Une erreur est survenue : ', error);
return Promise.reject(error.message || error);
View app.module.ts
import { NgModule } from '@angular/core';
import { UserService } from './user.service';
import { LogService } from './log.service';
@NgModule({
imports: [LogService, UserService]
})
class AppModule {
constructor(logService: LogService, userService: UserService) {