All files / src/app/core/services data.service.ts

68.42% Statements 13/19
100% Branches 2/2
53.84% Functions 7/13
90.9% Lines 10/11

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 3938x 38x   38x         38x   26x   26x       2x         14x         1x                   1x    
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from '@environments/environment';
 
@Injectable({
  providedIn: 'root',
})
export class DataService {
  // API's base URL
  private baseUrl = `${environment.apiUrl}/api/`;
  
  constructor(private http: HttpClient) {}
 
   // GET
   get<T>(endpoint: string, options?: {params?: HttpParams, headers?: HttpHeaders}): Observable<T> {
    return this.http.get<T>(`${this.baseUrl}${endpoint}`, { ...options, withCredentials: true});
  }
 
  // POST
  post<T>(endpoint: string, body: unknown, options?: {params?: HttpParams, headers?: HttpHeaders}): Observable<T> {
    return this.http.post<T>(`${this.baseUrl}${endpoint}`, body, {...options, withCredentials: true});
  }
 
  // PUT
  put<T>(endpoint: string, body: unknown, options?: {params?: HttpParams, headers?: HttpHeaders}): Observable<T> {
    return this.http.put<T>(`${this.baseUrl}${endpoint}`, body, {...options, withCredentials: true});
  }
 
  // PATCH
  patch<T>(endpoint: string, body: unknown, options?: {params?: HttpParams, headers?: HttpHeaders}): Observable<T> {
    return this.http.patch<T>(`${this.baseUrl}${endpoint}`, body, {...options, withCredentials: true});
  }
 
  // DELETE
  delete<T>(endpoint: string, options?: {params?: HttpParams, headers?: HttpHeaders}): Observable<T> {
    return this.http.delete<T>(`${this.baseUrl}${endpoint}`, {...options, withCredentials: true});
  }
}