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

55.55% Statements 15/27
100% Branches 5/5
25% Functions 4/16
47.82% Lines 11/23

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83  118x   118x 118x   118x               166x     166x 166x 166x       1x                                     1x       1x                                                                        
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError, map, Observable } from 'rxjs';
import { mapPage, Page } from '../../model/page';
import { mapPlugin, Plugin, writePlugin } from '../../model/plugin';
import { TagPageArgs } from '../../model/tag';
import { params } from '../../util/http';
import { OpPatch } from '../../util/json-patch';
import { ConfigService } from '../config.service';
import { LoginService } from '../login.service';
 
@Injectable({
  providedIn: 'root',
})
export class PluginService {
 
  constructor(
    private http: HttpClient,
    private config: ConfigService,
    private login: LoginService,
  ) { }
 
  private get base() {
    return this.config.api + '/api/v1/plugin';
  }
 
  create(plugin: Plugin): Observable<void> {
    return this.http.post<void>(this.base, writePlugin(plugin)).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  get(tag: string): Observable<Plugin> {
    return this.http.get(this.base, {
      params: params({ tag }),
    }).pipe(
      map(mapPlugin),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  page(args: TagPageArgs): Observable<Page<Plugin>> {
    return this.http.get(`${this.base}/page`, {
      params: params(args),
    }).pipe(
      map(mapPage(mapPlugin)),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  update(plugin: Plugin): Observable<void> {
    return this.http.put<void>(this.base, writePlugin(plugin)).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  patch(tag: string, cursor: string, patch: OpPatch[]): Observable<string> {
    return this.http.patch<string>(this.base, patch, {
      headers: { 'Content-Type': 'application/json-patch+json' },
      params: params({ tag, cursor }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  merge(tag: string, cursor: string, patch: Partial<Plugin>): Observable<string> {
    return this.http.patch<string>(this.base, patch, {
      headers: { 'Content-Type': 'application/merge-patch+json' },
      params: params({ tag, cursor }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  delete(tag: string): Observable<any> {
    return this.http.delete<void>(this.base, {
      params: params({ tag }),
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
}