import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {Injectable} from "@angular/core"; import {UtilsConfig} from "../utils.config"; @Injectable() export class HttpService { private httpOptions:{ body?: any; headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: 'body'; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType: 'json'; withCredentials?: boolean; } = { headers: { 'Content-Type' : 'application/json' , 'Access-Control-Allow-Origin' : '*', 'Access-Control-Allow-Methods' : 'POST,GET,PUT,DELETE,OPTIONS', 'Access-Control-Allow-Headers' : 'Authorization' }, responseType: 'json' }; constructor(private http: HttpClient, private config?: UtilsConfig) { } performRequest(args: PerformRequestArgs) { if(args.bypassCors) { args.url = this.config.corsUrl || 'https://us-central1-eatmatic-143319.cloudfunctions.net/corsCall'; args.body = { method: args.method, url: args.url, body: args.body } } if (args.body) { this.httpOptions.body = args.body; } if(args.headers) { this.httpOptions.headers = {...this.httpOptions.headers, ...args.headers}; } return this.http.request(args.method, args.url, this.httpOptions).toPromise(); } } export class PerformRequestArgs { method: 'get' | 'post' | 'put' | 'delete'; url: string; body?: any; headers?: any; bypassCors?: boolean; }