Advertisement
attilan

HTTP service

Oct 19th, 2021
875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpParams } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import { environment } from '@environments/environment';
  5. import { ParamArray } from '@shared/modules/http/classes/ParamArray';
  6.  
  7. /** Http Service Layer */
  8. @Injectable({
  9.   providedIn: 'root',
  10. })
  11. export class HttpService {
  12.   constructor(private http: HttpClient) {}
  13.  
  14.   /** generate url for backend call */
  15.   generateFullUrl(url: string): string {
  16.     let normalizedUrl = url;
  17.     if (url.startsWith('/')) {
  18.       normalizedUrl = url.substring(1);
  19.     }
  20.  
  21.     return `${environment.apiUrl}/${environment.backendVersion}/${normalizedUrl}`;
  22.   }
  23.  
  24.   /** generate url query params for backend call */
  25.   generateUrlQueryParams(
  26.     urlParams: Record<string, string> = null,
  27.     paramArray: ParamArray<any> = null
  28.   ): HttpParams {
  29.     let params = new HttpParams();
  30.  
  31.     if (urlParams) {
  32.       Object.keys(urlParams).forEach((property: string) => {
  33.         if (property in urlParams) {
  34.           params = params.append(property, urlParams[property]);
  35.         }
  36.       });
  37.     }
  38.  
  39.     if (paramArray && Array.isArray(paramArray.value)) {
  40.       paramArray.value.forEach((value) => {
  41.         params = params.append(`${paramArray.key}[]`, value);
  42.       });
  43.     }
  44.  
  45.     return params;
  46.   }
  47.  
  48.   get(
  49.     url,
  50.     urlParams: Record<string, string> = null,
  51.     paramArray: ParamArray<any> = null
  52.   ): Observable<any> {
  53.     return this.http.get(this.generateFullUrl(url), {
  54.       params: this.generateUrlQueryParams(urlParams, paramArray),
  55.     });
  56.   }
  57.  
  58.   getFile(
  59.     url,
  60.     urlParams: Record<string, string> = null,
  61.     paramArray: ParamArray<any> = null
  62.   ): Observable<any> {
  63.     return this.http.get(this.generateFullUrl(url), {
  64.       params: this.generateUrlQueryParams(urlParams, paramArray),
  65.       responseType: 'blob',
  66.       observe: 'response',
  67.     });
  68.   }
  69.  
  70.   post(url, data): Observable<any> {
  71.     return this.http.post(this.generateFullUrl(url), data);
  72.   }
  73.  
  74.   postWithHeaders(url, data = {}, headers): Observable<any> {
  75.     return this.http.post(this.generateFullUrl(url), data, { headers, withCredentials: true });
  76.   }
  77.  
  78.   put(url, data): Observable<any> {
  79.     return this.http.put(this.generateFullUrl(url), data);
  80.   }
  81.  
  82.   patch(url, data): Observable<any> {
  83.     return this.http.patch(this.generateFullUrl(url), data);
  84.   }
  85.  
  86.   delete(url): Observable<any> {
  87.     return this.http.delete(this.generateFullUrl(url));
  88.   }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement