Skip to content

Commit 22308d8

Browse files
committed
Add fetch service
1 parent ef787ad commit 22308d8

7 files changed

Lines changed: 1721 additions & 18 deletions

File tree

package-lock.json

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/app.module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { HttpFetchBackend } from '../lib/http-fetch-backend';
1111
declarations: [AppComponent, HelloComponent],
1212
imports: [NodeguiLibModule, HttpClientModule],
1313
providers: [
14-
GithubService,
15-
{
16-
provide: HttpClient,
17-
useFactory: () => new HttpClient(new HttpFetchBackend())
18-
}
14+
GithubService
15+
// {
16+
// provide: HttpClient,
17+
// useFactory: () => new HttpClient(new HttpFetchBackend()) // TODO: not work
18+
// }
1919
],
2020
bootstrap: [AppComponent],
2121
schemas: [NO_ERRORS_SCHEMA]

src/app/github.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Injectable } from '@angular/core';
2+
import { from } from 'rxjs';
23
import { HttpClient } from '@angular/common/http';
4+
import fetch from 'node-fetch';
35

46
const API_URL = 'https://api.github.com';
57

68
@Injectable({
79
providedIn: 'root'
810
})
911
export class GithubService {
10-
constructor(private http: HttpClient) {}
11-
1212
getUser(username: string) {
13-
return this.http.get(`${API_URL}/users/${username}`);
13+
return from(fetch(`${API_URL}/users/${username}`).then(res => res.json()));
1414
}
1515
}

src/app/hello/hello.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<view>
2-
<text>{{ avatarUrl }}</text>
2+
<text>{{ userName }}</text>
33
</view>

src/app/hello/hello.component.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Subscription } from 'rxjs';
1717
export class HelloComponent implements OnInit, OnDestroy {
1818
@Input() name;
1919

20-
public avatarUrl: string;
20+
public userName: string;
2121

2222
private subscribtion: Subscription;
2323

@@ -27,8 +27,7 @@ export class HelloComponent implements OnInit, OnDestroy {
2727
this.subscribtion = this.github
2828
.getUser(this.name)
2929
.subscribe((data: any) => {
30-
console.log(data);
31-
this.avatarUrl = data.avatar_url;
30+
this.userName = data.name;
3231
});
3332
}
3433

src/lib/http-fetch-backend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class HttpFetchBackend extends HttpBackend {
2020
credentials: req.withCredentials ? 'same-origin' : undefined,
2121
headers: req.headers ? strMapToObj(req.headers) : undefined,
2222
body: req.body ? JSON.stringify(req.body) : undefined
23-
})
23+
}).then(res => res.json())
2424
);
2525
}
2626
}

0 commit comments

Comments
 (0)