Skip to content

Commit b3b18e4

Browse files
committed
feat: add BASE_API_URL injection token and update API service URLs
1 parent 9534995 commit b3b18e4

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

apps/frontend/src/app/app.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@ import {
66
provideClientHydration,
77
withEventReplay,
88
} from '@angular/platform-browser';
9+
import { BASE_API_URL, getBaseApiUrl } from './shared/context/base-api-url';
910

1011
export const appConfig: ApplicationConfig = {
1112
providers: [
1213
provideClientHydration(withEventReplay()),
1314
provideZoneChangeDetection({ eventCoalescing: true }),
1415
provideRouter(appRoutes),
1516
provideHttpClient(withFetch()),
17+
{
18+
provide: BASE_API_URL,
19+
useFactory: getBaseApiUrl,
20+
},
1621
],
1722
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { InjectionToken, isDevMode } from '@angular/core';
2+
3+
export const BASE_API_URL = new InjectionToken<string>('BASE_API_URL');
4+
5+
export function getBaseApiUrl(): string {
6+
const isDevelopment =
7+
window.location.hostname === 'localhost' ||
8+
!window.location.protocol.startsWith('https');
9+
10+
return isDevelopment || isDevMode()
11+
? 'http://localhost:3001'
12+
: 'https://api.devswhorun.dev';
13+
}

apps/frontend/src/app/shared/services/auth-api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import { HttpClient } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { tap, catchError } from 'rxjs/operators';
55
import { UserProfile, AuthResponse } from '../types';
6+
import { BASE_API_URL } from '../context/base-api-url';
67

78
@Injectable({
89
providedIn: 'root',
910
})
1011
export class AuthApiService {
1112
private http = inject(HttpClient);
13+
private baseUrl = inject(BASE_API_URL);
1214

13-
private readonly apiUrl = 'http://localhost:3001/auth';
15+
private readonly apiUrl = `${this.baseUrl}/auth`;
1416

1517
public currentUser = signal<UserProfile | null>(null);
1618

apps/frontend/src/app/shared/services/event-api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ import { Injectable, inject } from '@angular/core';
22
import { HttpClient } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { Event } from '../types';
5+
import { BASE_API_URL } from '../context/base-api-url';
56

67
@Injectable({
78
providedIn: 'root',
89
})
910
export class EventApiService {
1011
private http = inject(HttpClient);
12+
private baseUrl = inject(BASE_API_URL);
1113

12-
private readonly apiUrl = 'http://localhost:3001/events';
14+
private readonly apiUrl = `${this.baseUrl}/events`;
1315

1416
getEvents(): Observable<Event[]> {
1517
return this.http.get<Event[]>(`${this.apiUrl}/all`);

0 commit comments

Comments
 (0)