Skip to content

Commit

Permalink
setting api url relative to host
Browse files Browse the repository at this point in the history
  • Loading branch information
szydlofj committed Feb 7, 2024
1 parent bfbd14a commit 4a064ea
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions web/src/services/api/data.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { AuthService } from 'src/services/auth/auth.service';
import { environment } from 'src/environments/environment';

@Injectable({ providedIn: 'root' })
export class DataService {

private API_BASE_URL: string = environment.apiUrl; // Set the base URL

constructor(private http: HttpClient, private authService: AuthService) {}

private createHeaders() {
Expand All @@ -16,12 +13,27 @@ export class DataService {
});
}

private getApiBaseUrl(): string {
const hostname = window.location.hostname
switch (hostname) {
case 'localhost':
return 'https://dev.nhsnlink.org/api'
default:
return `https://${hostname}/api`
}
}

private constructApiUrl(resource: string) {
const baseUrl = this.getApiBaseUrl()
return `${baseUrl}/${resource}`
}

private performRequest<T>(method: 'get' | 'post' | 'put', resource: string, data?: T) {
if (!this.authService.isLoggedIn()) {
throw new Error('User is not authenticated or token is invalid');
}

const url = `${this.API_BASE_URL}/${resource}`;
const url = this.constructApiUrl(resource)
const options = { headers: this.createHeaders() };

switch (method) {
Expand Down

0 comments on commit 4a064ea

Please sign in to comment.