All files / app/service/api scrape.service.ts

66.66% Statements 20/30
69.23% Branches 9/13
45.45% Functions 5/11
66.66% Lines 16/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69  68x     68x 68x   68x               68x     6x 6x 6x 6x 6x   6x 6x             2x       2x       2x     2x                                                  
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { autorun } from 'mobx';
import { catchError, map, Observable, throwError } from 'rxjs';
import { mapRef, Ref } from '../../model/ref';
import { catchAll } from '../../mods/sync/scrape';
import { Store } from '../../store/store';
import { params } from '../../util/http';
import { ConfigService } from '../config.service';
import { LoginService } from '../login.service';
import { RefService } from './ref.service';
 
@Injectable({
  providedIn: 'root',
})
export class ScrapeService {
 
  constructor(
    private http: HttpClient,
    private config: ConfigService,
    private store: Store,
    private refs: RefService,
    private login: LoginService,
  ) {
    autorun(() => {
      Iif (store.eventBus.event === '+plugin/scrape:defaults' || store.eventBus.event === '*:defaults') {
        this.defaults().subscribe();
      }
    });
  }
 
  private get base() {
    return this.config.api + '/api/v1/scrape';
  }
 
  webScrape(url: string): Observable<Ref> {
    return this.http.get<Ref>(`${this.base}/web`, {
      params: params({ url }),
    }).pipe(
      map(ref => {
        Iif (!ref) {
          throw 'Web scrape failed';
        }
        return ref;
      }),
      map(mapRef),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  rss(url: string): Observable<string> {
    return this.http.get(`${this.base}/rss`, {
      params: params({ url }),
      responseType: 'text'
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  defaults(): Observable<any> {
    return this.refs.update({ ...catchAll, origin: this.store.account.origin }).pipe(
      catchError(err => {
        if (err.status === 404) return this.refs.create({ ...catchAll, origin: this.store.account.origin });
        return throwError(() => err);
      })
    );
  }
}