|
1 |
| -import {Injectable} from '@angular/core'; |
| 1 | +import {EventEmitter, Injectable, OnDestroy} from '@angular/core'; |
2 | 2 | import {HttpClient, HttpParams} from '@angular/common/http';
|
3 | 3 | import {Observable} from 'rxjs';
|
| 4 | +import {Subscription} from 'rxjs/internal/Subscription'; |
| 5 | +import {map} from 'rxjs/operators'; |
| 6 | +import {Link} from 'ngx-linkifyjs'; |
| 7 | +import {LinkPreview} from '../..'; |
4 | 8 |
|
5 | 9 | @Injectable()
|
6 |
| -export class LinkPreviewService { |
| 10 | +export class LinkPreviewService implements OnDestroy { |
7 | 11 |
|
8 | 12 | private _accessKey = '5b54e80a65c77848ceaa4630331e8384950e09d392365';
|
9 | 13 | private _apiURL = 'http://api.linkpreview.net/';
|
10 | 14 |
|
| 15 | + subscription: Subscription; |
| 16 | + onLinkFound: EventEmitter<Array<Link>> = new EventEmitter<Array<Link>>(); |
| 17 | + |
| 18 | + links: Link[] = []; |
| 19 | + |
11 | 20 | constructor(private http: HttpClient) {
|
| 21 | + this.onLinkFound.subscribe((links: Array<Link>) => this.links = links); |
12 | 22 | }
|
13 | 23 |
|
14 |
| - sayHello(name?: String) { |
15 |
| - return `Hello ${name || 'Stanger'}!`; |
| 24 | + ngOnDestroy(): void { |
16 | 25 | }
|
17 | 26 |
|
18 |
| - fetchLink(url: string): Observable<any> { |
| 27 | + fetchLink(url: string): Observable<LinkPreview> { |
| 28 | + console.log('fetching the following link: ', url); |
19 | 29 | const params = new HttpParams()
|
20 | 30 | .append('key', this._accessKey)
|
21 | 31 | .append('q', url);
|
22 | 32 |
|
23 |
| - return this.http.get(this._apiURL, {params: params}); |
| 33 | + return this.http.get(this._apiURL, {params: params}).pipe(map(value => value as LinkPreview)); |
24 | 34 | }
|
25 | 35 | }
|
0 commit comments