Skip to content

Commit 98a2c53

Browse files
committed
feat(package): improved the link preview service
1 parent 8d6b57c commit 98a2c53

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
import {Injectable} from '@angular/core';
1+
import {EventEmitter, Injectable, OnDestroy} from '@angular/core';
22
import {HttpClient, HttpParams} from '@angular/common/http';
33
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 '../..';
48

59
@Injectable()
6-
export class LinkPreviewService {
10+
export class LinkPreviewService implements OnDestroy {
711

812
private _accessKey = '5b54e80a65c77848ceaa4630331e8384950e09d392365';
913
private _apiURL = 'http://api.linkpreview.net/';
1014

15+
subscription: Subscription;
16+
onLinkFound: EventEmitter<Array<Link>> = new EventEmitter<Array<Link>>();
17+
18+
links: Link[] = [];
19+
1120
constructor(private http: HttpClient) {
21+
this.onLinkFound.subscribe((links: Array<Link>) => this.links = links);
1222
}
1323

14-
sayHello(name?: String) {
15-
return `Hello ${name || 'Stanger'}!`;
24+
ngOnDestroy(): void {
1625
}
1726

18-
fetchLink(url: string): Observable<any> {
27+
fetchLink(url: string): Observable<LinkPreview> {
28+
console.log('fetching the following link: ', url);
1929
const params = new HttpParams()
2030
.append('key', this._accessKey)
2131
.append('q', url);
2232

23-
return this.http.get(this._apiURL, {params: params});
33+
return this.http.get(this._apiURL, {params: params}).pipe(map(value => value as LinkPreview));
2434
}
2535
}

0 commit comments

Comments
 (0)