import { HttpClient, HttpHeaders } from "@angular/common/http"; import { Observable, catchError, of, tap } from "rxjs"; import { Injectable } from "@angular/core"; import { IBase, IRslt } from "../../public-api"; @Injectable({ providedIn: 'root' }) export class Api { base: string = '' auth: string = ''; token: string = ''; location: string = '' get environment() { if (window) { if (window?.location?.hostname === 'localhost') { return 'dev' } else if (window?.location?.hostname.indexOf('stage') >= 0) { return 'stage' } else { return 'prod' } } return 'undefined' } constructor(public httpClient: HttpClient) { //this is specific to one client. This shouldn't be in a shared file. Maybe inject this as an interface using a config file or another solution if (this.environment !== "undefined") { if (this.environment === 'dev') { //local this.base = 'http://localhost:3000/api/' this.auth = 'http://localhost:3001/' this.location = 'local' } else if (this.environment === 'stage') { //stage this.base = 'https://be5cwi4cx6eebmreintdovx35u0wcfit.lambda-url.us-east-1.on.aws/api/' // this.base = 'https://api.thefriendlyfacilitator.com/api/' this.auth = 'https://ribdxsr26h.execute-api.us-east-1.amazonaws.com/Prod/' this.location = 'stage' } else { //production // this.base = 'https://api.sherpabuds.com/' this.base = 'https://be5cwi4cx6eebmreintdovx35u0wcfit.lambda-url.us-east-1.on.aws/api/' // this.base = 'https://api.thefriendlyfacilitator.com/api/' this.auth = 'https://ribdxsr26h.execute-api.us-east-1.amazonaws.com/Prod/' this.location = 'prod' } // console.log(this.location + ' build: ' + this.base) //check for token const auth = localStorage?.getItem('auth'); if (auth) { this.token = auth; } } console.log('Base: ' + this.base) console.log('Auth: ' + this.auth) console.log('Location: ' + this.location) console.log('Environment: ' + this.environment) console.log('Window: ' + window ? 'Exists' : 'Does not exist') } createInstance(type: (new () => T)): T { return new type(); } // updateRslt(to: Array, from: IRslt>) { // from.results.forEach(rslt => { // const found = to.findIndex(f => f._id === rslt._id); // if (found >= 0) { // to[found] = rslt as T // } else { // to.push(rslt as T) // } // }) // } // update(to: Array, from: Array) { // from.forEach(rslt => { // const found = to.findIndex(f => f._id === rslt._id); // if (found >= 0) { // to[found] = rslt as T // } else { // to.push(rslt as T) // } // }) // } buildAuth(token: string | undefined | null) { return { headers: new HttpHeaders() .set('Authorization', `Bearer ${token ? token : this.token}`) .set('Accept-Encoding','gzip') } } checkAuth(rslt: any) { console.log('test'); console.log(rslt); return rslt } get(url: string, token?: string | undefined | null): Observable> { // console.log('Url passed in: ' + url) return this.httpClient .get(url, this.buildAuth(token)) .pipe(tap((x: any) => { return this.checkAuth(x) })); } post(url: string, data: any): Observable> { return this.httpClient .post(url, data, this.buildAuth(undefined)) .pipe(tap((x: any) => { return this.checkAuth(x) })); } put(url: string, data: any): Observable> { return this.httpClient .put(url, data, this.buildAuth(undefined)) .pipe(tap((x: any) => { return this.checkAuth(x) })); } delete(url: string): Observable> { return this.httpClient .delete(url, this.buildAuth(undefined)) .pipe(tap((x: any) => { return this.checkAuth(x) })); } }