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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>My First Angular 2 app</title> | |
</head> | |
<body> | |
<my-app>Loading...</my-app> | |
<!-- dependencies --> | |
<script src="https://code.angularjs.org/tools/traceur-runtime.js"></script> |
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
import { Injectable } from '@angular/core'; | |
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpEventType } from '@angular/common/http'; | |
import { Observable } from 'rxjs/Observable'; | |
export class AuthInterceptor implements HttpInterceptor { | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<HttpEventType.Response>> { | |
const authReq = req.clone({ | |
setHeaders: { Authorization: `Bearer authtest` } | |
}); |
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
interface HttpInterceptor { | |
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>; | |
} |
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
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; | |
@NgModule({ | |
imports: [], | |
providers: [ | |
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true } | |
] | |
}) | |
export class AppModule {} |
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
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http'; | |
@NgModule({ | |
imports: [], | |
providers: [ | |
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true } | |
] | |
}) | |
export class AppModule {} |
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
// Http | |
this.users = this.http.get('https://api.github.com/users') | |
.map(response => response.json()) | |
.subscribe(data => console.log(data)); |
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
import { TestBed, getTestBed } from '@angular/core/testing'; | |
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | |
import { GithubApiService } from './github-api.service'; | |
describe('GithubApiService', () => { | |
let injector: TestBed; | |
let service: GithubApiService; | |
let httpMock: HttpTestingController; | |
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
import { Injectable } from '@angular/core'; | |
import { HttpClient, HttpParams } from '@angular/common/http'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/throw'; | |
export interface User { | |
login: string; | |
} | |
export interface Issue {} |
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
describe('#getUsers', () => { | |
it('should return an Observable<User[]>', () => { | |
const dummyUsers = [ | |
{ login: 'John' }, | |
{ login: 'Doe' } | |
]; | |
service.getUsers().subscribe(users => { | |
expect(users.length).toBe(2); | |
expect(users).toEqual(dummyUsers); |
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
describe('#search', () => { | |
const dummyParams = new HttpParams().set('q', 'cironunes'); | |
it('should throw an error if trying to search for not supported `what`', () => { | |
service.search('unknown', dummyParams) | |
.subscribe(() => {}, err => { | |
expect(err).toBe(`Searching for unknown is not supported. The available types are: ${service.WHAT.join(', ')}.`); | |
}); | |
httpMock.expectNone(`${service.API_URL}/search/users?q=cironunes`); |