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

45.45% Statements 15/33
41.66% Branches 5/12
25% Functions 4/16
42.3% Lines 11/26

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 84 85 86  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 { TagPageArgs } from '../../model/tag';
import { mapTemplate, Template, writeTemplate } from '../../model/template';
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 TemplateService {
 
  constructor(
    private http: HttpClient,
    private config: ConfigService,
    private login: LoginService,
  ) { }
 
  private get base() {
    return this.config.api + '/api/v1/template';
  }
 
  create(template: Template): Observable<void> {
    if (template.tag.startsWith('/')) template.tag = template.tag.substring(1);
    return this.http.post<void>(this.base, writeTemplate(template)).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  get(tag = ''): Observable<Template> {
    if (tag.startsWith('/')) tag = tag.substring(1);
    return this.http.get(this.base, {
      params: { tag },
    }).pipe(
      map(mapTemplate),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  page(args?: TagPageArgs): Observable<Page<Template>> {
    return this.http.get(`${this.base}/page`, {
      params: params(args),
    }).pipe(
      map(mapPage(mapTemplate)),
      catchError(err => this.login.handleHttpError(err)),
    );
  }
 
  update(template: Template): Observable<void> {
    return this.http.put<void>(this.base, writeTemplate(template)).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<Template>): 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> {
    if (tag.startsWith('/')) tag = tag.substring(1);
    return this.http.delete<void>(this.base, {
      params: { tag },
    }).pipe(
      catchError(err => this.login.handleHttpError(err)),
    );
  }
}